
#include <stdio.h>
# include "G:\express\mystack.h"
char piror[7][7]={'>','>','<','<','<','>','>',
'>','>','<','<','<','>','>',
'>','>','>','>','<','>','>',
'>','>','>','>','<','>','>',
'<','<','<','<','<','=','f',
'>','>','>','>','f','>','>',
'<','<','<','<','<','f','='};
int chartoint(char ch)
{
switch(ch)
{
case '+':return 0;
case '-':return 1;
case '*':return 2;
case '/':return 3;
case '(':return 4;
case ')':return 5;
case '#':return 6;
}
}
int numchar(char ch)
{
if (ch>='0' && ch<='9')
return 1;
else return 0;
}
int isoptr(char ch)
{
switch(ch)
{
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':return 1;
default :return 0;
}
}
float operate(float n1,char ch,float n2)
{
switch(ch)
{
case '+':return n1+n2;
case '-':return n1-n2;
case '*':return n1*n2;
case '/':return n1/n2;
}
}
main()
{
char str[80];
gets(str);
int i=0;
int number1,exp10;
char ch1,ch2,curroptr1;
float number,number2,n1,n2,n3,curroptr,ch3;
sqstack optr,opnd;
initstack(optr);push(optr,'#');
initstack(opnd);
while(str[i]!='\0')
{
number1=0;
while(numchar(str[i]))
{
number1=number1*10+(str[i]-'0');
i++;
}
//已经读取了一个整数
printf("number1_________uuuu=%d\n",number1);
if (str[i]=='.')
{
i++;
number2=0.0;exp10=10;
while(numchar(str[i]))
{
number2=number2+((str[i]-'0')*1.0)/exp10;
exp10=exp10*10;
i++;
//printf("number2=%f\n",number2);
}
number=number1+number2;
printf("number_float=%f\n",number);
push(opnd,number);
}
push(opnd,number1);
printf("number_int=%d\n",number1);
if(!isoptr(str[i]))
{
printf("express error---1!\n");
exit(0);
}
ch1=(char)gettop(optr);
ch2=str[i];
printf("ch1=%c,ch2=%c\n",ch1,ch2);
if(piror[chartoint(ch1)][chartoint(ch2)]=='>')
{
while(piror[chartoint(ch1)][chartoint(ch2)]=='>')
{
pop(opnd,n2);
pop(opnd,n1);
pop(optr,curroptr);
curroptr1=(char)curroptr;
printf("\n---%f %c %f=====%f\n",n1,curroptr1,n2,operate(n1,curroptr1,n2));
push(opnd,operate(n1,curroptr1,n2));
ch1=(char)gettop(optr);
}
push(optr,ch2);
}
else if(piror[chartoint(ch1)][chartoint(ch2)]=='<') push(optr,ch2);
else if(piror[chartoint(ch1)][chartoint(ch2)]=='=') pop(optr,ch3);
else
{
printf("express error---2!\n");
exit(0);
}
i++;
}
pop(opnd,n3);
printf("result=%f\n",n3);
}
#include <stdlib.h>
#include <stdio.h>
#define stackinitsize 50
#define stackincrement 8
typedef float elemtype;
typedef struct{
elemtype *base;
elemtype *top;
int stacksize;
}sqstack;
int initstack(sqstack &s)
{s.base=(elemtype * ) malloc(stackinitsize*sizeof(int));
s.top=s.base;
s.stacksize=stackinitsize;
return 1;
}
int push(sqstack &s,elemtype e)
{
*(s.top)=e;
s.top++;
return 1;
}
elemtype gettop(sqstack s)
{
return *(s.top-1);
}
int emptystack(sqstack s)
{if (s.top==s.base) return 1;
else return 0;
}
int pop(sqstack &s,elemtype &e)
{ if (emptystack(s)) return 0;
--s.top;
e=*(s.top);
return 1;
}