I'm using gcc to compile and I need to be able to run it by doing ./test 7 10 +

this is part an attempt of a Reverse Polish Notation calculator for one of my computer classes. My program works if I put it in quotes passing the sequence in as just 1 argument and I thought that was fine but I was told I need to be able to type the sequence without quotes at the command prompt i.e. just .test 7 10 + and not ./test "7 10 +" so I was just told to loop through using argc like below and append it to the string strpointer which is used in the rest of the program.

This seemed easy at first (coming from java) but I guess not, Being brand new at C, I don't know how to just append each separate number and arithmetic operator into one string (strPointer) which is used for the rest of the program. :'(

Doing the below sets Strpointer = to 710+ when I need it to be 7 1 0 +
(the stack program below depends on the spaces)

Better yet, is there a better approach to doing this program that won't require total redoing of my code?

Recap: My calculator named test.c should be able to do this at the command prompt

./test 7 10 +

result = 17.00

As it is below, I type ./test 7 1 0 4 + - at the gcc command prompt after compiling it

and strPointer is equal to 7104+- which results in my stack not being able to read it.

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

typedef struct node
{
	float numdata;
	struct node * nextnode;
} node;

static node *top = NULL; 

node * peek()
{
  //stack = top;
  return top;
}


bool Is_empty(node * stack)
{
   return peek(stack) == NULL;
}

node * push(node * stack, float number)
{
   static node * newItem;
  

   newItem = calloc(sizeof(node), 1);
   newItem -> numdata = number;
   newItem -> nextnode = stack;

   return newItem;
}

node * pop(node * stack)
{
   static node * temp; 
   temp = stack -> nextnode;
   free(stack);
   return temp; 
} 


void clear(void)//node * stack)
{
while(top != NULL)
{

  top = pop(top);
/*
  while(!is_empty(stack))
{
  pop(stack);
}
*/
}
}


int main(int argc, char*argv[])
{
  char strBuffer[1024];
  char* strPointer = strBuffer;
  int slen = 0;

  for (int i = 1; i < argc; i++)
{ 
   printf("argv[%d] = %s\n", i, argv[i]); //This is just for testing 
   slen += snprintf(strPointer + slen, 1024-slen,"%s", argv[i]);
}

printf("strPointer = %s \n", strPointer); // prints out 710+ instead of 7 10 +


	//char * strPointer = argv[1];

	char numStringordering[20];
	float currentNum;
	int numLength;
	int assigned = 0;
        static float result = 0;
        int flag = 0;

//printf("strPointer %s \n", strPointer);


	while (strPointer[0] != '\0')
     {
        
	if (isdigit(strPointer[0]))
	{
		numLength = strspn(strPointer,"0123456789");
 

		if (assigned)
		{
		  top = push(top, currentNum);
		}
		else
		{
		  assigned = 1;
		}
        
	strncpy(numStringordering, strPointer, numLength);
	numStringordering[numLength] = '\0';
       // printf("numStringordering %s \n", numStringordering);
	currentNum = atoi(numStringordering);
	strPointer += numLength;

	}
 	else
	{
		switch (strPointer[0])
		{
			case '+':
			{
				if (peek() != NULL)
				{
				         (float)currentNum;
					 currentNum += top -> numdata;
                                         result = currentNum;
					 top = pop(top);
					 
				}
				else
				{
					printf("Error! wrong syntax or wrong operation.\n");
					flag = 1;
				}
			}
			break;
			case '-':
			{
				if (peek() != NULL)
				{  
					(float)currentNum;
					currentNum = top -> numdata - currentNum;
				        result = currentNum;
					top = pop(top);
				}
				else
				{
					printf("Error! wrong syntax or wrong operation.\n");
					flag = 1;
				}
			}
			break;
			case '*':
			{
				if (peek() != NULL)
				{
					 (float)currentNum;
					 currentNum *= top -> numdata;
					 result = currentNum;
					 top = pop(top);
				}
				else
				{
					printf("Error! wrong syntax or wrong operation.\n");
					flag = 1;
				}
			}
			break;
			case '/':
			{
				if (peek() != NULL)
				{
					 (float)currentNum;
					 currentNum = top -> numdata/currentNum;
					 result = currentNum;
					 top = pop(top);
					
				}
				else
				{
					printf("Error! wrong syntax or wrong operation.\n");
					flag = 1;
				}
			}
			break;
		
	}
	strPointer++;
	}
	
	}

        if (flag == 1)
	  { 
		printf("Error! could not compute.\n");

          }
	else
	{
	printf("The result is: %0.2f\n", result);
	}	
			
        
}

I think I got it, I can't believe all that was needed was doing this

slen += snprintf(strPointer + slen, 1024-slen,"%s ", argv[i]);

basically just putting a space after %s.

I also discovered I have to precede every '*' symbol with a \ to do the multiply i.e. ./test 7 10 \* rather than just ./test 7 10 *otherwise it starts printing out stuff I don't want. If I don't use the \, argv would also print out my file names and other random ubuntu directories on the desktop and a bunch of other stuff that shouldn't be there :S, that doesn't look too good, since it looks like it's accessing other parts of memory and it could screw up the entire OS. Strange!

I also discovered I have to precede every '*' symbol with a \ to do the multiply i.e. ./test 7 10 \* rather than just ./test 7 10 *otherwise it starts printing out stuff I don't want. If I don't use the \, argv would also print out my file names and other random ubuntu directories on the desktop and a bunch of other stuff that shouldn't be there :S, that doesn't look too good, since it looks like it's accessing other parts of memory and it could screw up the entire OS. Strange!

No, it's not random at all. In *nix, a command line parameter * means "all files" (like *.*) in the current directory.

Yeah, I'm new to linux/unix too but slowly learning those little details.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.