这里是我的代码: -为什么计算不正确?
#include <iostream>
using namespace std;
int Add (int *x , int *y)
{
int a=*x;
int b=*y;
int c=a+b;
return (c);
}
int Sub (int *x , int *y)
{
int a=*x;
int b=*y;
int c=a-b;
return (c);
}
int Mul (int *x , int *y)
{
int a=*x;
int b=*y;
int c=a*b;
return (c);
}
int Div (int *x , int *y)
{
int a=*x;
int b=*y;
int c=a/b;
return (c);
}
int Mod (int *x , int *y)
{
int a=*x;
int b=*y;
int c=a%b;
return (c);
}
int InputFunction (int *a , int *b , char op)
{
int x=*a;
int y=*b;
int c=0;
cout<<"Please enter first number : ";
cin>>x;
cout<<"Please enter second number : ";
cin>>y;
cout<<endl;
cout<<"Please choose an operator to perform the operation :- "<<endl<<endl;
cout<<" \t \t \t + for addition"<<endl;
cout<<" \t \t \t - for sunbtraction"<<endl;
cout<<" \t \t \t x for mutiplication"<<endl;
cout<<" \t \t \t/for division"<<endl;
cout<<" \t \t \t % for modulus"<<endl<<endl<<endl;
cout<<" \t \t Your choice : ";
cin>>op;
switch (op)
{
case '+':
Add (&x , &y);
break;
case '-':
Sub (&x , &y);
break;
case 'x':
Mul (&x , &y);
break;
case '/':
Div (&x , &y);
break;
case '%':
Mod (&x , &y);
break;
default:
cout<<"Your symbol is not recognized!";
break;
}
int i=c;
return (i);
}
int main()
{
int a=0;
int b=0;
char op;
char ch;
int i;
do
{
InputFunction (&a , &b , op);
int m=i;
cout<<" \t \t Your answer : "<<m<<endl<<endl;
cout<<"Do you want to repeat the program ? (Y/N) ";
cin>>ch;
}while (ch == 'Y' || ch == 'y');
cout<<"Good- Bye"<<endl;
return 0;
}
为什么计算一些奇怪的长的答案?此外,编译器显示警告op
和i
main()
函数未初始化(虽然它们在上面初始化)。我是C++新手。帮助将不胜感激。
您能否将代码缩小到有问题的部分?什么“奇怪的长答案”?您能否为某些输入显示实际的(和预期的)输出? –
你并没有改变局部变量i。 –
它应该是:'int m = InputFunction(&a,&b,op);'并且我看不到输入函数传递参数的点... – SHR