求救,那位GG帮一下小妹? 我以前做过,如果要的话,发短消息给我.留下mail及说明. 因为目前我手头上没有.含有这段代码的工程在家里的笔记本上. 最早星期一. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 到www.codeproject.net和www.codeguru.com去找一下吧,我记得现成的数学表达式代码很多呀。 你的名字不错嘛, 勇者天才,呵呵。 这可是作业题目噢,你让大家帮你做。//sigh女孩子总是不喜欢编程的东东,不知道这种现象什么时候能改变....有没有喜欢编程的女孩子啊?这里离天堂的宽带还有多远? #include "stdio.h"#include "conio.h"#include "process.h"#include "string.h"//#define _DEBUG#include "stack.cpp"//·ÖÎö±íÓÃ16λÕûÊýÊý×é±íʾ,ÆäÖÐ0-7λΪS,Rϱê,8-11λΪS,R,ACC±êÖ¾,12-15λΪ//¹éԼʱPOPÕ»Êý.#define S(x) 0x100|x#define R(x,y) 0x200|x|(y<<12)#define SYM(x) 0x00|x#define E SYM(6)#define T SYM(7)#define F SYM(8)#define ACC 0x400#define R1 R(1,3)#define R2 R(2,3)#define R3 R(3,1)#define R4 R(4,3)#define R5 R(5,3)#define R6 R(6,1)#define R7 R(7,3)#define R8 R(8,1)int cur=0;char TN='A';char a[1024];//MΪ·ÖÎö±íint M[][11]={//i + - * / ( ) # E T F{S(5), 0, 0, 0, 0, S(4), 0, 0, S(1), S(2), S(3), },//S0{0, S(6), S(7), 0, 0, 0, 0, ACC, 0, 0, 0, },//S1{0, R3, R3, S(8), S(9), 0, R3, R3, 0, 0, 0, },//S2{0, R6, R6, R6, R6, 0, R6, R6, 0, 0, 0, },//S3{S(5), 0, 0, 0, 0, S(4), 0, 0, S(10), S(2), S(3), },//S4{0, R8, R8, R8, R8, 0, R8, R8, 0, 0, 0, },//S5{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, S(11), S(3), },//S6{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, S(12), S(3), },//S7{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, 0, S(13), },//S8{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, 0, S(14), },//S9{0, S(6), S(7), 0, 0, 0, S(15), 0, 0, 0, 0, },//S10{0, R1, R1, S(8), S(9), 0, R1, R1, 0, 0, 0, },//S11{0, R2, R2, S(8), S(9), 0, R2, R2, 0, 0, 0, },//S12{0, R4, R4, R4, R4, 0, R4, R4, 0, 0, 0, },//S13{0, R5, R5, R5, R5, 0, R5, R5, 0, 0, 0, },//S14{0, R7, R7, R7, R7, 0, R7, R7, 0, 0, 0, },//S15};void Error(char * str,int idx){ printf("%s %d\n",str,idx); exit(-1);}int FindIdx(char a) //·µ»Ø·ûºÅµÄË÷ÒýºÅ{ switch(a){ case '+':return SYM(1); case '-':return SYM(2); case '*':return SYM(3); case '/':return SYM(4); case '(':return SYM(5); case ')':return SYM(6); case '#':return SYM(7); case 'E':return SYM(8); case 'T':return SYM(9); case 'F':return SYM(10); default: if(a>='a'&&a<='z') return SYM(0); Error("Invalid char in string column ",cur); } return 0;}void GenerateFour(int idx) //Éú³ÉËÄԪʽ{ char b; static Stack y; switch(idx) { case 0: case 3: case 6: case 7:return; case 1:b='+';break; case 2:b='-';break; case 4:b='*';break; case 5:b='/';break; case 8:y.Push(a[cur-1]);return; }#ifdef _DEBUG y.ListAllItem("Y:\n");#endif printf("(%c,%c,%c,%c)\n",b,y.Pop(),y.Pop(),TN); y.Push(TN++);}void main(){Stack s,w;int ss,atr,i;#ifdef _DEBUG //µ÷ÊÔÓà char *tst="#a+(b+c)#"; memcpy(a,tst,30);#else a[0]='#'; //ÊäÈë×Ö·û printf("Input:\n"); scanf("%s",&a[1]); int len=strlen(a); a[len++]='#'; a[len]=0;#endifstep1: //Push S0 into s and push # into w if(a[cur]!='#') Error("Require #\n",0); s.Push(S(0)); w.Push('#'); cur++;step2:#ifdef _DEBUG s.ListAllItem("S:\n"); w.ListAllItem("W:\n");#endif atr=FindIdx(a[cur]); if(atr>7|atr<0) Error("Invalid char in string column ",cur); ss=s.GetTop()&0xff; int result=M[ss][atr]; switch(result&0xf00) //ÅжÏÊÇÒƽø,¹éÔ¼,½ÓÊÜ»ò³ö´í { case 0: Error("Operator required in column ",cur); case S(0): w.Push(a[cur]); s.Push(result); cur++;break; case R(0,0): for(i=0;i<(result>>12);i++) s.Pop(),w.Pop(); ss=s.GetTop()&0xff; switch(result&0xff) { case 1:w.Push('E');break; case 2:w.Push('E');break; case 3:w.Push('E');break; case 4:w.Push('T');break; case 5:w.Push('T');break; case 6:w.Push('T');break; case 7:w.Push('F');break; case 8:w.Push('F');break; default: Error("Matrix Error!",cur); } GenerateFour(result&0xff); atr=FindIdx(w.GetTop()); if(M[ss][atr]==0) Error("Operator required in column ",cur); s.Push(M[ss][atr]); break; case ACC: goto AllOk; default: Error("Matrix error!\n",0); } goto step2;AllOk: ;} #define BOOL int#define TRUE -1#define FALSE 0class Stack{ class s_elm{ //Õ»ÔªËØ public: s_elm* pre; //Ç°Ö¸Õë int dat; //Êý¾Ý };s_elm* Tail; //Õ»µÄβָÕëint cnt; //Õ»ÔªËؼÆÊýpublic: Stack() {cnt=0;Tail=NULL;} ~Stack(){DeleteAll();}void Push(int a) //PUSH ¶ÑÕ»ÔªËØ{ s_elm* tmp=new s_elm; //·ÖÅäÒ»¸öеÄÔªËØ tmp->pre=Tail; //¼ÓÈëµ¥ÏòÁ´±í Tail=tmp; tmp->dat=a; cnt++;}BOOL Delete() //ɾ³ýÕ»¶¥ÔªËØ{ if(cnt!=0) { s_elm* tmp=Tail; Tail=tmp->pre; delete tmp; //ÊÍ·ÅÔªËØ cnt--; return TRUE; } return FALSE;}int Pop() //POP Õ»{ int tmp=Tail->dat; Delete(); return tmp;}void DeleteAll();char GetTop() {return Tail->dat;} //È¡Õ»¶¥ÔªËØ#ifdef _DEBUG //DEBUG ʱ,ÓÃÀ´¹Û²ìÕ»ÔªËØvoid ListAllItem(char *a){ s_elm* tmp=Tail; printf("%s\n",a); for(int i=0;i<cnt;i++) { printf("Hex%X,char %c\n",tmp->dat,(char)tmp->dat); tmp=tmp->pre; } printf("\n");}#endif};void Stack::DeleteAll() //ɾ³ýËùÓÐÔªËØ{ for(int i=0;i<cnt;i++) Delete();} //stack.cpp#define BOOL int#define TRUE -1#define FALSE 0class Stack{ class s_elm{ //栈元素 public: s_elm* pre; //前指针 int dat; //数据 };s_elm* Tail; //栈的尾指针int cnt; //栈元素计数public: Stack() {cnt=0;Tail=NULL;} ~Stack(){DeleteAll();}void Push(int a) //PUSH 堆栈元素{ s_elm* tmp=new s_elm; //分配一个新的元素 tmp->pre=Tail; //加入单向链表 Tail=tmp; tmp->dat=a; cnt++;}BOOL Delete() //删除栈顶元素{ if(cnt!=0) { s_elm* tmp=Tail; Tail=tmp->pre; delete tmp; //释放元素 cnt--; return TRUE; } return FALSE;}int Pop() //POP 栈{ int tmp=Tail->dat; Delete(); return tmp;}void DeleteAll();char GetTop() {return Tail->dat;} //取栈顶元素#ifdef _DEBUG //DEBUG 时,用来观察栈元素void ListAllItem(char *a){ s_elm* tmp=Tail; printf("%s\n",a); for(int i=0;i<cnt;i++) { printf("Hex%X,char %c\n",tmp->dat,(char)tmp->dat); tmp=tmp->pre; } printf("\n");}#endif};void Stack::DeleteAll() //删除所有元素{ for(int i=0;i<cnt;i++) Delete();} //main.cpp#include "stdio.h"#include "conio.h"#include "process.h"#include "string.h"//#define _DEBUG#include "stack.cpp"//分析表用16位整数数组表示,其中0-7位为S,R下标,8-11位为S,R,ACC标志,12-15位为//归约时POP栈数.#define S(x) 0x100|x#define R(x,y) 0x200|x|(y<<12)#define SYM(x) 0x00|x#define E SYM(6)#define T SYM(7)#define F SYM(8)#define ACC 0x400#define R1 R(1,3)#define R2 R(2,3)#define R3 R(3,1)#define R4 R(4,3)#define R5 R(5,3)#define R6 R(6,1)#define R7 R(7,3)#define R8 R(8,1)int cur=0;char TN='A';char a[1024];//M为分析表int M[][11]={//i + - * / ( ) # E T F{S(5), 0, 0, 0, 0, S(4), 0, 0, S(1), S(2), S(3), },//S0{0, S(6), S(7), 0, 0, 0, 0, ACC, 0, 0, 0, },//S1{0, R3, R3, S(8), S(9), 0, R3, R3, 0, 0, 0, },//S2{0, R6, R6, R6, R6, 0, R6, R6, 0, 0, 0, },//S3{S(5), 0, 0, 0, 0, S(4), 0, 0, S(10), S(2), S(3), },//S4{0, R8, R8, R8, R8, 0, R8, R8, 0, 0, 0, },//S5{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, S(11), S(3), },//S6{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, S(12), S(3), },//S7{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, 0, S(13), },//S8{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, 0, S(14), },//S9{0, S(6), S(7), 0, 0, 0, S(15), 0, 0, 0, 0, },//S10{0, R1, R1, S(8), S(9), 0, R1, R1, 0, 0, 0, },//S11{0, R2, R2, S(8), S(9), 0, R2, R2, 0, 0, 0, },//S12{0, R4, R4, R4, R4, 0, R4, R4, 0, 0, 0, },//S13{0, R5, R5, R5, R5, 0, R5, R5, 0, 0, 0, },//S14{0, R7, R7, R7, R7, 0, R7, R7, 0, 0, 0, },//S15};void Error(char * str,int idx){ printf("%s %d\n",str,idx); exit(-1);}int FindIdx(char a) //返回符号的索引号{ switch(a){ case '+':return SYM(1); case '-':return SYM(2); case '*':return SYM(3); case '/':return SYM(4); case '(':return SYM(5); case ')':return SYM(6); case '#':return SYM(7); case 'E':return SYM(8); case 'T':return SYM(9); case 'F':return SYM(10); default: if(a>='a'&&a<='z') return SYM(0); Error("Invalid char in string column ",cur); } return 0;}void GenerateFour(int idx) //生成四元式{ char b; static Stack y; switch(idx) { case 0: case 3: case 6: case 7:return; case 1:b='+';break; case 2:b='-';break; case 4:b='*';break; case 5:b='/';break; case 8:y.Push(a[cur-1]);return; }#ifdef _DEBUG y.ListAllItem("Y:\n");#endif printf("(%c,%c,%c,%c)\n",b,y.Pop(),y.Pop(),TN); y.Push(TN++);}void main(){Stack s,w;int ss,atr,i;#ifdef _DEBUG //调试用 char *tst="#a+(b+c)#"; memcpy(a,tst,30);#else a[0]='#'; //输入字符 printf("Input:\n"); scanf("%s",&a[1]); int len=strlen(a); a[len++]='#'; a[len]=0;#endifstep1: //Push S0 into s and push # into w if(a[cur]!='#') Error("Require #\n",0); s.Push(S(0)); w.Push('#'); cur++;step2:#ifdef _DEBUG s.ListAllItem("S:\n"); w.ListAllItem("W:\n");#endif atr=FindIdx(a[cur]); if(atr>7|atr<0) Error("Invalid char in string column ",cur); ss=s.GetTop()&0xff; int result=M[ss][atr]; switch(result&0xf00) //判断是移进,归约,接受或出错 { case 0: Error("Operator required in column ",cur); case S(0): w.Push(a[cur]); s.Push(result); cur++;break; case R(0,0): for(i=0;i<(result>>12);i++) s.Pop(),w.Pop(); ss=s.GetTop()&0xff; switch(result&0xff) { case 1:w.Push('E');break; case 2:w.Push('E');break; case 3:w.Push('E');break; case 4:w.Push('T');break; case 5:w.Push('T');break; case 6:w.Push('T');break; case 7:w.Push('F');break; case 8:w.Push('F');break; default: Error("Matrix Error!",cur); } GenerateFour(result&0xff); atr=FindIdx(w.GetTop()); if(M[ss][atr]==0) Error("Operator required in column ",cur); s.Push(M[ss][atr]); break; case ACC: goto AllOk; default: Error("Matrix error!\n",0); } goto step2;AllOk: ;} 运行的时候,出现下述错误是怎么回事?fatal error C1083: Cannot open include file: 'stack.cpp': No such file or directory是不是C++里没有'stack.cpp'这个文件? 这个方法已经很简单了啊~注意,如果你是把上面的源码拷成的一个整的文件,你要把#include "stack.cpp"那句注视掉。因为我这个本来是2个文件的。以前学编译的时候做的作业,算法什么的都忘了。嘿嘿。编译好了,你可以试试输入如:a+b+c(a+c)*(c+d)之类的表达式试试,正确输出的应该是每一步计算的4元式。如果你要计算表达式的值,你要自己改代码了:P 又找到一个简单点的,依然想不起是什么算法#include "stdio.h"#include "iostream.h"#include "string.h"#include "process.h"#include "conio.h"#define BOOL int#define TRUE -1#define FALSE 0//#define _DEBUGclass Stack{ class s_elm{ public: s_elm* pre; char dat; };s_elm* Tail;int cnt;public: Stack(){ cnt=0; Tail=NULL;} ~Stack(){ DeleteAll();} void Push(char a){ s_elm* tmp=new s_elm; tmp->pre=Tail; Tail=tmp; tmp->dat=a; cnt++;}BOOL Delete(){ if(cnt!=0) { s_elm* tmp=Tail; Tail=tmp->pre; delete tmp; cnt--; return TRUE; } return FALSE;}char Pop(){ char tmp=Tail->dat; Delete(); return tmp;}void DeleteAll();char GetTop() {return Tail->dat;}#ifdef _DEBUGvoid ListAllItem(){ s_elm* tmp=Tail; for(int i=0;i<cnt;i++) { printf("%c",tmp->dat); tmp=tmp->pre; } printf("\n");}#endif};void Stack::DeleteAll(){ for(int i=0;i<cnt;i++) Delete();}// sym ( */ +- ) #// 0 1 2 3 4 5char *M[]={//012345 "xx>>>>",//sym "<<<<=x",//( "<<>>>>",//*/ "<<<>>>",//+- "xx>>>>",//) "<<<<x=",//#};int GetOprIdx(char opr){ if(opr>='a'&&opr<='z')return 0; switch(opr) { case '(': return 1; case '*':case '/': return 2; case '+':case '-': return 3; case ')':return 4; case '#':return 5; default: return -1; };}void Error(char* Err,int idx){ printf("%s%d\n",Err,idx); exit(-1);}void main(){ Stack w,a; char* inch=new char[1024]; int opr1,opr2; char f='A';//input#ifdef _DEBUG char *tst="#a+(b+c)#"; memcpy(inch,tst,30);#else inch[0]='#'; cout<<"Input:\n"; cin>>&inch[1]; int i=strlen(inch); inch[i++]='#'; inch[i]=0;#endif//processstep1: i=0; char cur=inch[i]; if(cur!='#') Error("Input Error,Require \'#\' column",0);step2: w.Push(cur);step3: cur=inch[++i];#ifdef _DEBUG printf("Now Scan %d\n w stack is :",i); w.ListAllItem(); printf("a stack is : "); a.ListAllItem(); getch();#endifstep4: opr1=GetOprIdx(w.GetTop()); opr2=GetOprIdx(cur); if(opr2==-1) Error("Invalid character in input string in column ",i ); switch(M[opr1][opr2]) { case 'x': Error("Operator Required in column ",i); case '<': goto step5; case '>': goto step6; case '=': goto step9; default: Error("Maxtrix Error !",-1); };step5: goto step2;step6: if(w.GetTop()<'a'||w.GetTop()>'z') { printf("(%c,%c,%c,%c)\n",w.Pop(),a.Pop(),a.Pop(),f); a.Push(f); f++; } else a.Push(w.Pop());step7: if(cur==')') goto step4;step8: if(w.GetTop()<'a'||w.GetTop()>'z') goto step4; else goto step2;step9:step10: if(w.GetTop()!='#') { w.Pop(); goto step3; } printf("Result in %c\n",--f); delete inch;} 摄像机平移量与图像中像素量 为何压缩数据库时,出现了打不开的问题 库函数重名怎么办? 关于软件项目交易 如何隐藏程序??在任务管理器中看不到! 界面切分后显示状态不稳定 关于OICQ穿透防火墙原理的讨论。请大家进来看看。 vs2013程序xp运行内存错误 现在开发三层结构的应用,那种工具环境比较好? GetPrivateProfileString 迭代器! 哪位大侠有CSocket的好的中文文章或教程(只要CSocket的编程,不要winsock API的),如果不大可不可以EMAIL给
我记得现成的数学表达式代码很多呀。
女孩子总是不喜欢编程的东东,不知道这种现象什么时候能改变....有没有喜欢编程的女孩子啊?这里离天堂的宽带还有多远?
#include "conio.h"
#include "process.h"
#include "string.h"//#define _DEBUG#include "stack.cpp"//·ÖÎö±íÓÃ16λÕûÊýÊý×é±íʾ,ÆäÖÐ0-7λΪS,Rϱê,8-11λΪS,R,ACC±êÖ¾,12-15λΪ
//¹éԼʱPOPÕ»Êý.#define S(x) 0x100|x
#define R(x,y) 0x200|x|(y<<12)
#define SYM(x) 0x00|x
#define E SYM(6)
#define T SYM(7)
#define F SYM(8)
#define ACC 0x400#define R1 R(1,3)
#define R2 R(2,3)
#define R3 R(3,1)
#define R4 R(4,3)
#define R5 R(5,3)
#define R6 R(6,1)
#define R7 R(7,3)
#define R8 R(8,1)int cur=0;
char TN='A';
char a[1024];
//MΪ·ÖÎö±í
int M[][11]={
//i + - * / ( ) # E T F
{S(5), 0, 0, 0, 0, S(4), 0, 0, S(1), S(2), S(3), },//S0
{0, S(6), S(7), 0, 0, 0, 0, ACC, 0, 0, 0, },//S1
{0, R3, R3, S(8), S(9), 0, R3, R3, 0, 0, 0, },//S2
{0, R6, R6, R6, R6, 0, R6, R6, 0, 0, 0, },//S3
{S(5), 0, 0, 0, 0, S(4), 0, 0, S(10), S(2), S(3), },//S4
{0, R8, R8, R8, R8, 0, R8, R8, 0, 0, 0, },//S5
{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, S(11), S(3), },//S6
{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, S(12), S(3), },//S7
{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, 0, S(13), },//S8
{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, 0, S(14), },//S9
{0, S(6), S(7), 0, 0, 0, S(15), 0, 0, 0, 0, },//S10
{0, R1, R1, S(8), S(9), 0, R1, R1, 0, 0, 0, },//S11
{0, R2, R2, S(8), S(9), 0, R2, R2, 0, 0, 0, },//S12
{0, R4, R4, R4, R4, 0, R4, R4, 0, 0, 0, },//S13
{0, R5, R5, R5, R5, 0, R5, R5, 0, 0, 0, },//S14
{0, R7, R7, R7, R7, 0, R7, R7, 0, 0, 0, },//S15
};void Error(char * str,int idx)
{
printf("%s %d\n",str,idx);
exit(-1);
}int FindIdx(char a) //·µ»Ø·ûºÅµÄË÷ÒýºÅ
{
switch(a){
case '+':return SYM(1);
case '-':return SYM(2);
case '*':return SYM(3);
case '/':return SYM(4);
case '(':return SYM(5);
case ')':return SYM(6);
case '#':return SYM(7);
case 'E':return SYM(8);
case 'T':return SYM(9);
case 'F':return SYM(10);
default:
if(a>='a'&&a<='z')
return SYM(0);
Error("Invalid char in string column ",cur);
}
return 0;
}
void GenerateFour(int idx) //Éú³ÉËÄԪʽ
{
char b;
static Stack y;
switch(idx)
{
case 0:
case 3:
case 6:
case 7:return;
case 1:b='+';break;
case 2:b='-';break;
case 4:b='*';break;
case 5:b='/';break;
case 8:y.Push(a[cur-1]);return;
}
#ifdef _DEBUG
y.ListAllItem("Y:\n");
#endif printf("(%c,%c,%c,%c)\n",b,y.Pop(),y.Pop(),TN);
y.Push(TN++);
}
void main()
{
Stack s,w;
int ss,atr,i;
#ifdef _DEBUG //µ÷ÊÔÓÃ
char *tst="#a+(b+c)#";
memcpy(a,tst,30);
#else
a[0]='#'; //ÊäÈë×Ö·û
printf("Input:\n");
scanf("%s",&a[1]);
int len=strlen(a);
a[len++]='#';
a[len]=0;
#endif
step1: //Push S0 into s and push # into w
if(a[cur]!='#')
Error("Require #\n",0);
s.Push(S(0));
w.Push('#');
cur++;
step2:
#ifdef _DEBUG
s.ListAllItem("S:\n");
w.ListAllItem("W:\n");
#endif
atr=FindIdx(a[cur]);
if(atr>7|atr<0)
Error("Invalid char in string column ",cur);
ss=s.GetTop()&0xff;
int result=M[ss][atr];
switch(result&0xf00) //ÅжÏÊÇÒƽø,¹éÔ¼,½ÓÊÜ»ò³ö´í
{
case 0:
Error("Operator required in column ",cur);
case S(0):
w.Push(a[cur]);
s.Push(result);
cur++;break;
case R(0,0):
for(i=0;i<(result>>12);i++)
s.Pop(),w.Pop();
ss=s.GetTop()&0xff;
switch(result&0xff)
{
case 1:w.Push('E');break;
case 2:w.Push('E');break;
case 3:w.Push('E');break;
case 4:w.Push('T');break;
case 5:w.Push('T');break;
case 6:w.Push('T');break;
case 7:w.Push('F');break;
case 8:w.Push('F');break;
default:
Error("Matrix Error!",cur);
}
GenerateFour(result&0xff);
atr=FindIdx(w.GetTop());
if(M[ss][atr]==0)
Error("Operator required in column ",cur);
s.Push(M[ss][atr]);
break;
case ACC:
goto AllOk;
default:
Error("Matrix error!\n",0);
}
goto step2;
AllOk:
;
}
#define BOOL int
#define TRUE -1
#define FALSE 0
class Stack{
class s_elm{ //Õ»ÔªËØ
public:
s_elm* pre; //Ç°Ö¸Õë
int dat; //Êý¾Ý
};
s_elm* Tail; //Õ»µÄβָÕë
int cnt; //Õ»ÔªËؼÆÊý
public:
Stack() {cnt=0;Tail=NULL;}
~Stack(){DeleteAll();}
void Push(int a) //PUSH ¶ÑÕ»ÔªËØ
{
s_elm* tmp=new s_elm; //·ÖÅäÒ»¸öеÄÔªËØ
tmp->pre=Tail; //¼ÓÈëµ¥ÏòÁ´±í
Tail=tmp;
tmp->dat=a;
cnt++;
}
BOOL Delete() //ɾ³ýÕ»¶¥ÔªËØ
{
if(cnt!=0)
{
s_elm* tmp=Tail;
Tail=tmp->pre;
delete tmp; //ÊÍ·ÅÔªËØ
cnt--;
return TRUE;
}
return FALSE;
}
int Pop() //POP Õ»
{
int tmp=Tail->dat;
Delete();
return tmp;
}
void DeleteAll();
char GetTop() {return Tail->dat;} //È¡Õ»¶¥ÔªËØ
#ifdef _DEBUG //DEBUG ʱ,ÓÃÀ´¹Û²ìÕ»ÔªËØ
void ListAllItem(char *a)
{
s_elm* tmp=Tail;
printf("%s\n",a);
for(int i=0;i<cnt;i++)
{
printf("Hex%X,char %c\n",tmp->dat,(char)tmp->dat);
tmp=tmp->pre;
}
printf("\n");
}
#endif
};
void Stack::DeleteAll() //ɾ³ýËùÓÐÔªËØ
{
for(int i=0;i<cnt;i++)
Delete();
}
#define BOOL int
#define TRUE -1
#define FALSE 0
class Stack{
class s_elm{ //栈元素
public:
s_elm* pre; //前指针
int dat; //数据
};
s_elm* Tail; //栈的尾指针
int cnt; //栈元素计数
public:
Stack() {cnt=0;Tail=NULL;}
~Stack(){DeleteAll();}
void Push(int a) //PUSH 堆栈元素
{
s_elm* tmp=new s_elm; //分配一个新的元素
tmp->pre=Tail; //加入单向链表
Tail=tmp;
tmp->dat=a;
cnt++;
}
BOOL Delete() //删除栈顶元素
{
if(cnt!=0)
{
s_elm* tmp=Tail;
Tail=tmp->pre;
delete tmp; //释放元素
cnt--;
return TRUE;
}
return FALSE;
}
int Pop() //POP 栈
{
int tmp=Tail->dat;
Delete();
return tmp;
}
void DeleteAll();
char GetTop() {return Tail->dat;} //取栈顶元素
#ifdef _DEBUG //DEBUG 时,用来观察栈元素
void ListAllItem(char *a)
{
s_elm* tmp=Tail;
printf("%s\n",a);
for(int i=0;i<cnt;i++)
{
printf("Hex%X,char %c\n",tmp->dat,(char)tmp->dat);
tmp=tmp->pre;
}
printf("\n");
}
#endif
};
void Stack::DeleteAll() //删除所有元素
{
for(int i=0;i<cnt;i++)
Delete();
}
//main.cpp
#include "stdio.h"
#include "conio.h"
#include "process.h"
#include "string.h"//#define _DEBUG#include "stack.cpp"//分析表用16位整数数组表示,其中0-7位为S,R下标,8-11位为S,R,ACC标志,12-15位为
//归约时POP栈数.#define S(x) 0x100|x
#define R(x,y) 0x200|x|(y<<12)
#define SYM(x) 0x00|x
#define E SYM(6)
#define T SYM(7)
#define F SYM(8)
#define ACC 0x400#define R1 R(1,3)
#define R2 R(2,3)
#define R3 R(3,1)
#define R4 R(4,3)
#define R5 R(5,3)
#define R6 R(6,1)
#define R7 R(7,3)
#define R8 R(8,1)int cur=0;
char TN='A';
char a[1024];
//M为分析表
int M[][11]={
//i + - * / ( ) # E T F
{S(5), 0, 0, 0, 0, S(4), 0, 0, S(1), S(2), S(3), },//S0
{0, S(6), S(7), 0, 0, 0, 0, ACC, 0, 0, 0, },//S1
{0, R3, R3, S(8), S(9), 0, R3, R3, 0, 0, 0, },//S2
{0, R6, R6, R6, R6, 0, R6, R6, 0, 0, 0, },//S3
{S(5), 0, 0, 0, 0, S(4), 0, 0, S(10), S(2), S(3), },//S4
{0, R8, R8, R8, R8, 0, R8, R8, 0, 0, 0, },//S5
{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, S(11), S(3), },//S6
{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, S(12), S(3), },//S7
{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, 0, S(13), },//S8
{S(5), 0, 0, 0, 0, S(4), 0, 0, 0, 0, S(14), },//S9
{0, S(6), S(7), 0, 0, 0, S(15), 0, 0, 0, 0, },//S10
{0, R1, R1, S(8), S(9), 0, R1, R1, 0, 0, 0, },//S11
{0, R2, R2, S(8), S(9), 0, R2, R2, 0, 0, 0, },//S12
{0, R4, R4, R4, R4, 0, R4, R4, 0, 0, 0, },//S13
{0, R5, R5, R5, R5, 0, R5, R5, 0, 0, 0, },//S14
{0, R7, R7, R7, R7, 0, R7, R7, 0, 0, 0, },//S15
};void Error(char * str,int idx)
{
printf("%s %d\n",str,idx);
exit(-1);
}int FindIdx(char a) //返回符号的索引号
{
switch(a){
case '+':return SYM(1);
case '-':return SYM(2);
case '*':return SYM(3);
case '/':return SYM(4);
case '(':return SYM(5);
case ')':return SYM(6);
case '#':return SYM(7);
case 'E':return SYM(8);
case 'T':return SYM(9);
case 'F':return SYM(10);
default:
if(a>='a'&&a<='z')
return SYM(0);
Error("Invalid char in string column ",cur);
}
return 0;
}
void GenerateFour(int idx) //生成四元式
{
char b;
static Stack y;
switch(idx)
{
case 0:
case 3:
case 6:
case 7:return;
case 1:b='+';break;
case 2:b='-';break;
case 4:b='*';break;
case 5:b='/';break;
case 8:y.Push(a[cur-1]);return;
}
#ifdef _DEBUG
y.ListAllItem("Y:\n");
#endif printf("(%c,%c,%c,%c)\n",b,y.Pop(),y.Pop(),TN);
y.Push(TN++);
}
void main()
{
Stack s,w;
int ss,atr,i;
#ifdef _DEBUG //调试用
char *tst="#a+(b+c)#";
memcpy(a,tst,30);
#else
a[0]='#'; //输入字符
printf("Input:\n");
scanf("%s",&a[1]);
int len=strlen(a);
a[len++]='#';
a[len]=0;
#endif
step1: //Push S0 into s and push # into w
if(a[cur]!='#')
Error("Require #\n",0);
s.Push(S(0));
w.Push('#');
cur++;
step2:
#ifdef _DEBUG
s.ListAllItem("S:\n");
w.ListAllItem("W:\n");
#endif
atr=FindIdx(a[cur]);
if(atr>7|atr<0)
Error("Invalid char in string column ",cur);
ss=s.GetTop()&0xff;
int result=M[ss][atr];
switch(result&0xf00) //判断是移进,归约,接受或出错
{
case 0:
Error("Operator required in column ",cur);
case S(0):
w.Push(a[cur]);
s.Push(result);
cur++;break;
case R(0,0):
for(i=0;i<(result>>12);i++)
s.Pop(),w.Pop();
ss=s.GetTop()&0xff;
switch(result&0xff)
{
case 1:w.Push('E');break;
case 2:w.Push('E');break;
case 3:w.Push('E');break;
case 4:w.Push('T');break;
case 5:w.Push('T');break;
case 6:w.Push('T');break;
case 7:w.Push('F');break;
case 8:w.Push('F');break;
default:
Error("Matrix Error!",cur);
}
GenerateFour(result&0xff);
atr=FindIdx(w.GetTop());
if(M[ss][atr]==0)
Error("Operator required in column ",cur);
s.Push(M[ss][atr]);
break;
case ACC:
goto AllOk;
default:
Error("Matrix error!\n",0);
}
goto step2;
AllOk:
;
}
fatal error C1083: Cannot open include file: 'stack.cpp': No such file or directory
是不是C++里没有'stack.cpp'这个文件?
以前学编译的时候做的作业,算法什么的都忘了。嘿嘿。
编译好了,你可以试试输入如:a+b+c
(a+c)*(c+d)之类的表达式试试,正确输出的应该是每一步计算的4元式。如果你要计算表达式的值,你要自己改代码了:P
#include "stdio.h"
#include "iostream.h"
#include "string.h"
#include "process.h"
#include "conio.h"#define BOOL int
#define TRUE -1
#define FALSE 0//#define _DEBUG
class Stack{
class s_elm{
public:
s_elm* pre;
char dat;
};
s_elm* Tail;
int cnt;
public:
Stack()
{
cnt=0;
Tail=NULL;
}
~Stack()
{
DeleteAll();
}
void Push(char a)
{
s_elm* tmp=new s_elm;
tmp->pre=Tail;
Tail=tmp;
tmp->dat=a;
cnt++;
}
BOOL Delete()
{
if(cnt!=0)
{
s_elm* tmp=Tail;
Tail=tmp->pre;
delete tmp;
cnt--;
return TRUE;
}
return FALSE;
}
char Pop()
{
char tmp=Tail->dat;
Delete();
return tmp;
}
void DeleteAll();
char GetTop() {return Tail->dat;}
#ifdef _DEBUG
void ListAllItem()
{
s_elm* tmp=Tail;
for(int i=0;i<cnt;i++)
{
printf("%c",tmp->dat);
tmp=tmp->pre;
}
printf("\n");
}
#endif
};
void Stack::DeleteAll()
{
for(int i=0;i<cnt;i++)
Delete();
}// sym ( */ +- ) #
// 0 1 2 3 4 5
char *M[]={
//012345
"xx>>>>",//sym
"<<<<=x",//(
"<<>>>>",//*/
"<<<>>>",//+-
"xx>>>>",//)
"<<<<x=",//#
};int GetOprIdx(char opr)
{
if(opr>='a'&&opr<='z')return 0;
switch(opr)
{
case '(': return 1;
case '*':case '/': return 2;
case '+':case '-': return 3;
case ')':return 4;
case '#':return 5;
default: return -1;
};
}
void Error(char* Err,int idx)
{
printf("%s%d\n",Err,idx);
exit(-1);
}
void main()
{
Stack w,a;
char* inch=new char[1024];
int opr1,opr2;
char f='A';//input
#ifdef _DEBUG
char *tst="#a+(b+c)#";
memcpy(inch,tst,30);
#else inch[0]='#';
cout<<"Input:\n";
cin>>&inch[1];
int i=strlen(inch);
inch[i++]='#';
inch[i]=0;
#endif
//process
step1:
i=0;
char cur=inch[i];
if(cur!='#')
Error("Input Error,Require \'#\' column",0);
step2:
w.Push(cur);
step3:
cur=inch[++i];
#ifdef _DEBUG
printf("Now Scan %d\n w stack is :",i);
w.ListAllItem();
printf("a stack is : ");
a.ListAllItem();
getch();
#endif
step4:
opr1=GetOprIdx(w.GetTop());
opr2=GetOprIdx(cur);
if(opr2==-1)
Error("Invalid character in input string in column ",i );
switch(M[opr1][opr2])
{
case 'x': Error("Operator Required in column ",i);
case '<': goto step5;
case '>': goto step6;
case '=': goto step9;
default: Error("Maxtrix Error !",-1);
};
step5:
goto step2;
step6:
if(w.GetTop()<'a'||w.GetTop()>'z')
{
printf("(%c,%c,%c,%c)\n",w.Pop(),a.Pop(),a.Pop(),f);
a.Push(f);
f++;
}
else
a.Push(w.Pop());
step7:
if(cur==')') goto step4;
step8:
if(w.GetTop()<'a'||w.GetTop()>'z') goto step4;
else goto step2;
step9:
step10:
if(w.GetTop()!='#')
{
w.Pop();
goto step3;
}
printf("Result in %c\n",--f); delete inch;
}