我是C ++的新手,我有点碰到这个问题,其中乘法和减法无法正常工作。我已经尝试过运行/编译它们,除法和加法都是唯一能正常工作的方法。
就我而言,将发生的事是,[[减法将加起来并在每个答案前留一个(-),并且类似于乘法的情况。
#include<iostream>
using namespace std;
int main(){
char a,b,c,d;
char operation;
int x,y,z, op = 0;
int num[2];
cout<<"\n\n\n\t\t\tCALCULATOR ";
cout<<"\n\t\t\t a - ADDITION";
cout<<"\n\t\t\t b - SUBTRACTION";
cout<<"\n\t\t\t c - MULTIPLICATION";
cout<<"\n\t\t\t d - DIVISION";
cout<<"\n\n\n ";
cout <<"\n\t\t\tChoose your operator: "; cin >> operation;
switch(operation){
case 'a':
case 'A':
cout<< "\n\t\t\tyou chose ADDIION";
cout<< "\n\t\t\tinput numbers: ";
for (x = 0; x < 2; x++)
{
cin >> num[x];
op += num[x];
}
cout << "\n\t\t\tSum = " << op << endl;
break;
case 'b':
case 'B':
cout<< "\n\t\t\tYou chose Subtraction";
cout<< "\n\t\t\tinput numbers: ";
for (x = 0; x < 2; x++)
{
cin >> num[x];
op *= num[x];
}
cout << "\n\t\t\tDifference = " << x << endl;
break;
case 'c':
case 'C':
cout<< "\n\t\t\tYou chose Multiplication";
cout<< "\n\t\t\tinput numbers: ";
for (x = 0; x < 2; x++)
{
cin >> num[x];
op *= num[x];
}
cout << "\n\t\t\tProduct = " << x << endl;
break;
case 'd':
case 'D':
cout<<"\n\t\t\tYou chose Division";
cout<<"\n\t\t\tinput numbers: ";
for (x=0; x < 2; x++)
{
cin >> num[x];
op /= num[x];
}
cout << "\n\t\t\tQuotient"<< x << endl;
break;
}
return 0;
}
op *= num[x];
相当于
op = op * num[x];
并且这是两个数字相乘而不是相减。还要注意,对于这些操作,op的初始值为0,仅对加法有效。对于减法,您将有
op = 0 - num[x]
对于乘法,您将有
op = 0 * num[x]
对于部门,您将有
op = 0 / num[x]
请确保您对预期的值(即x [0]和x [1])进行操作,并且正在打印op。看来您要打印x表示减,乘和除。