d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > My 2nd Project, Just Need Some Advice
123Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 29 2012 02:20am
Quote
For your second project, you are to write a Reverse Polish Calculator. It will
read a series of numbers and operators [+, -, *, /, u, p, e], (one at a time)
from the standard input and do the operations indicated.
An integer starts with a digit. If the current input is an integer, push it value
on the stack.
If any of ‘+’, ‘-’, ‘*’, or ‘/’ are the current input string, pop 2 integers from
the stack [call the first ‘s1’ and the second ‘s2’], do s2 oper s1 on them and
push the result onto the stack. (I.e., if the input is ‘2 3 -’, push 2, push 3,
see ‘-’, pop 3 = s1, pop 2 = s2, perform s2 - s1 = 2 - 3 = -1, and push -1)
If the operator ’u’ is the current input, pop a value, negate it, and push the
result.
If the operator ‘p’ is the current input, pop a value and write it to standard
output [cout].
If the operator ‘e’ is the current input, exit the program.



Should I create separate functions for every operator? Seems like it would be a bit bulky that way, but I like the organization aspect of it.

I haven't begun writing yet as I want to kind of think about how to tackle this from a pseudo stand point....it is by no means a hard project, but I want to know when I should/shouldn't use lots of functions
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 29 2012 04:38am
spent a few hours working on it. Got the code written but compiler errors galore...

the way our teacher taught us about stacks, he never mentioned passing the starting 'head' to functions


here is my code:

Code
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

struct stack
{
   int value;
   stack *next;
};

void push(int num, stack *calcStack);
int pop();
bool isEmpty();
void operation(char op);
void operation2(char op);


int main()
{
   stack *calcStack = NULL;
   string userIn;

   while(getline(cin,userIn))
   {
       for(int i = 0;i<int(userIn.length());i++)
       {
           char temp = userIn[i];
           if(isdigit(userIn[i]))
           {
               push(atoi(userIn.c_str()));
           }
           else if(temp == "+" || "-" || "*" || "/")
           {
               operation(userIn[i]);
           }
           else
           {
               operation2(userIn[i]);
           }
       }
   }



return 0;
}

void push(int num, stack *calcStack)
{
   stack *newItem = new(stack);
   newItem->value = num;
   newItem->next = calcStack;
   calcStack = newItem;
}

int pop()
{
   int item = calcStack->val;
   stack *tempPtr = calcStack;
   calcStack = calcStack->next;
   delete tempPtr;
   return item;
}

bool isEmpty()
{
   return calcStack == NULL;
}

void operation(char op)
{
   if(isEmpty())
   {
       cout << "Stack is empty. Please try again with correct inputs." << endl;
       exit(1);
   }
   int num1, num2;
   num1 = pop();
   num2 = pop();

   switch(op)
   {
       case "+":
           push(num2+num1);
           break;
       case "-":
           push(num2-num1);
           break;
       case "*":
           push(num2*num1);
           break;
       case "/":
           push(num2/num1);
           break;
       default:
           break;
   }
}

void operation2(char op)
{
   if(isEmpty())
   {
       cout << "Stack is empty. Please try again with correct inputs." << endl;
       exit(1);
   }
   int num;
   switch(op)
   {
       case "u":
           num = pop();
           push(-num);
           break;
       case "p":
           num = pop();
           cout << num << endl;
           break;
       case "e":
           cout << "Thanks for using the reverse polish calculator." << endl;
           exit(0);
       default:
           break;
   }
}



getting a DOOZY of compiler errors. Too sleepy to figure it out on my own right now, any advice would be appreciated:

