PTA-1034 有理数四则运算

本题要求编写程序,计算 2 个有理数的和、差、积、商。

输入格式:

输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为 0。

输出格式:

分别在 4 行中按照 有理数1 运算符 有理数2 = 结果 的格式顺序输出 2 个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式 k a/b,其中 k 是整数部分,a/b 是最简分数部分;若为负数,则须加括号;若除法分母为 0,则输出 Inf。题目保证正确的输出中没有超过整型范围的整数。

输入样例 1:

2/3 -4/2

输出样例 1:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

输入样例 2:

5/3 0/6

输出样例 2:

1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

测试点2怎么也过不了,有哪位大佬看出来了麻烦说一下,谢谢

#include<iostream>
#include<algorithm>
using namespace std;
int gcd(int x , int y)
{
	if(!y)   
		return x;
	else 
        return gcd(y, x%y);
}
void youlishu(long long int a,long long int b)
{
		if(a==0){
			cout<<"0";
		}
		else{
			long long int zhengshu=a;
			long long int fenzi=b;
			if(abs(a)>=b){
			if(a%b==0){
				if(a<0)
					cout<<"("<<a/b<<")";
				else
					cout<<a/b;
			}
			else{
				zhengshu=a/b;
				fenzi=abs(a-zhengshu*b);
				long long int gongyueshu=gcd(fenzi,b);
				if(gongyueshu!=0){
					if(a<0)
						cout<<"("<<zhengshu<<" "<<fenzi/gongyueshu<<"/"<<b/gongyueshu<<")";
					else
						cout<<zhengshu<<" "<<fenzi/gongyueshu<<"/"<<b/gongyueshu;		
				}
				else{
					if(a<0)
						cout<<"("<<zhengshu<<" "<<fenzi<<"/"<<b<<")";
					else
						cout<<zhengshu<<" "<<fenzi<<"/"<<b;		
				}
			}
		}
		else{
			long long int gongyueshu=gcd(abs(a),b);
			if(gongyueshu!=0){
				if(a<0)
					cout<<"("<<a/gongyueshu<<"/"<<b/gongyueshu<<")";
				else
					cout<<a/gongyueshu<<"/"<<b/gongyueshu; 	
				}
			else{
						if(a<0)
							cout<<"("<<a<<"/"<<b<<")";
						else
							cout<<a<<"/"<<b; 
				}
			}
		}
}
void jia(int a,int b,int c,int d)
{
	youlishu(a,b);
	cout<<" + ";
	youlishu(c,d);
	cout<<" = ";
	a=a*d;
	c=b*c;
	b=b*d;
	int fenzi=a+c;
	int fenmu=b;
	youlishu(fenzi,fenmu);
	cout<<endl;	
}
void jian(int a,int b,int c,int d)
{
	youlishu(a,b);
	cout<<" - ";
	youlishu(c,d);
	cout<<" = ";
	a=a*d;
	c=b*c;
	b=b*d;
	int fenzi=a-c;
	int fenmu=b;
	youlishu(fenzi,fenmu);	
	cout<<endl;
}
void cheng(int a ,int b,int c,int d)
{	
	youlishu(a,b);
	cout<<" * ";
	youlishu(c,d);
	cout<<" = ";

	long long int a1=a*c; 
	long long int b1=b*d;
	youlishu(a1,b1);	
	cout<<endl;
	
}
void chu(int a,int b,int c,int d)
{
	youlishu(a,b);
	cout<<" / ";
	youlishu(c,d);
	cout<<" = ";
	long long int a1;
	long long int b1;
	if(c<0)
	{
		a1=-a*(d);
		b1=abs(c)*abs(b);
	}
	else{
		a1=a*d;
		b1=c*b;
	}
	if(c==0)
		cout<<"Inf";
	else
		youlishu(a1,b1);	
	cout<<endl;
}
int main()
{	
	int a,b,c,d;
	char e,f;
	cin>>a>>e>>b>>c>>f>>d;
	jia(a,b,c,d);
	jian(a,b,c,d);
	cheng(a,b,c,d);
	chu(a,b,c,d);

	return 0; 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PTA(Programming Test and Assessment)平台上有很多简单的四则运算表达式题目,可以练习基本的算术运算和表达式求值。下面是一个例子: 题目描述: 给定一个只包含加、减、乘、除四种基本运算的表达式,求表达式的值。 输入格式: 第一行包含一个整数 T,表示共有 T 组测试数据。 每组数据占一行,包含一个长度不超过 50 的只包含数字与加、减、乘、除四种运算符的表达式。 输出格式: 对于每组数据,输出表达式的值,保留两位小数。 样例输入: 2 1+2*3-4/5 3+5*8-6/2 样例输出: 6.20 43.00 代码示例(C++): ```cpp #include <iostream> #include <stack> #include <string> #include <iomanip> using namespace std; int main() { int T; cin >> T; while (T--) { string s; cin >> s; stack<double> nums; stack<char> ops; for (int i = 0; i < s.size(); i++) { if (isdigit(s[i])) { double num = s[i] - '0'; while (i + 1 < s.size() && isdigit(s[i + 1])) { num = num * 10 + s[i + 1] - '0'; i++; } nums.push(num); } else if (s[i] == '(') { ops.push('('); } else if (s[i] == ')') { while (ops.top() != '(') { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.pop(); } else if (s[i] == '+' || s[i] == '-') { while (!ops.empty() && (ops.top() == '+' || ops.top() == '-' || ops.top() == '*' || ops.top() == '/')) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.push(s[i]); } else if (s[i] == '*' || s[i] == '/') { while (!ops.empty() && (ops.top() == '*' || ops.top() == '/')) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.push(s[i]); } } while (!ops.empty()) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } cout << fixed << setprecision(2) << nums.top() << endl; } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值