用这些函数编译完整包含四则运算的计算器
时间: 2023-11-29 09:06:51 浏览: 54
好的,我可以为您编写一个基于栈的四则运算计算器,可以支持加、减、乘、除、括号等基本运算符。以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXSIZE 100 // 栈的最大容量
typedef struct {
int top; // 栈顶指针
double data[MAXSIZE]; // 存储栈元素的数组
} Stack;
// 初始化栈
void init_stack(Stack *s) {
s->top = -1;
}
// 判断栈是否为空
int is_empty(Stack *s) {
return s->top == -1;
}
// 判断栈是否已满
int is_full(Stack *s) {
return s->top == MAXSIZE - 1;
}
// 入栈
int push(Stack *s, double x) {
if (is_full(s)) {
return 0;
} else {
s->data[++s->top] = x;
return 1;
}
}
// 出栈
double pop(Stack *s) {
if (is_empty(s)) {
return 0;
} else {
return s->data[s->top--];
}
}
// 获取栈顶元素
double top(Stack *s) {
if (is_empty(s)) {
return 0;
} else {
return s->data[s->top];
}
}
// 将字符型运算符转换成优先级
int change_opnd(char opnd) {
switch (opnd) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
case ')':
return 0;
default:
return -1;
}
}
// 将运算符压入栈
int push_opnd(Stack *s, char opnd) {
if (is_full(s)) {
return 0;
} else {
push(s, (double)opnd);
return 1;
}
}
// 将运算符弹出栈
char pop_opnd(Stack *s) {
if (is_empty(s)) {
return '\0';
} else {
return (char)pop(s);
}
}
// 将运算数压入栈
int push_num(Stack *s, double num) {
if (is_full(s)) {
return 0;
} else {
push(s, num);
return 1;
}
}
// 弹出运算数
double pop_num(Stack *s) {
if (is_empty(s)) {
return 0;
} else {
return pop(s);
}
}
// 处理运算符
int procede(Stack *opnd_stack, Stack *operator_stack) {
int cur_opnd = (int)pop(opnd_stack); // 获取当前运算数
int cur_operator = (int)pop_opnd(operator_stack); // 获取当前运算符
if (cur_operator == '+') { // 加法
double num1 = pop_num(opnd_stack);
double num2 = pop_num(opnd_stack);
push_num(opnd_stack, num2 + num1);
} else if (cur_operator == '-') { // 减法
double num1 = pop_num(opnd_stack);
double num2 = pop_num(opnd_stack);
push_num(opnd_stack, num2 - num1);
} else if (cur_operator == '*') { // 乘法
double num1 = pop_num(opnd_stack);
double num2 = pop_num(opnd_stack);
push_num(opnd_stack, num2 * num1);
} else if (cur_operator == '/') { // 除法
double num1 = pop_num(opnd_stack);
double num2 = pop_num(opnd_stack);
if (num1 == 0) {
printf("Error: divide by zero!\n");
exit(1);
}
push_num(opnd_stack, num2 / num1);
}
return cur_opnd;
}
// 语法检查并计算
int Make_str(char *str, int len) {
Stack opnd_stack, operator_stack;
init_stack(&opnd_stack);
init_stack(&operator_stack);
int i = 0;
while (i < len) {
if (isdigit(str[i])) { // 数字
double num = 0;
while (i < len && isdigit(str[i])) {
num = num * 10 + (str[i] - '0');
i++;
}
if (i < len && str[i] == '.') { // 小数
i++;
int k = 10;
while (i < len && isdigit(str[i])) {
num = num + (str[i] - '0') / (double)k;
k *= 10;
i++;
}
}
push_num(&opnd_stack, num);
} else if (str[i] == '(') { // 左括号
push_opnd(&operator_stack, str[i]);
i++;
} else if (str[i] == ')') { // 右括号
while (top(&operator_stack) != '(') {
int cur_opnd = procede(&opnd_stack, &operator_stack);
if (cur_opnd == -1) {
printf("Error: invalid input!\n");
exit(1);
}
}
pop_opnd(&operator_stack);
i++;
} else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') { // 运算符
while (!is_empty(&operator_stack) && change_opnd(str[i]) <= change_opnd((char)top(&operator_stack))) {
int cur_opnd = procede(&opnd_stack, &operator_stack);
if (cur_opnd == -1) {
printf("Error: invalid input!\n");
exit(1);
}
}
push_opnd(&operator_stack, str[i]);
i++;
} else if (str[i] == ' ') { // 空格,忽略
i++;
} else { // 非法输入
printf("Error: invalid input!\n");
exit(1);
}
}
while (!is_empty(&operator_stack)) { // 处理剩余运算符
int cur_opnd = procede(&opnd_stack, &operator_stack);
if (cur_opnd == -1) {
printf("Error: invalid input!\n");
exit(1);
}
}
if (is_empty(&opnd_stack)) { // 没有输出
printf("Error: no output!\n");
exit(1);
}
double result = pop_num(&opnd_stack);
if (!is_empty(&opnd_stack)) { // 输出不唯一
printf("Error: multiple outputs!\n");
exit(1);
}
printf("Result: %.2f\n", result);
return 1;
}
int main() {
char str[100];
printf("Please input an expression: ");
fgets(str, sizeof(str), stdin);
int len = strlen(str);
if (str[len-1] == '\n') {
str[len-1] = '\0';
len--;
}
Make_str(str, len);
return 0;
}
```
可以使用上述代码实现一个基于栈的四则运算计算器,支持加、减、乘、除、括号等基本运算符。