-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
128 lines (115 loc) · 3.13 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Nathan Musial 11/15/16
#include <iostream>
#include "LinkedList.h"
#include "LinkedList.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <ctype.h>
#include <cmath>
#include <iomanip>
using namespace std;
void readInFile(string,LinkedList);
int main()
{
LinkedList l; //linked list that hold the equation
readInFile("poly.txt",l); //reads in file
}
void readInFile(string filename,LinkedList l)
{
ifstream file;
ofstream outfile;
outfile.open("answers.txt"); //open output file
file.open(filename,ios::binary);
if(!file)
{
cout<< "can't open file";
return;
}
string line="";
double fnum=0;
double exp=0;
char beg=' ';
double coef;
// bool negative=false;
while(getline(file,line)) //goes through line by line
{
stringstream ss(line);
ss.ignore(2,'(');
ss >> fnum;
ss.ignore(2,'='); // ignores up to equal sign
// char c;
while(ss)
{
exp=0;
coef=0;
beg=' ';
ss >> beg; //checks first charactewre
if(beg==' ') // could break reading in file
break;
if(isdigit(beg)) //if first charcter is a digit
{
ss.seekg(-1,ios::cur);
ss >> coef;
}
else if(beg=='x') // if no coeficient
coef=1;
else if(beg=='-') // if negative in front
{
char test=ss.get();
//cout<<" Test "<<test <<endl;
if(ss.get()=='x'&&!isdigit(test)) // edge case for negative
{
ss.seekg(-1,ios::cur);
coef=-1;
}
else // normal negative
{
ss.seekg(-1,ios::cur);
if(isdigit(test))
ss.seekg(-1,ios::cur);
ss >> coef;
coef=coef*-1;
}
//coef=coef*-1;
}
else if(beg=='+') // if plus in front
{
ss.get();
if(ss.get()=='x') //if it is only X
{
ss.seekg(-1,ios::cur);
coef=1;
}
else //read in coefficient
{
ss.seekg(-1,ios::cur);
ss >> coef;
}
}
if(ss.get()=='x') //if there is an exponent
{
if(ss.get()=='^')
ss >> exp;
else
exp=1;
}
else{
exp=0; //no ^ so exponent is zero
ss.get();
}
l.AddNode(coef,exp); // adds node
// cout <<fnum << " "<<beg<< " " <<coef <<" "<<exp <<endl;
}
// cout<<"f("<<fnum<<") =";
// l.PrintToScreen();
l.PrintToFile(fnum, outfile);
// cout <<"= "<< fixed << setprecision(3)<< l.evaluateList(fnum) << setprecision(6) << endl;
// cout.unsetf(ios_base::fixed);
l.deleteList(); // deltes list to use again
// cout << line<< endl;
}
outfile.close();
file.close();
}