Advertisement
Guest User

just incase

a guest
Oct 2nd, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. #define MAX_NODE_NUMBER 5000
  8. #define MIN_NODE_NUMBER 0
  9.  
  10. void insertR (stringstream &insertStream);
  11. void modifyR(stringstream &modStream);
  12. void printR(stringstream &printStream);
  13. void printNode(stringstream &pNodeStream);
  14. void deleteR(stringstream &dStream);
  15. bool tooFewArgs(stringstream &stream);
  16. bool tooManyArgs(stringstream &extraStream);
  17.  
  18. int main(){
  19.     string userIn, command;
  20.    
  21.     cout <<"> ";
  22.     getline(cin, userIn);
  23.     //recieves line from keyboard and puts the whole thing in string userIn
  24.    
  25.     while(!cin.eof()){
  26.         //before user enters ^d
  27.         stringstream userInStream(userIn);
  28.         userInStream >> command;
  29.         //my user input stream will read the first command that the user entered
  30.      
  31.         if (command == "insertR"){
  32.             insertR(userInStream);
  33.         }
  34.         else if(command == "modifyR"){
  35.             modifyR(userInStream);
  36.         }
  37.         else if(command == "printR"){
  38.             printR(userInStream);
  39.         }
  40.         else if(command == "printNode"){
  41.             printNode(userInStream);
  42.         }
  43.         else if(command == "deleteR"){
  44.             deleteR(userInStream);
  45.         }
  46.         else{
  47.             cout << "Error: invalid command";
  48.         }
  49.         cout <<"> ";
  50.         getline(cin,userIn);
  51.     }//end while(more)
  52.     return 0;
  53. }//end main
  54.            
  55.            
  56. void insertR (stringstream &insertStream){
  57.     string name;
  58.     double resistance;
  59.     int nodeId1;
  60.     int nodeId2;
  61.    
  62.     insertStream >> name;
  63.    
  64.     if(tooFewArgs(insertStream))return;
  65.    
  66.     else if(name == "all"){
  67.         cout << "Error: resistor name cannot be the keyword \"all\"";
  68.     }
  69.     else{
  70.         insertStream >> resistance;
  71.        
  72.         if(tooFewArgs(insertStream))return;
  73.        
  74.         else if(resistance < 0.0){
  75.             cout << "Error: negative resistance";
  76.         }
  77.         else {
  78.             insertStream >> nodeId1;
  79.            
  80.             if(tooFewArgs(insertStream))return;
  81.            
  82.             else if((nodeId1 < 0) || (nodeId1 > 5000)){
  83.                 cout << "node" << nodeId1 << "is out of permitted range" <<"0-"<<MAX_NODE_NUMBER;  
  84.             }
  85.             else {
  86.                 insertStream >> nodeId2;
  87.                 if(tooFewArgs(insertStream))return;
  88.                 else if((nodeId2 < 0) || (nodeId2 > 5000)){
  89.                     cout << "Error: node " << nodeId2 << " is out of permitted range " <<"0-"<<MAX_NODE_NUMBER;  
  90.                 }
  91.                 else if(nodeId2 == nodeId1){
  92.                     cout << "Error: both terminals of resistor connect to node"<<nodeId2;
  93.                 }
  94.                 else if(tooManyArgs(insertStream))return;
  95.                 else {
  96.                     cout << "Inserted: resistor "<<name<<" "<<setprecision(2)<<fixed<<resistance<<" Ohms "<< nodeId1<<" -> "<< nodeId2              <<endl;
  97.                 }//printed message
  98.             }//end nodeId2 check
  99.         }//end nodeId1 check
  100.     }//end resistance check
  101. }//end of insertR    
  102.            
  103.            
  104. void modifyR(stringstream &modStream){
  105.     string modName;
  106.     double modResistance;
  107.     modStream >> modName;
  108.    
  109.     if(tooFewArgs(modStream))return;
  110.    
  111.     else if(modName == "all"){
  112.         cout << "Error: resistor name cannot be the keyword \"all\"";
  113.     }
  114.     else {
  115.         modStream >> modResistance;
  116.         if(tooFewArgs(modStream))return;
  117.        
  118.         else if(modResistance < 0.0){
  119.             cout << "Error: negative resistance";
  120.         }
  121.         else if(tooManyArgs(modStream))return;
  122.         else{
  123.             cout << "Modified: resistor "<< modName<<" to "<<setprecision(2)<<fixed<<modResistance<<" Ohms"<<endl;
  124.         }//printed message
  125.     }//end resistance check
  126. }//end modR
  127.  
  128. void printR(stringstream &printStream){
  129.    
  130.     string printName;
  131.    
  132.     printStream >> printName;
  133.    
  134.     if(tooFewArgs(printStream))return;
  135.    
  136.     else if(printName == "all"){
  137.         cout << "Print: all resistors";
  138.     }
  139.    
  140.     if(tooManyArgs(printStream))return;
  141.    
  142.     else {
  143.         cout <<"Print: resistor "<< printName<<endl;
  144.     }  
  145. }//end printR
  146.  //fix printNode
  147. void printNode(stringstream &pNodeStream){
  148.    
  149.     int nodeId;
  150.     string all;
  151.    
  152.     pNodeStream >> nodeId;
  153.        
  154.         if(pNodeStream.fail()){
  155.             if(pNodeStream.eof()){
  156.                 pNodeStream.clear();
  157.                 cout << "Error: too few arguments"<<endl;
  158.             }
  159.             else{
  160.                 pNodeStream >> all;
  161.                 if(all == "all"){
  162.                     cout << "Print: all nodes";
  163.                 }
  164.                 else{
  165.                     cout << "Error: invalid argument";
  166.                     cin.ignore(1000,'\n');
  167.                     //skip 1000 things or until you get to a space
  168.                     cin.clear();
  169.                     return;
  170.                 }
  171.             }
  172.         }
  173.         else{
  174.             cout << "Print: node "<< nodeId<<endl;
  175.         }//end print statement
  176. }//end printNode
  177.  
  178. void deleteR(stringstream &dStream){
  179.    
  180.     string deleteName;
  181.      
  182.     dStream >> deleteName;
  183.    
  184.     if(tooFewArgs(dStream))return;
  185.    
  186.     if(tooManyArgs(dStream))return;
  187.    
  188.     else if(deleteName == "all"){
  189.         cout << "Deleted: all resistors"<<endl;
  190.     }
  191.     else {
  192.         cout << "Deleted: resistor "<< deleteName<<endl;
  193.     }
  194. }
  195.  
  196. bool tooFewArgs(stringstream &stream){
  197.     if(stream.fail()){
  198.         if(stream.eof()){
  199.             stream.clear();
  200.             cout << "Error: too few arguments"<<endl;
  201.             return true;
  202.         }
  203.         else{
  204.             cout << "Error: invalid argument";
  205.             cin.ignore(1000,'\n');
  206.             //skip 1000 things or until you get to a space
  207.             cin.clear();
  208.             return false;
  209.         }
  210.     }
  211. }
  212. bool tooManyArgs(stringstream &extraStream){
  213.    
  214.     string extraStuff;
  215.    
  216.    
  217.     extraStream >> extraStuff;
  218.    
  219.     if(extraStream.fail()){
  220.         if(extraStream.eof()){
  221.             extraStream.clear();
  222.             return false;
  223.         }
  224.     }
  225.    
  226.     else{
  227.         cout << "Error: too many arguments"<<endl;
  228.         return true;
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement