PTA 1034 有理数四则运算 (c语言 + 分析)

1034 有理数四则运算 (20 分)

在这里插入图片描述
分析:这道题的难点就在于对数据的处理。下面我的代码中,我把它分成了三个部分来处理。

首先我写了一个求最大公约数的函数,这样就能很快的求出分子分母可不可以约分(是不是最简);
然后我写了一个函数,就是处理它是不是负数,以及让他除以最大公约数看应该符合哪种格式并输出;
以上两个步骤完成了运算之前的数据处理,最后我在主函数中用循环加上开关语句(当然if也可以)按题目要求分四种情况进行运算输出。代码如下:

#include<string.h>
int GYS(long a,long b)
{
	return b==0?a:GYS(b,a%b);
}
void func(long a,long b)
{
	int sign=1;
	long mcd=1;
	if(b==0)
	{
		printf("Inf");
			return;
	}
	if(a<0)
	{
		a=-a;
		sign*=-1;
	}
	if(b<0)
	{
		b=-b;
		sign*=-1;
	}
	mcd=GYS(a,b);
	a/=mcd;b/=mcd;
	if(sign<0)
		printf("(-");
	if(a/b&&a%b)
		printf("%ld %ld/%ld",a/b,a%b,b);
	else if(a%b)
		printf("%ld/%ld",a,b);
	else
		printf("%ld",a/b);
	if(sign<0)printf(")");
}
int main()
{
    long a1, b1, a2, b2;
    char ch[4] = {'+', '-', '*', '/'};
    int i;

    scanf("%ld/%ld %ld/%ld", &a1, &b1, &a2, &b2);
    for(i = 0; i < 4; i++){
        func(a1, b1);
        printf(" %c ", ch[i]);
        func(a2, b2);
        printf(" = ");
        switch(ch[i]){
            case '+': func(a1*b2+a2*b1,b1*b2);break;
            case '-': func(a1*b2-a2*b1,b1*b2); break;
            case '*': func(a1*a2,b1*b2); break;
            case '/': func(a1*b2,b1*a2); break;
        }
        printf("\n");
    }
    return 0;
}
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
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; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给个选择

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值