Task One: String Stream
Use ostringstream and istringstream to implement the following two functions in a file stringIO.cpp by assuming that the operators << and >> have been overloaded for type T. template<class T> string toString (T value); // convert a value into a string
template <class T>
T toValue(string str); // extract the value from a string
Defined a class Student in a file Student.h and implemented the member functions in a file
Student.cpp. The class Student can be defined like following class Student { private:
string firstname; string lastname; int id; // student number float gpa;
public:
// all necessary functions to be defined here
};
Implement a main() function in stringIO.cpp to test the two functions in the cases where T is integer, double and class Student.
You need to define and implement the student class properly in order to test the two template functions and assume the input string for the student class is in the following format.
First-name:last-name:student-number:gpa
For example:
John:Anderson:1234567:6.35
Testing:
Compile the program in this task by:
CC o task1 stringIO.cpp Student.cpp
Run the program (You may use different values)
./task1
Input an integer: 12345678
Integer to string: 12345678
String to integer: 12345678
Input a double: 3214.654
Double to string: 3214.65
String to double: 3214.65
Input a student record (first-name:last-name:number:gpa): David:Smith:1234567:3.65 Student to string: David:Smith:1234567:3.65 String to Student:
David:Smith:1234567:3.65
Note: The outputs above indicate different types of data.
Task two: Compare two vectors
The program vectorCompare.cpp intends to implement and test a predicate function:
bool same_elements(vector<int> a, vector<int> b)
that checks whether two vectors have the same elements with the same multiplicities. For example, 1 4 9 16 9 7 4 9 11 and 11 1 4 9 16 9 7 4 9 would be considered identical, but 1 4 9 16 9 7 4 9 11 and 11 11 7 9 16 4 1 would not.
And a function that removes duplicates from a vector:
void remove_duplicates(vector<int>& a)
Complete the implementation of these two functions in the given vectorCompare.cpp file . You will probably need one or more helper functions for the implementation. Place the helper functions in the specified place. Complete the main function and test your implementation.
Reviews
There are no reviews yet.