Code
!g++ proj2.cpp && a.out
proj2.cpp: In function `int main()':
proj2.cpp:12: error: too few arguments to function `void push(int, stack*)'
proj2.cpp:31: error: at this point in file
proj2.cpp:33: error: ISO C++ forbids comparison between pointer and integer
proj2.cpp:21: warning: unused variable 'calcStack'
proj2.cpp: In function `int pop()':
proj2.cpp:59: error: `calcStack' was not declared in this scope
proj2.cpp: In function `bool isEmpty()':
proj2.cpp:68: error: `calcStack' was not declared in this scope
proj2.cpp:68: warning: unused variable 'calcStack'
proj2.cpp: In function `void operation(char)':
proj2.cpp:84: error: case label does not reduce to an integer constant
proj2.cpp:50: error: too few arguments to function `void push(int, stack*)'
proj2.cpp:85: error: at this point in file
proj2.cpp:87: error: case label does not reduce to an integer constant
proj2.cpp:50: error: too few arguments to function `void push(int, stack*)'
proj2.cpp:88: error: at this point in file
proj2.cpp:90: error: case label does not reduce to an integer constant
proj2.cpp:50: error: too few arguments to function `void push(int, stack*)'
proj2.cpp:91: error: at this point in file
proj2.cpp:93: error: case label does not reduce to an integer constant
proj2.cpp:50: error: too few arguments to function `void push(int, stack*)'
proj2.cpp:94: error: at this point in file
proj2.cpp: In function `void operation2(char)':
proj2.cpp:111: error: case label does not reduce to an integer constant
proj2.cpp:50: error: too few arguments to function `void push(int, stack*)'
proj2.cpp:113: error: at this point in file
proj2.cpp:115: error: case label does not reduce to an integer constant
proj2.cpp:119: error: case label does not reduce to an integer constant




especially line 33. Why would it ever think that? Never is it doing what it says (comparing int to ptr)

edit: I am aware of some of the obvious ones. I changed one of the stack functions to be passed a ptr to struct stack as a test to check something, but forgot to fix the calls in the program.

This post was edited by Eep on Sep 29 2012 04:40am
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Sep 29 2012 06:40am
this wont work as you expect it:
Code
if(temp == "+" || "-" || "*" || "/")

you need to do it like this:
Code
(temp == "+" || temp == "-" || temp == "*" || temp == "/")


and there's another problem at this line...

'a' ---> this is a single char
"a" ---> this is a string. a string is an array of chars terminated by hex zero: '\x00'
so the string "+" is actually two bytes long... two chars... the first is '+' and the second is the hex zero (string terminator)
if you compare one byte with two bytes... how could the compiler do that? you need to do it like this:
Code
(temp == '+' || temp == '-' || temp == '*' || temp == '/')
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 29 2012 02:14pm
Quote (Richter @ Sep 29 2012 07:40am)
this wont work as you expect it:
Code
if(temp == "+" || "-" || "*" || "/")

you need to do it like this:
Code
(temp == "+" || temp == "-" || temp == "*" || temp == "/")


and there's another problem at this line...

'a' ---> this is a single char
"a" ---> this is a string. a string is an array of chars terminated by hex zero: '\x00'
so the string "+" is actually two bytes long... two chars... the first is '+' and the second is the hex zero (string terminator)
if you compare one byte with two bytes... how could the compiler do that? you need to do it like this:
Code
(temp == '+' || temp == '-' || temp == '*' || temp == '/')


thanks, I completely forgot about both of those things.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 29 2012 02:58pm
ok so I am having an issue currently with atoi


here is some code:

Code
int main()
{
   string userIn;
   int len;
   char *temp;

   while(getline(cin,userIn))
   {
       len = userIn.length();
       for(int i = 0; i<len; i++)
       {
           temp = new char[len+1];
           strcpy(temp, userIn.c_str());
           temp[len] = '\0';
           if(isdigit(userIn[i]))
           {
               push(atoi(temp[i]));
           }



atoi keeps saying error: conversion from char to const char *

I am guessing that by initializing the array, like temp[i], is giving me a 'char' value.

I am assuming atoi wants an ENTIRE string of chars.

But I only want to convert one thing at a time. How would I go about doing that? Or do I have to create yet another array of chars which only contains 1 element at a time?

This post was edited by Eep on Sep 29 2012 03:00pm
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Sep 29 2012 03:27pm
I guess you also want to deal with integers which are longer than just 1 digit? so taking 1 digit of an integer (which is as example 3 digits long) and converting each of those 'char' of the string to an integer is kind of... pointless...
you need a strategy... tell us possible inputs:
example:
1) 3 2 +
2) 13 5 -
3) 83 3 /
4) 54.3 4.2 *
5) 4
6) 2
7) /
8) 12.2
9) 4 1

I guess there are only integers, so number 8 and number 4 are not possible?
I guess that there is always a whitespace between the integers and the operators?
I guess that integers may be longer than 1 digit?
I guess that the program should do something after each 'newline'?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 29 2012 03:28pm
making more progress. Eliminated all compiler errors but 1:

Code
proj2.cpp: In function `int pop()':
proj2.cpp:63: error: 'struct stack' has no member named 'val'



