-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab3.c
169 lines (152 loc) · 4.13 KB
/
lab3.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include<math.h>
struct Stack {
int top;
unsigned capacity;
double* array;
};
//Function creates a new stack
struct Stack* createStack(unsigned capacity) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = -1;
stack->capacity = capacity;
stack->array = (double*)malloc(stack->capacity * sizeof(double));
return stack;
}
//Function to check if the stack is empty
bool isEmpty(struct Stack* stack) {
return stack->top == -1;
}
//Function checks stack is full
bool isFull(struct Stack* stack) {
return stack->top == stack->capacity - 1;
}
//Push an element
void push(struct Stack* stack, double item) {
if (isFull(stack))
return;
stack->array[++stack->top] = item;
}
// Pop an element
double pop(struct Stack* stack) {
if (isEmpty(stack))
return -1;
return stack->array[stack->top--];
}
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int precedence(char x)
{
if (x == '+' || x == '-')
return 1 ;
if (x == '*' || x == '/' )
return 2;
}
char* infixtoPostfix(char*exp){
struct Stack* stack = createStack(strlen(exp));
char* new_exp = (char*)malloc((strlen(exp)+1)*sizeof(char));
int i,j;
j=0;
for(i=0;i<strlen(exp);i++){
while(isdigit(exp[i])){
new_exp[j++]= exp[i];
i++;
}
if(isOperator(exp[i])){
while(!isEmpty(stack) && precedence(exp[i])<=precedence(stack->array[stack->top])){
new_exp[j++]= pop(stack);
}
push(stack,exp[i]);
if(isdigit(new_exp[j-1])){
new_exp[j++]=' ';
}
}
}
while(!isEmpty(stack)){
new_exp[j++] = pop(stack);
}
new_exp[j]='\0';
return new_exp;
}
double evaluateExp(char* exp) {
char* new_exp = infixtoPostfix(exp);
struct Stack* stack = createStack(strlen(new_exp));
int i ;
for(i=0;i<strlen(new_exp);i++){
if (isdigit(new_exp[i])) {
int num = 0;
while (isdigit(new_exp[i])) {
num = num * 10 + (new_exp[i] - '0');
i++;
}
push(stack, num);
i--;
} else if (isOperator(new_exp[i])) {
double val1 = pop(stack);
double val2 = pop(stack);
switch (new_exp[i]) {
case '+': push(stack, val2 + val1); break;
case '-': push(stack, val2 - val1); break;
case '*': push(stack, val2 * val1); break;
case '/': push(stack, val2 / val1); break;
}
}
}
return pop(stack);
}
char* find_expression(char* A, int B, char* current_exp, double current_result, int current_index){
if ((current_index +1)== strlen(A)){
//printf("%c\n",A[current_index-2]);
if(current_result == B)
return strdup(current_exp);
else
return NULL;}
double new_result;
int i;
char* result = NULL;
char operators[] = {'+','-','*','/'};
char new_exp[100];
for(i=0;i<5;i++){
if(i<4){
sprintf(new_exp, "%s%c%c", current_exp, operators[i], A[current_index]);
}else{
sprintf(new_exp, "%s%c", current_exp, A[current_index]);
}
new_result = evaluateExp(new_exp);
result = find_expression(A,B,new_exp,new_result,current_index+1);
if (result){
return result;
}
}
return NULL;
}
int main(){
FILE* input_file = fopen("input.txt", "r");
FILE* output_file = fopen("output.txt", "w");
if (input_file == NULL || output_file == NULL) {
printf("Error opening file");
return 1;
}
char A[100];
char D[100];
int B;
fgets(A,100,input_file);
fscanf(input_file, "%d", &B);
fclose(input_file);
strcpy(D,A);
D[1]='\0';
char* result = find_expression(A, B, D, (int)(D - '0'), 1);
char exp[100]= "1+2+3+45+6+78+9";
printf("%s\n", result);
if (result != NULL) {
fputs(result, output_file);
} else {
fputs("0\n", output_file);
}
fclose(output_file);
return 0;
}