C语言:任意算术表达式的求值
要程序代码,要有注释
追问:没有注释吗可以加注释吗?
谢谢
要程序代码,要有注释
追问:没有注释吗可以加注释吗?
谢谢
答案:#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <process.h>
#include <string.h>
#define N 100
double numStack[N]={0};
int numTop;
char opStack[N];
int opTop;
int op(char ch)
{
if(ch=='+'||ch=='-') return 2;
if(ch=='*'||ch=='/') return 3;
if(ch=='(') return -1;
return 0;
}
double result(double num1,char op,double num2)
{
if(op=='+') return num1+num2;
if(op=='-') return num1-num2;
if(op=='*') return num1*num2;
if(op=='/') return num1/num2;
return 0;
}
int compute(char str[])
{
double num=0;
int i=0,j=1,k=1;
int Flag=0;
numTop=opTop=0;
while(str[i]!='\0'||opTop>0)
{
if(str[i]>='0'&&str[i]<='9')
if(Flag==0)
num=num*10+str[i]-'0';
else
{
num+=(str[i]-'0')/(j*10.0);
j*=10;
}
else
if(str[i]=='.')
Flag=1;
else
if( k==1&&str[i]=='-'&&(i==0||op(str[i-1])) )
k=-1;
else
{
if(i>0&&!op(str[i-1])&&str[i]!='('&&str[i-1]!=')')
{
numStack[numTop++]=num*k;
num=0; j=1; Flag=0; k=1;
}
if(opTop==0||str[i]=='(')
opStack[opTop++]=str[i];
else
if(str[i]==')')
{
while(opTop>0&&opStack[--opTop]!='(')
{
numStack[numTop-2]=result(numStack[numTop-2],opStack[opTop],numStack[numTop-1]);
numTop--;
}
if(opStack[opTop]!='(') return 0;
}
else
{
if(str[i]=='\0'&&numTop==0) return 0;
while(opTop>0&&op(str[i])<=op(opStack[opTop-1]))
{
numStack[numTop-2]=result(numStack[numTop-2],opStack[--opTop],numStack[numTop-1]);
numTop--;
}
if(str[i]!='\0')
opStack[opTop++]=str[i];
}
}
if(str[i]!='\0')
i++;
}
if(numTop!=1||opTop!=0)
return 0;
return 1;
}
void face()
{
system("cls");
printf("__________________________________________________________________\n");
printf(" Save number(S) | Read number(R) | Clear(C) | Equal(E) | Quit(Q) \n");
printf("------------------------------------------------------------------\n");
}
main()
{
int i=0,j=0,k;
char str[N]="\0";
char num[N]="\0";
char save[N]="\0";
char ch;
double temp;
unsigned long temp2;
face();
printf("input an expression,press key 'E' to compute\n");
ch=getch();
while( 1 )
{
if(ch=='.'||ch==')'||op(ch)||ch>='0'&&ch<='9')
{
str[i++]=ch;
str[i]='\0';
face();
printf("input an expression,press key 'E' to compute\n");
printf("%s",str);
if( ch=='-'&&(i==1||op(str[i-2]))||ch>='0'&&ch<='9' )
{
num[j++]=ch;
num[j]='\0';
}
else j=0;
}
if(ch=='S'||ch=='s')
if(strlen(num))
{
face();
printf("%s has been saved\n",strcpy(save,num));
printf("input an expression,press key 'E' to compute\n");
printf("%s",str);
}
else
{
face();
printf("there is no number to save!\n");
printf("input an expression,press key 'E' to compute\n");
printf("%s",str);
}
if(ch=='R'||ch=='r')
if(strlen(save))
{
face();
printf("input an expression,press key 'E' to compute\n");
printf("%s",strcat(str,save));
i+=strlen(save);
}
if(ch=='C'||ch=='c')
{
if(strlen(str))
str[--i]='\0';
face();
printf("input an expression,press key 'E' to compute\n");
printf("%s",str);
}
if(ch=='E'||ch=='e')
{
if(compute(str))
{
printf("\n=%g\n",numStack[0]);
j=0; temp=numStack[0];
if(temp<0)
{
temp=-temp;
num[j++]='-';
num[j]='\0';
}
temp2=(unsigned long)temp;
k=1;
while(temp2/k>=10)
k*=10;
while(k)
{
num[j++]=temp2/k+'0';
num[j]='\0';
temp2=temp2%k;
k/=10;
}
temp=temp-(int)temp;
if(temp!=0)
{
num[j++]='.';
num[j]='\0';
temp+=0.0000005;
}
for(k=6;k>0;k--)
{
if(temp==0) break;
temp*=10;
num[j++]=(int)temp+'0';
num[j]='\0';
temp=temp-(int)temp;
}
}
else
{
face();
printf("input an expression,press key 'E' to compute\n");
printf("%s",str);
printf("\nwrong expression!");
}
i=0; j=0; str[0]='\0';
}
if(ch=='Q'||ch=='q')
{
printf("\nare you sure to quit?(Y/N)\n");
ch=getch();
if(ch=='Y'||ch=='y') break;
else
{
face();
printf("input an expression,press key 'E' to compute\n");
printf("%s",str);
}
}
ch=getch();
}
}#include <stdio.h>
#include <string.h>
#include <stdlib.h>#define MAX 50
int i = 0;
int judge_char( char ch );
int char_priority( char ch );
int judeg_number( char ch );
int conversion_char( char *conversion_pointer );
int evaluetion( int item1, int item2, char ch );int main( void )
{
char array1[MAX]; //存储中缀表达式
char stack[MAX]; //存储符号的栈
int array2[MAX]; //存储后缀表达式
int j = -1; //array2[]下标
int top = 0; //栈的下标
int item1, item2;
char ch;
stack[0] = '='; //让栈最初为'='号printf( "输入表达式:" );
gets( array1 );
while( array1[i] != '\0' && array1[i] != '=' )
{
if( judge_char( array1[i] ) == 1 ) //判断是否是符号
{
//比如符号的优先级
if( char_priority( array1[i] ) > char_priority( stack[top] ) )
{
stack[++top] = array1[i++];
}
else
{ //如果当然的符号小于栈项的符号,那就退栈
while( char_priority( array1[i] ) <= char_priority( stack[top] ) )
{
ch = stack[top--];
item1 = array2[j--];
item2 = array2[j--];
array2[++j] = evaluetion( item2, item1, ch );
}
stack[++top] = array1[i++];
}
}
//判断是否是数字
else if( ( judeg_number( array1[i] ) ) == 1 )
{
array2[++j] = conversion_char( array1 );
}
else if( array1[i] == '(' ) //如果是左括,直接进入
{
stack[++top] = array1[i++];
}
else if( array1[i] == ')' ) //如果是右括,那就输出栈中左括前的符号
{
while( stack[top] != '(' )
{
ch = stack[top];
item2 = array2[j--];
item1 = array2[j--];
array2[++j] = evaluetion( item1, item2, ch );
top--;
}
i++;
top--;
}
}
while( top > 0 )
{
ch = stack[top--]; //如果栈中还有符号,就完退栈
item1 = array2[j--];
item2 = array2[j--];
array2[++j] = evaluetion( item2, item1, ch );
}
printf( "%d\n", array2[j] );
return 0;
}//判断符号
int judge_char( char ch )
{
switch( ch )
{
case '+' :
case '-' :
case '*' :
case '/' : return 1;
default : return 0;
}
}//判断符号优先级
int char_priority( char ch )
{
switch( ch )
{
case '+' :
case '-' : return 1;
case '*' :
case '/' : return 2;
default : return 0;
}
}
//判断字符数
int judeg_number( char ch )
{
if( ch >= '0' && ch <= '9' )
{
return 1;
}
else
{
return 0;
}
}
//把字符转换为数字
int conversion_char( char *conversion_pointer )
{
char store_char[MAX] = { '\0' };
int j = -1;
int item;
while( judeg_number( conversion_pointer[i] ) == 1 && conversion_pointer[i] != '\0' )
{
store_char[++j] = conversion_pointer[i];
i++;
}
item