7 Replies - 1474 Views - Last Post: 18 April 2013 - 10:53 PM Rate Topic: -----

#1 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

RPN calculator trying to do an add of command line arguments

Posted 18 April 2013 - 04:45 PM

This rpn calculator is to bring two values onto the stack, assert it's not empty, pop them, add them together, push them back on the stack. With that being said, look at
void add(struct DynArr *stack)
I thought I could declare arg1 as an int for the first value, do the functions shown and then do the same thing for the second value. But it does not like int arg1 = stack. It says DynArr* can't be used to initialize entity of type int. Code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "dynamicArray.h"


/* 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;
	
	returnNum = strtod(s, &end);
	
	/* If there's anything in end, it's not a number */
	if(strcmp(end, "") == 0) {
		*num = returnNum;
		return 1;
	} else {
		return 0;
   }
}

/*	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 */
	assert(isEmptyDynArr(stack));
	int arg1 = stack;
	topDynArr(stack);
	popDynArr(stack);
	int arg2 = stack;
	topDynArr(stack);
	popDynArr(stack);
	int result = arg1 + arg2;
	pushDynArr(stack, result);
}

/*	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, number;
	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 
		{
		   if (isNumber(s, &number)){
		      pushDynArr(stack, number);
		   }
		   else{
		      printf("Not a number! Sorry");
		   }
			// 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;
}




Is This A Good Question/Topic? 0
  • +

Replies To: RPN calculator trying to do an add of command line arguments

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: RPN calculator trying to do an add of command line arguments

Posted 18 April 2013 - 06:02 PM

You cannot assign a struct pointer to an integer. There are ways to force this by casting, but you would only be inserting the memory location of the stack into the integer (not the value on top). If you are trying to get a value off the stack, you need to pop (or peek) the stack to retrieve the top most element.
Was This Post Helpful? 0
  • +
  • -

#3 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: RPN calculator trying to do an add of command line arguments

Posted 18 April 2013 - 08:04 PM

I'm attaching updated calc.c and dynamicArray.h because I get these errors and I don't know what's going on:

calc.c:(.text+0x60): undefined reference to `isEmptyDynArr'
calc.c:(.text+0x89): undefined reference to `topDynArr'
calc.c:(.text+0x9a): undefined reference to `popDynArr'
calc.c:(.text+0xa6): undefined reference to `isEmptyDynArr'
calc.c:(.text+0xcf): undefined reference to `topDynArr'
calc.c:(.text+0xe0): undefined reference to `popDynArr'
calc.c:(.text+0x100): undefined reference to `pushDynArr'
/tmp/ccT3YceQ.o: In function `calculate':
calc.c:(.text+0x138): undefined reference to `createDynArr'
calc.c:(.text+0x331): undefined reference to `pushDynArr'
calc.c:(.text+0x361): undefined reference to `topDynArr'
collect2: ld returned 1 exit status

dynamicArray.h:

#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



calc.c:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "dynamicArray.h"

/* 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;
	
	returnNum = strtod(s, &end);
	
	/* If there's anything in end, it's not a number */
	if(strcmp(end, "") == 0) {
		*num = returnNum;
		return 1;
	} else {
		return 0;
   }
}

/*	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)
{
	double arg1, arg2, result;
	assert(!isEmptyDynArr(stack));
	arg1 = topDynArr(stack);
	popDynArr(stack);
	assert(isEmptyDynArr(stack));
	arg2 = topDynArr(stack);
	popDynArr(stack);
	result = arg1 + arg2;
	pushDynArr(stack, result);
}

/*	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, number;
	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 
		{
		   if (isNumber(s, &number)){
		      pushDynArr(stack, number);
		   }
		   else{
		      printf("Not a number! Sorry");
		   }
			// 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.
	 */
	result = topDynArr(stack);
         printf("%d", result);
         printf("\n");
         return result;
	
	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;
}


Was This Post Helpful? 0
  • +
  • -

#4 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: RPN calculator trying to do an add of command line arguments

Posted 18 April 2013 - 08:36 PM

I first said it may have been the assert but I'm wrong. But please help.

This post has been edited by ZacCarlson: 18 April 2013 - 08:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,488
  • Joined: 05-May 12

Re: RPN calculator trying to do an add of command line arguments

Posted 18 April 2013 - 09:20 PM

The linker is complaining that you it can't find the body of the functions you declared in dynamicArray.h and are using in calc.c. Did you remember to add dynamicArray.c to your project/makefile/command line?

This post has been edited by Skydiver: 18 April 2013 - 09:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: RPN calculator trying to do an add of command line arguments

Posted 18 April 2013 - 09:28 PM

Can you be more specific on how I would do that?
Was This Post Helpful? 0
  • +
  • -

#7 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: RPN calculator trying to do an add of command line arguments

Posted 18 April 2013 - 10:07 PM

Also, if it helps, when I run it on Visual Studio it runs fine with no errors. Just when I runt it on flip or putty it gives those errors.
Was This Post Helpful? 0
  • +
  • -

#8 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: RPN calculator trying to do an add of command line arguments

Posted 18 April 2013 - 10:53 PM

Skydiver....you're a genius.....I wasn't thinking and it came to me what you said and it does make sense...it works now....thank you!!!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1