
c++编程作业 30
按如下要求实现分数的计算:1)实现分数的+,-,*,/,=,<,>,==;2)实现分数的化简,至少用两种方法实现(辗转相除法);3)实现将从键盘输入的小数化为简分数。4)...
按如下要求实现分数的计算:
1)实现分数的+,-,*,/,=,<,>,==;
2) 实现分数的化简,至少用两种方法实现(辗转相除法);
3)实现将从键盘输入的小数化为简分数。
4)实现分数的输入和输出。输入输出格式:
5/31; 6/5;100/300;
要求输出的分数是简分数;
5)比较两种分数化简方法的效率(算出运算时间)
6) 实现选择排序模板类,并将其用于对分数序列的排序(选做);
运行时界面如下:
*--------------------------------*
实验1:分数的实现
完成人:xxx,学号:011111
*-------------------------------*
1.输入第一个数
2.输入第二个数
3.输入两个数的运算符并给出结果
4.输入两个数的比较运算符并给出比较结果
5.测试两种不同化简方法的计算时间
6.清屏
*--------------------------------------------*
请选择: 展开
1)实现分数的+,-,*,/,=,<,>,==;
2) 实现分数的化简,至少用两种方法实现(辗转相除法);
3)实现将从键盘输入的小数化为简分数。
4)实现分数的输入和输出。输入输出格式:
5/31; 6/5;100/300;
要求输出的分数是简分数;
5)比较两种分数化简方法的效率(算出运算时间)
6) 实现选择排序模板类,并将其用于对分数序列的排序(选做);
运行时界面如下:
*--------------------------------*
实验1:分数的实现
完成人:xxx,学号:011111
*-------------------------------*
1.输入第一个数
2.输入第二个数
3.输入两个数的运算符并给出结果
4.输入两个数的比较运算符并给出比较结果
5.测试两种不同化简方法的计算时间
6.清屏
*--------------------------------------------*
请选择: 展开
1个回答
展开全部
#include <iostream>
#include <string>
#include <math.h>
#include <windows.h>
using namespace std;
enum Mode{GCD, LOOP};
struct Fract
{
int nume;//分子
int deno;//分母
};
int gcd(int x, int y)
{
if(x%y==0)
return y;
else
return gcd(y, x%y);
}
int loop(int x, int y)
{ int t=1;
while(t=x%y)
{
x=y;
y=t;
}
return y;
}
bool simple(Fract& valout, Mode mode = GCD)
{
int pos;
switch(mode)
{
case GCD:
pos = gcd(valout.nume, valout.deno);
valout.nume = valout.nume/pos;
valout.deno = valout.deno/pos;
break;
case LOOP:
pos = loop(valout.nume, valout.deno);
valout.nume = valout.nume/pos;
valout.deno = valout.deno/pos;
break;
default:
return false;
break;
}
return true;
}
bool fomat(char* valin, Fract& valout)
{
char* dest;
int pos;
char nume[128] = {0};
char deno[128] = {0};
if(NULL != (dest = strchr(valin, '.')) )
{
pos = dest - valin + 1;
strncpy(nume,valin,pos-1);//整数
strcpy(deno,valin+pos);//小数
valout.deno = pow(10, strlen(deno));
valout.nume = atoi(nume)*valout.deno + atoi(deno);
}
else if(NULL != (dest = strchr(valin, '/')) )
{
pos = dest - valin + 1;
strncpy(nume,valin,pos-1);
strcpy(deno,valin+pos);//小数
valout.deno = atoi(deno);
valout.nume = atoi(nume);
}
else
return false;
simple(valout);
return true;
}
int resolve(char optor)
{
return 0;
}
Fract operator+(Fract x,Fract y)
{
Fract sum;
sum.deno = x.deno * y.deno;
sum.nume = x.nume * y.deno + y.nume * x.deno;
simple(sum);
return sum;
}
Fract operator-(Fract x,Fract y)
{
Fract sum;
sum.deno = x.deno * y.deno;
sum.nume = x.nume * y.deno - y.nume * x.deno;
simple(sum);
return sum;
}
Fract operator*(Fract x,Fract y)
{
Fract sum;
sum.deno = x.deno * y.deno;
sum.nume = x.nume * y.nume;
simple(sum);
return sum;
}
Fract operator/(Fract x,Fract y)
{
Fract sum;
sum.deno = x.deno * y.nume;
sum.nume = x.nume * y.deno;
simple(sum);
return sum;
}
bool operator==(Fract x,Fract y)
{
simple(x);
simple(y);
if (x.deno == y.deno && x.nume == y.nume)
{
return true;
}
return false;
}
bool operator>(Fract x,Fract y)
{
Fract temp = x - y;
if (temp.nume > 0)
{
return true;
}
return false;
}
bool operator<(Fract x,Fract y)
{
Fract temp = x - y;
if (temp.nume < 0)
{
return true;
}
return false;
}
void useage()
{
cout<<"*--------------------------------*"<<endl
<<"实验1:分数的实现 "<<endl
<<"完成人:xxx,学号:011111"<<endl
<<"*-------------------------------*"<<endl
<<"for example input: 1.25 + 0.25"<<endl;
}
int main()
{
LARGE_INTEGER dwStart;
LARGE_INTEGER dwEnding;
char val[64];
Fract valx;
Fract valy;
Fract sum;
char opt;
while (true)
{
useage();
QueryPerformanceCounter(&dwStart);
cin>>val;
fomat(val,valx);
cin>>val;
if('=' == val[0] && '=' == val[1])
opt='=';
else if ('=' == val[0] && '=' != val[1])
{
cin.sync();
goto again;
}
else
opt = val[0];
cin>>val;
fomat(val,valy);
switch(opt)
{
case '+':
sum = valx + valy;
cout<<valx.nume<<"/"<<valx.deno<<" + "
<<valy.nume<<"/"<<valy.deno
<<" = "
<<sum.nume<<"/"<<sum.deno<<endl;
break;
case '-':
sum = valx - valy;
cout<<valx.nume<<"/"<<valx.deno<<" - "
<<valy.nume<<"/"<<valy.deno
<<" = "
<<sum.nume<<"/"<<sum.deno<<endl;
break;
case '*':
sum = valx * valy;
cout<<valx.nume<<"/"<<valx.deno<<" * "
<<valy.nume<<"/"<<valy.deno
<<" = "
<<sum.nume<<"/"<<sum.deno<<endl;
break;
case '/':
sum = valx / valy;
cout<<valx.nume<<"/"<<valx.deno<<" / "
<<valy.nume<<"/"<<valy.deno
<<" = "
<<sum.nume<<"/"<<sum.deno<<endl;
break;
case '=':
cout<<valx.nume<<"/"<<valx.deno<<" == "
<<valy.nume<<"/"<<valy.deno
<<boolalpha<<" "<<(valx == valy);
break;
case '>':
cout<<valx.nume<<"/"<<valx.deno<<" > "
<<valy.nume<<"/"<<valy.deno
<<boolalpha<<" "<<(valx > valy);
break;
case '<':
cout<<valx.nume<<"/"<<valx.deno<<" < "
<<valy.nume<<"/"<<valy.deno
<<boolalpha<<" "<<(valx < valy);
break;
default:
cout<<"ERROR"<<endl;
break;
}
QueryPerformanceCounter(&dwEnding);
cout<<"time-consumed "
<<dwEnding.LowPart - dwStart.LowPart
<<"microsecond\n"
<<"press continue\n";
cin.sync();
getchar();
again: system("cls");
}
return 0;
}
//--------------------------------------------------------------------
//ps:楼主也太小气了,一定要追加分那!
#include <string>
#include <math.h>
#include <windows.h>
using namespace std;
enum Mode{GCD, LOOP};
struct Fract
{
int nume;//分子
int deno;//分母
};
int gcd(int x, int y)
{
if(x%y==0)
return y;
else
return gcd(y, x%y);
}
int loop(int x, int y)
{ int t=1;
while(t=x%y)
{
x=y;
y=t;
}
return y;
}
bool simple(Fract& valout, Mode mode = GCD)
{
int pos;
switch(mode)
{
case GCD:
pos = gcd(valout.nume, valout.deno);
valout.nume = valout.nume/pos;
valout.deno = valout.deno/pos;
break;
case LOOP:
pos = loop(valout.nume, valout.deno);
valout.nume = valout.nume/pos;
valout.deno = valout.deno/pos;
break;
default:
return false;
break;
}
return true;
}
bool fomat(char* valin, Fract& valout)
{
char* dest;
int pos;
char nume[128] = {0};
char deno[128] = {0};
if(NULL != (dest = strchr(valin, '.')) )
{
pos = dest - valin + 1;
strncpy(nume,valin,pos-1);//整数
strcpy(deno,valin+pos);//小数
valout.deno = pow(10, strlen(deno));
valout.nume = atoi(nume)*valout.deno + atoi(deno);
}
else if(NULL != (dest = strchr(valin, '/')) )
{
pos = dest - valin + 1;
strncpy(nume,valin,pos-1);
strcpy(deno,valin+pos);//小数
valout.deno = atoi(deno);
valout.nume = atoi(nume);
}
else
return false;
simple(valout);
return true;
}
int resolve(char optor)
{
return 0;
}
Fract operator+(Fract x,Fract y)
{
Fract sum;
sum.deno = x.deno * y.deno;
sum.nume = x.nume * y.deno + y.nume * x.deno;
simple(sum);
return sum;
}
Fract operator-(Fract x,Fract y)
{
Fract sum;
sum.deno = x.deno * y.deno;
sum.nume = x.nume * y.deno - y.nume * x.deno;
simple(sum);
return sum;
}
Fract operator*(Fract x,Fract y)
{
Fract sum;
sum.deno = x.deno * y.deno;
sum.nume = x.nume * y.nume;
simple(sum);
return sum;
}
Fract operator/(Fract x,Fract y)
{
Fract sum;
sum.deno = x.deno * y.nume;
sum.nume = x.nume * y.deno;
simple(sum);
return sum;
}
bool operator==(Fract x,Fract y)
{
simple(x);
simple(y);
if (x.deno == y.deno && x.nume == y.nume)
{
return true;
}
return false;
}
bool operator>(Fract x,Fract y)
{
Fract temp = x - y;
if (temp.nume > 0)
{
return true;
}
return false;
}
bool operator<(Fract x,Fract y)
{
Fract temp = x - y;
if (temp.nume < 0)
{
return true;
}
return false;
}
void useage()
{
cout<<"*--------------------------------*"<<endl
<<"实验1:分数的实现 "<<endl
<<"完成人:xxx,学号:011111"<<endl
<<"*-------------------------------*"<<endl
<<"for example input: 1.25 + 0.25"<<endl;
}
int main()
{
LARGE_INTEGER dwStart;
LARGE_INTEGER dwEnding;
char val[64];
Fract valx;
Fract valy;
Fract sum;
char opt;
while (true)
{
useage();
QueryPerformanceCounter(&dwStart);
cin>>val;
fomat(val,valx);
cin>>val;
if('=' == val[0] && '=' == val[1])
opt='=';
else if ('=' == val[0] && '=' != val[1])
{
cin.sync();
goto again;
}
else
opt = val[0];
cin>>val;
fomat(val,valy);
switch(opt)
{
case '+':
sum = valx + valy;
cout<<valx.nume<<"/"<<valx.deno<<" + "
<<valy.nume<<"/"<<valy.deno
<<" = "
<<sum.nume<<"/"<<sum.deno<<endl;
break;
case '-':
sum = valx - valy;
cout<<valx.nume<<"/"<<valx.deno<<" - "
<<valy.nume<<"/"<<valy.deno
<<" = "
<<sum.nume<<"/"<<sum.deno<<endl;
break;
case '*':
sum = valx * valy;
cout<<valx.nume<<"/"<<valx.deno<<" * "
<<valy.nume<<"/"<<valy.deno
<<" = "
<<sum.nume<<"/"<<sum.deno<<endl;
break;
case '/':
sum = valx / valy;
cout<<valx.nume<<"/"<<valx.deno<<" / "
<<valy.nume<<"/"<<valy.deno
<<" = "
<<sum.nume<<"/"<<sum.deno<<endl;
break;
case '=':
cout<<valx.nume<<"/"<<valx.deno<<" == "
<<valy.nume<<"/"<<valy.deno
<<boolalpha<<" "<<(valx == valy);
break;
case '>':
cout<<valx.nume<<"/"<<valx.deno<<" > "
<<valy.nume<<"/"<<valy.deno
<<boolalpha<<" "<<(valx > valy);
break;
case '<':
cout<<valx.nume<<"/"<<valx.deno<<" < "
<<valy.nume<<"/"<<valy.deno
<<boolalpha<<" "<<(valx < valy);
break;
default:
cout<<"ERROR"<<endl;
break;
}
QueryPerformanceCounter(&dwEnding);
cout<<"time-consumed "
<<dwEnding.LowPart - dwStart.LowPart
<<"microsecond\n"
<<"press continue\n";
cin.sync();
getchar();
again: system("cls");
}
return 0;
}
//--------------------------------------------------------------------
//ps:楼主也太小气了,一定要追加分那!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询