用C语言设计一个简单的计算器,要求能够对输入的数 1.进行+,-,*,/,运算;2.可以带括号( );3.不限定运算式的输入长度.(小白版,用Microsoft Visual C++ 2010 Express运行)
时间: 2024-03-28 12:39:21 浏览: 101
一个简单计算器的设计 c++版
好的,我可以帮你设计一个简单的计算器。
首先,我们需要定义一些数据结构和函数来读取和计算表达式。以下是一个简单的设计:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_EXPR_LEN 1000
typedef struct {
char token;
int type;
} Token;
enum {
TOK_INVALID,
TOK_NUMBER,
TOK_PLUS,
TOK_MINUS,
TOK_MULTIPLY,
TOK_DIVIDE,
TOK_LPAREN,
TOK_RPAREN,
TOK_END
};
int get_token(Token *t) {
static char buffer[MAX_EXPR_LEN];
static int pos = 0;
static int len = 0;
while (isspace(buffer[pos])) {
pos++;
}
if (pos >= len) {
fgets(buffer, MAX_EXPR_LEN, stdin);
len = strlen(buffer);
pos = 0;
}
if (isdigit(buffer[pos])) {
int num = 0;
while (isdigit(buffer[pos])) {
num = num * 10 + (buffer[pos] - '0');
pos++;
}
t->token = num;
t->type = TOK_NUMBER;
} else if (buffer[pos] == '+') {
t->token = '+';
t->type = TOK_PLUS;
pos++;
} else if (buffer[pos] == '-') {
t->token = '-';
t->type = TOK_MINUS;
pos++;
} else if (buffer[pos] == '*') {
t->token = '*';
t->type = TOK_MULTIPLY;
pos++;
} else if (buffer[pos] == '/') {
t->token = '/';
t->type = TOK_DIVIDE;
pos++;
} else if (buffer[pos] == '(') {
t->token = '(';
t->type = TOK_LPAREN;
pos++;
} else if (buffer[pos] == ')') {
t->token = ')';
t->type = TOK_RPAREN;
pos++;
} else if (buffer[pos] == '\n') {
t->token = '\n';
t->type = TOK_END;
pos++;
} else {
t->token = buffer[pos];
t->type = TOK_INVALID;
pos++;
}
return t->type;
}
int parse_expr(void);
int parse_number(Token *t) {
if (t->type != TOK_NUMBER) {
printf("Syntax error: expected number\n");
exit(1);
}
int num = t->token;
get_token(t);
return num;
}
int parse_factor(Token *t) {
if (t->type == TOK_NUMBER) {
return parse_number(t);
} else if (t->type == TOK_LPAREN) {
get_token(t);
int result = parse_expr(t);
if (t->type != TOK_RPAREN) {
printf("Syntax error: expected ')'\n");
exit(1);
}
get_token(t);
return result;
} else if (t->type == TOK_PLUS || t->type == TOK_MINUS) {
int op = t->type;
get_token(t);
int result = parse_factor(t);
if (op == TOK_MINUS) {
result = -result;
}
return result;
} else {
printf("Syntax error: expected number or '(' or '+' or '-'\n");
exit(1);
}
}
int parse_term(Token *t) {
int left = parse_factor(t);
while (t->type == TOK_MULTIPLY || t->type == TOK_DIVIDE) {
int op = t->type;
get_token(t);
int right = parse_factor(t);
if (op == TOK_MULTIPLY) {
left *= right;
} else {
left /= right;
}
}
return left;
}
int parse_expr(Token *t) {
int left = parse_term(t);
while (t->type == TOK_PLUS || t->type == TOK_MINUS) {
int op = t->type;
get_token(t);
int right = parse_term(t);
if (op == TOK_PLUS) {
left += right;
} else {
left -= right;
}
}
return left;
}
int main() {
Token t;
int result;
while (1) {
printf("> ");
get_token(&t);
if (t.type == TOK_END) {
break;
}
result = parse_expr(&t);
printf("%d\n", result);
}
return 0;
}
```
这个计算器可以读取并计算一个表达式,支持加减乘除和括号,输入长度不限。你可以在控制台中输入表达式,例如:
```
> 1+2*3
7
> (1+2)*3
9
> 10/3
3
```
请注意,这个计算器还没有处理错误情况,例如输入了无效的字符或者不匹配的括号。你可以自己扩展它来处理这些情况。
阅读全文