-
Notifications
You must be signed in to change notification settings - Fork 0
/
reversepolish.c
executable file
·117 lines (86 loc) · 1.58 KB
/
reversepolish.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
int a;
struct node *next;
};
struct node *top;
struct node *trav;
struct node *temp;
struct node *flag;
void push(int var){
if (top == NULL){
top = (struct node *)(malloc(sizeof(struct node)));
top->a=var;
top->next=NULL;
printf("the pushed element is %d \n ",top->a );
return;
}
temp=(struct node *)(malloc(sizeof(struct node)));
temp->a=var;
temp->next=top;
printf("the pushed element is %d \n", temp->a);
top=temp;
}
int pop(){
if (top==NULL){
printf("no node is there so we cammot pop \n");
}
trav=top;
printf("the poped element is %d \n" , trav->a);
top=top->next;
free(trav);
return trav->a;
}
void print(){
flag = top;
while(flag != NULL){
printf("%d %p",flag->a,flag);
printf(" ");
flag=flag->next;
}
printf("\n");
}
int main(void){
int i = 0;
char b[100];
int var1,var2;
char a;
printf("enter the question \n");
a = getchar();
while(a != '\n'){
b[i] = a;
i++;
a = getchar();
}
b[i] = '\0';
printf("you have entered : %s \n" , b);
printf("the length of string you entered is :%d \n",strlen(b));
printf("your answer is the last pushed element \n");
for(i=0;i<strlen(b);i++){
if(b[i]== '*'){
var1 = pop();
var2 = pop();
push(var2*var1);
}
if(b[i]== '+'){
var1 = pop();
var2 = pop();
push(var2+var1);
}
if(b[i]== '-'){
var1 = pop();
var2 = pop();
push(var2-var1);
}
if(b[i]== '/'){
var1 = pop();
var2 = pop();
push(var2/var1);
}
if(b[i]>49 && b[i]<58){
push(b[i]-48);
}
}
}