RPN Calc trouble in C

Hey guys! I am having trouble with creating a RPN calculator and I have finished 2 files: a dynamicArray.h and a dynamicArray.c and those I believe are correct. I am having trouble with the actual implemantation of the calc.c program. Can anyone help me get the add function working and I am pretty sure I can get it from there. Here is the full description and code: Nothing needs to be changed to dynamicArray.c or .h I just wanted to show you what I was working with and what I have done.

I need to write a program called calc.c that uses one stack (in dynamicArray.c) to implement a command line RPN (Reverse Polish Notation) calculator. The "main" function reads in, from the command line, a sequence of numbers and operators separated by white spaces (an example command line entry would look like "calc 5 3.12 x 4 + 78 29.35 6 - / x" without the quotes) and computes and displays the resulting value. The sequence of numbers (including negatives and the constants "pi" and "e") and operators are passed to main via the "int argc" and "char *argv[]" parameters. Your program should change the strings "pi" and "e" into the values 3.14159265... and 2.7182818... respectively.

calc.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
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
#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include "dynamicArray.h"

#include "dynamicArray.c"





/* param: s the string

   param: num a pointer to double

   returns: true (1) if s is a number else 0 or false.

   postcondition: if it is a number, num will hold

   the value of the number

*/

int isNumber(char *s, double *num)

{

	char *end;

	double returnNum;



	if(strcmp(s, "0") == 0)

	{

		*num = 0;

		return 1;

	}

	else 

	{

		returnNum = strtod(s, &end);

		/* If there's anythin in end, it's bad */

		if((returnNum != 0.0) && (strcmp(end, "") == 0))

		{

			*num = returnNum;

			return 1;

		}

	}

	return 0;  //if got here, it was not a number

}



/*	param: stack the stack being manipulated

	pre: the stack contains at least two elements

	post: the top two elements are popped and 

	their sum is pushed back onto the stack.

*/

void add (struct DynArr *stack)

{

	/* FIXME: You will write this function */

}



/*	param: stack the stack being manipulated

	pre: the stack contains at least two elements

	post: the top two elements are popped and 

	their difference is pushed back onto the stack.

*/

void subtract(struct DynArr *stack)

{

	/* FIXME: You will write this function */

}



/*	param: stack the stack being manipulated

	pre: the stack contains at least two elements

	post: the top two elements are popped and 

	their quotient is pushed back onto the stack.

*/

void divide(struct DynArr *stack)

{

	/* FIXME: You will write this function */

}



double calculate(int numInputTokens, char **inputString)

{

	int i;

	double result = 0.0;

	char *s;

	struct DynArr *stack;



	//set up the stack

	stack = createDynArr(20);



	// start at 1 to skip the name of the calculator calc

	for(i=1;i < numInputTokens;i++) 

	{

		s = inputString[i];



		// Hint: General algorithm:

		// (1) Check if the string s is in the list of operators.

		//   (1a) If it is, perform corresponding operations.

		//   (1b) Otherwise, check if s is a number.

		//     (1b - I) If s is not a number, produce an error.

		//     (1b - II) If s is a number, push it onto the stack



		if(strcmp(s, "+") == 0)

			add(stack);

		else if(strcmp(s,"-") == 0)

			subtract(stack);

		else if(strcmp(s, "/") == 0)

			divide(stack);

		else if(strcmp(s, "x") == 0)

			/* FIXME: replace printf with your own function */

			printf("Multiplying\n");

		else if(strcmp(s, "^") == 0)

			/* FIXME: replace printf with your own function */

			printf("Power\n");

		else if(strcmp(s, "^2") == 0)

			/* FIXME: replace printf with your own function */

			printf("Squaring\n");

		else if(strcmp(s, "^3") == 0)

			/* FIXME: replace printf with your own function */

			printf("Cubing\n");

		else if(strcmp(s, "abs") == 0)

			/* FIXME: replace printf with your own function */

			printf("Absolute value\n");

		else if(strcmp(s, "sqrt") == 0)

			/* FIXME: replace printf with your own function */

			printf("Square root\n");

		else if(strcmp(s, "exp") == 0)

			/* FIXME: replace printf with your own function */

			printf("Exponential\n");

		else if(strcmp(s, "ln") == 0)

			/* FIXME: replace printf with your own function */

			printf("Natural Log\n");

		else if(strcmp(s, "log") == 0)

			/* FIXME: replace printf with your own function */

			printf("Log\n");

		else 

		{

			// FIXME: You need to develop the code here (when s is not an operator)

			// Remember to deal with special values ("pi" and "e")

			

		}

	}	//end for 



	/* FIXME: You will write this part of the function (2 steps below) 

	 * (1) Check if everything looks OK and produce an error if needed.

	 * (2) Store the final value in result and print it out.

	 */

	

	return result;

}



