3 Replies - 3057 Views - Last Post: 17 March 2010 - 03:30 PM Rate Topic: -----

#1 maddy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-March 10

Prefix equation evaluation program

Posted 17 March 2010 - 03:15 PM

hi i have developed the following program but it ot seems to be right, pls help me out with this program for prefix evaluation, i'm new in programing..pls help me ....thanx in advance

#include "iostream"
#include "stack"
#include "string"
#include "stdafx.h"
#include "stdafx.cpp"
#include <fstream>
using namespace std;
void main()
{
int i;
int string; prefixExp;
char token;
int value, value1, value2;
stack<int> s; //Declare a stack of int
ifstream myfile;
ofstream outfile;
myfile.open("Prefix.txt", ios::in); //opening streams
outfile.open("output.txt", ios::out);
while (!myfile.eof()) //while not end of input file
{
 stack<char> operators;
 stack<char> flags;
 string line;
 string outputLine;
 getline(myfile, line); //read the next line of file and store into ‘line’
 while((i < prefixExp.size()) && (token != '='))
 {
  if(isdigit(token))
  {
   value = token - '0';
   s.push(value);
  }
  else
  {
   value2 = s.top();
   s.pop();
   value1 = s.top();
   s.pop();
   switch(token)
  {
  case '+': value = value1 + value2;
  break;
  case '-': value = value1 - value2;
  break;
  case '*': value = value1*value2;
  break;
  case '/': value = value1/value2;
  break;
  case 'M': value = value1%value2
  }
  s.push(value);
  }
  i++;
  token = prefixExp[i];
 }
  value = s.top();
  s.pop();
  cout << prefixExp << " " << value << endl;
  break;
}

This post has been edited by JackOfAllTrades: 23 March 2010 - 08:50 AM
Reason for edit:: Fixed code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Prefix equation evaluation program

#2 joesyuh  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 174
  • Joined: 30-September 08

Re: Prefix equation evaluation program

Posted 17 March 2010 - 03:20 PM

You need to add the [/code] to the end of your code so that it is formatted properly.

Also, what specific problem are you having? Does it compile? Does it throw an error? does it give unexpected results?

This post has been edited by joesyuh: 17 March 2010 - 03:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 maddy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-March 10

Re: Prefix equation evaluation program

Posted 17 March 2010 - 03:25 PM

View Postjoesyuh, on 17 March 2010 - 02:20 PM, said:

You need to add the [/code] to the end of your code so that it is formatted properly.

Also, what specific problem are you having? Does it compile? Does it throw an error? does it give unexpected results?

ya it gives an error....actually i'm developing a program for prefix evaluation using STL and ADT using stack..if u know the code for that so pls help me out...thanx in advance
Was This Post Helpful? 0
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Prefix equation evaluation program

Posted 17 March 2010 - 03:30 PM

int string; prefixExp;
You don't give a type to prefixExp. Do this: int string, prefixExp

Use int main() rather than void main() main always returns a value

int string

This conflicts with std::string, which you include.
That produces errors on these lines:
string line;
string outputLine;



Instead, prefix these with std:: (or change the int's name)
std::string line;
std::string outputLine;


while((i < prefixExp.size()) && (token != '='))

prefixExp is an int, and as such has no method named size() (or any methods at all for that matter)

break;
case 'M': value = value1%value2 //need semicolon
Missing semicolon here


token = prefixExp[i];

prefixExp is an int, not an int *


You are also missing a closing brace to end main
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1