在while循环中减法

时间:2013-05-04 21:11:42

标签: while-loop subtraction

我是编程游戏的新手。通常当我遇到问题时,我可以很快帮助他们解决问题。然而这让我很难过。

我正在尝试构建一个基本的计算器,我正在使用while循环来完成它。

添加很简单,因为我只需输入: sum += a

然而,减法,乘法和除法不会那么容易。我想知道是否有人会指出我正确的方向,就像我在被困之前所说的那样。

以下是我的代码的一部分供参考:

    x = 0;

    while(x < y){
        if(operator == 1){          /*addition portion*/

    x += 1;
    printf("Please enter number %d: ", x);
    scanf("%lf", &a);

        sum += a;}

        else if(operator == 2){     /*subtracion portion*/
    x += 1;
    printf("Please enter number %d: ", x);
    scanf("%lf", &b);

        sum += b - sum;}}               /*NOT WORKING, FIX, RESEARCH*/

    printf("\nThe sum of the entered numbers = %.f\n\n", sum);

1 个答案:

答案 0 :(得分:1)

您可以分别使用-=*=/=等其他运算符进行减法,乘法和除法。例如:

sum -= c; // equivalent to "sum = sum - c;"
sum *= d; // equivalent to "sum = sum * d;"
sum /= e; // equivalent to "sum = sum / e;"

(顺便说一句,你可以在不同的地方找到大tables of all the operators in C and C++。他们现在可能有点压倒性的,但他们以后会很好的参考!)

相关问题