请写一个整数计算器,支持加减乘三种运算和括号。
数据范围:
,保证计算结果始终在整型范围内
要求:空间复杂度:
,时间复杂度
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 返回表达式的值
* @param s string字符串 待计算的表达式
* @return int整型
*/
#include <stdio.h>
#include <string.h>
int solve(char* s ) {
// write code here
char stack_ops[103];//符号栈
int stack_val[103];//值栈
int n = strlen(s);
int top_val = 0;
int top_ops = 0;
int val = 0;
for (int i = 0; i < n; i++) {
//取数、存数
if (s[i] >= '0' && s[i] <= '9') {
val = 0;
while (s[i] >= '0' && s[i] <= '9') {
val = val * 10 + s[i] - '0';
i++;
}
i--;
stack_val[top_val++] = val;
}
//符号
else {
//若识别到')',则将最近一个'('到此')'的所有字符出栈,消灭这一对括号
if (s[i] == ')') {
while (stack_ops[top_ops - 1] != '(') {
int x = stack_val[--top_val];
int y = stack_val[--top_val];
//注意,此时经过其他步骤,括号里只有一个运算符,可直接运算
if (stack_ops[top_ops-1] == '+') {
stack_val[top_val++] = x + y;
} else if (stack_ops[top_ops-1] == '-') {
stack_val[top_val++] = y - x;
} else if (stack_ops[top_ops-1] == '*') {
stack_val[top_val++] = y * x;
}
top_ops--;
}
top_ops--;
}
//若识别到(',入栈
else if(s[i]=='(')
{
stack_ops[top_ops++]='(';
}
//若识别到'+',判断优先级,如果栈顶运算符优先级较高,则将其出栈,并
//进行运算
else if (s[i]=='+'&&top_val>=2&&stack_ops[top_ops-1]=='*') {
while (s[i]=='+'&&top_val>=2&&stack_ops[top_ops-1]=='*') {
int x = stack_val[--top_val];
int y = stack_val[--top_val];
stack_val[top_val++] = y*x;
top_ops--;
}
stack_ops[top_ops++]='+';
}
//若识别到'+',看是否为连续加
else if (s[i]=='+'&&top_val>=2&&stack_ops[top_ops-1]=='+') {
while (s[i]=='+'&&top_val>=2&&stack_ops[top_ops-1]=='+') {
int x = stack_val[--top_val];
int y = stack_val[--top_val];
stack_val[top_val++] = y+x;
top_ops--;
}
stack_ops[top_ops++]='+';
}
//若识别到'-',判断优先级,如果栈顶运算符优先级较高,则将其出栈,并
//进行运算
else if (s[i]=='-'&&top_val>=2&&stack_ops[top_ops-1]=='*') {
while (s[i]=='-'&&top_val>=2&&stack_ops[top_ops-1]=='*') {
int x = stack_val[--top_val];
int y = stack_val[--top_val];
stack_val[top_val++] = y*x;
top_ops--;
}
stack_ops[top_ops++]='-';
}
//若识别到'-',看是否为连续减
//注意:连续减要从左往右算,这一步不可省略!!!,否则会出现从右往左减
//的情况
else if (s[i]=='-'&&top_val>=2&&stack_ops[top_ops-1]=='-') {
while (s[i]=='-'&&top_val>=2&&stack_ops[top_ops-1]=='-') {
int x = stack_val[--top_val];
int y = stack_val[--top_val];
stack_val[top_val++] = y-x;
top_ops--;
}
stack_ops[top_ops++]='-';
}
else {
stack_ops[top_ops++]=s[i];
}
}
}
//剩下的运算符直接运算
while (top_ops!=0) {
int x = stack_val[--top_val];
int y = stack_val[--top_val];
if(stack_ops[top_ops-1]=='+')
{
stack_val[top_val++] = y+x;
}
else if (stack_ops[top_ops-1]=='-') {
stack_val[top_val++] = y-x;
}
else if (stack_ops[top_ops-1]=='*') {
stack_val[top_val++] = y*x;
}
top_ops--;
}
return stack_val[0];
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 返回表达式的值
* @param s string字符串 待计算的表达式
* @return int整型
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
typedef struct my_stack {
int arry[MAX_LEN];
int top;
} my_stack;
typedef struct my_stack_fuhao {
char arry[MAX_LEN];
int top;
} my_stack_fuhao;
void init_my_stack(my_stack* s) {
s->top = -1;
}
void init_my_stack_fuhao(my_stack_fuhao* s) {
s->top = -1;
}
int is_empty(my_stack* s) {
return s->top == -1;
}
int is_empty_fuhao(my_stack_fuhao* s) {
return s->top == -1;
}
void push(my_stack* s, int val) {
if (s->top < MAX_LEN - 1) {
//printf("push: %d, s->top: %d\n", val, s->top);
s->arry[++s->top] = val;
} else {
printf("error: stack overflow\n");
}
}
void push_fuhao(my_stack_fuhao* s, char val) {
if (s->top < MAX_LEN - 1) {
s->arry[++s->top] = val;
} else {
printf("error: stack overflow\n");
}
}
int top(my_stack* s) {
if (!is_empty(s)) {
return s->arry[s->top];
} else {
return 0;
}
}
char top_fuhao(my_stack_fuhao* s) {
if (!is_empty_fuhao(s)) {
return s->arry[s->top];
} else {
return 0;
}
}
int pop(my_stack* s) {
if (!is_empty(s)) {
return s->arry[s->top--];
} else {
return 0;
}
}
char pop_fuhao(my_stack_fuhao* s) {
if (!is_empty_fuhao(s)) {
return s->arry[s->top--];
} else {
return 0;
}
}
int youxianji_3(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*') return 2;
return 0;
}
int applyOperation(int a, int b, char op) {
switch (op) {
case '+':
printf("a=%d b=%d\n", a, b);
return a + b;
case '-':
return a - b;
case '*':
return a * b;
}
return 0;
}
int solve(char* s ) {
// write code here
int sum = 0, m, n, p, q;
int num1 = 0, num2 = 0;
int len;
my_stack* haha = (my_stack*)malloc(sizeof(my_stack));
my_stack_fuhao* fuhao = (my_stack_fuhao*)malloc(sizeof(my_stack_fuhao));
len = strlen(s);
init_my_stack(haha);
init_my_stack_fuhao(fuhao);
for (int i = 0; i < len; i++) {
if (s[i] >= '0' && s[i] <= '9') {
int num = 0;
while (i < len && (s[i] >= '0' && s[i] <= '9')) {
printf("s[%d] = %d\n", i, (s[i]-'0'));
num = num * 10 + (s[i] - '0');
i++;
}
i--;
printf("i=%d num=%d\n", i, num);
push(haha, num);
} else if (s[i] == '(') {
push_fuhao(fuhao, s[i]);
} else if (s[i] == ')') {
while (top_fuhao(fuhao) != '(') {
int val2 = pop(haha);
int val1 = pop(haha);
char op = pop_fuhao(fuhao);
int result = applyOperation(val1, val2, op);
push(haha, result);
}
pop_fuhao(fuhao);
} else { //运算符号
printf("%c %d %d, %d\n", s[i], top_fuhao(fuhao), youxianji_3(top_fuhao(fuhao)), youxianji_3(s[i]));
while (youxianji_3(top_fuhao(fuhao)) >= youxianji_3(s[i])) {
printf("%c", s[i]);
int val2 = pop(haha);
int val1 = pop(haha);
char op = pop_fuhao(fuhao);
int result = applyOperation(val1, val2, op);
push(haha, result);
}
push_fuhao(fuhao, s[i]);
}
}
while (!is_empty_fuhao(fuhao)) {
printf("xiaobo here\n");
int val2 = pop(haha);
int val1 = pop(haha);
char op = pop_fuhao(fuhao);
int result = applyOperation(val1, val2, op);
printf("val1=%d val2=%d op=%c, result=%d\n", val1, val2, op, result);
printf("xiaobo 01 %d\n", haha->top);
push(haha, result);
printf("xiaobo 01 %d, result=%d, %d\n", haha->top, haha->arry[haha->top], haha->arry[1]);
}
return top(haha);
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 返回表达式的值
* @param s string字符串 待计算的表达式
* @return int整型
*/
#define MAXSIZE 100
//判断表达式的优先级
int symcmp(char a, char b) {
if (b == '(')
return 1;
else if ((b == '*' || b == '/') && (a == '+' || a == '-' || a == '('))
return 1;
else if ((b == '+' || b == '-') && (a == '('))
return 1;
else
return 0;
}
//计算运算值栈顶两个元素(a栈顶第一个元素,b栈顶第二个元素)
int calculate(char c, int a, int b) {
int tmp = 0;
switch (c) {
case '+':
tmp = b + a;
break;
case '-':
tmp = b - a;
break;
case '*':
tmp = b * a;
break;
case '/':
tmp = b / a;
break;
}
return tmp;
}
int solve(char* s ) {
int num_stack[MAXSIZE]; //运算值栈
int num_top = -1; //运算值栈指针
char sym_stack[MAXSIZE]; //操作符栈
int sym_top = -1; //操作符栈指针
int i = 0;
while (s[i] != '\0') {
if (isdigit(s[i])) {
//数字进入数字栈
int val = 0;
while (isdigit(s[i])) {
val = val * 10 + s[i] - '0';
i++;
}
num_stack[++num_top] = val;
} else {
//符号进去
if (s[i] == ')') {
while (sym_stack[sym_top] != '(') {
int ret = calculate(sym_stack[sym_top], num_stack[num_top],
num_stack[num_top - 1]);
sym_top--;
num_top -= 2;
num_stack[++num_top] = ret;
}
sym_top--;
i++;
} else if (!symcmp(sym_stack[sym_top], s[i])) {
while (sym_top > -1 && (!symcmp(sym_stack[sym_top], s[i]))) {
int ret = calculate(sym_stack[sym_top], num_stack[num_top],
num_stack[num_top - 1]); //计算栈顶两个元素
sym_top--;
num_top -= 2;
num_stack[++num_top] = ret;
}
sym_stack[++sym_top] = s[i];
i++;
}
else {
sym_stack[++sym_top] = s[i];
i++;
}
}
}
while (sym_top > -1) {
int ret = calculate(sym_stack[sym_top], num_stack[num_top], num_stack[num_top - 1]); //计算栈顶两个元素
sym_top--;
num_top -= 2;
num_stack[++num_top] = ret;
}
return num_stack[0];
}
int GetInt(char **s) {
int res=0;
bool GetInt = false;
while((**s)!=0) {
if((**s)>='0' && (**s)<='9') {
res *=10;
res += ((**s)-'0');
(*s)++;
GetInt = true;
}
else {
if(GetInt)
break;
else
(*s)++;
}
}
return res;
}
int bracketLen(char *s) {
int len=0;
if(s[0]!='(')
return 0;
else {
s++;
while(1) {
char c = s[len];
if(c=='(') {
len += bracketLen(s+len)+2;
continue;
}
else if(c==')' || c==0)
break;
len++;
}
}
return len;
}
int solve(char* s ) {
#define push(a) stack[pointStack++] = a
#define pop() stack[--pointStack]
int stack[100]={0}, pointStack=0;
int res=0;
char *p=s;
bool firstFlag = true;
while(*p) {
if(*p=='(') {
char *buffer;
int BKval;
int BKLen = bracketLen(p);
buffer = (char*)malloc(BKLen+1);
strncpy(buffer, p+1, BKLen);
buffer[BKLen] = 0;
BKval = solve(buffer);
if(!firstFlag) {
if(*(p-1)=='-')
push(-BKval);
else if(*(p-1)=='+')
push(BKval);
else if(*(p-1)=='*')
push(pop()*BKval);
}
else
push(BKval);
firstFlag = false;
p += BKLen+strlen("()");
}
else if(*p>='0' && *p<='9') {
if(!firstFlag) {
if(*(p-1)=='-')
push(-GetInt(&p));
else if(*(p-1)=='+')
push(GetInt(&p));
else if(*(p-1)=='*')
push(pop()*GetInt(&p));
}
else
push(GetInt(&p));
firstFlag = false;
}
else if(*p=='+' || *p=='-') {
while(pointStack)
res += pop();
p++;
}
else if(*p=='*' || *p==')')
p++;
}
while(pointStack) {
res += pop();
}
return res;
} #include <string.h>
//返回字符串长度
int sizestr(char* s) {
int i = 0;
while (s[i] != '\0')
i++;
return i + 1;
}
int solve(char* s) {
// write code here
int data[100], sign[100];
int no = 0, no_s = 0;
for (int i = 0;s[i] != '\0';i++) {
if (s[i] == '(')
data[no++] = solve(s + i + 1);//遇到'('时递归
if (s[i] == ')') {
memcpy(s, s + i + 1, sizestr(s + i + 1));//函数退出时,清理已经计算过的表达式,防止重复计算
break;
}
if (s[i] >= '0' && s[i] <= '9') {
int num = 0;
while (s[i] >= '0' && s[i] <= '9') {//字符数转int,存入数组
num = num * 10 + s[i] - '0';
i++;
}
i--;
data[no++] = num;
}
if (s[i] == '*') {//乘法可以先计算,先算后存
i++;
if (s[i] >= '0' && s[i] <= '9') {
int num = 0;
while (s[i] >= '0' && s[i] <= '9') {
num = num * 10 + s[i] - '0';
i++;
}
i--;
data[no - 1] *= num;//计算结果覆盖存入
}
else if (s[i] == '(')//出现'*('时的情况
data[no - 1] *= solve(s + i + 1);//同样先计算括号里的
}
else if (s[i] == '+') {//加减法,先存再算,此时只要存符号,数字上面存了
sign[no_s++] = s[i];
}
else if (s[i] == '-') {
sign[no_s++] = s[i];
}
}
for (int i = 0, j = 0;i < no_s;i++) {//计算
if (sign[i] == '+') {
data[j + 1] = data[j] + data[j + 1];
data[j++] = 0;
}
else if (sign[i] == '-') {
data[j + 1] = data[j] - data[j + 1];
data[j++] = 0;
}
}
return data[no - 1];
} /*
支持加减乘括号运算,"2*(3+4)"
1.建立两个栈,一个存数字,一个存运算符;
2.对于数字直接进栈,对于运算符,当前运算符大于栈顶运算符时入栈,否则从数栈中弹出两个数进行运算。
3.注意:字符串遍历完,同时运算符栈为空时,计算才结束;对于数字要循环读取字符直至运算符;
*/
int isOperator(char c){
if(c=='(' || c==')' || c=='+' || c=='-' || c=='*')
return 1;
else
return 0;
}
/*
运算符优先级比较一定考虑哪个在左边哪个在右边,
a ? b (a在左边,b在右边,即a是已经进栈字符,b是当前字符)
a,b ( ) + - *
( < = < < <
) ? > > > >
+ < > > > <
- < > > > <
* < > > > >
*/
char getCmp(char a,char b){//a在左边,b在右边
if(a=='('){
if(b==')')
return '=';
return '<';
}else if(a==')'){
return '>';
}else if(a=='+' || a=='-'){
if(b=='(' || b=='*')
return '<';
return '>';
}else{//a=='*'
if(b=='(')
return '<';
return '>';
}
}
int cal(int a,int b,char op){
if(op=='+')
return a+b;
else if(op=='-')
return a-b;
else
return a*b;
}
int solve(char* s ) {
int stk_num[100];
char stk_op[100],op;
int top_num=0,top_op=0,i=0,a,b;
while(s[i]!='\0'|| top_op!=0){
if(s[i]!='\0' && isOperator(s[i])==1){//是运算符
if(top_op==0 || getCmp(stk_op[top_op-1],s[i])=='<'){//当前运算符优先级较大,压栈
stk_op[top_op++]=s[i];
i++;
}else if(getCmp(stk_op[top_op-1],s[i])=='>'){//弹栈,进行运算
op=stk_op[--top_op];
b=stk_num[--top_num];
a=stk_num[--top_num];
stk_num[top_num++]=cal(a,b,op);//计算后再压栈
}else{//消掉左右括号
top_op--;
i++;
}
}else if(s[i]=='\0'){//字符全部压入栈,弹栈
while(top_op!=0){//内循环弹运算符栈,不用走大循环
b=stk_num[--top_num];
a=stk_num[--top_num];
op=stk_op[--top_op];
stk_num[top_num++]=cal(a,b,op);//计算后再压栈
}
}else{//是操作数
int x=0;
while(s[i]!='\0' && isOperator(s[i])!=1){//循环读取n位数,构成一个操作数
x=x*10+s[i]-'0';
i++;
}
stk_num[top_num++]=x;
}
}//while
return stk_num[0];
} char* noBlank(char *s){
char *p = (char *)malloc(sizeof(char) * (strlen(s)+1));
strcpy(p, s);
int len = strlen(s);
for(int i=0; i<len; i++){
if(*(p+i) == ' '){
for(int j=i; j<len; j++){
*(p+j) = *(p+j+1);
}
len--;
i--;
}
}
return p;
}
int solve(char* s ) {
// write code here
char *p;
char *q;
int t;
int len = strlen(s);
int quelen = 10;
p = noBlank(s);
int stackInt[quelen];
memset(stackInt, 0, sizeof(stackInt));
char stackChar[quelen];
memset(stackChar, 0, sizeof(stackChar));
int lock = 0;
int up = 0, top = 0;
while(*p != '\0'){
if(48 <= *p && *p <= 57){ //如果是数字
stackInt[up] = stackInt[up]*10 + (*p - 48);
lock = 1;
}
else{
if(stackChar[top-1] != 0){
if(*p == ')'){
t = top;
while(stackChar[t-1] != '('){
switch (stackChar[t-1]){
case '+':
stackInt[up-2] = stackInt[up-2] + stackInt[up-1];
stackInt[--up] = 0;
break;
case '-':
stackInt[up-2] = stackInt[up-2] - stackInt[up-1];
stackInt[--up] = 0;
break;
case '*':
stackInt[up-2] = stackInt[up-2] * stackInt[up-1];
stackInt[--up] = 0;
break;
case '(':
break;
}
t--;
top--;
}
top--;
}
else if(*p == '('){
stackChar[top++] = *p;
}
else{
switch (*p){
case '*':
switch (stackChar[top-1]){
case '(':
stackChar[top++] = *p;
break;
case '*':
stackInt[up-2] = stackInt[up-2] * stackInt[up-1];
stackInt[--up] = 0;
stackChar[top-1] = *p;
break;
case '+':
stackChar[top++] = *p;
break;
case '-':
stackChar[top++] = *p;
break;
}
break;
case '+':
switch (stackChar[top-1]){
case '(':
stackChar[top++] = *p;
break;
case '*':
stackInt[up-2] = stackInt[up-2] * stackInt[up-1];
stackInt[--up] = 0;
stackChar[top-1] = *p;
break;
case '+':
stackInt[up-2] = stackInt[up-2] + stackInt[up-1];
stackInt[--up] = 0;
stackChar[top-1] = *p;
break;
case '-':
stackInt[up-2] = stackInt[up-2] - stackInt[up-1];
stackInt[--up] = 0;
stackChar[top-1] = *p;
break;
}
break;
case '-':
switch (stackChar[top-1]){
case '(':
stackChar[top++] = *p;
break;
case '*':
stackInt[up-2] = stackInt[up-2] * stackInt[up-1];
stackInt[--up] = 0;
stackChar[top-1] = *p;
break;
case '+':
stackInt[up-2] = stackInt[up-2] + stackInt[up-1];
stackInt[--up] = 0;
stackChar[top-1] = *p;
break;
case '-':
stackInt[up-2] = stackInt[up-2] - stackInt[up-1];
stackInt[--up] = 0;
stackChar[top-1] = *p;
break;
}
break;
}}
}
else
stackChar[top++] = *p;
}
if(!(48 <= *(p+1) && *(p+1) <= 57) && lock == 1){
up++;
lock = 0;
}
p++;
}
while(top != 0){
switch (stackChar[top-1]){
case '+':
stackInt[up-2] = stackInt[up-2] + stackInt[up-1];
stackInt[--up] = 0;
break;
case '-':
stackInt[up-2] = stackInt[up-2] - stackInt[up-1];
stackInt[--up] = 0;
break;
case '*':
stackInt[up-2] = stackInt[up-2] * stackInt[up-1];
stackInt[--up] = 0;
stackChar[top-1] = *p;
break;
}
top--;
}
return stackInt[0];
}