初学数据结构,堆栈题目全部TLE
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define ERROR -1
#define OK 1
typedef struct stack{
int arr[MAXSIZE];
int top;
}*Stack;
int Push(int n,struct stack *s);
int Pop(struct stack *s);
int main(void){
Stack s = (Stack)malloc(sizeof(struct stack));
s->top=-1;
int arr[10];
int a,b,input;
int num=0;
scanf("%c",&input);
while(input!='@'){
if(input>='0'&&input<='9'){
num+=num*10+input-'0';
}
if(input=='.'){
Push(num,s);
num=0;
}
if(input=='+'){
a=Pop(s);
b=Pop(s);
Push(a+b,s);
}else if(input=='-'){
a=Pop(s);
b=Pop(s);
Push(b-a,s);
}else if(input=='*'){
a=Pop(s);
b=Pop(s);
Push(a*b,s);
}else if(input=='/'){
a=Pop(s);
b=Pop(s);
Push(b/a,s);
}
scanf("%c",&input);
}
printf("%d",Pop(s));
return 0;
}
int Push(int n,struct stack *s){
if(s->top==MAXSIZE-1){
return ERROR;
}else{
s->arr[++(s->top)]=n;
}
return OK;
}
int Pop(struct stack *s){
if(s->top==-1){
return ERROR;
}else{
return s->arr[(s->top)--];
}
}
是不是堆栈导致的?
num+=num*10+input-'0';
这里写错了
你试一下33 * 5即
33.5.*@
这个例子就知道了