求助,全部TLE了,初学数据结构
查看原帖
求助,全部TLE了,初学数据结构
837296
mobai4876162楼主2022/11/19 16:56
#include <stdio.h>
#include <stdlib.h>
#include <math.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 MakeNum(int arr[],int i);

int main(void){
    Stack s = (Stack)malloc(sizeof(struct stack));
    s->top=-1;

    int arr[10];
    int tmp[50];
    int a,b,input;
    int i=0;
    scanf("%c",&input);

    while(input!='@'){
        if(input>='0'&&input<='9'){
            tmp[i]=input;
            i++;
        }
        if(input=='.'){
            Push(MakeNum(tmp,i),s);
            i=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)--];
    }
}

int MakeNum(int arr[],int i){

    int num=0;
    while(i){
        num+=((arr[i-1]-48)*pow(10,i-1));
        i--;
    }
    return num;

}
2022/11/19 16:56
加载中...