当我添加或减去两个分数时,我试图显示加法和减法函数的结果。但是,每当我输入两个分数时,它都不会显示结果。它只是直接进入"按任意键继续"线。我想知道是否有人可以指导我解决方案。为我工作是没有必要的,但一些提示会有很大的帮助。我一直试图通过查看我的代码,使用互联网获取资源以及查看我的c ++书籍以获得一些帮助来解决这个问题。我确定我在显示功能上做错了什么。感谢您的时间。
#include <iostream>
using namespace std;
void info();
void input (int ,int ,int ,int ,int , int,char, char);
int add (int,int,int,int,int,int,char,char);
int subtract(int,int,int,int,int,int,char, char);
void display ();
int main()
{ int num1,den1,num2,den2,num_result, den_result;
char fraction_char;
char operation;
info();
input (num1,den1,num2,den2,num_result,den_result,fraction_char, operation);
add (num1,den1,num2,den2,num_result,den_result,fraction_char, operation);
subtract(num1,den1,num2,den2,num_result,den_result,fraction_char,operation);
display ();
return 0;
}
void info()
{ cout<<"Please follow instructions carefully."<<endl;
cout<<"Enter your operation like 1/2 + 1/4"<<endl;
cout<<"You must enter a valid operation."<<endl;
cout<<"Enter % operator to stop the program (0/1 % 0/1)"<<endl;
}
void input (int num1,int den1,int num2,int den2,int num_result, int den_result,char fraction_char, char operation)
{ cout<<"Please enter your operation."<<endl;
cin>>num1>>fraction_char>>den1>>num2>>fraction_char>>den2;
}
int add (int num1,int den1,int num2,int den2,int num_result,int den_result,char fraction_char, char operation)
{ int addition_answer;
if(operation== '+')
{ num_result = (num1*den2) + (num2*den1);
den_result = den1*den2;
addition_answer = num_result/den_result;
}
return addition_answer;
}
int subtract(int num1,int den1,int num2,int den2,int num_result,int den_result, char fraction_char, char operation)
{ int subtraction_answer;
if (operation == '-')
{ num_result = (num1*den2) - (num2*den1);
den_result = den1*den2;
subtraction_answer= num_result/den_result;
}
return subtraction_answer;
}
void display ()
{
int num1,den1,num2,den2,num_result,den_result,fraction_char,operation;
int fraction_added = add (num1,den1,num2,den2,num_result,den_result,fraction_char,operation);
int fraction_subtracted = subtract(num1,den1,num2,den2,num_result,den_result,fraction_char,operation);
if (operation== fraction_added)
{
cout<<"Your answer is: "<<add;
}
else if (operation== fraction_subtracted)
{
cout<<"You answer is: "<<subtract;
}
add(num1,den1,num2,den2,num_result,den_result,fraction_char,operation);
subtract(num1,den1,num2,den2,num_result,den_result,fraction_char,operation);
}
答案 0 :(得分:1)
输入函数不返回它从cin获得的任何变量。那是因为你是按值传递函数的参数,所以它从main获取变量的副本但不能改变主变量。
要使输入函数能够更改主变量,必须更改参数,以便通过引用或指针传递。
cout调用没有多大意义:例如,看起来你在fraction_added中得到了一个结果但是却没有输出那个结果。