int main(int argc , char** argv)

{

	// assume each argument is contained in the argv array

	// argc-1 determines the number of operands + operators

	if (argc == 1)

		return 0;



	calculate(argc,argv);

	return 0;

}

Last edited on
dynamicArray.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#include <assert.h>

#include <stdlib.h>

#include "dynamicArray.h"

#include <stdio.h>





struct DynArr

{

TYPE *data; /* pointer to the data array */

int size; /* Number of elements in the array */

int capacity; /* capacity ofthe array */

};





/* ************************************************************************

Dynamic Array Functions

************************************************************************ */



/* Initialize (including allocation of data array) dynamic array.



param: v pointer to the dynamic array

param: cap capacity of the dynamic array

pre: v is not null

post: internal data array can hold cap elements

post: v->data is not null

*/

void initDynArr(DynArr *v, int capacity)

{

assert(capacity > 0);

assert(v!= 0);

v->data = (TYPE *) malloc(sizeof(TYPE) * capacity);

assert(v->data != 0);

v->size = 0;

v->capacity = capacity;

}



/* Allocate and initialize dynamic array.



param: cap desired capacity for the dyn array

pre: none

post: none

ret: a non-null pointer to a dynArr of cap capacity

and 0 elements in it.

*/

DynArr* createDynArr(int cap)

{

assert(cap > 0);

DynArr *r = (DynArr *)malloc(sizeof( DynArr));

assert(r != 0);

initDynArr(r,cap);

return r;

}



/* Deallocate data array in dynamic array.



param: v pointer to the dynamic array

pre: none

post: d.data points to null

post: size and capacity are 0

post: the memory used by v->data is freed

*/

void freeDynArr(DynArr *v)

{

if(v->data != 0)

{

free(v->data); /* free the space on the heap */

v->data = 0; /* make it point to null */

}

v->size = 0;

v->capacity = 0;

}



/* Deallocate data array and the dynamic array ure.



param: v pointer to the dynamic array

pre: none

post: the memory used by v->data is freed

post: the memory used by d is freed

*/

void deleteDynArr(DynArr *v)

{

freeDynArr(v);

free(v);

}



/* Resizes the underlying array to be the size cap



param: v pointer to the dynamic array

param: cap the new desired capacity

pre: v is not null

post: v has capacity newCap

*/

void _dynArrSetCapacity(DynArr *v, int newCap)

{

/* FIXME: You will write this function */



TYPE *newArr = malloc(newCap * sizeof(TYPE));

assert(newArr != 0);



for(int i = 0; i<v->size; i++){

newArr[i] = v->data[i];

}

free(v->data);

v->data = newArr;

v->capacity=newCap;



}



/* Get the size of the dynamic array



param: v pointer to the dynamic array

pre: v is not null

post: none

ret: the size of the dynamic array

*/

int sizeDynArr(DynArr *v)

{

assert(v != 0);

return v->size;

}



/* Adds an element to the end of the dynamic array



param: v pointer to the dynamic array

param: val the value to add to the end of the dynamic array

pre: the dynArry is not null

post: size increases by 1

post: if reached capacity, capacity is doubled

post: val is in the last utilized position in the array

*/

void addDynArr(DynArr *v, TYPE val)

{

/* FIXME: You will write this function */



assert(v != 0);



v->size++;



if(v->size >= v->capacity){

_dynArrSetCapacity(v, v->capacity * 2);

}



v->data[v->size - 1] = val;





}



/* Get an element from the dynamic array from a specified position



param: v pointer to the dynamic array

param: pos integer index to get the element from

pre: v is not null

pre: v is not empty

pre: pos < size of the dyn array and >= 0

post: no changes to the dyn Array

ret: value stored at index pos

*/



TYPE getDynArr(DynArr *v, int pos)

{

/* FIXME: You will write this function */

assert( v!= 0);

assert(pos < v->size || pos >= 0);



/* FIXME: you must change this return value */

return v->data[pos];

}



/* Put an item into the dynamic array at the specified location,

overwriting the element that was there



param: v pointer to the dynamic array

param: pos the index to put the value into

param: val the value to insert

pre: v is not null

pre: v is not empty

pre: pos >= 0 and pos < size of the array

post: index pos contains new value, val

*/

void putDynArr(DynArr *v, int pos, TYPE val)

