is this code optimize to caculate operation from string
`
#define MAX 300
char ds[MAX];
int n;
int demngoac = 0;
struct Stack {
int top;
char st[MAX];
void init() {
top = -1;
}
bool isEmpty() {
if (top == -1) return true;
return false;
}
bool isFull() {
if (top == MAX - 1) return true;
return false;
}
char peek() {
if (isEmpty()) { cout << "rong"; return 'x'; }
return st[top];
}
void pop() {
if (isEmpty()) { cout << "rong"; return; }
top--;
}
void push(char data) {
if (isFull()) { cout << "day"; return; }
top++;
st[top] = data;
}
};
struct IntStack {
int top;
int st[MAX];
void init() {
top = -1;
}
bool isEmpty() {
if (top == -1) return true;
return false;
}
bool isFull() {
if (top == MAX - 1) return true;
return false;
}
int peek() {
if (isEmpty()) { cout << "rong"; return 'x'; }
return st[top];
}
void pop() {
if (isEmpty()) { cout << "rong"; return; }
top--;
}
void push(int data) {
if (isFull()) { cout << "day"; return; }
top++;
st[top] = data;
}
};
Stack stk;
Stack output;//hauto
IntStack temp;
char check(char a) {
if (a >= '0' && a <= '9') return 'h';
if (a == '+' || a == '-' || a == '*' || a == '/') return 't';
return 'x';
}
int chiacap(char a) {
if (a == '+' || a == '-') return 1;
if (a == '*' || a == '/' || a == '%') return 2;
if (a == '^') return 3;
return -1;
}
void trungtohauto() {
for (int i = 0; i < n; i++) {
if (check(ds[i]) == 'h') output.push(ds[i]);// nếu là toán hạng đẩy vào output
if (check(ds[i]) == 't') {// nếu là toán tử thì nếu cấp của ds[i] >= cấp stk.peek() thì đẩy vào stk, không thì đẩy hết toán tử từ stk sang ouput
while (!(stk.isEmpty()) && chiacap(ds[i]) < chiacap(stk.peek()) && chiacap(stk.peek()) != -1) {
output.push(stk.peek());
stk.pop();
}
stk.push(ds[i]);
}
if (ds[i] == '(') { //nếu là ( thì đẩy vào stk
demngoac++;
stk.push(ds[i]);
}
if (ds[i] == ')') {// nếu gặp ) thì đẩy hết từ stk sang output đến khi gặp (
demngoac++;
while (!(stk.isEmpty()) && stk.peek() != '(') {
output.push(stk.peek());
stk.pop();
}
if (!stk.isEmpty() && stk.peek() == '(') stk.pop();
}
}
//đẩy các phần tử còn lại
while (!stk.isEmpty()) {
output.push(stk.peek());
stk.pop();
}
}
int congtrunhanchia(int a, int b, char tt) {
int ai = a;
int bi = b;
int kq = 0;
if (tt == '*') kq = ai * bi;
if (tt == '+') kq = ai + bi;
if (tt == '-') kq = bi - ai;
if (tt == '/') kq = bi / ai;
return kq;
}
int tinhtoan() {
int rt = 0;
for (int i = 0; i < n - demngoac; i++) {
if (check(output.st[i]) == 'h') {
temp.push(output.st[i] - '0');
}
if (check(output.st[i]) == 't') {
int a = temp.peek();
temp.pop();
int b = temp.peek();
temp.pop();
int c = congtrunhanchia(a, b, output.st[i]);
temp.push(c);
}
}
rt = temp.st[0];
return rt;
}
int main() {
freopen("input.txt", "r", stdin);
int T = 1;
int tt = 1;
while (T--) {
stk.init();
output.init();
temp.init();
cin >> n;
char hehe[10];
cin.getline(hehe, 10);
for (int i = 0; i < n; i++) {
cin.get(ds[i]);
}
//for (int i = 0; i < n; i++) {
// cout << ds[i] << " ";
//}
//cout << endl;
trungtohauto();
//for (int i = 0; i < n - demngoac; i++) {
// cout << output.st[i] << " ";
//}
cout << "#" << tt << " " << tinhtoan() << endl;
tt++;
}//endwhile
return 0;
}
`
i want to solve problem for number >=10
Manh Dung Nguyen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.