-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
121 lines (108 loc) · 3.61 KB
/
main.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
117
118
119
120
121
/* @autor Andrii Torulia */
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include "stack.h"
#define MAX_BUFFER_SIZE 100
struct Variables
{
int a, b;
int temp;
}var;
//Start of main function
int main(void)
{
struct StackNode* root = NULL;
char buff[MAX_BUFFER_SIZE];
double x;
int result;
while(x != 'q'){
//Read a string from user
if( scanf("%s", buff) < 1 ){
printf("syntax error\n");
return 0;
}
//Checks if user entered a number
if(isdigit(buff[0]) || isdigit(buff[1])){
sscanf( buff, "%lf", &x);
push(&root, x);
} else {
//Place statements to do operations
switch(buff[0])
{
//To handle addition
case '+':
if(size(&root) >= 2){
var.a = pop(&root);
var.b = pop(&root);
result = var.a + var.b;
push(&root, result);
} else {
printf("Not enough operands in stack for operation \n");
}
break;
//To handle subtraction
case '-':
if(size(&root) >= 2){
var.a = pop(&root);
var.b = pop(&root);
result = var.b - var.a;
push(&root, result);
} else {
printf("Not enough operands in stack for operation \n");
}
break;
//To handle multiplication
case '*':
if(size(&root) >= 2){
var.a = pop(&root);
var.b = pop(&root);
result = var.a * var.b;
push(&root, result);
} else {
printf("Not enough operands in stack for operation \n");
}
break;
//To handle division
case '/':
if(size(&root) >= 2){
var.a = pop(&root);
var.b = pop(&root);
result = var.b * var.a;
push(&root, result);
} else {
printf("Not enough operands in stack for operation \n");
}
break;
//To handle printing the top stack item
case 'p':
printf("Top element is %d\n", peek(root));
break;
//To handle popping a number off the stack
case 'P':
pop(&root);
break;
//To handle dublicate top element
case 'd':
push(&root, peek(root));
break;
// To handle full print
case 'f':
fullPrint(&root);
break;
//To handle reverse
case 'r':
reverse(&root);
break;
//To handle clear stack
case 'c':
clear(&root);
break;
//To handle quitting the program
case 'q':
return 0;
break;
}
}
}
}