题目描写叙述

  1. For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference,
  2.  
  3. product and quotient.

输入描写叙述:

  1. Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2".
  2.  
  3. The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in
  4.  
  5. front of the numerator. The denominators are guaranteed to be non-zero numbers.

输出描写叙述:

  1. For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each
  2.  
  3. line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is
  4.  
  5. the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the
  6.  
  7. denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.

输入样例:

  1. 5/3 0/6

输出样例:

  1. 1 2/3 + 0 = 1 2/3
  2.  
  3. 1 2/3 - 0 = 1 2/3
  4.  
  5. 1 2/3 * 0 = 0
  6.  
  7. 1 2/3 / 0 = Inf
  8. #include <iostream>
  9. #include <cmath>
  10. using namespace std;
  11. //分数的结构体
  12. typedef struct fraction
  13. {
  14. 	int isNegative;	//正负号
  15. 	long int i,numerator,denominator;	//整数部分、分子、分母
  16. 	//初始化分数
  17. 	fraction():isNegative(1),i(0),numerator(0),denominator(1)
  18. 	{
  19. 	};
  20. }fraction;
  21. //重载 == 操作符
  22. bool operator==(fraction& f, long int x)
  23. {
  24. 	return f.numerator==0 && f.i*f.isNegative==x;
  25. }
  26. //myabs_求绝对值
  27. long int myabs(long int x)
  28. {
  29. 	if(x<0)
  30. 		return -x;
  31. 	return x;
  32. }
  33. //将分数转换为要求的形式
  34. fraction simply(long int lhs,long  int rhs)
  35. {
  36. 	fraction temp;
  37. 	if(0==lhs*rhs)
  38. 		return temp;
  39. 	if(lhs*rhs<0)
  40. 	{
  41. 		lhs=myabs(lhs);
  42. 		rhs=myabs(rhs);
  43. 		temp.isNegative=-1;
  44. 	}
  45. 	//化简为整数+分数形式
  46. 	temp.i=lhs/rhs;
  47. 	lhs%=rhs;
  48. 	//假设化简后,分子为0。如12/3化简后为4,则返回
  49. 	if(0==lhs)
  50. 		return temp;
  51. 	temp.numerator=lhs;
  52. 	temp.denominator=rhs;	
  53. 	//找出最大公约数
  54. 	long int t;
  55. 	while(rhs%lhs)
  56. 	{
  57. 		t=rhs;
  58. 		rhs=lhs;
  59. 		lhs=t%lhs;
  60. 	}
  61. 	//化为最简形式
  62. 	temp.numerator/=lhs;
  63. 	temp.denominator/=lhs;
  64. 	return temp;
  65. }
  66. void print(fraction& f)
  67. {
  68. 	if(f==0)
  69. 	{
  70. 		cout<<"0";
  71. 		return;
  72. 	}
  73. 	if(f.isNegative==-1)
  74. 		cout<<"(-";
  75. 	if(f.i)
  76. 	{
  77. 		cout<<f.i;
  78. 		if(f.numerator)
  79. 			cout<<" "<<f.numerator<<"/"<<f.denominator;
  80. 	}
  81. 	else if(f.numerator)
  82. 	{
  83. 		cout<<f.numerator<<"/"<<f.denominator;
  84. 	}
  85. 	if(f.isNegative==-1)
  86. 		cout<<")";
  87. }
  88. void printop(fraction& f1, fraction& f2, fraction& f3,char o)
  89. {
  90. 	print(f1);
  91. 	cout<<" "<<o<<" ";
  92. 	print(f2);
  93. 	cout<<" = ";
  94. 	print(f3);
  95. 	cout<<endl;
  96. }
  97. fraction op(long int n1, long int n2,long int n3,long int n4,char o)
  98. {
  99. 	fraction temp;
  100. 	long int numerator, denominator;
  101. 	denominator=n2*n4;
  102. 	switch(o)
  103. 	{
  104. 	case '+':
  105. 		numerator=n1*n4+n3*n2;
  106. 		break;
  107. 	case '-':
  108. 		numerator=n1*n4-n3*n2;
  109. 		break;
  110. 	case '*':
  111. 		numerator=n1*n3;
  112. 		break;
  113. 	case '/':
  114. 		denominator=n2*n3;
  115. 		numerator=n1*n4;
  116. 		break;
  117. 	}
  118. 	temp=simply(numerator,denominator);
  119. 	return temp;
  120. }
  121. int main()
  122. {
  123. 	long int a1,a2,a3,a4;
  124. 	fraction f1,f2,f3;
  125. 	char c1,c2;
  126. 	cin>>a1>>c1>>a2>>a3>>c2>>a4;
  127. 	f1=simply(a1,a2);
  128. 	f2=simply(a3,a4);
  129. 	a1=f1.isNegative*(f1.i*f1.denominator+f1.numerator);
  130. 	a2=f1.denominator;
  131. 	a3=f2.isNegative*(f2.i*f2.denominator+f2.numerator);
  132. 	a4=f2.denominator;
  133. 	//进行+、-、*、/、运算
  134. 	f3=op(a1,a2,a3,a4,'+');
  135. 	printop(f1,f2,f3,'+');
  136. 	f3=op(a1,a2,a3,a4,'-');
  137. 	printop(f1,f2,f3,'-');
  138. 	f3=op(a1,a2,a3,a4,'*');
  139. 	printop(f1,f2,f3,'*');
  140. 	f3=op(a1,a2,a3,a4,'/');
  141. 	if(f2==0)
  142. 	{
  143. 		print(f1);
  144. 		cout<<" / ";
  145. 		print(f2);
  146. 		cout<<" = Inf"<<endl;
  147. 	}
  148. 	else
  149. 		printop(f1,f2,f3,'/');
  150. 	return 0;
  151. }
  152. 贴个图

