Tính Biểu Thức Toán Dạng Hậu Tố.
Trang 1 trong tổng số 1 trang
Tính Biểu Thức Toán Dạng Hậu Tố.
#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
{ this->Pop(x);
cout<<
}
}
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
{ cout<<"Nhap x["<<<"] :";
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
{ j=0;
while (i tmp[j]='\0';
if (tmp[0]) strcpy(tp[n++],tmp);
while (i
if (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
{ 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
{ 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 :"<
for(i=0;i<<<" ";
cout<
a.RPN(tp,n,rpn,k,Error);
if(!Error)
{ cout<<"Bieu thuc hau to :"<
for(i=0;i<<<" ";
cout<
}
else cout<<"\nDoi hau to Error !\n";
n=a.DanhGia(rpn,k,Error);
if(!Error) cout<<"\nKet qua :"<<
else cout<<"nKet qua : Error !\n";
}
mysteriesmoonlight- Supper mem
- Tổng số bài gửi : 14
Số điểm : 43
Số lần được cám ơn : 12
Ngày đến diễn đàn: : 27/08/2009
Tuổi : 35
Re: Tính Biểu Thức Toán Dạng Hậu Tố.
int Stack::DanhGia(Elem rpn[],int k,int &Error)
thiếu while(i<k && !Error)
thiếu while(i<k && !Error)
mysteriesmoonlight- Supper mem
- Tổng số bài gửi : 14
Số điểm : 43
Số lần được cám ơn : 12
Ngày đến diễn đàn: : 27/08/2009
Tuổi : 35
Similar topics
» Thuật toán DDA_Line dạng tổng quát
» Sửa dùm mình bài đa thức
» Chương trình thực tập của trường DH TDT
» Có đi thực tập ở công ty larion hay không?
» Kiếm tiền kiểu này hay và thiết thực nhỉ
» Sửa dùm mình bài đa thức
» Chương trình thực tập của trường DH TDT
» Có đi thực tập ở công ty larion hay không?
» Kiếm tiền kiểu này hay và thiết thực nhỉ
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|