资源简介
1.自定义分数类fraction,使用该类可以完成分数的输入、分数的加、减、乘、除二目运算和一目减运算、分数的约分操作、分数的倒数运算、对两个分数进行六种比较运算、以及对分数的输出等操作。
2.将其中使用的普通函数尽量改写成运算符重载函数。如,可重载+、-、*、/、>、>=、<、<=、==、!=,以实现分数的各种运算。并且实现分数与整数的混合运算。
3.尽可能考虑程序的性能。
评分标准
1. 对两个类的成员合理地使用访问权限:1分
2. 实现分数与整数的混合运算:1分
3. 程序结构合理,布局清晰,正确地安排.h和.cpp:2分
4. 考虑程序的性能:1分
这个作业可以用到以下C++知识:
定义类,运算符重载、类型转换。

代码片段和文件信息
/*
* Copyright (c) 2012 Zhejiang University
* All rights reserved.
* file name :fraction.cpp
* version :1.0
* author :Song Yang
* date :2012-12-12
*/
# include
# include
# include
# include “fraction.h“
fraction::fraction(int numer){
this->numerator = numer;
this->denominator = 1;
}
fraction::fraction(int numerint denomin){
this->numerator = numer;
this->denominator = denomin;
}
fraction::fraction(const fraction& other){
numerator = other.numerator;
denominator = other.denominator;
}
fraction& fraction::operator=(const fraction &other){
numerator = other.numerator;
denominator = other.denominator;
return *this;
}
std::string fraction::toString(){
std::string fractionStr;
std::ostringstream os;
os<numerator;
if(this->denominator != 1){
os<<“/“;
os<denominator;
}
return os.str();
}
void fraction::input(){
std::cout<<“input a fraction (e.g. -2/24):“< std::string tmpStr;
std::cin>>tmpStr;
size_t found = tmpStr.find(‘/‘);
std::string tmpNumStrtmpDenomStr;
if(found !=std::string::npos){
tmpNumStr = tmpStr.substr(0found);
tmpDenomStr = tmpStr.substr(found+1);
this->numerator = atoi(tmpNumStr.c_str());
this->denominator = atoi(tmpDenomStr.c_str());
}
else{
this->numerator = atoi(tmpStr.c_str());
this->denominator = 1;
}
std::cout<<“input fraction success:“<toString()< std::cout<<“-----------------------------------------“< }
void fraction::reduce(){
int gcdNum = this->gcd(numeratordenominator);
int tmpNum = numerator / gcdNum;
int tmpDen = denominator / gcdNum;
numerator = tmpNum;
denominator = tmpDen;
}
fraction fraction::opposite(void){
fraction reciprocalFrac(-numerator denominator);
reciprocalFrac.reduce();
return reciprocalFrac;
}
fraction fraction::reciprocal(void){
fraction reciprocalFrac(denominator numerator);
if (numerator < 0)
{
reciprocalFrac.denominator *= -1;
reciprocalFrac.numerator *= -1;
}
reciprocalFrac.reduce();
return reciprocalFrac;
}
fraction fraction::add(const fraction &other){
int tmpNum = numerator*other.denominator + denominator*other.numerator;
int tmpDen = denominator*other.denominator;
fraction sumResult(tmpNumtmpDen);
sumResult.reduce();
return sumResult;
}
fraction fraction::minus(const fraction &other){
int tmpNum = numerator*other.denominator - denominator*other.numerator;
int tmpDen = denominator*other.denominator;
fraction diffResult(tmpNumtmpDen);
diffResult.reduce();
return diffResult;
}
fraction fraction::multiple(const fraction &other){
int tmpNum = numerator*other.numerator;
int tmpDen = denominator*other.denominator;
fraction multipleResult(tmpNumtmpDen);
multipleResult.reduce();
return multipleResult;
}
fraction fraction::divide(const fraction &other){
int tmpNum = numerator*other.denominator;
int tmpDen = denominator*other.numerator;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 81920 2012-12-13 05:28 Fraction\debug\Fraction.exe
文件 439716 2012-12-13 05:28 Fraction\debug\Fraction.ilk
文件 543744 2012-12-13 05:28 Fraction\debug\Fraction.pdb
文件 8906 2012-12-13 05:28 Fraction\Fraction\Debug\BuildLog.htm
文件 403 2012-12-13 05:28 Fraction\Fraction\Debug\Fraction.exe.em
文件 468 2012-12-13 05:28 Fraction\Fraction\Debug\Fraction.exe.em
文件 385 2012-12-13 05:28 Fraction\Fraction\Debug\Fraction.exe.intermediate.manifest
文件 113735 2012-12-13 05:27 Fraction\Fraction\Debug\fraction.obj
文件 65 2012-12-13 05:28 Fraction\Fraction\Debug\mt.dep
文件 94222 2012-12-13 05:28 Fraction\Fraction\Debug\testFraction.obj
文件 224256 2012-12-13 05:28 Fraction\Fraction\Debug\vc80.idb
文件 208896 2012-12-13 05:28 Fraction\Fraction\Debug\vc80.pdb
文件 7021 2012-12-13 04:51 Fraction\Fraction\fraction.cpp
文件 2191 2012-12-13 04:49 Fraction\Fraction\fraction.h
文件 4166 2012-12-13 03:44 Fraction\Fraction\Fraction.vcproj
文件 1423 2012-12-13 05:28 Fraction\Fraction\Fraction.vcproj.laiconglin-PC.laiconglin.user
文件 687 2012-12-13 05:16 Fraction\Fraction\funcUtil.h
文件 4096 2012-12-13 05:28 Fraction\Fraction\testFraction.cpp
文件 1403904 2012-12-13 05:28 Fraction\Fraction.ncb
文件 889 2012-12-12 02:04 Fraction\Fraction.sln
..A..H. 13312 2012-12-13 05:28 Fraction\Fraction.suo
目录 0 2012-12-13 05:28 Fraction\Fraction\Debug
目录 0 2012-12-13 05:28 Fraction\debug
目录 0 2012-12-13 05:28 Fraction\Fraction
目录 0 2012-12-12 03:42 Fraction
----------- --------- ---------- ----- ----
3154405 25
相关资源
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
- VC++基于OpenGL模拟的一个3维空间模型
评论
共有 条评论