用c语言写一个函数,输入一个数学表达式的字符串求结果
时间: 2023-06-07 20:08:44 浏览: 104
好的,这个问题属于技术问题,我可以帮您解答。以下是用C语言实现计算数学表达式的一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define STACK_SIZE 50
// 定义操作符栈
char operator_stack[STACK_SIZE];
int operator_top = -1;
// 定义数字栈
double number_stack[STACK_SIZE];
int number_top = -1;
// 定义操作符优先级
int priority(char op) {
switch(op) {
case '+': case '-':
return 1;
case '*': case '/':
return 2;
case '(': case ')':
return 0;
default:
return -1;
}
}
// 将数字入栈
void push_number(double num) {
if (number_top >= STACK_SIZE - 1)
printf("Number Stack Overflow.\n");
else
number_stack[++number_top] = num;
}
// 从数字栈中取出一个数字
double pop_number() {
if (number_top == -1) {
printf("Number Stack Underflow.\n");
return 0;
} else {
return number_stack[number_top--];
}
}
// 将操作符入栈
void push_operator(char op) {
if (operator_top >= STACK_SIZE - 1)
printf("Operator Stack Overflow.\n");
else
operator_stack[++operator_top] = op;
}
// 从操作符栈中取出一个操作符
char pop_operator() {
if (operator_top == -1) {
printf("Operator Stack Underflow.\n");
return '\0';
} else {
return operator_stack[operator_top--];
}
}
// 判断是否为有效的操作符
int is_operator(char op) {
return op == '+' || op == '-' || op == '*' || op == '/' || op == '(' || op == ')';
}
// 判断是否为数字或小数点
int is_number(char c) {
return isdigit(c) || c == '.';
}
// 计算表达式
double calculate(double num1, double num2, char op) {
switch(op) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
default:
printf("Invalid Operator.\n");
return 0;
}
}
double evaluate(char* expression) {
// 初始化操作符栈和数字栈
operator_top = -1;
number_top = -1;
int i = 0;
double result = 0;
while (expression[i] != '\0') {
// 跳过空白字符
if (expression[i] == ' ')
i++;
// 如果是数字或小数点,入栈
else if (is_number(expression[i])) {
double num = 0.0;
int flag = 0; // 标记是否发现小数点
while (is_number(expression[i]) || (!flag && expression[i] == '.')) {
push_number(expression[i]);
if (expression[i] == '.')
flag = 1;
else
num = num * 10 + (expression[i] - '0');
i++;
}
if (flag)
num /= 10.0;
push_number(num);
}
// 如果是操作符,入栈
else if (is_operator(expression[i])) {
if (expression[i] == '(')
push_operator(expression[i]);
else if (expression[i] == ')') {
while (operator_stack[operator_top] != '(') {
double num2 = pop_number();
double num1 = pop_number();
char op = pop_operator();
push_number(calculate(num1, num2, op));
}
pop_operator(); // 弹出左括号
} else {
int current_priority = priority(expression[i]);
while (operator_top >= 0 && priority(operator_stack[operator_top]) >= current_priority) {
double num2 = pop_number();
double num1 = pop_number();
char op = pop_operator();
push_number(calculate(num1, num2, op));
}
push_operator(expression[i]);
}
i++;
}
// 其他非法字符
else {
printf("Invalid Character.\n");
return 0;
}
}
// 处理剩余的操作符
while (operator_top >= 0) {
double num2 = pop_number();
double num1 = pop_number();
char op = pop_operator();
push_number(calculate(num1, num2, op));
}
// 最终结果为数字栈中剩余的唯一元素
if (number_top != 0) {
printf("Invalid Expression.\n");
return 0;
} else {
return pop_number();
}
}
int main() {
char expression[100];
printf("Please input a math expression: ");
fgets(expression, 100, stdin);
double result = evaluate(expression);
printf("Result: %f\n", result);
return 0;
}
```
您可以将需要计算的数学表达式作为程序的输入,运行即可得到计算结果。希望能对您有所帮助。
阅读全文