Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

BÀI 1:

#include <stdio.h>

#include <iostream>

#include <string>

using namespace std;

#define MAX 200

struct Stack

float DATA[MAX];

int TOP;

};

void init(Stack *S)

S->TOP = -1;

int isEmpty(Stack *S)

return S->TOP == -1 ? 1 : 0;

void Push(struct Stack* S, float item)

if (S->TOP == (MAX - 1))

printf("\nStack is full");
}

else

++S->TOP;

S->DATA[S->TOP] = item;

int top(Stack *S)

return (S->DATA[S->TOP]);

float Pop(struct Stack* S)

float ret = -1;

if (S->TOP == -1)

printf("\nStack is empty");

else

ret = S->DATA[S->TOP];

--S->TOP;

return ret;

int Precedence(char x)

if (x == '(')
return 0;

if (x == '+' || x == '-')

return 1 ;

if (x == '*' || x == '/' || x == '%')

return 2;

return 3;

void InfixtoPostfix(char infix[], char postfix[])

Stack S;

char x, token;

int i = 0, j = 0; // i - index of infix, j - index of postfix

init(&S);

for (i = 0; infix[i] != '\0'; i++)

token = infix[i];

if (isalnum(token))

postfix[j++] = token;

else

if (token == '(')

Push(&S, '(');

else

if (token == ')')

while ((x = Pop(&S)) != '(')

postfix[j++] = x;

else

{
while (Precedence(token) <= Precedence(top(&S)) && !
isEmpty(&S))

x = Pop(&S);

postfix[j++] = x;

Push(&S, token);

while (!isEmpty(&S))

x = Pop(&S);

postfix[j++] = x;

postfix[j] = '\0';

float Evaluate(char *Postfix)

struct Stack S;

char *p;

float op1, op2, result;

S.TOP = -1;

p = &Postfix[0];

while (*p != '\0')

{
while (*p == ' ' || *p == '\t')

p++;

if (isdigit(*p))

int num = 0;

while (isdigit(*p))

num = num * 10 + *p - 48;

*p++;

Push(&S, num);

else

op1 = Pop(&S);

op2 = Pop(&S);

switch (*p)

case '+':

result = op2 + op1;

break;

case '-':

result = op2 - op1;

break;

case '/':

result = op2 / op1;

break;
case '*':

result = op2 * op1;

break;

default:

printf("\nInvalid Operator");

return 0;

Push(&S, result);

p++;

result = Pop(&S);

return result;

int main()

char A[MAX], B[MAX];

printf("Infix : ");

gets(A);

InfixtoPostfix(A, B);

printf("Postfix: %s\n", B);

printf("Equals is %f\n", Evaluate(&B[0]));

gets(A);

return 0;

BÀI 2:
#include <iostream>

#include <string>

#include <fstream>

using namespace std;

class node {

public:

int data;

node *link;

node();

node(int d);

};

node::node() {

data = -1;

link = NULL;

};

node::node(int d) {

data = d;

link = NULL;

};

class stack {

private:

node *pHead;

int count;

public:

stack();
void push(int d);

node *pop();

int top();

bool isEmpty();

bool isFull();

int size();

~stack();

};

stack::stack() {

pHead = NULL;

count = 0;

};

void stack::push(int d) {

if (pHead == NULL)

pHead = new node(d);

if (pHead != NULL)

++count;

else
{

node *pTemp = pHead;

pHead = new node(d);

if (pHead != NULL)

pHead->link = pTemp;

++count;

};

node* stack::pop() {

if (pHead != NULL)

node *pTemp = pHead;

pHead = pHead->link;

pTemp->link = NULL;

--count;

return pTemp;

return NULL;

};

int stack::top() {

if (pHead != NULL)

return pHead->data;

return -1;

};
bool stack::isEmpty() {

if (pHead == NULL)

return true;

return false;

};

bool stack::isFull() {

node *pTemp = new node();

if (pTemp == NULL)

delete pTemp;

return true;

return false;

};

stack::~stack() {

node *pTemp;

while (pHead != NULL)

pTemp = pHead;

pHead = pHead->link;

delete pTemp;

};

int stack::size() {

return count;

};
bool isExpressionBalanced(string input) {

stack *s = new stack;

char x;

for (int i=0; i<input.length(); i++)

if (input[i]=='('||input[i]=='['||input[i]=='{')

s->push(input[i]);

continue;

if (s->isEmpty())

return false;

switch (input[i])

case ')':

x = s->top();

s->pop();

if (x=='{' || x=='[')

return false;

break;

case '}':
x = s->top();

s->pop();

if (x=='(' || x=='[')

return false;

break;

case ']':

x = s->top();

s->pop();

if (x =='(' || x == '{')

return false;

break;

if(s->isEmpty()==true) return true;

else return false;

int main()

return 0;

created

Oct '19
last reply

Oct '19

reply

2.3k

views

users

likes

Peppermint

KuroShiro

Oct '19

Mình s?a l?i hàm isExpressionBalanced c?a b?n nhu sau:

bool isExpressionBalanced(string input) {

stack *s = new stack;

for (int i=0; i<input.length(); i++)

if ((input[i] == ')' || input[i] == ']' || input[i] == '}') && s->isEmpty()) return false;

else if ( input[i] == '(' || input[i] == '[' || input[i] == '{') s->push(input[i]);

else if ((input[i] == ')' && s->top() == '(')||

(input[i] == ']' && s->top() == '[')||

(input[i] == '}' && s->top() == '{') ) s->pop();

else return false;

return s->isEmpty();
}

You might also like