C语言实现计算器
时间: 2023-08-02 13:17:35 浏览: 73
C 语言实现计算器可以使用以下步骤:
1. 读取用户输入的算术表达式,并进行表达式的语法分析。
2. 将表达式转换为后缀表达式,以便于计算器进行计算。可以使用栈来实现转换。
3. 对后缀表达式进行计算,可以使用栈来实现计算。具体实现方式是:遍历后缀表达式,如果是数字则入栈,如果是运算符则取出栈顶的两个数字进行相应的运算,并将结果入栈,最终栈中只剩下一个数字,即为表达式的计算结果。
4. 输出计算结果。
下面是一个简单的 C 语言实现计算器的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_EXPR_LEN 100 // 最长的表达式长度
// 定义栈结构
typedef struct {
int top;
double data[MAX_EXPR_LEN];
} Stack;
// 初始化栈
void init(Stack *s) {
s->top = -1;
}
// 判断栈是否为空
int is_empty(Stack *s) {
return s->top == -1;
}
// 判断栈是否已满
int is_full(Stack *s) {
return s->top == MAX_EXPR_LEN - 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 top(Stack *s) {
if (is_empty(s)) {
printf("Error: Stack is empty.\n");
exit(EXIT_FAILURE);
}
return s->data[s->top];
}
// 判断是否是操作符
int is_operator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
// 判断操作符的优先级
int operator_priority(char c) {
if (c == '+' || c == '-') {
return 1;
} else if (c == '*' || c == '/') {
return 2;
} else {
return 0;
}
}
// 将中缀表达式转换为后缀表达式
void infix_to_postfix(char *expr, char *postfix) {
Stack op_stack; // 运算符栈
init(&op_stack);
int i, j;
for (i = j = 0; expr[i] != '\0'; i++) {
if (isdigit(expr[i])) { // 如果是数字,直接输出到后缀表达式中
postfix[j++] = expr[i];
} else if (is_operator(expr[i])) { // 如果是运算符,将其入栈或者输出栈中的运算符
while (!is_empty(&op_stack) && operator_priority(top(&op_stack)) >= operator_priority(expr[i])) {
postfix[j++] = pop(&op_stack);
}
push(&op_stack, expr[i]);
} else if (expr[i] == '(') { // 如果是左括号,将其入栈
push(&op_stack, expr[i]);
} else if (expr[i] == ')') { // 如果是右括号,将栈中的运算符输出直到遇到左括号
while (!is_empty(&op_stack) && top(&op_stack) != '(') {
postfix[j++] = pop(&op_stack);
}
if (is_empty(&op_stack)) {
printf("Error: Mismatched parentheses.\n");
exit(EXIT_FAILURE);
}
pop(&op_stack); // 弹出左括号
} else { // 如果是非法字符,报错退出
printf("Error: Invalid character: %c\n", expr[i]);
exit(EXIT_FAILURE);
}
}
while (!is_empty(&op_stack)) { // 将栈中剩余的运算符输出
if (top(&op_stack) == '(') {
printf("Error: Mismatched parentheses.\n");
exit(EXIT_FAILURE);
}
postfix[j++] = pop(&op_stack);
}
postfix[j] = '\0'; // 将后缀表达式末尾置为'\0'
}
// 计算后缀表达式的值
double eval_postfix(char *postfix) {
Stack num_stack; // 数字栈
init(&num_stack);
int i;
for (i = 0; postfix[i] != '\0'; i++) {
if (isdigit(postfix[i])) { // 如果是数字,将其入栈
push(&num_stack, postfix[i] - '0');
} else if (is_operator(postfix[i])) { // 如果是运算符,取出栈顶的两个数字进行相应的运算,并将结果入栈
double num2 = pop(&num_stack);
double num1 = pop(&num_stack);
double result;
switch (postfix[i]) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
printf("Error: Divide by zero.\n");
exit(EXIT_FAILURE);
}
result = num1 / num2;
break;
}
push(&num_stack, result);
} else { // 如果是非法字符,报错退出
printf("Error: Invalid character: %c\n", postfix[i]);
exit(EXIT_FAILURE);
}
}
if (is_empty(&num_stack)) { // 如果数字栈为空,报错退出
printf("Error: Invalid expression.\n");
exit(EXIT_FAILURE);
}
double result = pop(&num_stack); // 将数字栈中剩余的数字作为表达式的值
if (!is_empty(&num_stack)) { // 如果数字栈中还有数字,说明表达式不合法,报错退出
printf("Error: Invalid expression.\n");
exit(EXIT_FAILURE);
}
return result;
}
int main() {
char expr[MAX_EXPR_LEN];
char postfix[MAX_EXPR_LEN];
printf("Enter an expression: ");
gets(expr);
infix_to_postfix(expr, postfix);
printf("Postfix expression: %s\n", postfix);
double result = eval_postfix(postfix);
printf("Result: %g\n", result);
return 0;
}
```
这个程序可以处理基本的四则运算,可以通过 infix_to_postfix 函数将中缀表达式转换为后缀表达式,然后通过 eval_postfix 函数计算后缀表达式的值。输入的算术表达式可以是以空格分隔的数字和运算符,例如:1 + 2 * 3 / 4。