{

/* FIXME: You will write this function */



assert(v != 0);



assert(pos < v->size || pos > 0);



v->data[pos] = val;





}



/* Swap two specified elements in the dynamic array



param: v pointer to the dynamic array

param: i,j the elements to be swapped

pre: v is not null

pre: v is not empty

pre: i, j >= 0 and i,j < size of the dynamic array

post: index i now holds the value at j and index j now holds the value at i

*/

void swapDynArr(DynArr *v, int i, int j)

{

/* FIXME: You will write this function */

assert( v != 0);



assert(i >= 0 || i < v->size || j >= 0 || i < v->size);



int temp = v->data[i];

v->data[i] = v->data[j];

v->data[j] = temp;



}



/* Remove the element at the specified location from the array,

shifts other elements back one to fill the gap



param: v pointer to the dynamic array

param: idx location of element to remove

pre: v is not null

pre: v is not empty

pre: idx < size and idx >= 0

post: the element at idx is removed

post: the elements past idx are moved back one

*/

void removeAtDynArr(DynArr *v, int idx)

{

/* FIXME: You will write this function */



assert(idx >= 0 || idx < v->size);



for(int i = idx + 1; i < v->size; i++){

v->data[i - 1] = v->data[i];

}



v->size--;



}

/* ************************************************************************

Stack Interface Functions

************************************************************************ */



/* Returns boolean (encoded in an int) demonstrating whether or not the

dynamic array stack has an item on it.



param: v pointer to the dynamic array

pre: the dynArr is not null

post: none

ret: 1 if empty, otherwise 0

*/

int isEmptyDynArr(DynArr *v)

{

/* FIXME: You will write this function */

if(v->size == 0){

return 1;

}

else{

/* FIXME: You will change this return value*/

return 0;

}

}



/* Push an element onto the top of the stack



param: v pointer to the dynamic array

param: val the value to push onto the stack

pre: v is not null

post: size increases by 1

if reached capacity, capacity is doubled

val is on the top of the stack

*/

void pushDynArr(DynArr *v, TYPE val)

{

/* FIXME: You will write this function */

assert(v != 0);

addDynArr(v, val);

//printf("Pushed %d on Stack\n",val);

}



/* Returns the element at the top of the stack



param: v pointer to the dynamic array

pre: v is not null

pre: v is not empty

post: no changes to the stack

*/

TYPE topDynArr(DynArr *v)

{

/* FIXME: You will write this function */

assert(v != 0);

/* FIXME: You will change this return value*/

return v->data[v->size-1];

}


void popDynArr(DynArr *v)

{

/* FIXME: You will write this function */

assert( v != 0);

v->size--;

}



/* ************************************************************************

Bag Interface Functions

***********************************************************************
*/

int containsDynArr(DynArr *v, TYPE val)

{

/* FIXME: You will write this function */



assert(v != 0);



for(int i = 0; i < v->size; i++){

if(v->data[i] == val){

return 1;

}

}



/* FIXME: You will change this return value */

return 0;



}


void removeDynArr(DynArr *v, TYPE val)

{

/* FIXME: You will write this function */



assert(v != 0);

for(int i = 0; i < v->size - 1; i++){

if(v->data[i] == val){

removeAtDynArr(v, i);

return;

}

}

}
dynamicArray.h:

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
/* 	dynamicArray.h : Dynamic Array implementation. */

#include<math.h>



#ifndef DYNAMIC_ARRAY_INCLUDED

#define DYNAMIC_ARRAY_INCLUDED 1





# ifndef TYPE

# define TYPE     double 

# define TYPE_SIZE sizeof(double)

# endif



# ifndef EQ

# define EQ(A, B) (fabs(A - B) < 10e-7)

# endif



typedef struct DynArr DynArr;



/* Dynamic Array Functions */

DynArr *createDynArr(int cap);

void deleteDynArr(DynArr *v);



int sizeDynArr(DynArr *v);



void addDynArr(DynArr *v, TYPE val);

TYPE getDynArr(DynArr *v, int pos);

void putDynArr(DynArr *v, int pos, TYPE val);

void swapDynArr(DynArr *v, int i, int  j);

void removeAtDynArr(DynArr *v, int idx);



/* Stack interface. */

int isEmptyDynArr(DynArr *v);

void pushDynArr(DynArr *v, TYPE val);

TYPE topDynArr(DynArr *v);

void popDynArr(DynArr *v);



/* Bag Interface */

int containsDynArr(DynArr *v, TYPE val);

void removeDynArr(DynArr *v, TYPE val);



#endif

Topic archived. No new replies allowed.