#include <stdio.h>
#include <stdlib.h>
#include <string.h>   
#include <math.h>
#define MAX_STACK_SIZE 100 /* maximum stack size */
#define MAX_EXPR_SIZE 100 /* max size of expression */


typedef enum { lparen, rparen, plus, minus, times, divide,
    mod, eos ,operand } precedence;
typedef struct{
    int precedence;
    float value; 
}oper;

oper expr[MAX_EXPR_SIZE]; /* input string */
oper exprPost[MAX_EXPR_SIZE]; /* input string */
int exprPostCount=0;
int exprCount = 0;
int top = -1;
precedence stack[MAX_STACK_SIZE];
int top2 = -1;
oper stack2[MAX_STACK_SIZE];
/* isp and icp arrays ? index is value of precedence lparen, rparen,
    plus, minus, times, divide, mode, eos */

void push(precedence item)
{
  if(top >= MAX_STACK_SIZE - 1) {
    printf("stack_full()n");
    return;
  }
  stack[++top] = item;
}                   

precedence pop() {
    if(top == -1)
    {
        printf("stack_empty()n"); exit(0);
    }
    return stack[(top)--];
}

void push2(oper item)
{
  if(top2 >= MAX_STACK_SIZE - 1) {
    printf("stack_full()n");
    return;
  }
  stack2[++(top2)] = item;
}                   

oper pop2() {
    if(top2 == -1)
    {
        printf("stack_empty()n"); exit(0);
    }
    return stack2[(top2)--];
}


static int isp[] = {0,19,12,12,13,13,13,0};
static int icp[] = {20,19,12,12,13,13,13,0};
precedence get_precedence(char symbol){
 switch(symbol) {
    case '(': return lparen;
    case ')': return rparen;
    case '+': return plus;
    case '-': return minus;
    case '/': return divide;
    case '*': return times;
    case '%': return mod;
    case ' ': return eos;
    default: return operand;
  }
}

 


void print_token(precedence  t) {
    switch(t) {
        case lparen : printf("( " );break;
        case rparen : printf(") " );break;
        case plus : printf("+ " ); break;
        case minus : printf("- " ); break;
        case divide : printf("/ " ); break;
        case times : printf("* " ); break;
        case mod : printf("% " ); break;
        case eos : printf("eos " ); break;
        default: printf("n ");
    }
    //if(!isoperand)
        exprPost[exprPostCount++].precedence =t;
    //}
}

float instrToNum(char* st,int start,int end){
    int i,c=0;
    char numstr[100];
    for(i=start;i<=end;i++){
        numstr[c++]=st[i];
    }
    numstr[c]=0;
    return (float)atof(numstr);

}
void inputexpr(char *st){
    int i,tmp,exprcount=0;
    for(i=0,tmp=0;i<MAX_EXPR_SIZE;i++){
        if( !(('0'<= st[i] && st[i] <= '9')||st[i]=='.') ){
            if(tmp==i){
                expr[exprcount].precedence = get_precedence( st[tmp] );
                expr[exprcount++].value = 0;
                tmp++;
               
            }else{
                expr[exprcount].precedence = operand ;
                expr[exprcount].value = instrToNum(st,tmp,i-1) ;
                tmp=i;
                expr[++exprcount].precedence = get_precedence( st[tmp] );
                expr[exprcount++].value = 0.;
               
                tmp++;
            }
        }
        if(expr[exprcount-1].precedence == eos)
                break;
    }
    exprCount=exprcount;

}
void postfix(void) {
    //
}


float eval(void)
{
     //
}

 

int main()
{
    char e[MAX_EXPR_SIZE]="3+((2*5)-4) ";

    inputexpr(e);
    postfix();    

    printf("Result is  %f.!n",eval());
}