2016-11-13 14 views
3

私は宿題に問題があります。私は先生が書いたコードをいくつか持っていて、電卓を作るためにそれを編集すると思います。だから私はそれがうまくいくと思った行の数を追加しましたが、悲しいことにそれはそうではありません。プログラムは、常にオペランドまたは演算子が間違っていることを返します。一見できますか?必要な入力を受け取っていないプログラム

main.cの

#include "stdio.h" 
#include "evalexpression.h" 

int main() { 
    char string[100]; 
    int result; 
    result = InterCalc(string); 
    CalcFilter(result, string); 
    return 0; 
} 

evalexpression.c

#include "stdio.h" 
#include "string.h" 
#include "evalexpression.h" 
#include "math.h" 
#include "float.h" 

static float f1, f2; 
static char op; 

int isValidExpression(const char *str) { 
    int res; 
    char ops[10]; 
    res = sscanf(str, "%f %s %f", &f1, ops, &f2); 
    if (res == 3) { 
     if (ops[0] == '+' || ops[0] == '-' || ops[0] == '^' || ops[0] == '*' || ops[0] == '/') { 
      op = ops[0]; 
      return 1; 
     } else 
      return 0; 
    } else 
     return 0; 
} 

int getOperator() { 
    return (op); 
} 

float getFstOperand() { 
    return (f1); 
} 

float getSecOperand() { 
    return (f2); 
} 

float getExprValue() { 
    int operation; 
    operation = getOperator(); 
    switch (operation) { 
    case 1: 
     return (getFstOperand() + getSecOperand()); 
     break; 
    case 2: 
     return (getFstOperand() - getSecOperand()); 
     break; 
    case 3: 
     return (getFstOperand()/getSecOperand()); 
     break; 
    case 4: 
     return (getFstOperand() * getSecOperand()); 
     break; 
    case 5: 
     return (pow(getFstOperand(), getSecOperand())); 
     break; 
    default: 
     return 0; 
    } 
} 

int InterCalc(char *my_string) { 
    fgets(my_string, sizeof(my_string), stdin); 
    if (strcmp(my_string, "exit\n") == 0) { 
     printf("Program ended\n"); 
     return 0; 
    } else 
    if (isValidExpression(my_string) == 0) { 
     printf("Expression error\n"); 
     return 0; 
    } else 
     return 1; 
} 

void CalcFilter(int a, char *str) { 
    float calculation_value; 
    printf("Press 'E' to display the invalid line or press 'V' to display the valid line\n"); 
    int choice; 
    choice = getchar(); 
    switch (choice) { 
    case 'E': 
    case 'e': 
     if (a == 0) printf("The line %s is invalid.\n", str); 
     else if (a == 1) printf("There's nothing wrong with the line %s\n", str); 
     break; 
    case 'V': 
    case 'v': 
     if (a == 1) { 
      calculation_value = getExprValue(); 
      printf("The result of %s is %f.\n", str, calculation_value); 
     } 
     if (a == 0) printf("The line %s is invalid\n", str); 
     break; 
    default: 
     printf("You haven't chosen the valid option of the switch\n"); 
     break; 
    } 
} 
+0

'のはsizeof(my_string)' 'はsizeof(char型*)' – BLUEPIXY

答えて

0

あなたは機能InterCalc()に宛先バッファのサイズを渡す必要があります。書かれているとおり、一度に読むことができるのはsizeof(char*) - 1バイトです。また、ファイルの終わりを確認する必要があります。 main()から

int InterCalc(char *my_string, size_t size) { 
    if (fgets(my_string, size, stdin) == NULL 
    || strcmp(my_string, "exit\n") == 0) { 
     printf("Program ended\n"); 
     return 0; 
    } else 
    if (isValidExpression(my_string) == 0) { 
     printf("Expression error\n"); 
     return 0; 
    } else { 
     return 1; 
    } 
} 

起動:

#include <stdio.h> 
#include "evalexpression.h" 

int main(void) { 
    char string[100]; 
    int result; 
    result = InterCalc(string, sizeof(string)); 
    CalcFilter(result, string); 
    return 0; 
} 

注:

  • あなたが標準ヘッダーのため<stdio.h>構文を使用する必要があります。 sscanf(str, "%f %9s %f", &f1, ops, &f2);

EDITを:あなたはsscanf()%sフォーマットの最大文字数を渡すことで、バッファオーバーフローを防ぐ必要があります

  • GetExrValue()内の別の問題があります:あなたはopため50から値を切り替えます操作文字の代わりに使用します。ここではこれを修正する方法です:

    float getExprValue(void) { 
        switch (getOperator()) { 
        case '+': 
         return getFstOperand() + getSecOperand(); 
        case '-': 
         return getFstOperand() - getSecOperand(); 
        case '/': 
         return getFstOperand()/getSecOperand(); 
        case '*': 
         return getFstOperand() * getSecOperand(); 
        case '^': 
         return pow(getFstOperand(), getSecOperand()); 
        default: 
         return 0; 
        } 
    } 
    
  • +0

    おかげで、今では問題がなくなっていますが、別のポップアウトがあります。プログラムは常に0.000を返し、私は理由を知らない。 – MarkAlanFrank

    +0

    @MarkAlanFrank: 'op'は数値コードではなく文字です。答えを更新しました。 – chqrlie

    関連する問題