第八周任务四(分数类中的对象可以和整型数进行四则运算)
2012-04-16 18:42
375 查看
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:分数类 * 作 者:冯珍珍 * 完成日期: 2012 年 4 月 16 日 * 版 本 号:略 * 对任务及求解方法的描述部分 * 输入描述: * 问题描述: 在任务3的基础上拓展。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。 * 程序输出: * 程序头部的注释结束 */ #include <iostream> using namespace std; class CFraction { private: int nume; // 分子 int deno; // 分母 public: CFraction(int nu=0,int de=1):nume(nu),deno(de){} void simplify(); void display(); //以下为用友员函数实现分数与整数的四则运算的函数声明部分 friend CFraction operator+(int i, const CFraction &c); //新加方法 friend CFraction operator-(int i, const CFraction &c); //新加方法 friend CFraction operator*(int i, const CFraction &c); //新加方法 friend CFraction operator/(int i, const CFraction &c); //新加方法 friend CFraction operator+(const CFraction &c, int i); //新加方法 friend CFraction operator-(const CFraction &c, int i); //新加方法 friend CFraction operator*(const CFraction &c, int i); //新加方法 friend CFraction operator/(const CFraction &c, int i); //新加方法 CFraction operator+(); //取正一目运算 CFraction operator-(); //取反一目运算 bool operator>(const CFraction &c); bool operator<(const CFraction &c); bool operator==(const CFraction &c); bool operator!=(const CFraction &c); bool operator>=(const CFraction &c); bool operator<=(const CFraction &c); }; // 分数化简 void CFraction::simplify() { int m, n, r; m=abs(deno); n=abs(nume); while(r=m%n) { m=n; n=r; } deno /= n; nume /= n; if (deno < 0) { deno =- deno; nume =- nume; } } void CFraction::display() { cout << "(" << nume << "/" << deno << ")" << endl; } CFraction CFraction:: operator+() { return *this; } CFraction CFraction:: operator-() { CFraction x; x.nume = -nume; x.deno = deno; return x; } bool CFraction::operator>(const CFraction &c) { int n, a, d; n = nume * c.deno; a = c.nume * deno; d = deno * c.deno; if (n > a && d > 0 || n < a && d < 0) { return true; } return false; } bool CFraction::operator<(const CFraction &c) { int n, a, d; n = nume * c.deno; a = c.nume * deno; d = deno * c.deno; if ((n - a) * d < 0) { return true; } else { return false; } } bool CFraction::operator==(const CFraction &c) { if (*this!=c) { return false; } else { return true; } } bool CFraction::operator!=(const CFraction &c) { if (*this > c || *this < c) { return true; } else { return false; } } bool CFraction::operator>=(const CFraction &c) { if (*this<c) { return false; } else { return true; } } bool CFraction::operator<=(const CFraction &c) { if (*this > c) { return false; } else { return true; } } //新加方法: CFraction operator+ (const CFraction &c, int i) { CFraction t; t.nume = c.nume + c.deno * i; t.deno = c.deno; t.simplify(); return t; } CFraction operator- (const CFraction &c, int i) { CFraction t; t.nume = c.nume - i * c.deno; t.deno = c.deno; t.simplify(); return t; } CFraction operator* (const CFraction &c, int i) { CFraction t; t.nume = c.nume * i; t.deno = c.deno; t.simplify(); return t; } CFraction operator/(const CFraction &c, int i) { CFraction t; t.deno *= i; t.nume = c.nume; t.simplify(); return t; } CFraction operator+ (int i, const CFraction &c) { CFraction t; t.nume = c.nume + c.deno * i; t.deno = c.deno; t.simplify(); return t; } CFraction operator- (int i, const CFraction &c) { CFraction t; t.nume = c.nume - i * c.deno; t.deno = c.deno; t.simplify(); return t; } CFraction operator* (int i, const CFraction &c) { CFraction t; t.nume = c.nume * i; t.deno = c.deno; t.simplify(); return t; } CFraction operator/(int i, const CFraction &c) { CFraction t; t.deno *= i; t.nume = c.nume; t.simplify(); return t; } int main() { CFraction x(1,3),y(-5,10),s; x.display(); y.display(); if (x>y) cout<<"x大于y"<<endl; if (x<y) cout<<"x小于y"<<endl; if (x==y) cout<<"x等于y"<<endl; cout<<endl; cout << "分数与整数相加的实现:" << endl; cout << "x + 5 = "; s = x + 5; s.display(); cout << "5 + x = " ; s = 5 + x; s.display(); cout << "x - 5 = " ; s = x - 5; s.display(); cout << "5 - x = " ; s = 5 - x; s.display(); cout << "x / 5 = " ; s = x / 5; s.display(); cout << "5 / x = " ; s = 5 / x; s.display(); cout << "x * 5 = " ; s = x * 5; s.display(); cout << "5 * x = " ; s = 5 * x; s.display(); system("pause"); return 0; }
相关文章推荐
- C++第八周【任务四】分数类中的对象可以和整型数进行四则运算,且运算符合交换律。
- 第八周上机任务四--实现分数类中的运算符重载,分数类中的对象可以和整型数进行四则运算,且运算符合交换律
- 第八周实验报告任务4 实现分数类中的运算符重载,分数类中的对象可以和整数型进行四则运算,且运算符合交换律。
- 第8周任务4(分数类中的对象可以和整型数进行四则运算,且满足交换律)
- 在(1)的基础上,实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。
- 实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+
- 第八周实验任务三--实现分数类中的运算符重载并可以完成分数的加减乘除、求反、比较的运算
- 第八周实验任务三--实现分数类中的运算符重载并可以完成分数的加减乘除、求反、比较的运算
- c++第八周【任务3】实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算。
- 第十五周C++【任务二】设计分数类,开发一个窗口式程序,可以完成分数的四则运算
- 第八周实验报告任务3 实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除,求反,比较6种关系的运算。
- 第八周任务三实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算。
- Composite 合成模式可以让客户程序把单个基本对象和对象的合成用一种统一的方式进行处理
- 第八周任务3:实现分数类中的运算符重载
- $.ajax() 方法中对data值进行封装。serializeJson() 可以是form表单,也可以是单个元素对象
- java Vecotr 读出的对象都需要进行显式转换才可以正确使用
- 《C++第八周实验报告3-1》---实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算
- 分数类中的运算符重载(第八周任务三)
- 抽象类和接口作为形式参数 多态 抽象类和接口虽然都不能直接创建对象,但是可以通过多态进行对象的创建
- 今天无意中发现JavaBean类基本都要求实现了Serializable接口,以前只是知道序列化以后,可以通过io流的方式将对象序列化和反序列化,进行存取,但不知道为什么需要序列化,今天总结一下