#include<iostream>
#include<string>
#include <algorithm>
#include <cstdlib>
#include<vector>
#include<cstring>
using namespace std;
inline int gcd(unsigned long a,unsigned long b){//最大公约数
if(b==0)return a;
return gcd(b,a%b);
}
inline int lcd(unsigned long a,unsigned long b){//最小公倍数
return abgcd(a,b);
}
class Fraction{
protected:
unsigned long numerator ;//分子
unsigned long denominator;//分母
char flag;//正负号
public:
Fraction(unsigned long n=0,unsigned long d=1,char f='+'){//有参数的构造函数
int temp=gcd(n,d);
if(temp==0)
temp=1;
numerator=n/temp;
denominator=d/temp;//规格化
flag=f;
};
Fraction(const Fraction &b ){//COPY构造函数
numerator=bnumerator;
denominator=bdenominator;
flag=bflag;
};
char getFlag(){return flag;}
Fraction reciprocal ()const{//倒数
return Fraction(this->denominator,this->numerator,this->flag);
}
friend Fraction reciprocal(Fraction &b){//倒数
return Fraction(bdenominator,bnumerator,bflag);
}
bool Correct(){//判断分数形式是否合法、是否是既约分数
if(denominator==0||gcd(denominator,numerator)!=1)
return false;
return true;
}
void Cout(){//格式化输出Fraction
if(flag=='-')
cout<<"-";
printf("%lu/",numerator);
printf("%lu",denominator);
}
const Fraction & operator =( const Fraction b ){//重载=
numerator=bnumerator;
denominator=bdenominator;
flag=bflag;
return this;
}
friend Fraction & operator -( const Fraction &a,const Fraction &b );
friend Fraction & operator +( const Fraction &a,const Fraction &b ){//重载+
Fraction c;
if(aflag!=bflag)
return a-Fraction(bnumerator,bdenominator,aflag);
unsigned long temp=gcd(adenominator,bdenominator);
cdenominator=lcd(bdenominator,adenominator);
cnumerator=anumeratorcdenominator/adenominator+bnumeratorcdenominator/bdenominator;
temp=gcd(cdenominator,cnumerator);
cdenominator/=temp;
cnumerator/=temp;
cflag=aflag;
return c;
}
friend Fraction & operator -( const Fraction &a,const Fraction &b ){//重载-
Fraction c;
if(aflag!=bflag)
return a+Fraction(bnumerator,bdenominator,aflag);
unsigned long temp=gcd(adenominator,bdenominator);
cdenominator=lcd(bdenominator,adenominator);
cnumerator=anumeratorcdenominator/adenominator-bnumeratorcdenominator/bdenominator;
temp=gcd(cdenominator,cnumerator);
cdenominator/=temp;
cnumerator/=temp;
cflag=aflag;
if(cnumerator<0){
cnumerator=0-cnumerator;
if(cflag=='+')
cflag='-';
else cflag='+';
}
return c;
}
friend Fraction & operator ( const Fraction &a,const Fraction &b ){//重载
Fraction c;
cdenominator=adenominatorbdenominator;
cnumerator=anumeratorbnumerator;
unsigned long temp=gcd(cdenominator,cnumerator);
cdenominator/=temp;
cnumerator/=temp;
if(aflag==bflag)
cflag='+';
else cflag='-';
return c;
}
friend Fraction & operator /( const Fraction &a,const Fraction &b ){//重载/
Fraction c(breciprocal());
return ac;
}
friend bool operator==(const Fraction &a,const Fraction &b ){//重载==
return adenominator==bdenominator&&anumerator==bnumerator&&aflag==bflag;
}
};
inline Fraction Fraction_random(){//产生随机分数
return Fraction(rand(),rand()+1,(rand()&1)'-':'+');
}
Fraction fra[10][3];//分别存储随机生成的10个多项式的第一个运算数,第二个运算数,结果
char pol[10];//存储随机产生的10个多项式的运算类型
//举例,如a+b=c,则fra[i][0]=a,fra[i][1]=b,fra[i][2]=c,pol[i]='+'
//题目没有要求构造多项式类,所以这里就这么简化处理了~~
void Polynomial_random(){//生成全部10个随机多项式和其结果
for(int i=0,temp;i<10;i++){
temp=rand()%4;
fra[i][0]=Fraction_random();
fra[i][1]=Fraction_random();
if(temp==0){
pol[i]='+';
fra[i][2]=fra[i][0]+fra[i][1];
}
if(temp==1){
pol[i]='-';
fra[i][2]=fra[i][0]-fra[i][1];
}
if(temp==2){
pol[i]='';
fra[i][2]=fra[i][0]fra[i][1];
}
if(temp==3){
pol[i]='/';
fra[i][2]=fra[i][0]/fra[i][1];
}
}
}
void Cout(){//格式化输出10个多项式
for(int i=0;i<10;i++){
cout<<i+1<<"、\t";
fra[i][0]Cout();
printf(" %c ",pol[i]);
if(fra[i][1]getFlag()=='-'){
cout<<"(";
fra[i][1]Cout();
cout<<")";
}
else fra[i][1]Cout();
cout<<"=\n";
}
}
void Cin(){//读入答案并判断正误,最后统计分数
cout<<"\n";
cout<<" 请顺序输入每题答案: \n";
cout<<" (注意:1、输入的答案必须为既约分数,否则视为错误答案 \n";
cout<<" 2、若答案为0请输入0/1,否则视为错误答案) \n";
cout<<"\n";
string s;
unsigned long a,b;
int pos,score=0;
for(int i=0;i<10;i++){
pos=0;
a=0;
b=0;
cout<<i+1<<"、\t";
cin>>s;
while(s[pos]!='/'&&s[pos])
pos++;
if(s[0]!='-'){
for(int j=pos-1,k=1;j>=0;j--,k=10)
a+=(s[j]-'0')k;
}
else {
for(int j=pos-1,k=1;j>0;j--,k=10)
a+=(s[j]-'0')k;
}
for(int j=ssize()-1,k=1;j>pos;j--,k=10)
b+=(s[j]-'0')k;
if(Fraction(a,b,'+')==fra[i][2]){
cout<<"正确"<<endl;
score+=10;
}
else {
cout<<"错误,正确答案为:\t";
fra[i][2]Cout();
cout<<endl;
}
}
cout<<endl;
cout<<"总分:"<<score<<endl;
}
int main(){
Polynomial_random();
Cout();
Cin();
return 0;
}
#include<stdioh>
int main()
{
printf("请输入第一个成绩:");
int a = 0;
int b = 0;
int c = 0;
int d = 0;
double e = 0;
scanf_s("%d,", &a);
printf("请输入第二个成绩:");
scanf_s("%d,", &b);
printf("请输入第三个成绩:");
scanf_s("%d,", &c);
//总成绩
d = a + b + c;
printf("总成绩为:%d\n",d);
//平均成绩
e = d / 3;
printf("平均成绩为:%1lf\n", e);
return 0;
}
完整的C程序:
#include "stdioh"
/ 分数结构 /
typedef struct
{
int numerator; / 分子 /
int denominator; / 分母 /
} Fraction;
int GCD(int a,int b);
int LCM(int a,int b);
Fraction Add(Fraction f1, Fraction f2);
void main()
{
Fraction f1, f2, f3;
f1numerator = 1;
f1denominator = 3;
f2numerator = 1;
f2denominator = 6;
f3 = Add(f1, f2);
printf("%d/%d + %d/%d = %d/%d\n", f1numerator, f1denominator, f2numerator, f2denominator, f3numerator, f3denominator);
}
/ 返回两个分数相加的结果 /
Fraction Add(Fraction f1, Fraction f2)
{
Fraction retValue;
int lcm = LCM(f1denominator, f2denominator); / 计算最小公倍数 /
int gcd;
retValuedenominator = lcm;
retValuenumerator = f1numerator lcm / f1denominator + f2numerator lcm / f2denominator;
gcd = GCD(retValuenumerator, retValuedenominator);
retValuenumerator /= gcd;
retValuedenominator /= gcd;
return retValue;
}
/ 返回两个整数的最大公约数 /
int GCD(int a,int b)
{
int i,temp_gcd;
for(i=a;i>=1;i--)
{
if(a%i==0)
{
if(b%i==0)
{
temp_gcd=i;
return temp_gcd;
}
}
}
}
/ 返回两个整数的最小公倍数 /
int LCM(int a,int b)
{
int temp_lcm;
temp_lcm = a b / GCD(a,b);
return temp_lcm;
}
运行测试:
以上就是关于C++编写的随机分数计算全部的内容,包括:C++编写的随机分数计算、编写程序,实现从键盘输入某个学生的三门课考试成绩(以百分制计算),计算该生的考试总成绩、评价成绩(、如何在c语言中实现分数相加,以分数形式输出,并化为最简等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)