请使用C语言实现一个控制台计算器系统。n功能要求:n1能够读取用户输入的操作数与操作符进行分析并存储进栈中n2能够完成支持嵌套功能的四则运算并正确输出结果n3支持小数级别的运算n4能够完成幂的运算能够进行根号的运算
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <math.h>
#define STACK_SIZE 100
typedef struct { int top; double data[STACK_SIZE]; } Stack;
void initialize(Stack *s) { s->top = -1; }
int is_empty(Stack *s) { return s->top == -1; }
int is_full(Stack *s) { return s->top == STACK_SIZE - 1; }
void push(Stack *s, double value) { if (is_full(s)) { printf("Error: stack is full.\n"); exit(EXIT_FAILURE); } s->data[++(s->top)] = value; }
double pop(Stack *s) { if (is_empty(s)) { printf("Error: stack is empty.\n"); exit(EXIT_FAILURE); } return s->data[(s->top)--]; }
double peek(Stack *s) { if (is_empty(s)) { printf("Error: stack is empty.\n"); exit(EXIT_FAILURE); } return s->data[s->top]; }
int is_operator(char ch) { return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^' || ch == 's' || ch == 'c' || ch == 't' || ch == 'q' || ch == 'l'; }
int precedence(char ch) { if (ch == '+' || ch == '-') { return 1; } else if (ch == '*' || ch == '/') { return 2; } else if (ch == '^') { return 3; } else if (ch == 's' || ch == 'c' || ch == 't' || ch == 'q' || ch == 'l') { return 4; } else { return 0; } }
double calculate(double x, double y, char op) { switch (op) { case '+': return x + y; case '-': return x - y; case '*': return x * y; case '/': return x / y; case '^': return pow(x, y); case 's': return sin(x); case 'c': return cos(x); case 't': return tan(x); case 'q': return sqrt(x); case 'l': return log(x); default: return 0.0; // should never happen } }
double evaluate_expression(char *expression) { Stack operand_stack, operator_stack; initialize(&operand_stack); initialize(&operator_stack);
while (*expression != '\0') {
if (isdigit(*expression) || *expression == '.') {
double value = strtod(expression, &expression);
push(&operand_stack, value);
} else if (is_operator(*expression)) {
while (!is_empty(&operator_stack) && precedence(*expression) <= precedence(peek(&operator_stack))) {
double y = pop(&operand_stack);
double x = pop(&operand_stack);
char op = (char) pop(&operator_stack);
double result = calculate(x, y, op);
push(&operand_stack, result);
}
push(&operator_stack, *expression);
++expression;
} else if (*expression == '(') {
push(&operator_stack, *expression);
++expression;
} else if (*expression == ')') {
while (peek(&operator_stack) != '(') {
double y = pop(&operand_stack);
double x = pop(&operand_stack);
char op = (char) pop(&operator_stack);
double result = calculate(x, y, op);
push(&operand_stack, result);
}
pop(&operator_stack);
++expression;
} else {
++expression;
}
}
while (!is_empty(&operator_stack)) {
double y = pop(&operand_stack);
double x = pop(&operand_stack);
char op = (char) pop(&operator_stack);
double result = calculate(x, y, op);
push(&operand_stack, result);
}
return pop(&operand_stack);
}
int main() { char expression[1024]; printf("Enter an expression to evaluate: "); fgets(expression, sizeof(expression), stdin); double result = evaluate_expression(expression); printf("Result: %f\n", result); return 0; }
原文地址: https://www.cveoy.top/t/topic/qQe 著作权归作者所有。请勿转载和采集!