2016-11-13 44 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字节。你还应该检查文件的结尾。

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; 
    } 
} 

援引main()

#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>语法标准的头。

  • 你应该防止缓冲区溢出通过传递的最大字符数%s格式sscanf()sscanf(str, "%f %9s %f", &f1, ops, &f2);

编辑:有一个在GetExrValue()另外一个问题:你的价值观切换从05op而不是操作角色。这里有一个方法来纠正这一点,

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