I am starting to develop a calculator in C++ which accepts the expression in the form:
2+3 or
2/3 ,etc. ( for the time being, only two operands and one operator) Once i get that done, I will begin the process of continuely expanding it :)
The problem I am having is, that I read the book by Stroustrup, and there was an entire chap on it on Low level input. And since I have read that, my brain has gotten trapped and I continuely am adopting the same procedure to make my own program.

Here is the code:

#include<iostream>
using namespace std;

enum Type_exp{ PRINT,NUM, PLUS='+',MINUS='-',DIV='/',MUL='*',SPACE,END};
Type_exp curr_exp=END;
double num;
Type_exp getexp();
double oper();
int main()
{
    double ans;
    cout<<"Enter the expression to evaluate";
    ans=oper();
    cout<<endl<<endl<<ans;
    return 0;
}
Type_exp getexp()
{
     char ch=0;
     cin>>ch;
     switch(ch)
     {
      case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 0: 
           cin.putback(ch);
           cin>>num;
           return curr_exp=NUM;
      case '+': return curr_exp=PLUS;
      case '-': return curr_exp=MINUS;
      case '/': return curr_exp=DIV;
      case '*': return curr_exp=MUL;
      case '\n':
      case ';': return curr_exp=PRINT;
      }
}

double oper()
{
  getexp();
  double left;
  while(curr_exp!=PRINT)
  {
   switch(curr_exp)
   {
   case PRINT:
         return left;
    case NUM: 
      left=num;
      getexp();
      break;
    case PLUS:
      getexp();
      while(curr_exp==SPACE) 
       getexp();
      left+=num;
      getexp();
      break;
    case MINUS:
      getexp();
      while(curr_exp==SPACE)
       getexp();
      left-=num;
      getexp();
      break;
    case DIV:
     getexp();
     while(curr_exp==SPACE)
      getexp();
     left/=num;
      getexp();
      break;
    case MUL:
     getexp();
     while(curr_exp==SPACE)
      getexp();
     left*=num;
      getexp();
      break;
    }
  }
}

Any help would be greatly appreciated:)

Recommended Answers

All 6 Replies

I am starting to develop a calculator in C++ which accepts the expression in the form:
2+3 or
2/3 ,etc. ( for the time being, only two operands and one operator) Once i get that done, I will begin the process of continuely expanding it :)

Good idea.

The problem I am having is, that I read the book by Stroustrup, and there was an entire chap on it on Low level input. And since I have read that, my brain has gotten trapped and I continuely am adopting the same procedure to make my own program.

What the heck does that mean?

Are you another one that didn't read the post titled Read Me: Read This Before Posting? Don't leave it to us to figure out what's wrong. You need to tell us.

Good idea.

What the heck does that mean?

Are you another one that didn't read the post titled Read Me: Read This Before Posting? Don't leave it to us to figure out what's wrong. You need to tell us.

I guess i was in a hurry while writing this post, so i did mess up. Heading should have been Calculater in C++. Anyways, the code that i posted is not working and I can't seem to figure out whats going wrong. the same approach was used in the book, thats what i actually meant. I just twisted it a little to suit my needs.
I hope this clears things up. :)

The main problem is inputting the expression...I need help on how to input the expression without the need to press enter after each operand or operator...Any help would be gr8!

Input the whole expression as a string using the getline() method appropriate for the style of string you are using. Then parse the input expression into tokens using a stringstream. First create the stringstream by using the input string as a parameter to the stringstream constructor. Then use the >> operator to separate the tokens.

I guess i was in a hurry while writing this post, so i did mess up. Heading should have been Calculater in C++. Anyways, the code that i posted is not working and I can't seem to figure out whats going wrong. the same approach was used in the book, thats what i actually meant. I just twisted it a little to suit my needs.
I hope this clears things up. :)

No, not at all. "code that i posted is not working" tells us nothing at all. What is not working? Why do you think it's not working -- what are the clues? What is supposed to happen?

The main problem is inputting the expression...I need help on how to input the expression without the need to press enter after each operand or operator...Any help would be gr8!

Better. Lerner is correct. Input using getline() . If you can't use stringstream, look at each individual character and process it as you need (digits will become operands, nondigits should be operators)

Please note that case 1: case 2: case 3: case 4: case 5: don't do what you intend.
You want to use case '1':case '2': '1' '2' being chars.

Ok, I stand corrected. cin does parse numbers. case 1: will work. However it parses the entire number that will fit into a char.
input: 123
cin >> ch;
ch == 123, not 1

Also you are better off using an if/else if condition block rather than a switch() in the tokenizer. The primary reason for if/else if is the flexibility it offers when you need to read with peek ahead for more complex expressions. Consider "-1**2" and "2 - -1", etc.

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.