2016-11-13 1 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 (숯불 *)' – 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);

편집 : 당신이 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

    관련 문제