Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
//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" ){