Viết chương trình máy tính bỏ túi sử dụng các toán tử +, -, * , /, (), các toán hạng: sử dụng kí pháp BaLan.
#include <iostream>
#include<stdio.h>
#include <string>
#define maxx 255
using namespace std;
struct Nodeint
{
int _a;
Nodeint * _next;
};
struct anode{
char ope; int val;
};
struct node{
char ope; int val;
node *l,*r;
};
struct STACK{
Nodeint * _Top;
};
void Init(STACK &s){
s._Top = NULL;
}
bool IsEmpty(STACK s){
if(s._Top == NULL) return true;
return false;
}
Nodeint *GetNode(int a){
Nodeint *p = new Nodeint;
if (p == NULL) {cout<<"khong du bo nho de cap phat"; return NULL;}
p->_a = a;
p->_next =NULL;
return p;
}
bool Push(STACK &s, Nodeint *p){
if(IsEmpty(s) == true){
s._Top = p; return true;
}
p->_next = s._Top;
s._Top = p;
return true;
}
bool Pop(STACK &s, int &a){
if(IsEmpty(s) == true){
cout<<" stack rong;"; return false;
}
Nodeint * p =s._Top;
a = p->_a;
s._Top = s._Top->_next; ///// ti phai xem lai cho nayyyyyyyyyyyyy
delete p;
return true;
}
char num[10]={'0','1','2','3','4','5','6','7','8','9'};
char ope[4]={'+' , '-' , '*' , '/'};
void read(string&);
void write(anode[],int);
bool isope(char);
bool isnum(char);
int power(int,int);
int ctoi(char[],int);
int prio(char);
void get(string,anode[],int&);
int tinhtoan(anode[],int,STACK &s);
int main(){
STACK s;
string expr; anode suff[maxx]; int n = 0;
read(expr);
get(expr,suff,n);
//write(suff,n);
cout<<endl;
cout<<"The result is:"<<tinhtoan(suff,n,s);
fflush(stdin); getchar();
return 0;
}
void read(string &expr){
cout <<"Nhap bieu thuc tinh toan: ";
cin >>expr;
}
void write(anode suff[],int n){
cout <<"Chuyen thanh bieu thuc hau to: \n";
for (int i = 0;i < n;i++)
if (suff[i].ope != '.') cout <<suff[i].ope <<' ';
else cout <<suff[i].val <<' ';
cout <<endl;
}
bool isope(char c){
int i = 0;
while ((i < 4)&&(c != ope[i])) ++i;
if (i < 4) return true;
else return false;
}
bool isnum(char c){
int i = 0;
while ((i < 10)&&(c != num[i])) ++i;
if (i < 10) return true;
else return false;
}
int power(int x,int y){
int p = 1;
for (int i = 0;i < y;i++) p *= x;
return p;
}
int ctoi(char c[],int n){
int m = 0;
for (int i = 0; i <= n;i++){
int j = 0;
while ((j < 10)&&(c[i] != num[j])) j++;
m += j*power(10,n-i);
}
return m;
}
int prio(char c){
if (c == '$') return 0;
else if ((c == '(')||(c == ')')) return 1;
else if ((c == '+')||(c == '-')) return 2;
else return 3;
}
void get(string expr,anode suff[],int& n){
char cnum[5],stack[maxx]; int m,top = 0,i = 0;
stack[top] = '$';
while (i < expr.size()){
if (isope(expr[i]))
if (prio(expr[i]) > prio(stack[top]))
stack[++top] = expr[i++];
else{
while (prio(expr[i]) <= prio(stack[top])){
suff[n].val = -1;
suff[n++].ope = stack[top--];
}
stack[++top] = expr[i++];
}
if (isnum(expr[i])){
m = -1;
while ((i < expr.size())&&(isnum(expr[i])))
cnum[++m] = expr[i++];
m = ctoi(cnum,m);
suff[n].ope = '.';
suff[n++].val = m;
}
if (expr[i] =='(') stack[++top] = expr[i++];
if (expr[i] ==')'){
while (stack[top] != '('){
suff[n].val = -1;
suff[n++].ope = stack[top--];
}
top--;
i++;
}
}
while (stack[top] != '$'){
suff[n].val = -1;
suff[n++].ope = stack[top--];
}
}
int tinhtoan(anode suff[],int n,STACK &s){
//cout <<"Chuyen thanh bieu thuc hau to: \n";
Nodeint *p;
for (int i = 0;i < n;i++){
if (suff[i].ope != '.'){
int a1,a2; int ketqua;
Pop(s,a1);
Pop(s,a2);
if(suff[i].ope == '+') ketqua = a2 + a1;
if(suff[i].ope == '-') ketqua = a2 - a1;
if(suff[i].ope == '*') ketqua = a2 * a1;
if(suff[i].ope == '/') ketqua = a2 / a1;
p = GetNode(ketqua);
Push(s,p);
}
else {
p = GetNode(suff[i].val);
Push(s,p);
}}
int ketquacuoicung;
Pop(s,ketquacuoicung);
return ketquacuoicung;
}
#include <iostream>
#include<stdio.h>
#include <string>
#define maxx 255
using namespace std;
struct Nodeint
{
int _a;
Nodeint * _next;
};
struct anode{
char ope; int val;
};
struct node{
char ope; int val;
node *l,*r;
};
struct STACK{
Nodeint * _Top;
};
void Init(STACK &s){
s._Top = NULL;
}
bool IsEmpty(STACK s){
if(s._Top == NULL) return true;
return false;
}
Nodeint *GetNode(int a){
Nodeint *p = new Nodeint;
if (p == NULL) {cout<<"khong du bo nho de cap phat"; return NULL;}
p->_a = a;
p->_next =NULL;
return p;
}
bool Push(STACK &s, Nodeint *p){
if(IsEmpty(s) == true){
s._Top = p; return true;
}
p->_next = s._Top;
s._Top = p;
return true;
}
bool Pop(STACK &s, int &a){
if(IsEmpty(s) == true){
cout<<" stack rong;"; return false;
}
Nodeint * p =s._Top;
a = p->_a;
s._Top = s._Top->_next; ///// ti phai xem lai cho nayyyyyyyyyyyyy
delete p;
return true;
}
char num[10]={'0','1','2','3','4','5','6','7','8','9'};
char ope[4]={'+' , '-' , '*' , '/'};
void read(string&);
void write(anode[],int);
bool isope(char);
bool isnum(char);
int power(int,int);
int ctoi(char[],int);
int prio(char);
void get(string,anode[],int&);
int tinhtoan(anode[],int,STACK &s);
int main(){
STACK s;
string expr; anode suff[maxx]; int n = 0;
read(expr);
get(expr,suff,n);
//write(suff,n);
cout<<endl;
cout<<"The result is:"<<tinhtoan(suff,n,s);
fflush(stdin); getchar();
return 0;
}
void read(string &expr){
cout <<"Nhap bieu thuc tinh toan: ";
cin >>expr;
}
void write(anode suff[],int n){
cout <<"Chuyen thanh bieu thuc hau to: \n";
for (int i = 0;i < n;i++)
if (suff[i].ope != '.') cout <<suff[i].ope <<' ';
else cout <<suff[i].val <<' ';
cout <<endl;
}
bool isope(char c){
int i = 0;
while ((i < 4)&&(c != ope[i])) ++i;
if (i < 4) return true;
else return false;
}
bool isnum(char c){
int i = 0;
while ((i < 10)&&(c != num[i])) ++i;
if (i < 10) return true;
else return false;
}
int power(int x,int y){
int p = 1;
for (int i = 0;i < y;i++) p *= x;
return p;
}
int ctoi(char c[],int n){
int m = 0;
for (int i = 0; i <= n;i++){
int j = 0;
while ((j < 10)&&(c[i] != num[j])) j++;
m += j*power(10,n-i);
}
return m;
}
int prio(char c){
if (c == '$') return 0;
else if ((c == '(')||(c == ')')) return 1;
else if ((c == '+')||(c == '-')) return 2;
else return 3;
}
void get(string expr,anode suff[],int& n){
char cnum[5],stack[maxx]; int m,top = 0,i = 0;
stack[top] = '$';
while (i < expr.size()){
if (isope(expr[i]))
if (prio(expr[i]) > prio(stack[top]))
stack[++top] = expr[i++];
else{
while (prio(expr[i]) <= prio(stack[top])){
suff[n].val = -1;
suff[n++].ope = stack[top--];
}
stack[++top] = expr[i++];
}
if (isnum(expr[i])){
m = -1;
while ((i < expr.size())&&(isnum(expr[i])))
cnum[++m] = expr[i++];
m = ctoi(cnum,m);
suff[n].ope = '.';
suff[n++].val = m;
}
if (expr[i] =='(') stack[++top] = expr[i++];
if (expr[i] ==')'){
while (stack[top] != '('){
suff[n].val = -1;
suff[n++].ope = stack[top--];
}
top--;
i++;
}
}
while (stack[top] != '$'){
suff[n].val = -1;
suff[n++].ope = stack[top--];
}
}
int tinhtoan(anode suff[],int n,STACK &s){
//cout <<"Chuyen thanh bieu thuc hau to: \n";
Nodeint *p;
for (int i = 0;i < n;i++){
if (suff[i].ope != '.'){
int a1,a2; int ketqua;
Pop(s,a1);
Pop(s,a2);
if(suff[i].ope == '+') ketqua = a2 + a1;
if(suff[i].ope == '-') ketqua = a2 - a1;
if(suff[i].ope == '*') ketqua = a2 * a1;
if(suff[i].ope == '/') ketqua = a2 / a1;
p = GetNode(ketqua);
Push(s,p);
}
else {
p = GetNode(suff[i].val);
Push(s,p);
}}
int ketquacuoicung;
Pop(s,ketquacuoicung);
return ketquacuoicung;
}
No comments:
Post a Comment