PAT Rational Arithmetic (20)的更多相关文章

  1. pat1088. Rational Arithmetic (20)

    1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...

  2. PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

    题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...

  3. PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

    输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...

  4. PAT (Advanced Level) 1088. Rational Arithmetic (20)

    简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...

  5. 【PAT甲级】1088 Rational Arithmetic (20 分)

    题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...

  6. 1088. Rational Arithmetic (20)

    1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...

  7. PAT_A1088#Rational Arithmetic

    Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...

  8. PAT1088:Rational Arithmetic

    1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...

  9. PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]

    1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...

随机推荐

  1. tab切换组件nz-tab

    <nz-card [nzBordered]="true" nzTitle="卡片标题"> <nz-card style="width ...

  2. 【传智播客】Libevent学习笔记(一):简介和安装

    目录 00. 目录 01. libevent简介 02. Libevent的好处 03. Libevent的安装和测试 04. Libevent成功案例 00. 目录 @ 01. libevent简介 ...

  3. cssrefresh.js-CSS文件自动刷新

    一.如何使用cssrefresh.js 使用很简单,类似下面的代码: <head> <link rel="stylesheet" type="text/ ...

  4. [kuangbin带你飞]专题四 最短路练习

    对于最短路,我主要使用的就是dijkstra,Floyd,SPFA这三个算法.先来介绍一下这三个算法. 1. dijkstra算法.它适用于边权为正的情况,它是单源最短路,就是从单个源点出发到所有的结 ...

  5. 页面jsp向后端发送:HTTP 400错误 - 请求无效(Bad request)

    HTTP 400错误 - 请求无效(Bad request) jsp页面有误 在ajax请求后台数据时有时会报 HTTP 400 错误 - 请求无效 (Bad request);出现这个请求无效报错说 ...

  6. Linux下启动tomcat报java.lang.OutOfMemoryError: PermGen space

    一.错误信息 java.lang.reflect.InvocationTargetException    at sun.reflect.NativeMethodAccessorImpl.invoke ...

  7. centos passwo文件被删除

    错误提示 该问题一般由/etc/passwd被清空,删除,移动,改名等造成,需要通过救援模式恢复,操作步骤如下 真实环境已经解决,这里使用vmware模拟.光盘启动,选择救援模式: 语言选择,键盘布局 ...

  8. c++基础_矩阵乘法

    #include <iostream> using namespace std; int main(){ int a,b; cin>>a>>b; long c[a] ...

  9. Django之模板引擎(母版)

    Django之模板引擎(母版) 母版:存放所有页面的基本信息,基本样式 子班:继承母版 自定义当前页面私有的样式信息 母版的样式: {% block xxx(名称) %} xxxxxxx(数据) {% ...

  10. poj 2186 强连通分量

    poj 2186 强连通分量 传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33414 Acc ...