/*--------------- W r i t e P w l F c n F i l e ( ) --------------- PURPOSE Write out a pwlFcn file. INPUT PARAMETERS cmdLine -- the execute command command line. pwlFcn -- the pwlFcn list. */ void WritePwlFcnFile(string cmdLine, DblLinkedList &pwlFcn) { ofstream pwlFcnStream; // The output stream string pwlFcnFile; // The output file name // Ask for the file name and then open the file. pwlFcnFile = GetFileName(cmdLine); if (pwlFcnFile.length() == 0) return; pwlFcnStream.open(pwlFcnFile.c_str()); if (!pwlFcnStream.is_open()) { cout << "*** ERROR: Failed to open file " << pwlFcnFile << endl; return; } // Write out the entire pwlFcn. pwlFcn.Rewind(); while (!pwlFcn.AtEnd()) { pwlFcnStream << pwlFcn.CurrentItem().X() <<" " << pwlFcn.CurrentItem().Y() << endl; pwlFcn.Forward(); } pwlFcnStream.close(); pwlFcn.Rewind(); }
/*--------------- D i s p l a y P w l F c n ( ) --------------- PURPOSE Display a pwlFcn from beginning to end. INPUT PARAMETERS pwlFcn -- the pwlFcn to be displayed. */ void DisplayPwlFcn(DblLinkedList &pwlFcn) { double x; double y; //Check if empty if(pwlFcn.Empty()) { cout<<"The pwlFcn is empty."<<endl; return; } //Start at the beginning and print everything pwlFcn.Rewind(); cout<<"PIECEWISE LINEAR FUNCTION DEFINITION"<<endl; while(!pwlFcn.AtEnd()) { x = pwlFcn.CurrentItem().X(); y = pwlFcn.CurrentItem().Y(); cout<<"("<<x<<","<<y<<")"<<endl; pwlFcn.Forward(); } //Reset to beginning pwlFcn.Rewind(); }
/*--------------- G e t C m d ( ) --------------- PURPOSE Accept a command from the keyboard. INPUT PARAMETERS cmdStream -- the stream from which commands are to be read pwlFcn -- the pwlFcn list. RETURN VALUE The command letter. */ string GetCmd(istream &cmdStream, DblLinkedList &pwlFcn) { // Display the current point before accepting each command. if (!pwlFcn.AtEnd()/* && cmdStream == cin*/) { // Display the current item. cout << "\nCURRENT ITEM" << endl; cout << pwlFcn.CurrentItem() << endl; } // Prompt for a new command. cout << "\n>"; string cmdLine; // Command line // Quit at end of a command file. if (cmdStream.peek() == EOF) { cmdLine = QuitCmd; if (cmdStream != cin) cout << cmdLine << endl; return cmdLine; } // Get the next command and return it. getline(cmdStream, cmdLine); if (cmdStream != cin) cout << cmdLine << endl; return cmdLine; }
/*--------------- I n t e r p o l a t e ( ) --------------- PURPOSE Determine the value of the waveform at time t by linear interpolation. INPUT PARAMETERS pwlFcn -- the pwlFcn list t -- the time at which the waveform value is to be obtained */ double Interpolate(DblLinkedList &pwlFcn, double t) { double lowerBound; double upperBound; Point low; Point high; lowerBound = pwlFcn.FirstItem().X(); upperBound = pwlFcn.LastItem().X(); //modulate t to be in the period of the wave while(t<lowerBound) { t+=upperBound; } while(t>upperBound) { t-=upperBound; } t=fmod(t,upperBound); //find the upper and lower points to use to interp pwlFcn.Rewind(); while(!pwlFcn.AtEnd()) { if(t>pwlFcn.CurrentItem().X()) { low = pwlFcn.CurrentItem(); } else { high = pwlFcn.CurrentItem(); break; } pwlFcn.Forward(); } //calculate line double m = (high.Y()-low.Y())/(high.X()-low.X()); //slope double b = (high.Y())-(m*high.X()); //offset return (m*t+b); //Compute and return y }
/*--------------- G e n e r a t e ( ) --------------- PURPOSE Generate the periodic function. INPUT PARAMETERS cmdLine -- the execute command command line. pwlFcn -- the pwlFcn list. */ void Generate(string cmdLine, DblLinkedList &pwlFcn) { // Make sure the function is defined. if (pwlFcn.Empty()) { cout << "The pwlFcn is empty." << endl; return; } Point testPoint; // The point to test const unsigned MinCmdSize = 2; // To check for an empty test command istringstream cmdStream(cmdLine.erase(0,1)); // Command stream // Ignore if empty test parameters. if (cmdLine.length() < MinCmdSize) return; // Display the current entry. cout << "\nCURRENT ENTRY\n" << pwlFcn.CurrentItem() << endl << endl; // Read the start and stop times and the number of points. double tStart; double tEnd; unsigned numPts; cmdStream >> tStart >> tEnd >> numPts; double dt = (tEnd - tStart) / (numPts - 1); // Generate the waveform starting at t = 0. double t = tStart; pwlFcn.Rewind(); for (unsigned i=0; i<numPts; i++) { Point pt = Point(t, Interpolate(pwlFcn, t)); cout << pt << endl; t += dt; } pwlFcn.Rewind(); }
//------------------- DblLinkedList::DeepCopy() ------------------- // Deep copy source to this object void DblLinkedList::DeepCopy(DblLinkedList &source) { Node *tempNode; assert(tempNode = new(nothrow) Node()); tempNode = source.current; //Delete the old list this->~DblLinkedList(); //Rewind the source list source.Rewind(); //Copy that linked list. while (source.current != 0) { InsertItem(source.CurrentItem()); source.Forward(); } source.current = tempNode; current = tempNode; tempNode = 0; delete tempNode; }
/*--------------- C o u n t P o i n t s ( ) --------------- PURPOSE Count the number of points in the pwlFcn. The list is passed by value to invoke the copy constructor. The destructor is executed upon return when the parameter is destroyed. */ void CountPoints(DblLinkedList pwlFcn) { unsigned numPts = 0; // Number of points in the pwlFcn // Display the current entry. cout << "\nCURRENT ENTRY\n" << pwlFcn.CurrentItem() << endl << endl; // Rewind to the beginning. pwlFcn.Rewind(); // Add 1 point and move forward until reaching the end of the pwlFcn. while (!pwlFcn.AtEnd()) { ++numPts; pwlFcn.Forward(); } // Rewind on the way out. pwlFcn.Rewind(); // Display the count. cout << "Number of Points: " << numPts << endl; }