作者在 2010-04-15 14:37:19 发布以下内容
按1输入表达式(不用输入=号)
按2计算
按3输出结果
按4退出
程序如下:
#include<stdio.h>
#include <string.h>
#include <conio.h>
#define PLUS 0
#define MINUS 1
#define POWER 2
#define DIVIDE 3
#define LEFTP 4
#define RIGHP 5
#define STARTEND 6
#define DIGIT 7
#define POINT 8
#define NUM 7
#define NO 32767
#define STACKSIZE 20
char a[]={'+','-','*','/','(',')','#'};
int j,PriorityTable[7][7]={{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{-1,-1,-1,-1,-1, 0, NO},
{ 1, 1, 1, 1,NO, 1, 1},
{-1,-1,-1,-1,-1,NO, 0}
};
int menu(void);
void
InputExpression(char str[])
{ int len;
printf("Input expression string:\n");
scanf("%s",str);
len=strlen(str);
str[len]='#';
str[len+1]='\0';
}
int GetCharType(char ch)
{ int i;
for(i=0;i<NUM;i++) if(ch==a[i]) return(i);
if(ch>='0' && ch<='9') return(DIGIT);
if(ch=='.') return(POINT);
return(-1);
}
int EXCUTE(char *str,double *Result)
{
int pp,strlength,topTr,topNd,CharType,OPTR[STACKSIZE],a;
double number,temp,OPND[STACKSIZE];
OPTR[0]=STARTEND;
topTr=1;
topNd=0;
pp=0;
while((str[pp]))
{ CharType=GetCharType(str[pp]);
switch(CharType)
{ case -1:
return(0);
case DIGIT:
number=0;
while
(str[pp]>='0' && str[pp]<='9')
{ number=number*10+(str[pp]-48);
pp++;
}
if(str[pp]=='.')
{ temp=10.0;
pp++;
while(str[pp]>='0' && str[pp]<='9')
{ number=number+(str[pp]-48)/temp;
temp=temp*10;
pp++;
}
}
OPND[topNd]=number;
topNd++;
break;
case POINT:
number=0;
temp=10.0;
pp++;
while(str[pp]>='0' && str[pp]<='9')
{ number=number+(str[pp]-48)/temp;
temp=temp*10;
pp++;
}
OPND[topNd]=number;printf("%lf",number);
topNd++;
break;
case PLUS:
case MINUS:
case POWER:
case DIVIDE:
{int i;
i=PriorityTable[OPTR[topTr-1]][CharType];
if(i==-1)
{OPTR[topTr]=CharType;
topTr++;
}
if(i==1)
{while(i==1)
{
j=OPTR[topTr-1];
if(j==0)OPND[topNd-2]=OPND[topNd-2]+OPND[topNd-1];
if(j==1)OPND[topNd-2]=OPND[topNd-2]-OPND[topNd-1];
if(j==2)OPND[topNd-2]=OPND[topNd-2]*OPND[topNd-1];
if(j==3)OPND[topNd-2]=OPND[topNd-2]/OPND[topNd-1];
topNd--;
topTr--;
i=PriorityTable[OPTR[topTr-1]][CharType];
}
OPTR[topTr]=CharType;topTr++;
}
pp++;break;
}
case LEFTP:
{
OPTR[topTr]=LEFTP;
topTr++;pp++;
break;
}
case RIGHP:
{ while(1)
if(OPTR[topTr-1]==LEFTP){topTr--;pp++;break;}
else if(OPTR[topTr-1]==STARTEND)return(0);
else
{j=OPTR[topTr-1];
if(j==0)OPND[topNd-2]=OPND[topNd-2]+OPND[topNd-1];
else if(j==1)OPND[topNd-2]=OPND[topNd-2]-OPND[topNd-1];
else if(j==2)OPND[topNd-2]=OPND[topNd-2]*OPND[topNd-1];
else if(j==3)OPND[topNd-2]=OPND[topNd-2]/OPND[topNd-1];
topNd--;
topTr--;
}
break;
}
case STARTEND:
while(OPTR[topTr-1]!=STARTEND)
{
j=OPTR[topTr-1];
if(j==0)OPND[topNd-2]=OPND[topNd-2]+OPND[topNd-1];
else if(j==1)OPND[topNd-2]=OPND[topNd-2]-OPND[topNd-1];
else if(j==2)OPND[topNd-2]=OPND[topNd-2]*OPND[topNd-1];
else if(j==3)OPND[topNd-2]=OPND[topNd-2]/OPND[topNd-1];
topNd--;
topTr--;
}
if(topNd==1)
{ *Result=OPND[0];return(1);
}
else return(0);
}
}
return(1
);
}
void main()
{ int num,flag;
double result;
char str[256];
str[0]=0;
while(1)
{ num=menu();
switch(num)
{
case 1:
InputExpression(str);
flag=0;
printf("%s\n",str);
getchar();
getchar();
break;
case 2:
if(str[0]==0)
{ printf("Expresson is Empty!");
getchar();
break;
}
if(!EXCUTE(str,&result))
{ printf("The expression has error!\n");
getchar();
}
else
{ printf("calulation has finished!\n");
getchar();
flag=1;
}
break;
case 3:
if(flag)
{ printf("#%s=%lf\n",str,result);
getchar();
getchar();
}
break;
case 4:
break;
}
if(num==4) break;
}
}
int menu(void)
{ int num;
clrscr();
printf("%20c1--input expression\n",' ');
printf("%20c2--calculation expression\n",' ');
printf("%20c3--print result\n",' ');
printf("%20c4--Quit\n",' ');
printf(" please select 1,2,3,4:");
do{
scanf("%d",&num);
}while(num<1 || num>4);
return(num);
}
按2计算
按3输出结果
按4退出
程序如下:
#include<stdio.h>
#include <string.h>
#include <conio.h>
#define PLUS 0
#define MINUS 1
#define POWER 2
#define DIVIDE 3
#define LEFTP 4
#define RIGHP 5
#define STARTEND 6
#define DIGIT 7
#define POINT 8
#define NUM 7
#define NO 32767
#define STACKSIZE 20
char a[]={'+','-','*','/','(',')','#'};
int j,PriorityTable[7][7]={{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{-1,-1,-1,-1,-1, 0, NO},
{ 1, 1, 1, 1,NO, 1, 1},
{-1,-1,-1,-1,-1,NO, 0}
};
int menu(void);
void
InputExpression(char str[])
{ int len;
printf("Input expression string:\n");
scanf("%s",str);
len=strlen(str);
str[len]='#';
str[len+1]='\0';
}
int GetCharType(char ch)
{ int i;
for(i=0;i<NUM;i++) if(ch==a[i]) return(i);
if(ch>='0' && ch<='9') return(DIGIT);
if(ch=='.') return(POINT);
return(-1);
}
int EXCUTE(char *str,double *Result)
{
int pp,strlength,topTr,topNd,CharType,OPTR[STACKSIZE],a;
double number,temp,OPND[STACKSIZE];
OPTR[0]=STARTEND;
topTr=1;
topNd=0;
pp=0;
while((str[pp]))
{ CharType=GetCharType(str[pp]);
switch(CharType)
{ case -1:
return(0);
case DIGIT:
number=0;
while
(str[pp]>='0' && str[pp]<='9')
{ number=number*10+(str[pp]-48);
pp++;
}
if(str[pp]=='.')
{ temp=10.0;
pp++;
while(str[pp]>='0' && str[pp]<='9')
{ number=number+(str[pp]-48)/temp;
temp=temp*10;
pp++;
}
}
OPND[topNd]=number;
topNd++;
break;
case POINT:
number=0;
temp=10.0;
pp++;
while(str[pp]>='0' && str[pp]<='9')
{ number=number+(str[pp]-48)/temp;
temp=temp*10;
pp++;
}
OPND[topNd]=number;printf("%lf",number);
topNd++;
break;
case PLUS:
case MINUS:
case POWER:
case DIVIDE:
{int i;
i=PriorityTable[OPTR[topTr-1]][CharType];
if(i==-1)
{OPTR[topTr]=CharType;
topTr++;
}
if(i==1)
{while(i==1)
{
j=OPTR[topTr-1];
if(j==0)OPND[topNd-2]=OPND[topNd-2]+OPND[topNd-1];
if(j==1)OPND[topNd-2]=OPND[topNd-2]-OPND[topNd-1];
if(j==2)OPND[topNd-2]=OPND[topNd-2]*OPND[topNd-1];
if(j==3)OPND[topNd-2]=OPND[topNd-2]/OPND[topNd-1];
topNd--;
topTr--;
i=PriorityTable[OPTR[topTr-1]][CharType];
}
OPTR[topTr]=CharType;topTr++;
}
pp++;break;
}
case LEFTP:
{
OPTR[topTr]=LEFTP;
topTr++;pp++;
break;
}
case RIGHP:
{ while(1)
if(OPTR[topTr-1]==LEFTP){topTr--;pp++;break;}
else if(OPTR[topTr-1]==STARTEND)return(0);
else
{j=OPTR[topTr-1];
if(j==0)OPND[topNd-2]=OPND[topNd-2]+OPND[topNd-1];
else if(j==1)OPND[topNd-2]=OPND[topNd-2]-OPND[topNd-1];
else if(j==2)OPND[topNd-2]=OPND[topNd-2]*OPND[topNd-1];
else if(j==3)OPND[topNd-2]=OPND[topNd-2]/OPND[topNd-1];
topNd--;
topTr--;
}
break;
}
case STARTEND:
while(OPTR[topTr-1]!=STARTEND)
{
j=OPTR[topTr-1];
if(j==0)OPND[topNd-2]=OPND[topNd-2]+OPND[topNd-1];
else if(j==1)OPND[topNd-2]=OPND[topNd-2]-OPND[topNd-1];
else if(j==2)OPND[topNd-2]=OPND[topNd-2]*OPND[topNd-1];
else if(j==3)OPND[topNd-2]=OPND[topNd-2]/OPND[topNd-1];
topNd--;
topTr--;
}
if(topNd==1)
{ *Result=OPND[0];return(1);
}
else return(0);
}
}
return(1
);
}
void main()
{ int num,flag;
double result;
char str[256];
str[0]=0;
while(1)
{ num=menu();
switch(num)
{
case 1:
InputExpression(str);
flag=0;
printf("%s\n",str);
getchar();
getchar();
break;
case 2:
if(str[0]==0)
{ printf("Expresson is Empty!");
getchar();
break;
}
if(!EXCUTE(str,&result))
{ printf("The expression has error!\n");
getchar();
}
else
{ printf("calulation has finished!\n");
getchar();
flag=1;
}
break;
case 3:
if(flag)
{ printf("#%s=%lf\n",str,result);
getchar();
getchar();
}
break;
case 4:
break;
}
if(num==4) break;
}
}
int menu(void)
{ int num;
clrscr();
printf("%20c1--input expression\n",' ');
printf("%20c2--calculation expression\n",' ');
printf("%20c3--print result\n",' ');
printf("%20c4--Quit\n",' ');
printf(" please select 1,2,3,4:");
do{
scanf("%d",&num);
}while(num<1 || num>4);
return(num);
}