minhlapb Thành viên bậc 2
Tổng số bài gửi : 32 Points : 90 Reputation : 2 Join date : 22/07/2009
| Tiêu đề: Bài Ký pháp nghịch đảo Balan cài đặt bằng OOP Sun Aug 09, 2009 12:47 pm | |
| Đây là bài ký pháp nghịch đảo Balan được viết bằng OOP - Code:
-
//Kí phap nghich dao ba lan #include <iomanip.h> #include <string.h> #include <iostream.h> #include <stdlib.h>
typedef char Elem[10];
#define M 100
//Stack class Stack { private: int KichThuoc; int Top; Elem *Data; public: Stack(int); ~Stack(); int StackRong(); int StackDay(); void Push(Elem); void Pop(Elem &); void Nhap(int &); void DuyetStack(int); //-------------KHai bao ham hau to--------------- void Tach(char bt[], Elem tp[], int &n); int Dut(char pt); void RPN(Elem tp[], int n, Elem rpn[], int &k, int &Error); int TinhToan(int x, int y, char pt); int DanhGia(Elem rpn[], int k, int &Error); };
Stack::Stack(int KichThuoc) { Top = -1; this->KichThuoc = KichThuoc; Data = new Elem[this->KichThuoc]; }
Stack::~Stack() { delete []Data; }
int Stack::StackRong() { return Top == -1; }
int Stack::StackDay() { return Top == (this->KichThuoc) - 1; }
void Stack::Push(Elem x) { if(StackDay()) { cout<<"Stack day"; exit(0); } else { Top++; memcpy(this->Data[this->Top],x,sizeof(Elem)); } }
void Stack::Pop(Elem &x) { if(StackRong()) { cout<<"Stack rong"; exit(0); } else memcpy(x, this->Data[this->Top--],sizeof(Elem)); }
void Stack::DuyetStack(int k) { int i; Elem x; for(i=0; i<k; i++) { this->Pop(x); cout<<x<<endl; } }
void Stack::Nhap(int &n) { do { cout<<"Nhap so phan tu:"; cin>>n; }while(n<0); cin.ignore(); Elem x; for(int i=0; i<n; i++) { cout<<"Nhap x["<<i<<"] :"; cin.getline(x,10); this->Push(x); } } //---------------Ham su ly hau to-------------- void Stack::Tach(char bt[], Elem tp[], int &n) { Elem tmp; int j,i=0,l=strlen(bt); n=0; while(i<l) { j=0; while (i<l && !strchr("+-*/()",bt[i])) tmp[j++]=bt[i++]; tmp[j]='\0'; if (tmp[0]) strcpy(tp[n++],tmp); while (i<l && bt[i]==' ') i++; if (i<l && strchr("+-*/()",bt[i])) { tp[n][0]=bt[i++]; tp[n++][1]='\0'; } } }
int Stack::Dut (char pt) { switch(pt) { case '(': return 0; case '+': case '-': return 1; case '*': case '/': return 3; } return 0; }
void Stack::RPN (Elem tp[], int n, Elem rpn[], int &k, int &Error) { int i,stop; // Stack s; Elem pt; // CreateStack(s); k=i=0; Error=0; while (i<n && !Error) { switch (tp[i][0]) { case '(': Push(tp[i]);break; case ')': do { if (StackRong()) Error=1; else { Pop(pt); if (pt[0]!='(') strcpy(rpn[k++],pt); } }while (pt[0]!='(' && !Error);break; case '+': case '-': case '*': case '/': stop=0; while (!stop && !StackRong()) { Pop(pt); if (Dut(tp[i][0])>Dut(pt[0])) { Push(pt); stop=1; } else strcpy(rpn[k++],pt); } Push(tp[i]);break; default: strcpy(rpn[k++],tp[i]); } i++; } while (!StackRong() && !Error) { Pop(pt); if (pt[0]!='(') strcpy(rpn[k++],pt); else Error=1; } }
int Stack::TinhToan(int x,int y,char pt) { switch(pt) { case '+': return x+y; case '-': return x-y; case '*': return x*y; case '/': return x/y; } return 0; } int Stack::DanhGia(Elem rpn[],int k,int &Error) { Elem x,y; // Stack s(100); int i=0,ix,iy;
while(i<k && !Error) { if(strchr("+-*/",rpn[i][0])) { if(!StackRong()) { Pop(x); ix=atoi(x); if(!StackRong()) { Pop(y); iy=atoi(y); ix=TinhToan(iy,ix,rpn[i][0]); itoa(ix,x,10); Push(x); } else Error=1; } else Error=1; } else Push(rpn[i]); i++; } if(!Error) { Pop(x); ix=atoi(x); if(!StackRong()) Error=1; else return ix; } return 0; }
void main() { int n; Stack a(9); a.StackRong(); char bt[80]; Elem tp[20],rpn[20]; int i,k,Error; cin.getline(bt,80); a.Tach(bt,tp,n); cout<<"Tach bieu thuc trung to :"<<endl; for(i=0;i<n;i++) cout<<tp[i]<<" "; cout<<endl; a.RPN(tp,n,rpn,k,Error); if(!Error) { cout<<"Bieu thuc hau to :"<<endl; for(i=0;i<k;i++) cout<<rpn[i]<<" "; cout<<endl; } else cout<<"\nDoi hau to Error !\n"; n=a.DanhGia(rpn,k,Error); if(!Error) cout<<"\nKet qua :"<<n<<endl; else cout<<"nKet qua : Error !\n"; }
| |
|