-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.c
More file actions
143 lines (125 loc) · 2.12 KB
/
executor.c
File metadata and controls
143 lines (125 loc) · 2.12 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
//Maciej Andrearczyk, ma333856
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdbool.h>
#include "err.h"
#define MAX_LINE_SIZE 1000000
/*
* String z linjką wejściową.
*/
char in[MAX_LINE_SIZE];
/*
* String z linijką wyjściową.
*/
char res[MAX_LINE_SIZE];
/*
* Pomocnicza zmienna trzymająca potęgi 10.
*/
int p10;
/*
* Sprawdza, czy znak jest operatorem.
*/
bool is_operand(int ind)
{
return (in[ind] == '+' ||
(in[ind] == '-' && (in[ind+1] == ' ' || in[ind+1] == '\n'))||
in[ind] == '*' ||
in[ind] == '/');
}
/*
* Dodaje cyfrę do liczby.
*/
int add_digit(int j, int x)
{
if (in[j] == '-')
x *= -1;
else
{
x += p10 * (in[j] -'0');
p10 *= 10;
}
return x;
}
/*
* Wykonuje odpowiednią operację wskazaną w in[j]
* na a i b.
*/
int do_operation(int a, int b, int j)
{
if (in[j] == '+')
return a += b;
if (in[j] == '-')
return a -= b;
if (in[j] == '*')
return a *= b;
return a /= b; //in[i] == '/'
}
void clear_res(int n)
{
for (int i = 0; i < n; i++)
res[i] = '\0';
}
/*
* Robi jeden krok obliczeń i zapisuje wynik w tabeli res.
*/
void one_step()
{
int a = 0, b = 0;
for (int i = 0; in[i] != '\0'; i++)
{
if (is_operand(i))
{
int j;
p10 = 1;
for (j = i-2; in[j] != ' '; j--)
b = add_digit(j, b);
p10 = 1;
for (j--; in[j] != ' '; j--)
a = add_digit(j, a);
j--;
int k;
for (k = 0; k <= j; k++)
res[k] = in[k];
res[k] = '\0';
k++;
sprintf(res, "%s %d", res, do_operation(a, b, i));
while (res[k] != '\0') k++;
i++;
for (; in[i] != '\0'; i++, k++)
res[k] = in[i];
res[k] = '\n';
res[k+1] = '\0';
return;
}
}
strcpy(res, in);
}
/*
* Sprawdzam, czy nie powinienem się zakończyć. (# na wejściu)
*/
int check_if_finished()
{
return (in[0] == '#') ? 1 : 0;
}
int main(int argc, char *argv[])
{
setvbuf(stdout, NULL, _IONBF, 0);
while(1)
{
fgets(in, MAX_LINE_SIZE, stdin);
if (check_if_finished() == 1)
{
printf("#\n");
break;
}
one_step();
printf("%s", res);
}
wait(0);
exit(0);
}