0

私はCで簡単な電卓を作成しようとしています。現在、問題は1つだけです。つまり、文字の配列に格納された入力値に演算子の値を割り当てようとすると、それが割り当てられますが、終了するとfor ループは割り当てられなくなりました。malloc を使用してみましたが、これは機能しません。前もって感謝します

int calculator()
{
int exit;
exit = 1;
while(exit == 1){

    printf("Welcome to the calculator, please enter the calculation you wish to make, if you wish to exit type EXIT\n");

    float num1;
    float num2;
    char operation;
    float ans;
    char string[10];
    int beenhere = 0;

    scanf("%s", &string);
    int result = strncmp(string, "EXIT", 10);

    if(result == 0){
        exit = 0;
    }
    else{
        int length = strlen(string);
        int i;
        for(i = 0; i <= length; i++){
            if(isdigit(string[i]) != 0){
                if(beenhere == 0){
                    num1 = (float)string[i] - '0';
                    beenhere = 1;
                }
                else{
                    num2 = (float)string[i] - '0';
                }
            }
            else{
                operation = string[i];
            }
        }
        printf("num1 %f\n", num1);
        printf("%c\n", operation);
        printf("num2 %f\n", num2);

        if(operation == '+'){
            ans = num1 + num2;
        }
        if(operation == '-'){
            ans = num1 - num2;
        }
        if(operation == '/'){
            ans = num1 / num2;
        }
        if(operation == '*'){
            ans = num1 * num2;
        }
        if(operation == '^'){
            ans = (float)pow(num1,num2);
        }

        printf("Your answer is %f\n", ans);

        }
}
return 0;

}

編集:私は forloop を参照しています。ここで、割り当ては次のとおりです: operation = string[i];

4

2 に答える 2