1

足し算から始めて、さまざまな算術関数を実行できる初歩的な電卓を作ろうとしています! これで、基本的なロジックが完成しましたが、2 つの入力を取得して出力する方法が正確にはわかりません。

#include <stdio.h>

int main()
{
    char mychar;
    int a;
    int op1;
    int op2;

    printf("Welcome to Andrew Hu's calculator program!\n"); //Greeting

    while(1)
    {    printf("Enter a mathematical operation to perform:\n");
        scanf("%c", &mychar);

    if(mychar == '+') //Valid Operators
        a = 1;
    else
        a = 0;


    if(a == 0) //Operator Checker, error if invalid
        printf("\nError, not a valid operator\n");
    else if(a == 1){
        printf("%c\n", &mychar),
        printf("Enter OP1:\n"),

       /* not sure what to put here to echo the character as a decimal*/

        printf("Enter OP2:\n"),

         /* not sure what to put here to echo the character as a decimal either*/

        printf("Result of %d %c %d = %d\n", op1, mychar, op2, (op1 + op2) )
        /* this last line I'm not too sure of. I'm trying to print out the expression
           which is op1 + op2 = the sum of both. */
              ;
    }
    }        
4

2 に答える 2

5

scanf ステートメントを使用して、数学演算子を使用したのと同じ方法で入力を取得します。電卓を実装するには、switch case ステートメントを使用するとよいでしょう。

scanf(" %d",&op1);
于 2013-04-17T08:06:08.717 に答える