C++课程设计分数计算器.doc
C+课程设计报告 题目:分数计算器 学号: 姓名: 班级:指导教师: 提交日期:2011-9-15 一、 程序功能简介分数计算及测试程序,具有对输入的运算进行“+-*/”的功能。二、 课程设计要求1、 将主程序设计成一个分数计算器。2、 可完成“+-*/等基本运算。3、 可将分数化为十进制小数和带分数。4、 可执行“+-*/”(含括号)四则运算。三、 设计思路利用程序进行加法 减法 乘法 除法四种基本运算,并约分。主要用到的算法:最小公倍数,最大公约数,交换。加法的原理:结果的分子 是两个数的分子分母交换相乘相加的和,结果的分母是 两分母的最小公倍数。减法的原理与加法类似。乘法的原理:分母相乘,分子相乘。除法的原理:第二个分数上下交换后,使用乘法原理。约分的原理:分子分母分别除以分子分母的最大公约数得到的数组合成新的分子分母。四、 关键源代码注解 /*#ifndef _TGR_FRACTIONS_H#define _TGR_FRACTIONS_H*/#include <iostream.h>#include <conio.h>#include <stdlib.h>#include <math.h>/#include <bool.h>#include <string.h>class fraction /分数类定义public: fraction();/constructorsfraction(int initn, int initd);/accessorsint numerator() const; /取分子,const成员函数,不能修改数据成员int denominator() const; /取分母double decimal() const; /将分数转换为对应的小数fraction absval() const; /分数的绝对值/modifiersvoid setnum(int newnum); /设置分子void setden(int newden); /设置分母void simplify(); /约分void recip(); /分子分母转置const fraction & operator+= (int rhs);private:int n;int d;/free (nonmember) functions/miscellanyvoid simple(); /"简单计算"函数void turn(); /"转换"函数void test(); /测试函数void complex(); /"混合运算"函数void exam(); /"随机测试题"函数int GCF(int x, int y); /求x,y的最大公因子/addition operatorsfraction operator+ (const fraction &lhs, const fraction &rhs);fraction operator+ (const fraction &lhs, int rhs);/subtraction operatorsfraction operator- (const fraction &lhs, const fraction &rhs);fraction operator- (const fraction &lhs, int rhs);/multiplication operatorsfraction operator* (const fraction &lhs, const fraction &rhs);fraction operator* (const fraction &lhs, int rhs);/division operatorsfraction operator/ (const fraction &lhs, const fraction &rhs);fraction operator/ (const fraction &lhs, int rhs);/power operatorfraction operator (const fraction &base, int exp); /(幂)运算符重载/comparison operatorsbool operator= (const fraction &lhs, const fraction &rhs);bool operator!= (const fraction &lhs, const fraction &rhs);bool operator>= (const fraction &lhs, const fraction &rhs);bool operator<= (const fraction &lhs, const fraction &rhs);bool operator> (const fraction &lhs, const fraction &rhs);bool operator< (const fraction &lhs, const fraction &rhs);bool operator= (const fraction &lhs, int rhs);bool operator!= (const fraction &lhs, int rhs);bool operator>= (const fraction &lhs, int rhs);bool operator<= (const fraction &lhs, int rhs);bool operator> (const fraction &lhs, int rhs);bool operator< (const fraction &lhs, int rhs);/ I/O operators/functionsostream & operator<< (ostream &os, const fraction &f); / 输出运算符重载istream & operator>> (istream &is, fraction &f); / 输入运算符重载void PrintAsMixed(ostream &os, const fraction &f); / 输出带分数/*#endif*/* fraction.cppFractions class - implementation file-分数类执行文件开始*/#include "fraction.h"fraction:fraction() : n(0), d(1) ; /缺省构造函数(=0/1)fraction:fraction(int initn, int initd) : n(initn), d(initd) ;int fraction:numerator() const /取分子return n;int fraction:denominator() const /取分母return d;double fraction:decimal() const /分数转换小数return double(n) / double(d);fraction fraction:absval() const /取分数绝对值fraction returnValue(abs(n), abs(d); /通过构造函数取分母分子绝对值return returnValue;void fraction:setnum(int newnum) /设置分子的值n = newnum;void fraction:setden(int newden) /设置分母的值if(newden != 0) /分母非零时设置d = newden;else /若分母为零,显示错误信息并退出cout << "错误:除数为零!n" << "按任意键终止!" getch(); abort(); /退出void fraction:simplify() /分数约分int r = GCF(n, d);n /= r;d /= r;void fraction:recip() /分子分母交换int temp = n;n = d;d = temp;const fraction & fraction:operator+= (int rhs) /重载"+=" fraction newrhs(rhs, 1); /将作为参数的整数RHS转换为分数类的对象 return (*this+newrhs); /int GCF(int x, int y) /辗转相除求x,y的最大公约数int r = x%y;if(r=0)return y;elsereturn GCF(y, r);fraction operator+ (const fraction &lhs, const fraction &rhs) /重载"+",完成两分数相加情况fraction sum; /定义分数类对象,通过两参数对象对其赋值int newnum = (lhs.numerator() * rhs.denominator()+(rhs.numerator() * lhs.denominator();/得到相加后的分子int newden = (lhs.denominator() * rhs.denominator(); /得到相加后的分母sum.setnum(newnum); /设置分子sum.setden(newden); /设置分母sum.simplify(); /分数约分return sum;fraction operator+ (const fraction &lhs, int rhs) /重载"+"分数与整数相加 fraction newrhs(rhs, 1); /将RHS转换为分数类对象 return lhs + newrhs;fraction operator- (const fraction &lhs, const fraction &rhs)/重载"-"完成两分数相减fraction newrhs = rhs;newrhs.setnum(-rhs.numerator(); /设置分母为其相反数return lhs + newrhs; /利用"+"的重载fraction operator- (const fraction &lhs, int rhs) /重载"-"完成分数与整数的相减 fraction newrhs(rhs, -1); return lhs + newrhs; /利用"+"的重载fraction operator* (const fraction &lhs, const fraction &rhs) /重载"*"完成两分数相乘fraction product;int newnum = lhs.numerator() * rhs.numerator(); /分子相乘int newden = lhs.denominator() * rhs.denominator(); /分母相乘product.setnum(newnum); /设置分子product.setden(newden); /设置分母product.simplify(); /分数约分return product;fraction operator* (const fraction &lhs, int rhs) /重载"*"完成分数与整数的相乘 fraction prod(lhs.numerator() * rhs, lhs.denominator(); /通过构造函数完成赋值 prod.simplify(); /分数约分 return prod;fraction operator/ (const fraction &lhs, const fraction &rhs) /重载"/"完成两分数相除 if(rhs = 0) /若分母为零,显示错误信息并退出 cout << "Error: Division by zero attempted!n" << "Press any key to terminate" getch(); abort(); /退出 fraction newrhs = rhs;newrhs.recip(); /将作分母的分数倒置return lhs * newrhs; /利用分数乘法fraction operator/ (const fraction &lhs, int rhs) /重载"/"完成分数与整数的相除 if(rhs = 0) /若分母为零,显示错误信息并退出 cout << "错误:除数为零!n" << "按任意键终止!" getch(); abort(); fraction quot(lhs.numerator(), lhs.denominator() * rhs); /通过构造函数完成赋值 quot.simplify(); /分数约分 return quot;fraction operator (const fraction &base, int exp)fraction returnValue;returnValue.setnum(pow(base.numerator(), exp); /通过函数完成 returnValue.setden(pow(base.denominator(), exp); /分子分母幂乘return returnValue;bool operator= (const fraction &lhs, const fraction &rhs) /重载"="判断两分数是否相等 return lhs.decimal() = rhs.decimal();bool operator!= (const fraction &lhs, const fraction &rhs) /重载"!="判断两分数是否不等return lhs.decimal() != rhs.decimal();bool operator>= (const fraction &lhs, const fraction &rhs) /重载">="判断前分数是否大于等/于后分数 return lhs.decimal() >= rhs.decimal();bool operator<= (const fraction &lhs, const fraction &rhs) /重载"<="判断前分数是否小于等/于后分数 return lhs.decimal() <= rhs.decimal();bool operator> (const fraction &lhs, const fraction &rhs) /重载">"判断前分数是否大于/后分数 return lhs.decimal() > rhs.decimal();bool operator< (const fraction &lhs, const fraction &rhs) /重载"<"判断前分数是否小于/后分数return lhs.decimal() < rhs.decimal();bool operator= (const fraction &lhs, int rhs) /重载"="判断分数与整数是否相等 return lhs.decimal() = rhs;bool operator!= (const fraction &lhs, int rhs) /重载"!="判断分数与整数是否不等 return lhs.decimal() != rhs;bool operator>= (const fraction &lhs, int rhs) /重载">="前分数是否大于等于整数 return lhs.decimal() >= rhs;bool operator<= (const fraction &lhs, int rhs) /重载"<="前分数是否小于等于整数 return lhs.decimal() <= rhs;bool operator> (const fraction &lhs, int rhs) /重载">"前分数是否大于整数 return lhs.decimal() > rhs;bool operator< (const fraction &lhs, int rhs) /重载"<"前分数是否小于整数 return lhs.decimal() = rhs;ostream & operator<< (ostream &os, const fraction &f) /重载输出运算符fraction temp = f.absval();int numer = f.numerator();int denom = f.denominator();if(!(numer > 0 && denom > 0) | (numer < 0 && denom < 0) && f != 0) os << '-' /分子分母不同号就输出"-"if(temp.numerator() = 0 | temp.denominator() = 1)os << temp.numerator(); / 分子为0或分母为1时就输出分子elseos << temp.numerator() << '/' << temp.denominator();/其他情况输出此分数return os; / istream & operator>> (istream &is, fraction &f) /is:输入流,f:待输入的分数int newnum, newden;char slash = ' 'dois >> newnum >> slash >> newden; / while(slash != '/');f.setnum(newnum);f.setden(newden);return is; /将分数f按整数,真分数格式输出到流osvoid PrintAsMixed(ostream &os, const fraction &f) int wholePart = f.numerator() / f.denominator();/wholepart获得整数部分 fraction fracPart = f - wholePart; /fracpart获得真分数部分cout <<"带分数为:" if(wholePart != 0) cout << wholePart << " " /输出整数部分 if(fracPart != 0) cout << fracPart; /输出真分数部分/准备工作结束,下面开始主函数。void main()int choice;char CLS;cout<<"是否清屏?(Y/N)n"cin>>CLS;if(CLS='Y'|CLS='y')system("cls"); /getch();cout<<" 欢迎使用分数计算器!n"cout<<" n" cout<<" n"cout<<" <1>简单计算 n" cout<<" n"cout<<" <2>分数化为十进制小数和带分数 n" cout<<" n"cout<<" <3>分数测试 n" cout<<" n"cout<<" <4>分数混合运算 n" cout<<" n"cout<<" <5>随机测试题 n" cout<<" n" cout<<" n" cout<<"请选择:n" /选择功能cin>>choice; if(choice>6|choice<1) cout<<"输入无效,请重新选择:n" main(); /回到主菜单 else switch(choice) /进入函数 case 1: simple();break; case 2: turn();break; case 3: test();break; case 4: complex();break; case 5: exam();break; /main(); /- void simple() /"简单计算"函数 fraction f1; fraction f2; fraction sum; char fuhao,yn; int f3,kind;choose: cout<<" <1>分数与分数n" cout<<" <2>分数与整数n" cout<<" <3>返回n" cout<<"请选择:n" cin>>kind; if(kind=1) /分数之间计算 cout<<"请输入第一个分数:" /输入数据cin>>f1;cout<<"n请输入第二个分数:"cin>>f2;cout<<"n请输入运算符:"cin>>fuhao;f1.simplify(); /分数约分f2.simplify();switch(fuhao) /进入计算 case '+':cout<<"nsum="<<f1+f2<<endl;break;case '-':cout<<"nsum="<<f1-f2<<endl;break;case '*':cout<<"nsum="<<f1*f2<<endl;break;case '/':cout<<"nsum="<<f1/f2<<endl;break; else if(kind=2) /分数与整数之间运算cout<<"请输入一个分数:"cin>>f1;cout<<"n请输入一个整数:"cin>>f3;cout<<"n请输入运算符:"cin>>fuhao;f1.simplify();switch(fuhao) /进入计算case '+':cout<<"nsum="<<f1+f3<<endl;break;case '-':cout<<"nsum="<<f1-f3<<endl;break;case '*':cout<<"nsum="<<f1*f3<<endl;break;case '/':cout<<"nsum="<<f1/f3<<endl;break;/case '':cout<<"nsum="<< f1 f3 <<endl;break;else if(kind=3)main(); /返回else cout<<"输入错误!请重新选择:n"goto choose; getch(); cout<<endl; cout<<"是否继续?(Y/N)n" /询问继续与否 cin>>yn; if(yn='Y'|yn='y')simple(); else main(); /返回菜单 /- void turn() /"转换"函数 fraction f1; fraction Result; char yn; cout<<"请输入一个分数:n" cin>>f1; cout<<"对应小数为:"<<f1.decimal()<<endl;/使用"分转小"函数 if(f1>1) /输出带分数 PrintAsMixed(cout,f1); else cout<<"此分数为真分数!"<<endl; getch(); cout<<endl; cout<<"是否继续?(Y/N)n" cin>>yn; if(yn='Y'|yn='y')turn(); else main(); /- void test() /测试函数 char yn; fraction f1; fraction f2; /定义两个分数f1,f2 int i; / clrscr(); /利用>>重载输入f1,f2 cout << "第一个分数:n" cin >> f1; cout << "n第二个分数:n" cin >> f2; cout << "n输入一个整数:n" cin >> i; f1.simplify(); f2.simplify(); cout<<"对两分数进行+,-,*,/运算n" fraction sum = f1 + f2; fraction diff = f1 - f2; fraction prod = f1 * f2; fraction quot; if(f2 != 0) quot = f1 / f2; cout<<"输出小数运算结果n" cout << f1 << " 十进制为: " << f1.decimal() << 'n' cout << f2 << " 十进制为: " << f2.decimal() << "nn" cout<<"利用<<重载输出运算结果n" cout << f1 << " + " << f2 << " = " << sum << " (" << sum.decimal() << ")n" cout << f1 << " - " << f2 << " = " << diff << " (" << diff.decimal() << ")n" cout << f1 << " * " << f2 << " = " << prod << " (" << prod.decimal() << ")n" cout << f1 << " / " << f2 << " = " if(f2 != 0) cout << quot << "