-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rparser.cpp
166 lines (147 loc) · 4.99 KB
/
Rparser.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* File: RParser.cpp
* Author: masil
*
* Created on October 2, 2016, 5:13 PM
*/
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include "Rparser.h"
using namespace std;
/*4 errors:
* Error: no nodes have their voltage set -- when solve is called
* Error: resistor 'name' not found -- modify/print/delete
* Error: resistor 'name' already exists -- adding a resistor
* Error: node 'id' not found // setting/unsetting
*/
Rparser::Rparser(){}
Rparser::~Rparser(){} //Destructor
void Rparser::parse(string in, NodeList& nodes){
string cmd, name; //Function variable declarations
double resistance;
int nodeIn[2];
double rOld; //Used in modifyR
stringstream ss(in);
ss >> cmd;
if(cmd=="insertR"){ //Function to insert a resistor
ss >> name;
if(nodes.getR(name)!=NULL){
cout << "Error: resistor " << name << " already exists" << endl;
ss.clear();
return;
}
ss >> resistance;
ss >> nodeIn[0];
ss >> nodeIn[1];
Resistor* temp1 = new Resistor(name,resistance,nodeIn);
Resistor* temp2 = new Resistor(name,resistance,nodeIn);
Node* n1 = nodes.findAddNode(nodeIn[0]);
Node* n2 = nodes.findAddNode(nodeIn[1]);
n1->addResistor(temp1);
n2->addResistor(temp2);
cout<<"Inserted: resistor "<<name<<" "<<setprecision(2)<<fixed<<
resistance<<" Ohms "<<nodeIn[0]<<" -> "<<nodeIn[1]<<endl;
}
else if(cmd=="modifyR"){ //Used to change an existing resistor's resistance
ss >> name;
int* points = nodes.getPoints(name);
if(points==NULL){
cout << "Error: resistor " << name << " not found" << endl;
ss.clear();
return;
}
Node* n1 = nodes.search(points[0]);
Node* n2 = nodes.search(points[1]);
ss >> resistance;
rOld = n1->modifyR(name,resistance);
n2->modifyR(name,resistance);
cout<<"Modified: resistor "<<name<<" from "<<setprecision(2)<<fixed<<rOld
<< " Ohms to " <<setprecision(2)<<fixed<<resistance<< " Ohms" << endl;
}
else if(cmd=="printR"){
ss >> name;
Resistor* temp = nodes.getR(name);
if(temp==NULL){
cout << "Error: resistor " << name << " not found" << endl;
ss.clear();
return;
}
cout << "Print:" << endl;
temp->print();
}
else if(cmd=="printNode"){ //Print a single node, or all nodes
ss >> name;
if(name=="all"){
cout << "Print:" << endl;
nodes.printAll();
return;
}
istringstream caster(name); //cast the str input to an int
caster >> nodeIn[0];
Node* temp = nodes.search(nodeIn[0]);
if(temp==NULL){
cout << "Error: node " << nodeIn[0] << " not found" << endl;
ss.clear();
return;
}
cout << "Print:" << endl; //Print a single node
temp->print();
}
else if(cmd=="deleteR"){
ss >> name;
if(name=="all"){
cout<<"Deleted: all resistors"<<endl;
nodes.clear();
return;
}
int* points = nodes.getPoints(name);
if(points==NULL){
cout << "Error: resistor " << name << " not found" << endl;
ss.clear();
return;
}
Node* n1 = nodes.search(points[0]);
Node* n2 = nodes.search(points[1]);
n1->deleteR(name);
n2->deleteR(name);
cout << "Deleted: resistor " << name << endl;
}
else if(cmd=="setV"){ //Make set = true, update its voltage
ss >> nodeIn[0];
Node* temp = nodes.search(nodeIn[0]);
if(temp==NULL){
cout << "Error: node " << nodeIn[0] << " not found" << endl;
ss.clear();
return;
}
double voltage;
ss >> voltage;
temp->setV(voltage);
temp->forceSet();
cout << "Set: node " << nodeIn[0] << " to "<<voltage<<" Volts" << endl;
}
else if(cmd=="unsetV"){ //Make set = false, voltage is unknown
ss >> nodeIn[0];
Node* temp = nodes.search(nodeIn[0]);
if(temp==NULL){
cout << "Error: node " << nodeIn[0] << " not found" << endl;
ss.clear();
return;
}
temp->unsetV();
cout << "Unset: the solver will determine the voltage of node "
<< nodeIn[0] << endl;
}
else if(cmd=="solve"){ //Solve for all with set = false
if(nodes.unknown()){
cout << "Error: no nodes have their voltage set" << endl;
ss.clear();
return;
}
nodes.solve();
}
ss.clear();
}