#include<conio.h>
#include<string.h>
#include<iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
typedef char DATA;
typedef struct NODE
{
DATA Info;
NODE *pNext;
};
typedef struct STACK
{
NODE *pHead;
};
void Initialize(STACK &list)
{
list.pHead = NULL;
}
bool IsEmpty(STACK list)
{
return list.pHead == NULL;
}
NODE* CreateNode(DATA x)
{
NODE *p = new NODE; //cap phat vung nho cho p
if(p!=NULL) //cap phat duoc
{
p->Info = x;
p->pNext = NULL;
}
return p;
}
bool InsertHead(STACK &list, DATA x) //Them x vao dau ds
{
NODE *p = CreateNode(x); //Dua x vao NODE
if(p!=NULL) //tao duoc NODE
{
p->pNext = list.pHead;
list.pHead = p;
return true;
}
return false;
}
void OutputList(STACK list)
{
for(NODE*p = list.pHead; p!=NULL; p=p->pNext)
cout<<p->Info<<"\t";
cout<<endl;
}
bool DeleteHead(STACK &list)
{
if(!IsEmpty(list))
{
NODE *p = list.pHead;
list.pHead = list.pHead->pNext;
delete p;
return true; //xoa duoc
}
return false; //xoa ko duoc
}
bool Push(STACK &s, DATA x)
{
return InsertHead(s, x);
}
DATA Pop(STACK &s)
{
if(!IsEmpty(s))
{
DATA x = s.pHead->Info;
DeleteHead(s);
return x;
}
return -1; //Gia tri rac
}
DATA GetTop(STACK s)
{
if(!IsEmpty(s))
{
DATA x = s.pHead->Info;
return x;
}
return -1; //Gia tri rac
}
bool IsOperator(char c)
{
if(c == '+' || c == '-' || c == '*' || c == '/') return true;
return false;
}
int Priority(char c)
{
if(c == '(') return 0;
else if(c=='+' || c=='-')
return 1;
else //*, /
return 2;
}
int Tinhbxa(int b, char x, int a)
{
switch(x)
{
case '+':
return b+a;
case '-':
return b-a;
case '*':
return b*a;
case '/':
return b/a;
case '^':
return pow(b,a);
default:
cin>>"%s","Toan tu khong hop le";
exit(1);
}
}
void CalculatePostfix(char Postfix[])
{
int n = strlen(Postfix);
for(int i=0; i<n; i++)
{
int x= Postfix[i];
if(x>='0' && x<='9')
Push(s, x- '0');
else if(IsOperator(x))
{
int x= Pop(s);
int y= Pop(s);
}
}
}
void Infix2Postfix(char Infix[], char Postfix[])
{
int n = strlen(Infix);
int k=0; //chi so mang kq;
STACK s; //chua phep toan
Initialize(s);
for(int i=0; i<n; i++)
{
char x = Infix[i];
if(x>='0' && x <= '9') //So ==> Postfix
Postfix[k++] = x;
else if(x == '(') //( ==> Stack
Push(s, x);
else if(IsOperator(x)) //Bieu thuc
{
while(!IsEmpty(s))
{
char o2 = GetTop(s);
if(Priority(x) <= Priority(o2))
{
o2 = Pop(s); //Pop o2
Postfix[k++] = o2; //o2 ==> Postfix
}
else
break;
};
Push(s, x); //Bieu thuc ==> Stack
}
else if(x ==')')
{
char c;
do
{
c = Pop(s);
if(c!='(') Postfix[k++] = c; //c ==> Postfix
} while (c != '(');//Pop cho toi khi gap (
}
}
//Het for <==> Het bieu thuc
//Pop het Stack ==> Postfix
char c;
while (!IsEmpty(s))
{
c = Pop(s);
Postfix[k++] = c;
}
Postfix[k] = '\0';
}
int main()
{
STACK s;
Initialize(s);
char Infix[30];
cout<<"Nhap vao bieu thuc trung to: ";
gets(Infix);
char Postfix[30]; //Chuoi kq
Infix2Postfix(Infix, Postfix);
cout<<Postfix<<endl;
}
Nhờ sửa lỗi code tính giá trị hậu tố
Bạn đăng đoạn code này để làm gì vậy bạn?
3 Likes
Xin lỗi quên nói mục đích là hỏi mọi người lỗi sai ạ
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?