예제 #1
0
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;
}
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" ){