-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
executable file
·155 lines (123 loc) · 2.91 KB
/
main.c
File metadata and controls
executable file
·155 lines (123 loc) · 2.91 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include <stdlib.h>
#include "Operations.h"
#include "LinkedList.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "./infix.h"
int isnumeric(char s){
if (s - '0' >= 0 && s - '0' <= 9){
return 1;
}
return 0;
}
Number **L;
int main(){
printf("bc 1.0\n");
printf("Copyright 2023 COEP's Free Software Foundation ");
printf("\nThis is free software with ABSOLUTELY NO WARRANTY.\n");
while(1){
List operators = (List) malloc(sizeof(Node));
operators->data = INT_MIN;
char* input;
size_t size;
int len;
int i;
int c;
int index;
List temp;
input = NULL;
size = 0;
getline(&input, &size, stdin);
if (!strcmp(input, "quit\n") )
exit(0);
len = strlen(input);
i = 0;
c = 1;
RemoveSpaces(input);
while(input[i] != '\0'){
if (!isnumeric(input[i])){
c++;
}
i++;
}
char infix[c*3];
i = 0;
L = (Number **) malloc(sizeof(Number *)*(c*4));
index = 0;
initNumber(&L[0]);
temp = L[0]->numbers;
if((input[0] == '+') || (input[0] == '-')){
L[0]->sign = input[0];
i++;
} else{
L[0]->sign = '+';
}
while(input[i]=='0'){i++;
if(!isnumeric(input[i])){
i--;
break;
}
}
c = 0;
while(input[i] != '\n'){
if(isnumeric(input[i])){
pushf(&L[index]->numbers, input[i] - '0');
L[index]->count ++;
} else if (input[i] == '('){
push(&operators, input[i]);
if (input[i+1] == '+' || input[i+1] == '-' ){
if(L[index]->sign == input[i+1])
L[index]->sign = '+';
else
L[index]->sign = '-';
i++;
}
while(input[i+1]=='0'){i++;}
} else if (input[i] == ')'){
push(&operators, '0'+c++);
while(input[i] == ')'){
push(&operators, input[i]);
i++;
}
i--;
} else{
if (input[i-1] != ')'){
push(&operators, '0'+c);
c++;
}
push(&operators, input[i]);
index++;
initNumber(&L[index]);
temp = L[index]->numbers;
if((input[i+1] == '+') || (input[i+1] == '-') ){
L[index]->sign = input[i+1];
i++;
}
while(input[i+1]=='0'){i++;}
if(!isnumeric(input[i+1]) && input[i+1] != '(')
i--;
}
i++;
}
if(input[i-1] != ')'){
push(&operators, '0'+c);
}
i = 0;
while(operators->next != NULL){
infix[i] = operators->data;
operators = operators->next;
i++;
}
infix[i] = operators->data;
infix[++i] = '\n';
char postfix[250];
infix_to_postfix(infix, postfix);
int rest = eval_postfix(postfix, L);
if( (L[rest]->sign != '+' ) && !(L[rest]->numbers->data == 0 && L[rest]->count == 1)){
printf("%c", L[rest]->sign);
}
displayList(&L[rest]->numbers);
}
}