?? fouryuan.cpp
字號:
/* opposite bo-lan expression writen on Nov.13.1999 */
#define N 30
#include <stdio.h>
#include <conio.h>
#include <string.h>
typedef struct unit{char symbol;int num1,num2,result;} node;
node Four[N]; /* four unit expression h*/
char inputs[N]; /* input stream ;i*/
char symbols[N]; /* char stream ;j*/
int process[N]; /*processcedur stream ;k*/
char B=' ';
int i=0, // point to inputs[N]
j=0, // point to symbols[N]
k=0, //point to process[N]
h=0; // point to Four[N]
void init()
{printf("This is processgram of four unit expression\nPlease input an arithmetic \
expression\nexample: 2+4*(6-9/3+(5-12*4)/2+8)+12\n ");
scanf("%s",inputs);
strcat(inputs,"#");
symbols[j]='~';
j++;
}
void print()
{ int s;
for(s=0;s<h;s++)
printf("(%c,%d,%d,%d)\n",Four[s].symbol,Four[s].num1,Four[s].num2,Four[s].result);
}
PUSH(char A[N],char a)
{
A[j]=a;
j++;
return 0;
}
PUSH1( int A[N],char a)
{ a-=48; // change char to integer
A[k]=a;
k++;
return 0;
}
POP(char A[N])
{char d;
d=A[j-1];
j--;
return d;
}
Creat4Units()
{Four[h].symbol=symbols[j-1];
Four[h].num1=process[k-2];
Four[h].num2=process[k-1];
if (Four[h].symbol=='+')
Four[h].result=Four[h].num1+Four[h].num2;
if (Four[h].symbol=='-')
Four[h].result=Four[h].num1-Four[h].num2;
if (Four[h].symbol=='*')
Four[h].result=Four[h].num1*Four[h].num2;
if (Four[h].symbol=='/')
Four[h].result=Four[h].num1/Four[h].num2;
k=k-2;
j--;
process[k]=Four[h].result;
k++;
h++;
return 0;
}
analyze()
{i=0;
while(1)
{if (inputs[i]>='0' && inputs[i]<='9') // now only can input 0 to 9 ,can't 10 ,111
{
PUSH1(process,inputs[i]);
i++;
}
else
if (inputs[i]=='(')
{PUSH(symbols,'~');
i++;
}
else
if (inputs[i]=='+' || inputs[i]=='-' || inputs[i]=='*' || inputs[i]=='/')
{if (symbols[j-1]=='~') //identifer of rapen
{PUSH(symbols,inputs[i]);
i++;
}
else
if ((symbols[j-1]=='+'||symbols[j-1]=='-')&&(inputs[i]=='*'||inputs[i]=='/'))
{PUSH(symbols,inputs[i]);
i++;
}
else
{Creat4Units();
}
}
else
if (inputs[i]==')')
{if (symbols[j-1]=='~')
{B=POP(symbols);
i++;
}
else
{Creat4Units();
}
}
else
if (inputs[i]=='#')
{ if (symbols[j-1]=='~')
{break;
}
else
{Creat4Units();
}
}
else{printf("The exptession is error!\n");
return(0);
}
}
return(1); }
void main()
{
init();
analyze();
print();
getch();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -