//prints out the resistor information void Node::printR() const{ Resistor *p = head; while (p != NULL){ p->printRes(); p = p->getNext(); } }
void spanResetWires(Pin* source){ if (source == NULL) return; if (source->visited()) return; source->visited(true); Wire *wire = source->wire(); if (!wire) return; //cout <<"spanResetWires wire: "<< wire->info()<<endl; wire->voltage(false); list<Pin *>pins = wire->pins(); list<Pin *>::iterator it; for (it = pins.begin(); it != pins.end(); it++){ Pin *pin = (*it); if (pin == source) continue; pin->visited(true); Element *element = pin->element(); if (element->type() == "switch"){ Switch *switc = (Switch *)element; Pin *nextPin = switc->outPin(pin); spanResetWires(nextPin); } else if (element->type() == "resistor"){ Resistor *resistor = (Resistor *)element; Pin *nextPin = resistor->outPin(pin); spanResetWires(nextPin); } else if (element->type() == "bridge"){ Bridge *bridge = (Bridge *)element; Pin *nextPin = bridge->outPin(pin); spanResetWires(nextPin); } } }
int resetVisited(Element *e){ if (e->type() == "source"){ Source *s = (Source *)e; s->pin()->visited(false); } else if (e->type() == "ground"){ Ground *g = (Ground *)e; g->pin()->visited(false); } else if (e->type() == "resistor"){ Resistor *r = (Resistor *)e; r->pin("p0")->visited(false); r->pin("p1")->visited(false); } else if (e->type() == "switch"){ Switch *r = (Switch *)e; r->pin("p0")->visited(false); r->pin("p1")->visited(false); r->pin("in")->visited(false); } else if (e->type() == "bridge"){ Bridge *b = (Bridge *)e; b->in()->visited(false); b->out()->visited(false); } else if (e->type() == "meter"){ Meter *g = (Meter *)e; g->pin()->visited(false); } else{ cerr << "Unknown element: "<< e->info()<<endl; } return 0; }
void activateResistors(list<Resistor *>resistors){ list<Resistor *>::iterator rit; for (rit = resistors.begin(); rit != resistors.end(); rit++){ Resistor *resistor = (*rit); resistor->isActive(true); debug("activated resistor: "<<resistor->fullName()); } }
int deactivateResistorAndResetVisited(Element *e){ //int deactivateResistor(Element *e){ if (e->type() == "resistor"){ Resistor *r = (Resistor *)e; r->isActive(false); } resetVisited(e); return 0; }
void ResistorList::insert_resistor_at_end() { Resistor *ptr = NULL; Resistor *temp; for(ptr = head; ptr !=NULL; ptr = ptr->get_resistor_next()) { } ptr->set_next_resistor(temp); temp->set_next_resistor(NULL); }
bool Node::searchResistorandChange(string Label, double _resistance) { Resistor *p = head; while (p != NULL){ //if it finds it if (p->getName() == Label){ p->setResistance(_resistance); return true; } p = p->getNext(); } return false; }
bool Node::isUniqueResName(string _resName){ Resistor *currRes = connectedResistors->getHead(); while(currRes != NULL){ if(currRes->getName() == _resName){ return false; } else currRes = currRes->getNext(); } //There wasn't any other resistor with the same name return true; }
Resistor* Node::findResistor(string Label){ Resistor *p = head; while (p != NULL){ //if it finds it if (p->getName() == Label){ //return a pointer to the resistor if it was found return p; } p = p->getNext(); } //return null if not found return NULL; }
//void spanResistors(Pin *source, list<Wire *>wires){ void spanResistors(Pin *source, list<Resistor *>resistors){ //cout <<"entering spanResistors"<<endl; /* 1. try to reach the end. * 2. if end is reached, must go back and turn all wires on in the path */ if (source == NULL) return; if (source->visited()) return; source->visited(true); //debug("spanResistors source pin: "<< source->fullName()); Wire *wire = source->wire(); if (!wire) return; //debug("spanResistors wire: "<< wire->name()); list<Pin *>pins = wire->pins(); list<Pin *>::iterator it; for (it = pins.begin(); it != pins.end(); it++){ Pin *pin = (*it); if (pin == source) continue; pin->visited(true); //get element for source Element *element = pin->element(); if (element->type() == "source"){ //cerr << "Encountered source "<< element->info() <<" while completing circuit."<<endl; return; } else if (element->type() == "ground"){ //cout <<"ground reached: "<<endl; //end reached. activateResistors(resistors); //upWires(wires); } else if (element->type() == "switch"){ Switch *switc = (Switch *)element; if (!switc->isOn()) continue; Pin *nextPin = switc->outPin(pin); spanResistors(nextPin,resistors); } else if (element->type() == "resistor"){ Resistor *resistor = (Resistor *)element; resistors.push_back(resistor); Pin *nextPin = resistor->outPin(pin); spanResistors(nextPin, resistors); } else if (element->type() == "bridge"){ Bridge *bridge = (Bridge *)element; Pin *nextPin = bridge->outPin(pin); spanResistors(nextPin, resistors); } } }
// Returns the resistor that has the given name. // Puts the previous resistor in "previous". Puts NULL in "previous" if the // given name is the first resistor in the list. Resistor* ResistorList::find_resistor(string name, Resistor*& previous) { Resistor *ptr = NULL; previous = NULL; for(ptr = head; ptr !=NULL; ptr = ptr->get_resistor_next()) { if(name == ptr->getName()) { return ptr; } previous = ptr; } return NULL; }
//insert a resistor at the end of the list void Node::insertRes(string _name,double _resistance,int _endpoints[2]){ // now we either have p pointing to the node that already exists, or false if that doesnt exist // if it isnt NULL, resistor with that value exists if (head == NULL){ head = new Resistor(_name, _resistance, _endpoints); } else { //header is not null Resistor* pointer = head; while(pointer->getNext() != NULL){ pointer = pointer->getNext(); } //create the new Resistor //set the last element in our current list's next equal to n, the new resistor pointer->setNext(new Resistor(_name, _resistance, _endpoints)); } }
void Node::print(){ //If this function was called, we know that the node is not empty. //Therefore, no need to check if NULL Resistor *currRes = connectedResistors->getHead(); while (currRes != NULL){ //For the find command, parser_find already says "Found: label....." cout <<setw(MAX_RESISTOR_NAME_LEN)<<currRes->getName()<<" "<<setw(7)<<currRes->getResistance()<<" Ohms " <<currRes->getNodeID(1)<<" "<<currRes->getNodeID(2)<<endl; currRes = currRes->getNext(); } return; }
//crash if any resistance is 0 //helping function for solve - gets the new voltage double NodeList:: solveHelper(Node * temp) { Resistor *ptr = temp->getResistorList().getHead(); int nodeid = temp->nodeId; double value1 = 0; // 1/ (1/Ra + 1/Rb + 1/Rc +.....) double value2 = 0; // V1/Ra + V2/Rb + V3/Rc + .... while (ptr != NULL) { //getting the different end node of the resistor if (ptr->getEndpoints(0) == nodeid) value2 = value2 + (this->findOrInsert(ptr->getEndpoints(1))->voltage)/ptr->getResistance(); else if (ptr->getEndpoints(1) == nodeid) value2 = value2 + (this->findOrInsert(ptr->getEndpoints(0))->voltage)/ptr->getResistance(); // adding all 1/resistors value1 = value1 + (1/(ptr->getResistance())); ptr = ptr->getNext(); } return ((1/value1)*value2); }
int Rparser (void){ string line, command; int wCount; Node *nodeList=NULL; //start cout << "> "; getline(cin, line); //Infinite loop while(!cin.eof()){ //Get the line put it in //how many words are there wCount = word_per_line(line); stringstream lineStream (line); lineStream >> command; if(command == "insertR"){ if(wCount > INSERT_VARIABLE_COUNT) too_many_arguments(); else if(wCount < INSERT_VARIABLE_COUNT) too_few_arguments(); else{ string rName; double res; int node1, node2; //get resistor name lineStream >> rName; //check if we have the same name bool sameResName = false; if(nodeList==NULL) sameResName=false; else{ sameResName=findResName(nodeList, rName); } if(rName == "all"){ //error reserved word resistor_name_all_error(); }else if(sameResName){ //rName already exists resistor_name_exists_error(rName); }else{ //rName is okay //get resistance value lineStream >> res; if(!(lineStream.peek() == ' ')){ //error: invalid type invalid_argument(); }else if(res < 0){ //error: res is negative negative_resistance(); }else{ //res value is okay lineStream >> node1; if(!(lineStream.peek() == ' ')){ //error: invalid type invalid_argument(); }else{ lineStream >> node2; //checking if we have the nodes bool haveNode1=false; bool haveNode2=false; if(nodeList==NULL){ haveNode1=false; haveNode2=false; }else{ Node *temp = nodeList; while(temp != NULL){ if(temp->getId() == node1) haveNode1 = true; if(temp->getId() == node2) haveNode2 = true; temp = temp->getNext(); } } if(!haveNode1){ //creating node1 Node *newNode = new Node(node1); nodeList->sortedInsert(nodeList, newNode); haveNode1=true; } if(!haveNode2){ //creating node2 Node *newNode = new Node(node2); nodeList->sortedInsert(nodeList, newNode); haveNode2 = true; } if(node1 == node2){ //error: node numbers are same same_node_numbers(node1); }else if(haveNode1 && haveNode2){ //nodes are created int endpoints[2]; endpoints[0] = node1; endpoints[1] = node2; Resistor *newResistor1 = new Resistor(rName, res, endpoints); Resistor *newResistor2 = new Resistor(rName, res, endpoints); //add resistor to node1 bool found=false; Node *temp = nodeList; while (temp != NULL && !found ){ if(temp->getId()== node1){ found = true; temp->updateResNum(); temp->addResistor(newResistor1); }else{ temp = temp->getNext(); } } //add resistor to node2 found = false; temp=nodeList; while (temp != NULL && !found ){ if(temp->getId()==node2){ found = true; temp->updateResNum(); temp->addResistor(newResistor2); }else{ temp = temp->getNext(); } } insert_resistor(rName, res, node1, node2); } } } } } }//END OF INSERTR else if ( command == "modifyR"){ if(wCount > MODIFY_VARIABLE_COUNT) too_many_arguments(); else if(wCount < MODIFY_VARIABLE_COUNT) too_few_arguments(); else{ string rName; //get resistor name lineStream >> rName; double res; if(rName == "all"){ //error reserved word resistor_name_all_error(); }else{ //rName is okay //get resistance value lineStream >> res; if(res < 0){ //error: res is negative negative_resistance(); }else{ //res value is okay ///MODIFY FUNC IS OKAY double resbefore; //look for resistor in the array int found = 0; Node *temp = nodeList; while(temp != NULL && found<2){ Resistor *currentResList = temp->getResList(); while (currentResList != NULL && found<2){ if(currentResList->getName() == rName){ found++; resbefore = currentResList->getResistance(); currentResList->setResistance(res); } currentResList = currentResList->getNext(); } temp = temp->getNext(); } //print out messages if(found) modify_resistor(rName,resbefore,res); else resistor_name_not_found(rName); } } } }//END OF MODIFYR else if( command == "printR" ){
int Wire::initSpeed() { if(this->speed!=0) { return this->speed; } if(this->connected1!=NULL && this->connected1->getParentElement()->getName()=="Ground") { setSpeed(0); return 0; } if(this->connected1!=NULL) { if(connected1->getParentElement()->getName()=="Res") { Resistor* resTemp = (Resistor*)connected1->getParentElement(); double tmpPotential = resTemp->getAnotherWire(this)->getPotential(); if(tmpPotential>this->potential) { double i = abs(tmpPotential-this->potential)*resTemp->getValue(); //i%=10; this->setSpeed((int)i); return i; } else { double i = abs(tmpPotential-this->potential)*resTemp->getValue(); //i%=10; this->setSpeed((int)-i); return i; } } else if (connected1->getParentElement()->getName()=="Emf") { if(this->connected2==NULL) { EMF* emfTemp = (EMF*)connected1->getParentElement(); int direction = emfTemp->getEmfDirection(this); QList<Wire*>::Iterator i; for(i=this->wires->begin();i!=wires->end();i++) { if((*i)==this) { continue; } this->speed+=(*i)->initSpeed(); } this->speed*direction; return speed; } } } if(this->connected2!=NULL) { if(connected2->getParentElement()->getName()=="Res") { Resistor* resTemp = (Resistor*)connected2->getParentElement(); double tmpPotential = resTemp->getAnotherWire(this)->getPotential(); if(tmpPotential>this->potential) { double i = abs(tmpPotential-this->potential)*resTemp->getValue(); //i%=10; this->setSpeed((int)-i); return i; } else { double i = abs(tmpPotential-this->potential)*resTemp->getValue(); //i%=10; this->setSpeed((int)i); return i; } } else if (connected2->getParentElement()->getName()=="Emf") { if(this->connected1==NULL) { EMF* emfTemp = (EMF*)connected2->getParentElement(); int direction = emfTemp->getEmfDirection(this); QList<Wire*>::Iterator i; for(i=this->wires->begin();i!=wires->end();i++) { if((*i)==this) { continue; } this->speed+=(*i)->initSpeed(); } this->speed*direction; return speed; } } } }
Resistor operator*(const int n, const Resistor& r){ if (n < 1) throw "CGQ956"; return Resistor(r.getR() * n); }
int spanVoltage(Pin* source, bool voltage){ if (source == NULL) return 0; if (source->visited()) return 0; source->visited(true); Wire *wire = source->wire(); if (!wire) { return 0; //have separate audit if you want to check connectivity cerr << "source has no wire. source: " << source->name() <<endl; return 20; } /* if (wire->visited()){ bool wireVoltage = wire->voltage(); if (wireVoltage != voltage){ cerr << "inconsistent voltage found while updating voltages on wire: " << wire->info() <<endl; return 21; } return 0; }*/ //cout <<"spanVoltage setting wire: "<< wire->info()<< " to voltage: "<<voltage<<endl; wire->voltage(voltage); list<Pin *>pins = wire->pins(); list<Pin *>::iterator it; for (it = pins.begin(); it != pins.end(); it++){ Pin *pin = (*it); //cout <<"spanvoltage pin name: "<<pin->name()<<endl; if (pin == source) continue; pin->visited(true); Element *element = pin->element(); if (element->type() == "source"){ Source *source = (Source *)element; //warning? } else if(element->type() == "ground"){ bool gv = pin->wire()->voltage(); if (gv){ cerr << "error: voltage at ground is high. " << pin->wire()->info()<<endl; return 24; } } else if (element->type() == "switch"){ Switch *switc = (Switch *)element; if (!switc->isOn()) continue; Pin *nextPin = switc->outPin(pin); int rVal = spanVoltage(nextPin, voltage); if (rVal != 0) return rVal; } else if (element->type() == "resistor"){ Resistor *resistor = (Resistor *)element; Pin *nextPin = resistor->outPin(pin); bool nextVoltage = voltage; if (resistor->isActive()) nextVoltage = false; int rVal = spanVoltage(nextPin, nextVoltage); if (rVal != 0) return rVal; } else if (element->type() == "bridge"){ Bridge *bridge = (Bridge *)element; Pin *nextPin = bridge->outPin(pin); int rVal = spanVoltage(nextPin, voltage); if (rVal != 0) return rVal; } } return 0; }