第8周任务4(分数类中的对象可以和整型数进行四则运算,且满足交换律)
2012-04-10 22:54
344 查看
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称: * 作 者: 臧鹏 * 完成日期: 2012 年 4 月 10 日 * 版 本 号: * 对任务及求解方法的描述部分 * 输入描述: * 问题描述:在任务3的基础上扩展,分数类中的对象可以和整型数进行四则运算,且运算符合交换律。 * 程序输出: * 程序头部的注释结束 */ #include<iostream> using namespace std; class CFraction { private: int nume; // 分子 int deno; // 分母 public: CFraction(int nu=0,int de=1); //构造函数,初始化用 void Set(int nu=0,int de=1); //置值,改变值时用 friend CFraction operator+(CFraction &t1,CFraction &t2); friend CFraction operator-(CFraction &t1,CFraction &t2); friend CFraction operator*(CFraction &t1,CFraction &t2); friend CFraction operator/(CFraction &t1,CFraction &t2); friend CFraction operator-(CFraction &t);//取反 friend CFraction operator+(CFraction &t,int h); //整数在后 friend CFraction operator-(CFraction &t,int h); friend CFraction operator*(CFraction &t,int h); friend CFraction operator/(CFraction &t,int h); friend CFraction operator+(int h,CFraction &t); //整数在前 friend CFraction operator-(int h,CFraction &t); friend CFraction operator*(int h,CFraction &t); friend CFraction operator/(int h,CFraction &t); bool operator>(CFraction &t); bool operator<(CFraction &t); bool operator>=(CFraction &t); bool operator<=(CFraction &t); bool operator==(CFraction &t); bool operator!=(CFraction &t); void Simplify(int n); //化简(使分子分母没有公因子) int gcd(int x,int y); void output(int style=0); //输出:以8/6为例,style为0时,输出8/6;CFraction(int nu=0,int de=1); //构造函数,初始化用 }; CFraction::CFraction(int nu,int de) //构造函数 { nume = nu; deno = de; } void CFraction::Set(int nu,int de) //置值,改变值时用 { if(de !=0) { nume = nu; deno = de; } else { cout<<"分母不能为零"<<endl; exit(0); } nume = nu; deno = de; } void CFraction::Simplify(int n) //化简(使分子分母没有公因子) { n = gcd(nume,deno); nume = nume/n; deno = deno/n; } int CFraction::gcd(int x,int y) //求公约数的函数 { int r; while( y!= 0) { r = x%y; x = y; y = r; } return x; } void CFraction::output(int style) //输出:以8/6为例,style为0时,输出8/6;CFraction(int nu=0,int de=1); //构造函数,初始化用 { switch(style) { case 0: { cout<<nume<<'/'<<deno<<endl; break; } case 1: { int m = gcd(nume,deno); cout<<(nume/m)<<'/'<<(deno/m)<<endl; break; } case 2: { int nu,de,g; g = gcd(nume,deno); nu = nume/g; de = deno/g; cout<<(nu/de)<<"("<<(nu%de)<<'/'<<de<<")"<<endl; break; } default: { cout<<nume<<'/'<<deno<<endl; } } } CFraction operator+(CFraction &t1,CFraction &t2) { CFraction cf; if(t1.deno==t2.deno) //分母相同时加分子 { cf.deno=t1.deno; cf.nume=t1.nume+t2.nume; } else //否则同分比较分子 { cf.deno=t1.deno*t2.deno; cf.nume=t1.nume*t2.deno+t1.deno*t2.nume; } return cf; } CFraction operator-(CFraction &t1,CFraction &t2) { CFraction cf; if(t1.deno==t2.deno) { cf.deno=t1.deno; cf.nume=t1.nume-t2.nume; } else { cf.deno=t1.deno*t2.deno; cf.nume=t2.deno*t1.nume-t2.nume*t1.deno; } return cf; } CFraction operator*(CFraction &t1,CFraction &t2) { CFraction cf; cf.deno = t1.deno*t2.deno; cf.nume = t1.nume*t2.nume; return cf; } CFraction operator/(CFraction &t1,CFraction &t2) { CFraction cf; cf.deno = t1.deno*t2.nume; cf.nume = t1.nume*t2.deno; return cf; } CFraction operator-(CFraction &t)//取反 { CFraction cf; cf.deno = -t.deno; cf.nume = t.nume; return(cf); } bool CFraction::operator>(CFraction &t) { if(nume*t.deno>t.nume*deno) return true; else return false; } bool CFraction::operator<(CFraction &t) { if(nume*t.deno<t.nume*deno) return true; else return false; } bool CFraction::operator>=(CFraction &t) { if(nume*t.deno>t.nume*deno||nume*t.deno==t.nume*deno) return true; else return false; } bool CFraction::operator<=(CFraction &t) { if(nume*t.deno<t.nume*deno||nume*t.deno==t.nume*deno) return true; else return false; } bool CFraction::operator==(CFraction &t) { if(nume*t.deno==t.nume*deno) return true; else return false; } bool CFraction::operator!=(CFraction &t) { if(!operator==(t)) return true; else return false; } CFraction operator + (CFraction &t, int h) { CFraction t2 = t; t2.nume = t.nume + h * t.deno; return t2; } CFraction operator - (CFraction &t, int h) { CFraction t2 = t; t2.nume = t.nume - h * t.deno; return t2; } CFraction operator * (CFraction &t, int h) { CFraction t2 = t; t2.nume = t.nume * h; return t2; } CFraction operator / (CFraction &t, int h) { CFraction t2 = t; t2.deno = t.deno * h; return t2; } CFraction operator + (int h, CFraction &t) { CFraction t2 = t; t2.nume = t.nume + h * t.deno; return t2; } CFraction operator - (int h, CFraction &t) { CFraction t2 = t; t2.nume = h * t.deno - t.nume; return t2; } CFraction operator * (int h, CFraction &t) { CFraction t2 = t; t2.nume = h * t.nume; return t2; } CFraction operator / (int h, CFraction &t) { CFraction t2 = t; t2.nume = h * t.deno; t2.deno = t.nume; return t2; } int main () { CFraction cf1(17,5),cf2(9,6),cf3; int i =6; cout<<"cf1 ="; cf1.output (); cout<<"cf2 ="; cf2.output (); cf3=cf1*cf2; cout<<"cf1*cf2="; cf3.output(); cout<<"约分后:"; cf3.output(1); cout<<"化简后的形式:"; cf3.output(2); cf3=cf1-cf2; cout<<"cf1-cf2="; cf3.output(1); cf3=cf1+cf2; cout<<"cf1+cf2="; cf3.output(1); cf3=cf1/cf2; cout<<"cf1/cf2="; cf3.output(1); cf3=-cf1; cout<<"-cf1 ="; cf3.output(1); if (cf1>cf2) cout<<"cf1>cf2"<<endl; if (cf1<cf2) cout<<"cf1<cf2"<<endl; if (cf1==cf2) cout<<"cf1=cf2"<<endl; if (cf1!=cf2) cout<<"cf1≠cf2"<<endl; if (cf1>=cf2) cout<<"cf1≥cf2"<<endl; if (cf1<=cf2) cout<<"cf1≤cf2"<<endl; cout<<"i = 6"<<endl; cf3 = cf1 + i; cout<<"cf1 + i ="; cf3.output(1); cf3 = cf1 - i; cout<<"cf1 - i ="; cf3.output(1); cf3 = cf1 * i; cout<<"cf1 * i ="; cf3.output(1); cf3 = cf1 / i; cout<<"cf1 / i ="; cf3.output(1); cf3 = i + cf1 ; cout<<"i + cf1 ="; cf3.output(1); cf3 = i - cf1 ; cout<<"i - cf1 ="; cf3.output(1); cf3 = i * cf1 ; cout<<"i * cf1 ="; cf3.output(1); cf3 = i / cf1 ; cout<<"i / cf1 ="; cf3.output(1); system("pause"); return 0; }
相关文章推荐
- 第八周上机任务四--实现分数类中的运算符重载,分数类中的对象可以和整型数进行四则运算,且运算符合交换律
- C++第八周【任务四】分数类中的对象可以和整型数进行四则运算,且运算符合交换律。
- 在(1)的基础上,实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。
- 第八周实验报告任务4 实现分数类中的运算符重载,分数类中的对象可以和整数型进行四则运算,且运算符合交换律。
- 实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+
- 第八周任务四(分数类中的对象可以和整型数进行四则运算)
- C++中提供了多种基本的数据类型。实际上,这些远不能满足我们的需求,如复数(第10章的例子大多是处理虚数的),再如分数。本任务将设计一个简单的分数类,完成对分数的几个运算。一则巩固基于对象编程的方法,
- 第十五周C++【任务二】设计分数类,开发一个窗口式程序,可以完成分数的四则运算
- 写一个算法,使对象可以像数组一样进行foreach循环,要求属性必须是私有的(Interator模式的PHP5实现,写一类实现Interator接口)
- 《C++第九周实验报告3-1》----接第8周任务3,定义分数类中<<和>>运算符重载,实现分数的输入输出
- php中如何使对象可以像数组一样进行foreach循环
- 非“自动实现的属性”也可以用“对象初始值设定项”进行初始化
- C++中的初始化列表中可以对那些变量或对象进行初始化
- 使对象可以像数组一样进行foreach循环,要求属性必须是私有
- 例子:3、2不同种类的整型数据可以进行算术运算
- 分数类和整型数的四则运算(只写了加法)
- js中参数太长,可以传入对象的方式进行参数设置
- C++中的初始化列表中可以对那些变量或对象进行初始化
- java中创建file对象成功后,可以进行的操作
- Factory 定义一个接口,客户可以使用这个接口创建一个对象.同时,我们还可以控制对那个类进行实例化