-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex4B.c
More file actions
246 lines (205 loc) · 7.55 KB
/
ex4B.c
File metadata and controls
246 lines (205 loc) · 7.55 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <stdio.h>
#include "string.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>
#define MAXIMUM 512
typedef struct {// struct
char *str;// input
int Ans;// answer
} polynomAns;
static int val = 0;// starting val as zero
void basicMethod();
int doPow(int a, int b);
int numOfXs(const char str[]);
int numOfPlus(const char str[]);
void print_data(polynomAns *Pol);
char *getVal(char str[], char getVal[]);
char *getExponent(char str[], char getPower[]);
char *getCoefficient(const char str[], char coefficient[]);
int main() {
basicMethod();
return 0;
}
void basicMethod() {
char str[MAXIMUM]; // array to save user input
char copyStr[MAXIMUM]; // array to copy user input to it
char *ptr; // pointer to pass it to strol
char *split; // using it with strtok
int i, k, status, Ans, plus; // variables used in the program
pid_t pid;// to use fork
while (1) {
Ans = 0;
printf("Enter a Polynom and a Val in this order (POLYNOM, Val) or done to exit\n");
fgets(str, MAXIMUM, stdin); // get user input
if ((strlen(str) > 0) && (str[strlen(str) - 1] == '\n')) { // if the last char is \n we change it to \0
str[strlen(str) - 1] = '\0';
}
if (strlen(str) == 0) { // if the user entered nothing
continue;
}
if (strcmp(str, "done") == 0) { // if the user entered done the program exits
exit(0);
}
if (str[strlen(str) - 1] == '0' && str[strlen(str) - 2] == ' ') {// a special case , if the last char is 0 and the char before it is a space
printf("Answer is: %d\n", 0);
continue;
}
for (i = 0; i < strlen(str); i++) { // copying the user input until the comma
if (str[i] == ',' || str[i] == ' ') {
copyStr[i] = '\0';
break;
}
copyStr[i] = str[i];
}
k = numOfXs(str); // number of letter x in the string
plus = numOfPlus(str); // number of pluses in the string
val = (int) strtol(getVal(str, ""), &ptr, 10); // value using strtol
if (val == 0 || str[strlen(str) - 1] < '0' || str[strlen(str) - 1] > '9') {// if the value is zero(not a num) or the value is written without a space before it
printf("Value is not calculable, enter a valid number or add a space before Val\n");
continue;
}
polynomAns *pol;
key_t key = ftok("/tmp", 'm'); // creating shared memory
int shmID;
if ((shmID = shmget(key, 1024, IPC_CREAT | 0600)) == -1)
perror("shmget failed");
if ((pol = (polynomAns *) shmat(shmID, 0, 0)) < 0)
perror("shmat failed");
if (plus != 0) {// if we have pluses in the polynom
for (i = 0; i < strlen(str); i++) {// change the comma to \0 in order to stop splitting there
if (str[i] == ',')
str[i] = '\0';
}
split = strtok(str, "+");// using strtok to split the polynom
i = 0;
while (i < k && split != NULL) {
pol[i].str = (char *) malloc(sizeof(char) * (strlen(str) / k)); //allocating memory for each split
strcpy(pol[i].str, split);// copying the split string into the struct
i++;
split = strtok(NULL, "+");// in order to check if we have more splits
}
} else { // if we don't have pluses
split = strtok(str, ","); // we do the same as above but with (,)
i = 0;
while (i < k && split != NULL) {
pol[i].str = (char *) malloc(sizeof(char) * (strlen(str) / k));
strcpy(pol[i].str, split);
i++;
split = strtok(NULL, ",");
}
}
i = 0;
while (i < k) {
pid = fork(); // creating fork k times
if (pid < 0) { // child was not entered
perror("child was not entered\n");
exit(1);
} else if (pid == 0) { // if pid is zero
print_data(&pol[i]); // calling print_data on pol[i] so each child does one calculation
exit(0);
} else
wait(&status);// father waits
i++;
}
i = 0;
while (i < k) {
Ans += pol[i].Ans;// add each result to the Ans
i++;
}
if(k==plus){ // for the last number which is not multiplied by x
for(i=(int)strlen(copyStr)-1;i>=0;i--){
if (copyStr[i] > '9'||copyStr[i]<'0') {
printf("not a valid number\n");
break;
}
if(copyStr[i-1]=='+'||i==0){// when we reach the plus mark, strtol returns the number after the plus mark
Ans +=(int) strtol(&(copyStr[i]), &ptr, 10);
printf("Answer is: %d\n", Ans);// print the answer
break;
}}}
if(k!=plus) {
printf("Answer is: %d\n", Ans);// print the answer
}
shmdt(pol); // delete shared memory
shmctl(shmID, IPC_RMID, NULL);
}
}
char *getVal(char str[], char getVal[]) { //here we get the value that's after the comma
getVal = strchr(str, ',');
return getVal + 2;
}
char *getExponent(char str[], char getPower[]) { // get the power
char *power = strchr(str, '^');
while (NULL != power) {
int j = 0;
for (int i = 1; i < strlen(power); i++) { // if the power is a number, then copy it to getPower
if (power[i] >= '0' && power[i] <= '9') {
getPower[j] = power[i];
j++;
}
}
getPower[j] = '\0';
return getPower; // return the power
}
return "1";
}
int numOfXs(const char str[]) { // count the number of x in the polynom
int ThreadCount = 0;
int i = 0;
while (str[i] != '\0') {
if (str[i] == 'x') {
ThreadCount++;
}
i++;
}
return ThreadCount;
}
int numOfPlus(const char str[]) { // count the number of plus in the polynom
int ThreadCount = 0;
int i = 0;
while (str[i] != '\0') {
if (str[i] == '+') {
ThreadCount++;
}
i++;
}
return ThreadCount;
}
char *getCoefficient(const char str[], char coefficient[]) { // get the factor of x
int i = 0, j = 0;
if (str[0] == 'x' || strlen(str) == 0)// if there is no coefficient it mans we have 1
return "1";
while (str[i] != '\0') { // if we have an x and * then we change x and * to \0, and we return the coefficient
if (str[i + 1] == 'x' && str[i] == '*') {
coefficient[j] = '\0';
coefficient[j + 1] = '\0';
return coefficient;
}
if (str[i] >= '0' && str[i] <= '9') {// if the coefficient is a number we copy it to coefficient array
coefficient[j] = str[i];
j++;
i++;
}
}
coefficient[i] = '\0';
return coefficient;
}
int doPow(int a, int b) { // do val^ coefficient
int x = 1;
while (b != 0) {
x *= a;
b--;
}
return x;
}
void print_data(polynomAns *Pol) {
char getStr[MAXIMUM];
char *ptr;
int coefficient = (int) strtol(getCoefficient(Pol->str, getStr), &ptr, 10);// coefficient using strtol
int exponent = (int) strtol(getExponent(Pol->str, getStr), &ptr, 10);// exponent using strtol
Pol->Ans = (int) (coefficient * doPow(val, exponent)); // calculating the answer
}