PAT 1088 甲级 Rational Arithmetic (20分)

本文介绍了一种使用C++实现分数加减乘除运算的方法。通过定义结构体管理分数的符号、分子和分母,并实现了求最大公约数、简化分数、加法、减法、乘法和除法等关键函数。文章提供了完整的代码示例,展示了如何输入两个分数并计算它们的和、差、积和商。

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.
Input Specification:

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

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is number1 operator number2 = result. Notice that all the rational numbers must be in their simplest form k a/b, where k is 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 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.
Sample Input 1:

2/3 -4/2

Sample Output 1:

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

Sample Input 2:

5/3 0/6

Sample Output 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

分数的加减乘除。
解题的时候,注意求最大公约数和简化分式。
定义struct统一管理。

#include<iostream>
#include<algorithm>
using namespace std;

struct Form {
	long int sym;
	long int numerator;//分子
	long int denominator;//分母
	Form():sym(0),numerator(0),denominator(1) {}
};

long int  gcd(long int a, long int b) {
	return !b? a : gcd(b, a%b);
}

Form reduce(Form f) {
	if(f.denominator < 0) {
		f.numerator = - f.numerator;
		f.denominator = - f.denominator;
	}
	if(f.numerator == 0) {
		f.denominator = 1;
		f.sym = 0;
		return f;
	}
	if(f.denominator == 0) {
		return f;
	}
	long int d = gcd(abs(f.numerator), f.denominator);//最大公约数
	f.denominator /= d;
	f.numerator /= d;
//	bool flag = false;
//	if(f.numerator < 0) {
//		flag = true;
//	}
	if(abs(f.numerator) >= f.denominator) {
		f.sym = f.numerator / f.denominator;//abs()
		f.numerator = abs(f.numerator) % f.denominator;
	}
	return f;
}

Form add(Form f1,Form f2) {
	Form f;
	f.denominator = f1.denominator * f2.denominator;
	f.numerator = f1.numerator * f2.denominator + f2.numerator * f1.denominator;
	f = reduce(f);
	return f;
}

Form difference(Form f1,Form f2) {
	Form f;
	f.denominator = f1.denominator * f2.denominator;
	f.numerator = f1.numerator * f2.denominator - f2.numerator * f1.denominator;
	f = reduce(f);
	return f;
}

Form product(Form f1,Form f2) {
	Form f;
	f.denominator = f1.denominator * f2.denominator;
	f.numerator = f1.numerator * f2.numerator;
	f = reduce(f);
	return f;
}

Form quotient(Form f1,Form f2) {
	Form f;
	f.denominator = f1.denominator * f2.numerator;
	f.numerator = f1.numerator * f2.denominator;
	f = reduce(f);
	return f;
}


int main() {
	Form f[2];
	Form opt[4];//f_sum,f_difference,f_product,f_quotient,
	bool q = true;

	for(int i = 0; i < 2; i++) {
		scanf("%ld/%ld",&f[i].numerator,&f[i].denominator);
	}

	opt[0] = add(f[0],f[1]);
	opt[1] = difference(f[0],f[1]);
	opt[2] = product(f[0],f[1]);
	opt[3] = quotient(f[0],f[1]);

	f[0] = reduce(f[0]);
	f[1] = reduce(f[1]);

	for(int i = 0; i < 4; i++) {
		if(f[0].sym < 0 ||f[0].numerator < 0) {
			q = false;
			cout<<"(";
		}
		if(!f[0].sym) {
			if(!f[0].numerator) {
				cout<<0;
				if(q){
					cout<<" ";
				}
			} else {
				cout<<f[0].numerator<<"/"<<f[0].denominator;
				if(q){
					cout<<" ";
				}
			}
		} else {
			if(!f[0].numerator) {
				cout<<f[0].sym;
				if(q){
					cout<<" ";
				}
			} else {
				cout<<f[0].sym<<" "<<f[0].numerator<<"/"<<f[0].denominator;
				if(q){
					cout<<" ";
				}
			}
		}
		if(f[0].sym < 0 ||f[0].numerator < 0) {
			cout<<") ";
		}
		switch (i) {
			case 0:
				cout<<"+ ";
				break;
			case 1:
				cout<<"- ";
				break;
			case 2:
				cout<<"* ";
				break;
			case 3:
				cout<<"/ ";
				break;
		}
		if(f[1].sym < 0 ||f[1].numerator < 0) {
			cout<<"(";
		}
		if(!f[1].sym) {
			if(!f[1].numerator) {
				cout<<0;
			} else {
				cout<<f[1].numerator<<"/"<<f[1].denominator;
			}
		} else {
			if(!f[1].numerator) {
				cout<<f[1].sym;
			} else {
				cout<<f[1].sym<<" "<<f[1].numerator<<"/"<<f[1].denominator;
			}
		}

		if(f[1].sym < 0||f[1].numerator < 0) {
			cout<<") = ";
		} else {
			cout<<" = ";
		}

		if(opt[i].denominator == 0) {
			cout<<"Inf";
			continue;
		}

		if(opt[i].sym < 0 || opt[i].numerator < 0) {
			cout<<"(";
		}
		if(!opt[i].sym) {
			if(!opt[i].numerator) {
				cout<<0;
			} else {
				cout<<opt[i].numerator<<"/"<<opt[i].denominator;
			}
		} else {
			if(!opt[i].numerator) {
				cout<<opt[i].sym;
			} else {
				cout<<opt[i].sym<<" "<<opt[i].numerator<<"/"<<opt[i].denominator;
			}
		}
		if(opt[i].sym < 0 || opt[i].numerator < 0) {
			cout<<")";
		}

		if(i < 3 ) {
			cout<<endl;
		}
	}
	return 0;
}

前辈传送门…

内容概要:本文档介绍了基于3D FDTD(时域有限差)方法在MATLAB平台上对微带线馈电的矩形天线进行仿真析的技术方案,重点在于模拟超MATLAB基于3D FDTD的微带线馈矩形天线析[用于模拟超宽带脉冲通过线馈矩形天线的传播,以计算微带结构的回波损耗参数]宽带脉冲信号通过天线结构的传播过程,并计算微带结构的回波损耗参数(S11),以评估天线的匹配性能和辐射特性。该方法通过建立三维电磁场模型,精确求解麦克斯韦方程组,适用于高频电磁仿真,能够有效析天线在宽频带内的响应特性。文档还提及该资源属于一个涵盖多个科研方向的综合性MATLAB仿真资源包,涉及通信、信号处理、电力系统、机器学习等多个领域。; 适合人群:具备电磁场与微波技术基础知识,熟悉MATLAB编程及数值仿真的高校研究生、科研人员及通信工程领域技术人员。; 使用场景及目标:① 掌握3D FDTD方法在天线仿真中的具体实现流程;② 析微带天线的回波损耗特性,优化天线设计参数以提升宽带匹配性能;③ 学习复杂电磁问题的数值建模与仿真技巧,拓展在射频与无线通信领域的研究能力。; 阅读建议:建议读者结合电磁理论基础,仔细理解FDTD算法的离散化过程和边界条件设置,运行并调试提供的MATLAB代码,通过调整天线几何尺寸和材料参数观察回波损耗曲线的变化,从而深入掌握仿真原理与工程应用方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值