Skip to content

Commit 0f7c420

Browse files
authored
Add files via upload
1 parent 60f95da commit 0f7c420

File tree

5 files changed

+441
-0
lines changed

5 files changed

+441
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
5+
int operation(int val1,int val2,char ch)
6+
{
7+
if(ch=='+')
8+
{
9+
return val1+val2;
10+
}
11+
else if(ch=='-')
12+
{
13+
return val1-val2;
14+
}
15+
else if(ch=='*')
16+
{
17+
return val1*val2;
18+
}
19+
else
20+
{
21+
return val1/val2;
22+
}
23+
}
24+
25+
void prefixConversion(string s)
26+
{
27+
stack<int> val;
28+
stack<string> in;
29+
stack<string> post;
30+
31+
for(int i=s.size()-1;i>=0;i--)
32+
{
33+
char ch=s[i];
34+
if(ch=='+'|| ch=='-' ||ch=='*'|| ch=='/')
35+
{
36+
int val1=val.top();
37+
val.pop();
38+
int val2=val.top();
39+
val.pop();
40+
int ans=operation(val1,val2,ch);
41+
val.push(ans);
42+
43+
string in1=in.top();
44+
in.pop();
45+
string in2=in.top();
46+
in.pop();
47+
string inAns='('+in1+ch+in2+')';
48+
in.push(inAns);
49+
50+
string post1=post.top();
51+
post.pop();
52+
string post2=post.top();
53+
post.pop();
54+
string postAns=post1+post2+ch;
55+
post.push(postAns);
56+
}
57+
else if(ch==' ')
58+
{
59+
continue;
60+
}
61+
else
62+
{
63+
val.push(ch-'0');
64+
in.push(string(1,ch));
65+
post.push(string(1,ch));
66+
}
67+
}
68+
69+
cout<<val.top()<<endl;
70+
cout<<in.top()<<endl;
71+
cout<<post.top()<<endl;
72+
}
73+
int main()
74+
{
75+
string s;
76+
getline(cin,s);
77+
prefixConversion(s);
78+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
5+
int celeb(int n,int **ar)
6+
{
7+
stack<int>s;
8+
for(int i=0;i<n;i++)
9+
{
10+
s.push(i);
11+
}
12+
13+
while(s.size()>=2)
14+
{
15+
int i=s.top();
16+
s.pop();
17+
int j=s.top();
18+
s.pop();
19+
if(ar[i][j]==1) //i knows j --> i can not be celeb.
20+
{
21+
s.push(j);
22+
}
23+
else
24+
{ //i does not know j --> j can not be celeb.
25+
s.push(i);
26+
}
27+
}
28+
29+
int pot=s.top();
30+
s.pop();
31+
32+
for(int i=0;i<n;i++)
33+
{
34+
if(pot!=i)
35+
{
36+
if(ar[i][pot]==0 || ar[pot][i]==1 )
37+
{
38+
return 0;
39+
}
40+
}
41+
}
42+
return pot;
43+
}
44+
int main()
45+
{
46+
int n;
47+
cin>>n;
48+
int **ar=new int*[n];
49+
for(int i=0;i<n;i++)
50+
{
51+
ar[i]=new int[n];
52+
for(int j=0;j<n;j++)
53+
{
54+
cin>>ar[i][j];
55+
}
56+
}
57+
int ans=celeb(n,ar);
58+
if(ans==0)
59+
{
60+
cout<<"none"<<endl;
61+
}
62+
else
63+
cout<<ans<<endl;
64+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
5+
int precedence(char oprtr)
6+
{
7+
if(oprtr=='+')
8+
{
9+
return 1;
10+
}
11+
else if(oprtr=='-')
12+
{
13+
return 1;
14+
}
15+
else if(oprtr=='*')
16+
{
17+
return 2;
18+
}
19+
else
20+
{
21+
return 2;
22+
}
23+
}
24+
25+
int operation(int v1,int v2,char oprtr)
26+
{
27+
if(oprtr=='+')
28+
{
29+
return v1+v2;
30+
}
31+
else if(oprtr=='-')
32+
{
33+
return v1-v2;
34+
}
35+
else if(oprtr=='*')
36+
{
37+
return v1*v2;
38+
}
39+
else
40+
{
41+
return v1/v2;
42+
}
43+
}
44+
45+
int infixEval(string s)
46+
{
47+
stack<char> op;
48+
stack<int> opand;
49+
for(int i=0;i<s.size();i++)
50+
{
51+
char ch=s[i];
52+
if(ch=='(')
53+
{
54+
op.push(ch);
55+
}
56+
else if(isdigit(ch))
57+
{
58+
opand.push(ch-'0');
59+
}
60+
else if(ch==')')
61+
{
62+
while(op.top()!='(')
63+
{
64+
char oprtr=op.top();
65+
op.pop();
66+
int val2=opand.top();
67+
opand.pop();
68+
int val1=opand.top();
69+
opand.pop();
70+
int ans=operation(val1,val2,oprtr);
71+
opand.push(ans);
72+
}
73+
op.pop();
74+
}
75+
else if(ch=='+'|| ch=='-' || ch=='*' || ch=='/')
76+
{
77+
while(op.size()>0 && op.top()!='(' && precedence(ch)<=precedence(op.top()))
78+
{
79+
char oprtr=op.top();
80+
op.pop();
81+
int val2=opand.top();
82+
opand.pop();
83+
int val1=opand.top();
84+
opand.pop();
85+
int ans=operation(val1,val2,oprtr);
86+
opand.push(ans);
87+
}
88+
op.push(ch);
89+
}
90+
else
91+
{
92+
continue;
93+
}
94+
}
95+
while(op.size()!=0)
96+
{
97+
char oprtr=op.top();
98+
op.pop();
99+
int val2=opand.top();
100+
opand.pop();
101+
int val1=opand.top();
102+
opand.pop();
103+
int ans=operation(val1,val2,oprtr);
104+
opand.push(ans);
105+
}
106+
return opand.top();
107+
}
108+
109+
int main()
110+
{
111+
string s;
112+
getline(cin,s);
113+
int ans=infixEval(s);
114+
cout<<ans<<endl;
115+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
5+
int precedence(char ch)
6+
{
7+
if(ch=='+')
8+
{
9+
return 1;
10+
}
11+
else if(ch=='-')
12+
{
13+
return 1;
14+
}
15+
else if(ch=='*')
16+
{
17+
return 2;
18+
}
19+
else
20+
{
21+
return 2;
22+
}
23+
}
24+
void infixConversion(string s)
25+
{
26+
stack<char>oprtr;
27+
stack<string>pre;
28+
stack<string>post;
29+
for(int i=0;i<s.size();i++)
30+
{
31+
char ch=s[i];
32+
if(ch=='(')
33+
{
34+
oprtr.push(ch);
35+
}
36+
else if((ch>='1' && ch<='9') || (ch>='a' && ch<='z' ) || (ch>='A' && ch<='Z'))
37+
{
38+
pre.push(string(1,ch));
39+
post.push(string(1,ch));
40+
}
41+
else if(ch==')')
42+
{
43+
while(oprtr.top()!='(')
44+
{
45+
char op=oprtr.top();
46+
oprtr.pop();
47+
string val2=pre.top();
48+
pre.pop();
49+
string val1=pre.top();
50+
pre.pop();
51+
string val2Post=post.top();
52+
post.pop();
53+
string val1Post=post.top();
54+
post.pop();
55+
56+
string prefix=op+val1+val2;
57+
string postfix=val1Post+val2Post+op;
58+
pre.push(prefix);
59+
post.push(postfix);
60+
}
61+
oprtr.pop();
62+
}
63+
else
64+
{
65+
if(oprtr.size()>0 && oprtr.top()!='(' && precedence(ch)<=precedence(oprtr.top()))
66+
{
67+
char op=oprtr.top();
68+
oprtr.pop();
69+
string val2=pre.top();
70+
pre.pop();
71+
string val1=pre.top();
72+
pre.pop();
73+
string val2Post=post.top();
74+
post.pop();
75+
string val1Post=post.top();
76+
post.pop();
77+
78+
string prefix=op+val1+val2;
79+
string postfix=val1Post+val2Post+op;
80+
pre.push(prefix);
81+
post.push(postfix);
82+
}
83+
oprtr.push(ch);
84+
}
85+
}
86+
while(oprtr.size()!=0)
87+
{
88+
char op=oprtr.top();
89+
oprtr.pop();
90+
string val2=pre.top();
91+
pre.pop();
92+
string val1=pre.top();
93+
pre.pop();
94+
string val2Post=post.top();
95+
post.pop();
96+
string val1Post=post.top();
97+
post.pop();
98+
99+
string prefix=op+val1+val2;
100+
string postfix=val1Post+val2Post+op;
101+
pre.push(prefix);
102+
post.push(postfix);
103+
}
104+
cout<<pre.top()<<endl;
105+
cout<<post.top()<<endl;
106+
}
107+
int main()
108+
{
109+
string s;
110+
getline(cin,s);
111+
infixConversion(s);
112+
}

0 commit comments

Comments
 (0)