我需要编写一个程序,可以使用用户输入的运算符计算分数。我有不同的功能来减少分数并找到最大的公分母(我不确定我是否正确设置它们),然后我有函数calculate()来找到答案。我必须以分数形式打印出最终答案,而不是十进制。
我遇到的问题是我不知道如何将calculate()中的num3和den3返回到main函数中。如果有人能帮忙,我会非常亲切。谢谢。
到目前为止,这是我的代码:
/*Problem: Write a program to manipulate fractions. It allows for the addition, subtraction, multiplication, or division of fractions.
Inputs:
Outputs:
*/
#include <iostream>
using namespace std;
void calculate(int num1, int den1, int& num3, int& den3, int num2, int den2);
void reduce(int& num, int& den);
int gcd(int a, int b);
int main(){
int num1, num2, den1, den2, add, sub, mult, div;
int op, calc;
int num3, den3;
cout << "Enter the numerator of the first fraction" << endl;
cin >> num1;
cout << "Enter the denominator of the first fraction" << endl;
cin >> den1;
cout << "Enter the numerator of the second fraction" << endl;
cin >> num2;
cout << "Enter the denominator of the second fraction" << endl;
cin >> den2;
cout << "Enter a 1 for addition, 2 for subtraction, 3 for multiplication, or 4 for division" << endl;
cin >> op;
calculate(op, num1, den1, num3, den3, num2, den2);
cout << "The answer using option: " << op << endl;
cout << "is " << num3 << " / " << den3 << endl;
return 0;
}
void calculate(int op, int num1, int den1, int& num3, int& den3, int num2, int den2){
if(op==1){
num3 = num1 + num2;
den3 = den1;
}
else if(op==2){
num3 = num1 - num2;
den3 = den1;
}
else if(op==3){
num3 = num1 * num2;
den3 = den1 * den2;
}
else if(op==4){
num3 = num1 * den2;
den3 = num2 * den1;
}
}
void reduce(int& num, int& den){
int reduced;
reduced = gcd(num, den);
num = num/reduced;
den = den/reduced;
}
int gcd(int a, int b){
int divisor=1, temp;
while(b!= 0 || b>a){
temp = a % b;
a = b;
b = temp = divisor;
}
while(a!=0 || a>b){
temp = b % a;
b = a;
a = temp = divisor;
}
return divisor;
}
答案 0 :(得分:1)
1)实现有理数字类,或2)使用std :: pairs <int, int>
作为分子和分母会更好吗?那么你总是可以在1的情况下返回有理数,或者在2的情况下返回一对。
方法1允许您自然地处理奇点。如果分母为零,则可以抛出异常。
只是另一种方法......
答案 1 :(得分:0)
看一下你的函数声明,你就省去了第一个参数,op。
void calculate(int num1, int den1, int& num3, int& den3, int num2, int den2);
然后按你的意愿调用计算,
calculate(op, num1, den1, num3, den3, num2, den2);
函数定义有op,所以你注意到声明和定义之间存在参数不匹配,
void calculate(int op, int num1, int den1, int& num3, int& den3, int num2, int den2)
处理有理数的规范方法是声明std :: pair,或者用分子和分母声明一个类。
您的加法和减法代码中也存在缺陷。您需要考虑如何进行合理的加法/减法(参见:Rational addition/Subtraction)。这在下面修复。您可能会仔细查看gcd以确保其正常工作。
另外,您可以考虑使用switch / case语句来决定操作,并且可以轻松地将数字输入转换为字母操作码。
以下是修复了一些缺陷的代码,以及其他一些修改,
#include <iostream>
using namespace std;
class ratn //rational
{
public:
int num; //numerator
int denom; //denominator
friend std::ostream& operator<< ( std::ostream& stream, const ratn& r);
};
std::ostream& operator<< (std::ostream& stream, const ratn& r)
{
stream << r.num << " / " << r.denom;
return stream;
}
int gcd(int a, int b);
void reduce(ratn& r);
void calculate(int op, ratn a, ratn b, ratn& r );
int gcd(int a, int b)
{
int divisor=1, temp;
while(b!= 0 || b>a){
temp = a % b;
a = b;
b = temp = divisor;
}
while(a!=0 || a>b){
temp = b % a;
b = a;
a = temp = divisor;
}
return divisor;
}
void
reduce(ratn& r )
{
int reduced;
reduced = gcd(r.num, r.denom);
r.num = r.num/reduced;
r.denom = r.denom/reduced;
}
char operands[8] = { ' ','+','-','*','/','%','^' };
void
calculate(int op, ratn a, ratn b, ratn& r )
{
switch(op)
{
case ('+'):
r.num = a.num*b.denom + b.num*a.denom;
r.denom = a.denom*b.denom;
//reduce(r);
break;
case ('-'):
r.num = a.num*b.denom - b.num*a.denom;
r.denom = a.denom*b.denom;
//reduce(r);
break;
case ('*'):
r.num = a.num * b.num;
r.denom = a.denom * b.denom;
break;
case ('/'):
r.num = a.num * b.denom;
r.denom = b.num * a.denom;
break;
case ('%'):
//r.num = a.num * b.denom;
//r.denom = b.num * a.denom;
break;
case ('^'):
//r.num = a.num * b.denom;
//r.denom = b.num * a.denom;
break;
default :
break;
}
}
int
main()
{
int op;
ratn a, b, r;
cout << "Enter first fraction, numerator" << endl;
cin >> a.num;
cout << "Enter first fraction, denominator" << endl;
cin >> a.denom;
cout << "Enter second fraction, numerator" << endl;
cin >> b.num;
cout << "Enter second fraction, denominator" << endl;
cin >> b.denom;
cout << "Enter operator, 1=addition, 2=subtraction, 3=multiplication, 4=division" << endl;
cin >> op;
calculate(operands[op], a, b, r );
cout << "Answer:" ;
cout << a << " "<<operands[op]<<" " << b << " = " << r << endl;
return 0;
}