offending area:

Code
int pop()
{
   [b]int item = calcStack->val;[/b]
   stack *tempPtr = calcStack;
   calcStack = calcStack->next;
   delete tempPtr;
   return item;
}


declarations:

Code
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

struct stack
{
   int value;
   stack *next;
};

stack *calcStack = NULL;

void push(int num);
int pop();
bool isEmpty();
void operation(char op);
void operation2(char op);



Not sure why it won't work? Obviously it doesn't point to anything because nothing has been pushed yet. How do you get pop to behave when there is nothing there yet?


Quote (Richter @ Sep 29 2012 04:27pm)
I guess you also want to deal with integers which are longer than just 1 digit? so taking 1 digit of an integer (which is as example 3 digits long) and converting each of those 'char' of the string to an integer is kind of... pointless...
you need a strategy... tell us possible inputs:
example:
1) 3 2 +
2) 13 5 -
3) 83 3 /
4) 54.3 4.2 *
5) 4
6) 2
7) /
8) 12.2
9) 4 1

I guess there are only integers, so number 8 and number 4 are not possible?
I guess that there is always a whitespace between the integers and the operators?
I guess that integers may be longer than 1 digit?
I guess that the program should do something after each 'newline'?



Here is an example input (professor wrote it)
Code
2 4 * 3 - p
5 2 4 + * p
5 u 4 - 3 / p
5 4 - u 2 * p
7 u 12 - u 3 4 - * 7 / p e


er.....crap. Now I have to deal with 2 digit ints? damn....


edit: let me sum up my strategy I guess:


I need to read in a line of inputs. They can be +, -. *. /, p, e , u or an integer

if its an integer, push it on the stack. If its an operator, perform the selected operator.

What I wanted to do was read a line of input up until a newline char was found.

Put that input into a string (I guess...)

check each element to see what it contains. If it contains an integer, use atoi (thought it would be easy, wasn't) and push it on the stack.

If it contains something else, check to see whether it was a mathematical op or if it was something else. Then perform those selected ops.

It would loop through user inputs until the end of file was reached.



This post was edited by Eep on Sep 29 2012 03:35pm
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Sep 29 2012 03:32pm
Code
struct stack
{
  int value;

clearly you need to use "stack->value" instead of "stack->val" ;)

edit:
how you separate the stuff which is separated by the whitespaces?

This post was edited by Richter on Sep 29 2012 03:38pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 29 2012 03:36pm
Quote (Richter @ Sep 29 2012 04:32pm)
Code
struct stack
{
  int value;

clearly you need to use "stack->value" instead of "stack->val" ;)


ugh thanks. I swear I tell myself I will do this things smarter everytime I tackle a new assignment, end up doing the same stupid stuff ><


edit:
how you separate the stuff which is separated by the whitespaces?

uhh, good question. I am not. Which I think might be one of the problems I am having right now.


Here is my code again


Code
int main()
{
   string userIn;
   int len;
   char temp[2];
   while(getline(cin,userIn,'\n'))
   {
       len = userIn.length();
       for(int i = 0; i<len; i++)
       {
           len = userIn.copy(temp,1,i);
           temp[len] = '\0';
           if(isdigit(userIn[i]))
           {
               push(atoi(temp));
           }
           else if(temp[1] == '+' ||temp[1] == '-' ||temp[1] == '*' ||temp[1] == '/')
           {
               operation(userIn[i]);
           }
           else
           {
               operation2(userIn[i]);
           }
       }
   }



return 0;
}


at least for main.

I realized 2 things:


1: not checking for white space
2: not sure if that while loop is working the way I think


need to figure these out.

This post was edited by Eep on Sep 29 2012 03:42pm
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Sep 29 2012 04:05pm
This finds the position (the pointer to there) of the first space in the string called a:
Code
char *p = strchr(a, ' ');

so your code could be built by something like this:
Code

char *p = a; //let p be a new pointer
while(length(p) > ...) //check if there are still chars in p (the first time p=a)
{
 char *p = strchr(a, ' '); //find first space in a
 if (NULL != p) //there was still a space
   *p = '\0'; //replace the space with a string terminator
 if (isdigit(a)) //everything before the first space, is it a digit?
   push(atoi(a))
 else
   performoperation(a)
 p++; //go 1 char to the right of the space
 a = p;
}
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll