C++程序设计实践指导1.6分数运算改写要求实现
改写要求:重载>>和<<实现分数类对象的直接输入输出,重载+完成多个分数对象加法
#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分数运算改写要求实现的更多相关文章
- C++程序设计实践指导1.2二维数组的操作运算改写要求实现
改写要求1:改写为以单链表表示二维数组 #include <cstdlib> #include <iostream> using namespace std; struct L ...
- C++程序设计实践指导1.15找出回文数改写要求实现
改写要求1:用单链表实现 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode ...
- C++程序设计实践指导1.14字符串交叉插入改写要求实现
改写要求:1:以指针为数据结构开辟存储空间 改写要求2:被插入字符串和插入字符串不等长,设计程序间隔插入 如被插入字符串长度为12,待插入字符串长度为5 则插入间隔为2 改写要求3:添加函数Inser ...
- C++程序设计实践指导1.13自然数集中找合数改写要求实现
改写要求1:用单链表实现 改写要求2:析构函数中依次将链表结点删除 #include <cstdlib> #include <iostream> using namespace ...
- C++程序设计实践指导1.12数组中数据线性变换改写要求实现
改写要求1:分别用指针pa.pb代替数组 改写要求2:从键盘输入data元素 元素个数任意,输入0结束 #include <cstdlib> #include <iostream&g ...
- C++程序设计实践指导1.10二维数组元素换位改写要求实现
改写要求1:改写为以单链表和双向链表存储二维数组 改写要求2:添加函数SingleLinkProcess()实现互换单链表中最大结点和头结点位置,最小结点和尾结点位置 改写要求3:添加函数Double ...
- C++程序设计实践指导1.1删除序列中相同的数改写要求实现
改写要求1:改写为以指针为数据结构 #include <iostream> #include <cstdlib> using namespace std; class ARP ...
- C++程序设计实践指导1.7超长数列中n个数排序改写要求实现
改写要求1:将以上程序改写为适合超长整数 改写要求2:将以上程序改写为适合超长数列 改写要求3:将数列中指定位置m开始的n个结点重新按降序排序 改写要求4:输出指定位置m开始的n个结点的超长整数 #i ...
- C++程序设计实践指导1.9统计与替换字符串中的关键字改写要求实现
改写要求1:将字符数组str改为字符指针p,动态开辟存储空间 改写要求2:增加统计关键字个数的函数void CountKeyWords() 改写要求3: 增加替换函数void FindKeyWords ...
随机推荐
- nginx搭建flv、mp4流媒体服务
1.安装pcre-8.33.tar.bz2 #tar -xvf 1.pcre-8.33.tar.bz2 #cd pcre-8.33/ #./configure #make && mak ...
- iOS 面试基础题
1.UIWindow和UIView和 CALayer 的联系和区别? 答:UIView是视图的基类,UIViewController是视图控制器的基类,UIResponder是表示一个可以在屏幕上响应 ...
- 编译错误“The run destination My Mac 64-bit is not valid for Running the scheme '***',解决办法
1. iOS APP Project or Mac APP Project编译错误提示: “The run destination My Mac 64-bit is not valid for Ru ...
- 关于css的优先级
样式的优先级 外部样式 < 内部样式 < 内联样式 选择器的优先权 解释: 1. 内联样式表的权值最高 1000: 2. ID 选择器的权值为 100; 3. Class 类选择器的 ...
- YUI Array 之hash
hash就是把两个参数合并成一个类似hashMap结构的对象,用第一个数组的元素为key,第二个的为value,如果第二个参数未指定,则把对象的对应的值置为true YUI原码 YUI hashYAr ...
- [原]此程序专用来说明C++模板的用法
#include using namespace std; //此程序专用来说明模版的使用 template void swap1(T& a,T& b){ T temp=a; ...
- PHP结合Linux的cron命令实现定时任务
PHP死循环 来处理定时任务的效率是很低的.(众多网友评价)大家都建议使用Linux内置的定时任务crontab命令来调用php脚本来实现. PHP定时任务的两种方法:1.web方式调用php网页,但 ...
- python----特性003
python特性003:计算特性 #!/usr/local/bin/python3.5 class MyNumber(object): def __init__(self,number): self. ...
- 编译安装hph
一.安装相关的依赖: yum -y install gcc gcc-c++ bzip2-devel libjpeg-devel libpng-devel freetype-devel libxml2- ...
- Android IntentService 与Alarm开启任务关闭任务
1:MyService public class MyService extends IntentService{ AlarmManager alarmManager = null; PendingI ...