5752116 [rkeene@sledge /home/rkeene/personal/school/comp-sci-ii/lab5]$ cat -n lab5.cpp
 1 /* Roy Keene
 2    CS 2314
 3    Section 04
 4    Lab 05
 5    08 Oct 02
 6    lab5.cpp
 7 
 8    Fully tested configurations:
 9         *MSVC++       6.0    i386    Windows XP
10         *Sun Workshop 6.0    sun4u   Solaris
11         *Intel        6.0    i386    Linux
12         *g++          3.0.3  sun4u   Solaris
13         *g++          3.0.2  ip27    IRIX
14         *g++          2.95.4 i386    FreeBSD
15         *g++          2.95.4 alpha   Linux
16         *g++          2.95.4 sun4u   Linux
17         *g++          2.95.4 powerpc Linux
18         *g++          2.95.4 armv4l  Linux
19         *g++          2.96   i386    Linux
20         *g++          2.95.3 i386    Linux
21         *g++          2.95.3 sun4u   Solaris
22         *c++          2.95.2 powerpc MacOS X
23         *g++          2.95.2 i386    Linux
24         *g++          2.8.1  sun4u   Solaris
25 */
26 
27 #include <iostream>
28 #include <fstream>
29 #include <stack>
30 #include <string>
31 
32 using namespace std;
33 #include "poly.h"
34 
35 int main(void) {
36     ifstream ifd;
37     stack<Poly *> exp_stack;
38     Poly *ret, *o1;
39     string oper;
40     unsigned char ocd;
41     int exp, coeff, n, i;
42 
43     ifd.open("poly.dat");
44     while (ifd >> oper) {
45         ocd=oper[0];
46         switch (ocd) {
47             case 'p':
48                 ifd >> n;
49                 ret=new Poly;
50                 for (i=0;i<n;i++) {
51                     ifd >> exp;
52                     ifd >> coeff;
53                     ret->insert_term(coeff, exp);
54                 }
55                 exp_stack.push(ret);
56                 break;
57             case '*':
58                 ret=exp_stack.top(); exp_stack.pop();
59                 o1=exp_stack.top(); exp_stack.pop();
60                 cout << ret->equ() << " * " << o1->equ();
61                 ret=o1->Mul(*ret);
62                 cout << " = " << ret->equ() << "\n";
63                 exp_stack.push(ret);
64                 delete o1;
65                 break;
66             case '+':
67                 ret=exp_stack.top(); exp_stack.pop();
68                 o1=exp_stack.top(); exp_stack.pop();
69                 cout << ret->equ() << " + " << o1->equ();
70                 ret=o1->Add(*ret);
71                 cout << " = " << ret->equ() << "\n";
72                 exp_stack.push(ret);
73                 delete o1;
74                 break;
75             case '-':
76                 ret=exp_stack.top(); exp_stack.pop();
77                 o1=exp_stack.top(); exp_stack.pop();
78                 cout << ret->equ() << " - " << o1->equ();
79                 ret=o1->Sub(*ret);
80                 cout << " = " << ret->equ() << "\n";
81                 exp_stack.push(ret);
82                 delete o1;
83                 break;
84             case 'e':
85                 ifd >> n;
86                 ret=exp_stack.top();
87                 cout << "At x=" << n << " " << ret->equ() << " = " << ret->Evaluate(n) << "\n";
88                 break;
89             default:
90                 cerr << "Unknown operator '"  << oper[0] << "'\n";
91         }
92     }
93     ifd.close();
94 
95     return(0);
96 }
5752117 [rkeene@sledge /home/rkeene/personal/school/comp-sci-ii/lab5]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2002-10-02 22:21:19