//brian jahns
//cs252 1999
//driver for the Infinateint class, uses Node and List

#include <iostream.h>
#include "Node.h"
#include "List.h"
#include "Infinateint.h"

int main()
{
Infinateint int1;
Infinateint int2;

cout<<"enter int1:"<<endl;
cin>>int1;

cout<<"enter int2:"<<endl;
cin>>int2;

cout<<"int1="<<endl;
cout<<int1;
cout<<"int2="<<endl;
cout<<int2;

cout<<"sum is="<<endl;
cout<< int1+int2<<endl;
cout<<"diff is="<<endl;
cout<<int1-int2<<endl;

return 0;
}
#include <iostream.h>
#include <fstream.h>
#include <ctype.h>
#include "Node.h"
#include "List.h"
#include "Infinateint.h"
#include "Stack.h"

//function declarations
Infinateint eval(istream& is);
void getoperand(istream& is, Stack<Infinateint>& datastack, 
Stack<char>& opstack);
Infinateint evaloperator(istream& is, Stack<Infinateint>& datastack, 
Stack<char>& opstack);
void evalnextop(Stack<Infinateint>& datastack, Stack<char>& opstack);
bool isnextopenparen(istream& is);
bool isnextclosedparen(istream& is);

int main()
{
ifstream bob;
char name[33], buf[1024];
Infinateint accum;

cout<<"enter filename: ";
cin>>name;
cout<<endl;

bob.open(name);
if(bob.bad())
cerr<<"ground control to major tom: bob is bad, i repeat, bob is bad"
<<endl;

accum=eval(bob);
cout<<"result = "<<accum<<endl;

bob.close();

return 0;
}

Infinateint eval(istream& is)
{
Stack<Infinateint> datastack;
Stack<char> opstack;
Infinateint result;
char c;

c=is.peek();
while (isdigit(c))
{
cout<<"in eval"<<endl;
getoperand(is, datastack, opstack);
cout<<"after getoperand"<<endl;
result=evaloperator(is, datastack, opstack);
if (datastack.isempty())
return result;
c=is.peek();
}
return result;
}

void getoperand(istream& is, Stack<Infinateint>& datastack, 
Stack<char>& opstack)
{
Infinateint d;
char e;

while(isnextopenparen(is))
{
is.get();
opstack.push('(');
}
is>>d;
cout<<"upi read into d"<<endl;
datastack.push(d);
cout<<"d is pushed"<<endl;
while(isnextclosedparen(is))
{
cout<<"in while isnextclosed is"<<endl;
is.get();
while(opstack.isempty())
{
cout<<"in while not opstack.isempty"<<endl;
opstack.gettop(e);
if (e!='(')
evalnextop(datastack, opstack);
else
{
opstack.pop();
break;
}
}
}
}

Infinateint evaloperator(istream& is, Stack<Infinateint>& datastack, 
Stack<char>& opstack)
{
char ch, e;
Infinateint result;

is>>ch;
if(('ch'=='+')||('ch'=='-'))
{
while((!opstack.isempty())&&(opstack.gettop(e)&&e!='('))
opstack.push(ch);
}
else if('ch'=='*')
{
while((!opstack.isempty())&&(opstack.gettop(e)&&e=='*'))
{
evalnextop(datastack, opstack);
opstack.push(ch);
}
}
else if('ch'=='=')
{
while(!opstack.isempty())
evalnextop(datastack, opstack);
}
else
cerr<<"Ground control to major Tom: error, operator not recognized"<<endl;

datastack.gettop(result);
return result; 
}

void evalnextop(Stack<Infinateint>& datastack, Stack<char>& opstack)
{
Infinateint d1, d2;
char e;

opstack.gettop(e);
cout<<"e= "<<e<<endl;
switch(e)

case '+': 
datastack.pop(d2);
datastack.pop(d1);
datastack.push(d1+d2);
break;

case '-':
datastack.pop(d2);
datastack.pop(d1);
datastack.push(d1-d2);
break; 

case '*':
datastack.pop(d2);
datastack.pop(d1);
datastack.push(d1*d2);
break; 

default:
cerr<<"weve got an error in the switch of evalnextop"<<endl;
return;
}
}

bool isnextopenparen(istream& is)
{
int next=is.peek();
while((next!=EOF)&&(next<=' '))
{
is.get();
next=is.peek();
}
return(is.peek()=='(');
}

bool isnextclosedparen(istream& is)
{
int next=is.peek();
while((next!=EOF)&&(next<=' '))
{
is.get();
next=is.peek();
}
return (is.peek()!=')');
}