Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <iomanip>
- using namespace std;
- #define MAX_NODE_NUMBER 5000
- #define MIN_NODE_NUMBER 0
- void insertR (stringstream &insertStream);
- void modifyR(stringstream &modStream);
- void printR(stringstream &printStream);
- void printNode(stringstream &pNodeStream);
- void deleteR(stringstream &dStream);
- bool tooFewArgs(stringstream &stream);
- bool tooManyArgs(stringstream &extraStream);
- int main(){
- string userIn, command;
- cout <<"> ";
- getline(cin, userIn);
- //recieves line from keyboard and puts the whole thing in string userIn
- while(!cin.eof()){
- //before user enters ^d
- stringstream userInStream(userIn);
- userInStream >> command;
- //my user input stream will read the first command that the user entered
- if (command == "insertR"){
- insertR(userInStream);
- }
- else if(command == "modifyR"){
- modifyR(userInStream);
- }
- else if(command == "printR"){
- printR(userInStream);
- }
- else if(command == "printNode"){
- printNode(userInStream);
- }
- else if(command == "deleteR"){
- deleteR(userInStream);
- }
- else{
- cout << "Error: invalid command";
- }
- cout <<"> ";
- getline(cin,userIn);
- }//end while(more)
- return 0;
- }//end main
- void insertR (stringstream &insertStream){
- string name;
- double resistance;
- int nodeId1;
- int nodeId2;
- insertStream >> name;
- if(tooFewArgs(insertStream))return;
- else if(name == "all"){
- cout << "Error: resistor name cannot be the keyword \"all\"";
- }
- else{
- insertStream >> resistance;
- if(tooFewArgs(insertStream))return;
- else if(resistance < 0.0){
- cout << "Error: negative resistance";
- }
- else {
- insertStream >> nodeId1;
- if(tooFewArgs(insertStream))return;
- else if((nodeId1 < 0) || (nodeId1 > 5000)){
- cout << "node" << nodeId1 << "is out of permitted range" <<"0-"<<MAX_NODE_NUMBER;
- }
- else {
- insertStream >> nodeId2;
- if(tooFewArgs(insertStream))return;
- else if((nodeId2 < 0) || (nodeId2 > 5000)){
- cout << "Error: node " << nodeId2 << " is out of permitted range " <<"0-"<<MAX_NODE_NUMBER;
- }
- else if(nodeId2 == nodeId1){
- cout << "Error: both terminals of resistor connect to node"<<nodeId2;
- }
- else if(tooManyArgs(insertStream))return;
- else {
- cout << "Inserted: resistor "<<name<<" "<<setprecision(2)<<fixed<<resistance<<" Ohms "<< nodeId1<<" -> "<< nodeId2 <<endl;
- }//printed message
- }//end nodeId2 check
- }//end nodeId1 check
- }//end resistance check
- }//end of insertR
- void modifyR(stringstream &modStream){
- string modName;
- double modResistance;
- modStream >> modName;
- if(tooFewArgs(modStream))return;
- else if(modName == "all"){
- cout << "Error: resistor name cannot be the keyword \"all\"";
- }
- else {
- modStream >> modResistance;
- if(tooFewArgs(modStream))return;
- else if(modResistance < 0.0){
- cout << "Error: negative resistance";
- }
- else if(tooManyArgs(modStream))return;
- else{
- cout << "Modified: resistor "<< modName<<" to "<<setprecision(2)<<fixed<<modResistance<<" Ohms"<<endl;
- }//printed message
- }//end resistance check
- }//end modR
- void printR(stringstream &printStream){
- string printName;
- printStream >> printName;
- if(tooFewArgs(printStream))return;
- else if(printName == "all"){
- cout << "Print: all resistors";
- }
- if(tooManyArgs(printStream))return;
- else {
- cout <<"Print: resistor "<< printName<<endl;
- }
- }//end printR
- //fix printNode
- void printNode(stringstream &pNodeStream){
- int nodeId;
- string all;
- pNodeStream >> nodeId;
- if(pNodeStream.fail()){
- if(pNodeStream.eof()){
- pNodeStream.clear();
- cout << "Error: too few arguments"<<endl;
- }
- else{
- pNodeStream >> all;
- if(all == "all"){
- cout << "Print: all nodes";
- }
- else{
- cout << "Error: invalid argument";
- cin.ignore(1000,'\n');
- //skip 1000 things or until you get to a space
- cin.clear();
- return;
- }
- }
- }
- else{
- cout << "Print: node "<< nodeId<<endl;
- }//end print statement
- }//end printNode
- void deleteR(stringstream &dStream){
- string deleteName;
- dStream >> deleteName;
- if(tooFewArgs(dStream))return;
- if(tooManyArgs(dStream))return;
- else if(deleteName == "all"){
- cout << "Deleted: all resistors"<<endl;
- }
- else {
- cout << "Deleted: resistor "<< deleteName<<endl;
- }
- }
- bool tooFewArgs(stringstream &stream){
- if(stream.fail()){
- if(stream.eof()){
- stream.clear();
- cout << "Error: too few arguments"<<endl;
- return true;
- }
- else{
- cout << "Error: invalid argument";
- cin.ignore(1000,'\n');
- //skip 1000 things or until you get to a space
- cin.clear();
- return false;
- }
- }
- }
- bool tooManyArgs(stringstream &extraStream){
- string extraStuff;
- extraStream >> extraStuff;
- if(extraStream.fail()){
- if(extraStream.eof()){
- extraStream.clear();
- return false;
- }
- }
- else{
- cout << "Error: too many arguments"<<endl;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement