Test

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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
   Project #6  CSCE 206


*/

/*
   CSCE 206 - Project 6
   Arithmetic operations on fractions

   To use this program you need to provide input
   on the command line.

   Example:
   project6 mul 2/3 3/4

   Output will be in this form:
   2/3 * 3/4 = 1/2

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct {
   int num, den;
} Fraction;

typedef enum { ADD = 1, SUB, MUL, DIV } Operator;


int GCD(int a, int b);
void simplify(Fraction *f);

Fraction *newFraction(int n, int d);
Fraction *addFractions(Fraction *f1, Fraction *f2);
Fraction *mulFractions(Fraction *f1, Fraction *f2);
Fraction *subFractions(Fraction *f1, Fraction *f2);
Fraction *divFractions(Fraction *f1, Fraction *f2);

Fraction *readFraction(char s[]);
Operator readOperator(char s[]);
Fraction *execute(Operator op, Fraction *f1, Fraction *f2);
   
char *fraction2str(Fraction *f);
char operator2char(Operator op);
        
        
int main(int argc, char *argv[])
{
   Fraction *result, *f1, *f2;
   Operator .op;
   char op_char;

   if (argc != 4) { /* do not change */
      printf("There must be 3 command-line arguments: \n"
             "   operator, the first fraction, the second fraction\n\n");
      exit(1);
   }
 
        op = readOperator(argv[1]);                                    /$
   
        f1 = readFraction(argv[2]);                                    /$
      
        f2 = readFraction(argv[3]);                                    /$
    
        result = execute((Operator) op, Fraction *f1, Fraction *f2);   /$
      
        op_char = operator2char((Operator) op);                        /$
      
   printf("%s %c %s = %s\n",  /* output - do not change */
          fraction2str(f1),
          op_char,
          fraction2str(f2),
          fraction2str(result));

   return 0;
}

/* compute the greatest common divisor of a and b */
int GCD(int a, int b)
{
   int r;
   if (a < b) { /* swap a and b */
      r = a;
      a = b;
      b = r;
   }
   while (b != 0) {
      r = b;
      b = a % b;
      a = r;
   }
   return a;
}
          
/* reduce the fraction f to lowest terms */
void simplify(Fraction *f)
{
int gcd = GCD(abs(f->num), abs(f->den));
   f->num /= gcd;
   f->den /= gcd;
}
   
   
Fraction *execute(Operator op, Fraction *f1, Fraction *f2)
      
{
    
        switch(op) 
      
        {
      
        case ADD:
   
                addFractions(Fraction *f1, Fraction *f2);
          
                break;

        case SUB:
 
                subFractions(Fraction *f1, Fraction *f2);
   
                break;
   
        case DIV:

                divFractions(Fraction *f1, Fraction *f2);
 
                break;
        case MUL:  
      
                mulFractions(Fraction *f1, Fraction *f2);
      
                break;
   
        default:
          
                ;

                break;
   
        }
   
}
   
char *fraction2str(Fraction *f)

{
 
                char *s;
        
                s=calloc(22, sizeof(char));
                
                sprintf(s,"%d%c%d", f->num, '/', f->den);
                
                return s;
        
}         
                 

            




char operator2char(Operator op)

{
 
        if (op = ADD)
                
                return '+';
                
        else if (op = SUB)
                
                return '-';
                
        else if (op = MUL)
          
                return '*';

        else if (op = DIV)
   
                return '/';
   
}


 
 
        
                
                
                
        
Fraction *readFraction(char s[])
                
{               
        
                int num;
                
                int den;
        
                Fraction *f;
                
                sscanf(s,"%d%d", &num, &den);
 
                f=newfraction(num,den);

                simplify(f);
 
       return f;
                
                
                
        

                
                
}       
                
                
                
        
                
                
                
 
                
Operator readOperator(char s[])
                
{
       
             Operator op;
                
                
        
               if(strcmp(s,"add")==0)
                
                       op=ADD;
        
               if(strcmp(s,"sub")==0)
                
                       op=SUB;
               
               if(strcmp(s,"mul")==0)
                
                       op=MUL;
 
               if(strcmp(s,"div")==0)

                       op=DIV;
 
               else if(!(strcmp(s,
"add")==0)&&(!(strcmp(s,"sub")==0))&&(!(strcmp(s,
"mul")==0))&&(!(strcmp(s, "div")==0)))
                
               {
               
                    printf("\nUse add, div, sub, mul.\n");
                       
                    exit(1);
               
               }
                       
               return op;
               
}               
                       
 
               

                       
 
               


Fraction *newFraction(int n, int d)
                
{              
                    
       if(d==0)        
                    
       {       
                
            printf("\n Incorrect fraction (zero denominator) \n");

          exit(1);
                
       }               
 
       if(d<0) 

       {               
 
                        printf("\n The negative sign must be in the
numerator.\n\t(2/-3) & (-2/-3) are incorrect;\n");

                        exit(1);   
                
       }       
                    
       if(n==0&&d>0)   
                    
       {       
                
               n=0;
               
               d=1; 
                
       }               
 
       Fraction *f;

       f=calloc(1,sizeof(Fraction));
 
       f->num=n;

       f->den=d;
                        
       return f;
               
}                   
       
        
               
        
               
        
                
        
       
        
       
Fraction *addFractions(Fraction *f1, Fraction *f2)
       
{                      
       
        Fraction *result;
       
        result=calloc(1,sizeof(Fraction));
 
        if(f1->num==0)
                
        return f2;
                
        else if(f2->num==0)

        return f1;
        
        else
        
   {   

                result->num = f1->num * f2->den + f2->num *
f1->den;
    
                result->den = f1->den * f2->den;
    
                simplify(result);
    
                return result;
    
        }
    
}



Last edited on
closed account (Lv0f92yv)
Test? Does this mean you want someone here to test this program for you?

Is there anything in particular you are looking for?
Did you get the answer? Mr program is very similar to this one but this is the error I'm getting

project6.c: In function `main':
project6.c:69: error: syntax error before "Fraction"
project6.c: In function `execute':
project6.c:121: error: syntax error before "Fraction"
project6.c:124: error: syntax error before "Fraction"
project6.c:127: error: syntax error before "Fraction"
project6.c:130: error: syntax error before "Fraction"

which in his program it would be 118-135 and line 69.
I put this up to see my line numbers and to get debugging advice. Here are the following errors I'm getting when I try to compile:

project6.c: In function `main':
project6.c:54: error: syntax error before '.' token
project6.c:63: error: `op' undeclared (first use in this function)
project6.c:63: error: (Each undeclared identifier is reported only once
project6.c:63: error: for each function it appears in.)
project6.c:69: error: syntax error before "Fraction"
project6.c: In function `execute':
project6.c:118: error: syntax error before "Fraction"
project6.c:124: error: syntax error before "Fraction"
project6.c:130: error: syntax error before "Fraction"
project6.c:135: error: syntax error before "Fraction"
project6.c: In function `readFraction':
project6.c:211: warning: assignment makes pointer from integer without a cast
Last edited on
theres a "." before the declaration of your Operator object op that explains the first few errors anyway
Do i declare op as type char after line 55?
Nevermind ^^
Now just getting the syntax errors before "Fraction" any clue what's up with that?
in line 211 change f to F in newFraction
Oh, that was an easy fix. Thanks. Are you still getting your syntax errors btw lines 118-135? Are you in 206 as well?
yea, im still getting the syntax errors and yes im in 206.
I was able to get rid of them by changing a few things but whenever i would compile the program it would return

There must be 3 command-line arguments:
operator, the first fraction, the second fraction

so not quite sure what to do at this point anymore
Once it properly compiles the code we have here, I don't believe there's anything else we have to include, correct?
correct
well if you have any luck with the project keep me posted and ill do the same.
Topic archived. No new replies allowed.