改写要求:重载>>和<<实现分数类对象的直接输入输出,重载+完成多个分数对象加法

#include <cstdlib>
#include <iostream> using namespace std;
class Fract
{ int num,den;
public:
friend ostream& operator << (ostream& output,Fract& temp);
friend istream& operator >> (istream& intput,Fract& temp1);
Fract operator + (Fract& temp1);
int gcd(int m,int n)
{
int r;
if(m<n)
{
r=m;m=n;n=r;
}
while(r=m%n)
{
m=n;
n=r;
}
return n;
} }; istream& operator >>(istream& input,Fract& temp1)
{
input>>temp1.num>>temp1.den;
return input;
} ostream& operator <<(ostream& output,Fract& temp)
{
output<<temp.num<<"/"<<temp.den<<endl;
return output;
} Fract Fract::operator + (Fract& temp1)
{
Fract sum;
int x,y,temp;
x=num*temp1.den+den*temp1.num;
y=den*temp1.den;
temp=gcd(x,y);
x=x/temp;
y=y/temp;
sum.num=x;
sum.den=y;
return sum;
} int main(int argc, char *argv[])
{
Fract temp1,temp2,temp;
cin>>temp1>>temp2;
temp=temp1+temp2;
cout<<temp;
system("PAUSE");
return EXIT_SUCCESS;
}

C++程序设计实践指导1.6分数运算改写要求实现的更多相关文章

  1. C++程序设计实践指导1.2二维数组的操作运算改写要求实现

    改写要求1:改写为以单链表表示二维数组 #include <cstdlib> #include <iostream> using namespace std; struct L ...

  2. C++程序设计实践指导1.15找出回文数改写要求实现

    改写要求1:用单链表实现 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode ...

  3. C++程序设计实践指导1.14字符串交叉插入改写要求实现

    改写要求:1:以指针为数据结构开辟存储空间 改写要求2:被插入字符串和插入字符串不等长,设计程序间隔插入 如被插入字符串长度为12,待插入字符串长度为5 则插入间隔为2 改写要求3:添加函数Inser ...

  4. C++程序设计实践指导1.13自然数集中找合数改写要求实现

    改写要求1:用单链表实现 改写要求2:析构函数中依次将链表结点删除 #include <cstdlib> #include <iostream> using namespace ...

  5. C++程序设计实践指导1.12数组中数据线性变换改写要求实现

    改写要求1:分别用指针pa.pb代替数组 改写要求2:从键盘输入data元素 元素个数任意,输入0结束 #include <cstdlib> #include <iostream&g ...

  6. C++程序设计实践指导1.10二维数组元素换位改写要求实现

    改写要求1:改写为以单链表和双向链表存储二维数组 改写要求2:添加函数SingleLinkProcess()实现互换单链表中最大结点和头结点位置,最小结点和尾结点位置 改写要求3:添加函数Double ...

  7. C++程序设计实践指导1.1删除序列中相同的数改写要求实现

    改写要求1:改写为以指针为数据结构 #include <iostream> #include <cstdlib> using namespace std; class ARP ...

  8. C++程序设计实践指导1.7超长数列中n个数排序改写要求实现

    改写要求1:将以上程序改写为适合超长整数 改写要求2:将以上程序改写为适合超长数列 改写要求3:将数列中指定位置m开始的n个结点重新按降序排序 改写要求4:输出指定位置m开始的n个结点的超长整数 #i ...

  9. C++程序设计实践指导1.9统计与替换字符串中的关键字改写要求实现

    改写要求1:将字符数组str改为字符指针p,动态开辟存储空间 改写要求2:增加统计关键字个数的函数void CountKeyWords() 改写要求3: 增加替换函数void FindKeyWords ...

随机推荐

  1. jQ全选效果

    <ul id="list"> <li><label><input type="checkbox" value=&quo ...

  2. C++中const

    [const] 0.普通const对象定义在栈空间中 { ; ; cout << &a << ' ' << &b; } Result: 0x22ab ...

  3. browsersync 浏览器自动刷新神器

    官网:http://www.browsersync.cn/ 基于node,具体安装方法和使用方法参见官网,可以结合gulp等构建工具来用,也可以单独使用.不错~

  4. C# 根据URL返回HTML_根据URL获取图片信息/缩略图

    /// <summary> /// 根据URL 返回HTML /// </summary> private List<string> GetHtmlByUrl(st ...

  5. 生成excel文件

    java操作Excel最常用的开源组件有poi与jxl.jxl是韩国人开发的,发行较早,但是更新的很慢,目前似乎还不支持excel2007.poi是apache下的一个子项目,poi应该是处理ms的o ...

  6. 诺基亚HERE地图

    1.基本图层 2.3D图层 3.卫星图层 4.地形图层 5.在线帮助

  7. Remove Duplicates from Sorted Array 解答

    Question Given a sorted array, remove the duplicates in place such that each element appear only onc ...

  8. HDU 3308 LCIS(线段树单点更新区间合并)

    LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...

  9. 对Linux 专家非常有用的20 个命令

    谢谢你你给了我们在这篇文章前两个部分的喜欢,美言和支持.在第一部分文章中我们讨论了那些都只是切换到 Linux 和linux新手所需的必要知识的用户的命令. 对 Linux 新手非常有用的 20 个命 ...

  10. UISearchBar 点击X 按钮收键盘

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;{ NSLog(@"textD ...