// PrefixExpressionEvaluator.cpp : main project file.
#include "stdafx.h"
#include <stack>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace std;
template <class T>
bool from_string(T& t,
const std::string& s,
std::ios_base& (*f)(std::ios_base&))
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}
int main(array<System::String ^> ^args)
{
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
{
int value, value1, value2;
char token;
stack<int> s;
stack<int> aux;
stack<char> operators;
stack<char> flags;
std::string line;
std::string outputLine;
std::string intvalue;
getline(myfile, line); //read the next line of file and store into ‘line’
if ( line.length() == 0 )
continue;
for(int i=0 ; i < line.length() ; i++)
{
char token = line.operator [](i);
if ( token.Equals(' '))
{
if ( intvalue.length() > 0 )
{
int val = 0;
from_string<int>(val,intvalue,std::dec);
s.push(val);
intvalue.clear();
}
continue;
}
if(isdigit(token))
{
intvalue += token;
if ( i == (line.length() - 1) )
{
int val = 0;
from_string<int>(val,intvalue,std::dec);
s.push(val);
intvalue.clear();
}
}
else
{
operators.push(token);
}
}
while ( !s.empty() )
{
value = s.top();
s.pop();
aux.push(value);
}
value = 0;
while ( !operators.empty() )
{
token = operators.top();
operators.pop();
value2 = aux.top();
aux.pop();
value1 = aux.top();
aux.pop();
switch(token)
{
case '+': value = value1 + value2;
break;
case '-': value = value1 - value2;
break;
case '*': value = value1 * value2;
break;
case 'D': value = value1/value2;
break;
case 'M': value = value1%value2;
}
aux.push(value);
}
const char* str = (const char*)(Marshal::StringToHGlobalAnsi( String::Format("Prefix Value: {0} \n",value))).ToPointer();
outfile.write(str,String::Format("Prefix Value: {0} \n",value)->Length);
Console::WriteLine(String::Format("Prefix Value is : {0} ",value));
}
myfile.close();
outfile.close();
Console::ReadLine();
return 0;
}
pls help me with this...

New Topic/Question
Reply




MultiQuote


|