Moderator: Board moderators
#include<iostream>
using namespace std;
int main(){
char t, stack[50+1] = {'@'};
int n, pri[255] = {}, top;
pri['@'] = -1, pri['+'] = pri['-'] = 1, pri['*'] = pri['/'] = 2,
pri['('] = 4, pri[')'] = 0;
scanf("%d%*c%*c", &n);
while(n--){
top = 1;
while(t = getchar(), (t != '\n' && t != EOF)){
getchar();
if(t >= '0' && t <= '9'){
putchar(t);
}else{
while(pri[stack[top-1]] >= pri[t] && stack[top-1] != '('){
putchar(stack[--top]);
}
(t != ')')?(stack[top++] = t):(top--);
}
}
while(--top){
putchar(stack[top]);
}
if(n){
printf("\n\n");
}
}
//system("pause");
return 0;
}
#include <stdio.h>
#include <stack>
using namespace std;
int main(){
stack<char> st;
int cases,count = 0,newline = 0;
char ch,ch1,str[3];
int prec[200];
prec['('] = 1 ;
prec['+'] = 2 ; prec['-'] = 2 ;
prec['*'] = 3 ;
prec['\''] = 3;
scanf("%d",&cases);
scanf("%c",&ch);
scanf("%c",&ch);
while( count ++ < cases ){
if(newline++)
printf("\n");
while(gets(str) != NULL && str[0] != '\0' ){
ch = str[0];
if( ch >= 48 && ch <= 57)
printf("%c",ch);
else if( ch == '(')
st.push(ch);
else if(ch == ')'){
while( st.top() != '(' ){
printf("%c",st.top());
st.pop();
}
st.pop();
}
else {//operator
while(!st.empty() && prec[ch] <= prec[st.top()]){
printf("%c",st.top());
st.pop();
}
st.push(ch);
}
}
while(!st.empty()){
printf("%c",st.top());
st.pop();
}
printf("\n");
}
}
// acc
void take_input()
{
char c1;
k = 0;
char str[10];
while(gets(str))
{
if(str[0]=='\0')
break;
c1=str[0];
input[k++] = c1;
}
input[k] = '\0';
}removed after accepted
Operators at the same precedence level associate from left to right. Parentheses act as grouping symbols that over-ride the operator priorities.
input:
1
(
3
*
5
/
2
*
6
/
(
3
+
2
)
*
5
)
+
4
Actual Output:
35*2/6*32+/5*4+
Your Output:
352/*632+/*5*4+
Users browsing this forum: No registered users and 0 guests