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

//BÀI 3

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main(){
string s;
cin >> s;

stack <char> st;


int len = s.size();
for (int i = 0; i < len; i++){
if (s[i] >= 'a' && s[i] <= 'z')
cout << s[i];
else if (s[i] == ')'){
cout << st.top();
st.pop();
}
else if (s[i] != '(')
st.push(s[i]);
}

return 0;
}
//Baif 4;
#include <iostream>
#include <stack>
#include <sstream>
#include <fstream>
#include <string>

using namespace std;

struct Stack
{
int top;
char list[100];
};
void init(Stack &s)
{
s.top = 0;
}
bool Full(Stack s)
{
if (s.top == 1)
return 1;
return 0;
}
bool empty(Stack s)
{
if (s.top == -1)
return 1;
return 0;
}
void push(Stack& s, int x)

{
if (!Full(s))
s.list[++s.top] = x;
}
char top(Stack s)

{
return s.list[s.top];
}
void pop(Stack & s)
{
if (!empty(s))
--s.top;
}
int chuyenChuoiSo(string n)
{
stringstream ss;
ss << n;
int va;
ss >> va;
return va;
}
int str(string s)
{
stringstream ss;
ss << s;
int v;
ss >> v;
return v;
}
bool kt(char c)
{
if (c == '+' || c == '-' || c == '*') return true;
return false;
}
int tinhHauTo(string s)
{
stack<int> st;
for (int i = 0; i < s.size(); i++)
{
if (isdigit(s[i]))
{
string tmp = "";
while (s[i] != ' ' && !kt(s[i]))
{
tmp += s[i++];
}
st.push(chuyenChuoiSo(tmp));
}
else
if (kt(s[i]))
{
switch (s[i])
{
case '+':
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
st.push(b + a);
break;
}
case '-':
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
st.push(b - a);
break;
}
case '*':
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
st.push(b * a);
break;
}
}
}
}
return st.top();
}

int main()
{
string t;
getline(cin, t);
cout << tinhHauTo(t);
return 0;
}

You might also like