Singer class
describe
Implement a Singer class and pass the following tests:
int main()
{
Singer s1,s2;
cin>>s1>>s2;
cout<
if(s1>s2)
cout<
else if(s1==s2)
cout<
else
cout<
return 0;
}
input
The input includes two lines: the first line is the information of singer s1 and the second line is the information of singer s2. The information of each singer includes name (excluding spaces), gender, age and score; Names, gender, age and scores are separated by spaces
output
The output is three lines. The first two lines are the information of singers s1 and s2 respectively. The third line is output according to the comparison results of s1 and s2 (the comparison results of s1 and s2 are consistent with the comparison results of their scores). See the main function for details
Input sample 1
Mary F 28 99.5 Peter M 26 98
Output sample 1
Mary F 28 99.5 Peter M 26 98 Mary's score is higher than Peter's.
#include<iostream> using namespace std; class Singer { private: string name; char sex; int age; double grade; public: string getName(); Singer(string name = "", char s = '?', int a = 0, double n = 0); friend istream& operator>>(istream& stream, Singer& s); friend ostream& operator<<(ostream& stream, const Singer& s); friend bool operator>(const Singer& s1, const Singer& s2) { if (s1.grade > s2.grade) return true; else return false; } friend bool operator==(const Singer& s1, const Singer& s2) { if (s1.grade == s2.grade) return true; else return false; } }; Singer::Singer(string name, char s, int a,double n ):name(name),sex(s),age(a),grade(n){} istream& operator>>(istream& stream, Singer& s) { stream >> s.name >> s.sex >> s.age >> s.grade; return stream; } ostream& operator<<(ostream& stream, const Singer& s) { stream << s.name << " " << s.sex << " " << s.age << " " << s.grade; return stream; } string Singer::getName() { return name; } int main() { Singer s1, s2; cin >> s1 >> s2; cout << s1 << "\n" << s2 << endl; if (s1 > s2) cout << s1.getName() << "'s score is higher than " << s2.getName() << "'s.\n"; else if (s1 == s2) cout << s1.getName() << "'s score is equal to " << s2.getName() << "'s.\n"; else cout << s1.getName() << "'s score is lower than " << s2.getName() << "'s.\n"; return 0; }
Question 2:
Sales_data class
describe
Achieve the following Sales_data class (including its friend functions):
class Sales_data {
//Enter the book number, sales volume and revenue in sequence
friend istream & operator>>(istream&, Sales_data &);
//Output book number, sales volume, revenue and average price in turn
friend ostream & operator<<(ostream &, const Sales_data &);
friend bool operator==(const Sales_data &, const Sales_data &);
friend bool operator!=(const Sales_data &, const Sales_data &);
// for "+", assume that both objects refer to the same book
friend Sales_data operator+(const Sales_data &, const Sales_data &);
public:
Sales_data(): units_sold(0), revenue(0.0) {}
Sales_data(const string & s, unsigned n, double r): bookNo(s), units_sold(n), revenue(r) {}
string get_bookNo() const;
// for "+=", assume that both objects refer to the same book
Sales_data & operator+=(const Sales_data &);
private:
double avg_price() const; / / average price, equal to revenue divided by sales
string bookNo; / / book number
unsigned units_sold; // sales volume
double revenue; / / revenue
};
Pass the test of the following main function
int main(){
Sales_data item1,item2;
while(cin>>item1>>item2){
cout<
if(item1==item2)
cout<
if(item1!=item2)
cout<
cout<<(item1+item2)<<"\n";
item1 += item2;
cout<
}
return 0;
}
input
Enter multiple groups of data, each group of data has two rows, and each row represents one Sales_data object, followed by book number, sales volume and revenue
output
For each group of data, 5 lines are output. See main function and output example for details
Input sample 1
001 10 100.0 001 10 100.0
Output sample 1
001 10 100 10 001 10 100 10 001 equals 001 001 20 200 10 001 20 200 10
Input sample 2
002 5 250 003 8 400
Output sample 2
002 5 250 50 003 8 400 50 002 doesn't equal 003 002 13 650 50 002 13 650 50
#include<iostream> using namespace std; class Sales_data { //Enter the book number, sales volume and revenue in sequence friend istream& operator>>(istream&, Sales_data&); //Output book number, sales volume, revenue and average price in turn friend ostream& operator<<(ostream&, const Sales_data&); friend bool operator==(const Sales_data&, const Sales_data&); friend bool operator!=(const Sales_data&, const Sales_data&); // for "+", assume that both objects refer to the same book friend Sales_data operator+(const Sales_data&, const Sales_data&); public: Sales_data() : units_sold(0), revenue(0.0) {} Sales_data(const string& s, unsigned n, double r) : bookNo(s), units_sold(n), revenue(r) {} string get_bookNo() const; // for "+=", assume that both objects refer to the same book Sales_data& operator+=(const Sales_data&); private: double avg_price() const; //Average price, equal to revenue divided by sales string bookNo; //Book number unsigned units_sold; //sales volume double revenue; //income }; double Sales_data::avg_price() const { return(revenue / units_sold); } ostream& operator<<(ostream& stream, const Sales_data& s) { stream << s.bookNo << " " << s.units_sold << " " << s.revenue << " " << s.avg_price(); //Function call return stream; } istream& operator>>(istream& stream, Sales_data& s) { stream >> s.bookNo >> s.units_sold >> s.revenue; return stream; } bool operator==(const Sales_data& s1, const Sales_data& s2) { if (s1.units_sold == s2.units_sold) return true; else return false; } bool operator!=(const Sales_data& s1, const Sales_data& s2) { if (s1.units_sold != s2.units_sold) return true; else return false; } Sales_data operator+(const Sales_data& s1, const Sales_data& s2) { Sales_data s; s.bookNo = s1.bookNo; s.units_sold = s1.units_sold + s2.units_sold; s.revenue = s1.revenue + s2.revenue; return s; } string Sales_data::get_bookNo() const { return bookNo; } Sales_data& Sales_data::operator+=(const Sales_data& s2) { units_sold += s2.units_sold; revenue += s2.revenue; return *this; } int main() { Sales_data item1, item2; while (cin >> item1 >> item2) { cout << item1 << "\n" << item2 << "\n"; if (item1 == item2) cout << item1.get_bookNo() << " equals " << item2.get_bookNo() << "\n"; if (item1 != item2) cout << item1.get_bookNo() << " doesn't equal " << item2.get_bookNo() << "\n"; cout << (item1 + item2) << "\n"; item1 += item2; cout << item1 << "\n"; } return 0; }
Complex class
describe
Realize the following Complex class Complex, and realize the input and output of Complex and related operations through operator re truncation.
class Complex
{
private:
double x;
double y;
public:
Complex(double x = 0.0, double y = 0.0);
Complex & operator+=(const Complex &);
Complex & operator-=(const Complex &);
Complex & operator*=(const Complex &);
Complex & operator/=(const Complex &);
friend Complex operator+(const Complex &, const Complex &);
friend Complex operator-(const Complex &, const Complex &);
friend Complex operator*(const Complex &, const Complex &);
friend Complex operator/(const Complex &, const Complex &);
friend bool operator==(const Complex &, const Complex &);
friend bool operator!=(const Complex &, const Complex &);
friend ostream & operator<<(ostream &, const Complex &);
friend istream & operator>>(istream &, Complex &);
};
Pass the following main function test:
int main()
{
Complex c1, c2;
cin >> c1 >> c2;
cout << "c1 = " << c1 << "\n" << "c2 = " << c2 << endl;
cout << "c1+c2 = " << c1 + c2 << endl;
cout << "c1-c2 = " << c1 - c2 << endl;
cout << "c1*c2 = " << c1 * c2 << endl;
cout << "c1/c2 = " << c1 / c2 << endl;
cout << (c1 += c2) << endl;
cout << (c1 -= c2) << endl;
cout << (c1 *= c2) << endl;
cout << (c1 /= c2) << endl;
cout << (c1 == c2) << " " << (c1 != c2) << endl;
return 0;
}
input
The input has two lines, and each line inputs two floating-point numbers representing the complex numbers c1 and c2.
output
There are 11 lines of output, which respectively represent the operations between complex numbers. See the main function and output examples for details
Input sample 1
-4 6 2 5
Output sample 1
c1 = -4 + 6i c2 = 2 + 5i c1+c2 = -2 + 11i c1-c2 = -6 + 1i c1*c2 = -38 + -8i c1/c2 = 0.758621 + 1.10345i -2 + 11i -4 + 6i -38 + -8i -4 + 6i 0 1
Tips
Complex addition formula: (a + bi) + (c + di) = (a + c) + (b + d)i
Complex subtraction formula: (a + bi) - (c + di) = (a - c) + (b - d)i
Complex multiplication formula: (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
Complex division formula: (a + bi) / (c + di) = [(ac + bd) / (c * c + d * d)] + [(bc - ad) / (c * c + d * d)]i
#include <iostream> #include<string> using namespace std; class Complex { private: double x; double y; public: Complex(double x = 0.0, double y = 0.0); Complex& operator+=(const Complex&); Complex& operator-=(const Complex&); Complex& operator*=(const Complex&); Complex& operator/=(const Complex&); friend Complex operator+(const Complex&, const Complex&); friend Complex operator-(const Complex&, const Complex&); friend Complex operator*(const Complex&, const Complex&); friend Complex operator/(const Complex&, const Complex&); friend bool operator==(const Complex&, const Complex&); friend bool operator!=(const Complex&, const Complex&); friend ostream& operator<<(ostream&, const Complex&); friend istream& operator>>(istream&, Complex&); }; Complex::Complex(double x, double y) { this->x = x; this->y = y; } Complex operator*(const Complex& b1, const Complex& b2) { Complex s; s.x = b1.x * b2.x - b1.y * b2.y; s.y = b1.x * b2.y + b1.y * b2.x; return s; } Complex& Complex::operator+=(const Complex& s) { x += s.x; y += s.y; return *this; } Complex& Complex::operator-=(const Complex& s) { x -= s.x; y -= s.y; return *this; } Complex operator/(const Complex& s1, const Complex& s2) { Complex s; s.x = (s1.x * s2.x + s1.y * s2.y) / (s2.x * s2.x + s2.y * s2.y); s.y = (s1.y * s2.x - s1.x * s2.y) / (s2.x * s2.x + s2.y * s2.y); return s; } Complex& Complex::operator*=(const Complex& c) { *this = *this * c; return *this; } Complex& Complex::operator/=(const Complex& p) { *this = *this / p; return *this; } Complex operator+(const Complex& s1, const Complex& s2) { Complex s; s.x = s1.x + s2.x; s.y = s1.y + s2.y; return s; } Complex operator-(const Complex& s1, const Complex& s2) { Complex s; s.x = s1.x - s2.x; s.y = s1.y - s2.y; return s; } bool operator==(const Complex& s1, const Complex& s2) { if (s1.x == s2.x || s1.y == s2.y) return true; else return false; } bool operator!=(const Complex& s1, const Complex& s2) { if (s1.x != s2.x || s1.y != s2.y) return true; else return false; } istream& operator>>(istream& stream, Complex& s) { stream >> s.x >> s.y; return stream; } ostream& operator<<(ostream& stream, const Complex& s) { stream << s.x << " + " << s.y << "i"; return stream; } int main() { Complex c1, c2; cin >> c1 >> c2; cout << "c1 = " << c1 << "\n" << "c2 = " << c2 << endl; cout << "c1+c2 = " << c1 + c2 << endl; cout << "c1-c2 = " << c1 - c2 << endl; cout << "c1*c2 = " << c1 * c2 << endl; cout << "c1/c2 = " << c1 / c2 << endl; cout << (c1 += c2) << endl; cout << (c1 -= c2) << endl; cout << (c1 *= c2) << endl; cout << (c1 /= c2) << endl; cout << (c1 == c2) << " " << (c1 != c2) << endl; return 0; }
describe
Implement the following String classes:
class String
{
private:
char * s;
public:
String();
String(const char *);
String(const String &);
~String();
String & operator=(const char *);
String & operator=(const String &);
String operator+(const char *);
String operator+(const String &);
String & operator+=(const char *);
String & operator+=(const String &);
friend istream & operator>>(istream &, String &);
friend ostream & operator<<(ostream &, const String &);
friend bool operator==(const String &, const char *);
friend bool operator==(const String &, const String &);
friend bool operator!=(const String &, const char *);
friend bool operator!=(const String &, const String &);
};
Use the following main function to test:
int main()
{
String s;
s += "hello";
cout< String s1("String1");
String s2("copy of ");
s2 += "String1";
cout << s1 << "\n" << s2 << endl;
String s3;
cin >> s3;
cout << s3 << endl;
String s4("String4"), s5(s4);
cout << (s5 == s4) << endl;
cout << (s5 != s4) << endl;
String s6("End of "), s7("my string.");
s6 += s7;
cout << s6 << endl;
return 0;
}
input
Value of s3
output
See main function and output sample for details
Input sample 1
String3
Output sample 1
hello String1 copy of String1 String3 1 0 End of my string.
#include<iostream> #include<cstring> using namespace std; class String { private: char* s; public: String(); String(const char*); String(const String&); ~String(); String& operator=(const char*); String& operator=(const String&); String operator+(const char*); String operator+(const String&); String& operator+=(const char*); String& operator+=(const String&); friend istream& operator>>(istream&, String&); friend ostream& operator<<(ostream&, const String&); friend bool operator==(const String&, const char*); friend bool operator==(const String&, const String&); friend bool operator!=(const String&, const char*); friend bool operator!=(const String&, const String&); }; String::String() { s = new char[100]; } String::String(const char* c) { s = new char[strlen(c) + 1]; strcpy(s, c); } String::String(const String& c) { s = new char[strlen(c.s) + 1]; strcpy(s, c.s); } String::~String() { delete[]s; } String& String::operator=(const char* c) { return operator=(String(c)); } String& String::operator=(const String& c) { s = new char[strlen(c.s) + 1]; strcpy(s, c.s); return *this; } String String::operator+(const char* c) { return String(s) + String(c); } String String::operator+(const String& c) { return String(s) + c; } String& String::operator+=(const char* c) { return operator+=(String(c)); } String& String::operator+=(const String& c) { char* r = new char[strlen(s) + strlen(c.s) + 1]; r = this->s; strcat(r, c.s); this->s = r; return *this; } istream& operator>>(istream& stream, String& c) { stream >> c.s; return stream; } ostream& operator<<(ostream& stream, const String& c) { stream << c.s; return stream; } bool operator==(const String& c1, const char* c2) { if (strcmp(c1.s, c2) == 0) return true; else return false; } bool operator==(const String& c1, const String& c2) { if (strcmp(c1.s, c2.s) == 0) return true; else return false; } bool operator!=(const String& c1, const char* c2) { return(strcmp(c1.s, c2) != 0); } bool operator!=(const String& c1, const String& c2) { return(strcmp(c1.s, c2.s) != 0); } int main() { String x; x += "hello"; cout << x << endl; String s1("String1"); String s2("copy of "); s2 += "String1"; cout << s1 << "\n" << s2 << endl; String s3; cin >> s3; cout << s3 << endl; String s4("String4"), s5(s4); cout << (s5 == s4) << endl; cout << (s5 != s4) << endl; String s6("End of "), s7("my string."); s6 += s7; cout << s6 << endl; return 0; }
describe
The auto increment (+) and auto decrement (- -) operators are often implemented by classes such as iterators that provide pointer like behavior to access elements in a sequence. For example, define the following class CheckedPtr, which points to an int array and provides access checks for the elements in the array.
class CheckedPtr
{
public:
CheckedPtr(int * b, int * e) : beg(b), end(e), curr(b) { }
CheckedPtr & operator ++(); // prefix ++
CheckedPtr & operator --(); // prefix --
CheckedPtr operator ++(int); // postfix ++
CheckedPtr operator --(int); // postfix --
int * GetBeg();
int * GetEnd();
int * GetCurr();
private:
int * beg; // pointer to beginning of the array
int * end; // one past the end of the array
int * curr; // current position within the array
};
Implement the CheckedPtr class and test it through the following main function.
int main(){
int n;
cin>>n;
int * array = new int[n];
for(int i=0;i
cin>>array[i];
CheckedPtr cp(array, array+n);
for(;cp.GetCurr()
cout<<*cp.GetCurr()<<" ";
cout<
for(--cp;cp.GetCurr()>cp.GetBeg();cp--)
cout<<*cp.GetCurr()<<" ";
cout<<*cp.GetCurr()<
delete [] array;
return 0;
}
input
The input is two lines. The first line represents the length n of the array, and the second line represents the N numbers
output
The output is in two lines. The first line successively outputs the 0th to the last element of the array, and the second line reversely outputs all elements of the array (refer to the output example)
Input sample 1
5 1 2 3 4 5
Output sample 1
1 2 3 4 5 5 4 3 2 1
#include<iostream> #include<string> using namespace std; class CheckedPtr { public: CheckedPtr(int* a, int* e) : beg(a), end(e), curr(a) {} CheckedPtr& operator ++(); CheckedPtr& operator --(); CheckedPtr operator ++(int); CheckedPtr operator --(int); int* GetBeg(); int* GetEnd(); int* GetCurr(); private: int* beg; int* end; int* curr; }; CheckedPtr& CheckedPtr::operator ++() { ++curr; return *this; } CheckedPtr& CheckedPtr::operator--() { --curr; return *this; } CheckedPtr CheckedPtr::operator++(int) { CheckedPtr s(*this); ++* this; return s; } CheckedPtr CheckedPtr::operator--(int) { CheckedPtr s(*this); --* this; return s; } int* CheckedPtr::GetBeg() { return beg; } int* CheckedPtr::GetEnd() { return end; } int* CheckedPtr::GetCurr() { return curr; } int main() { int n; cin >> n; int* array = new int[n]; for (int i = 0; i < n; i++) cin >> array[i]; CheckedPtr cp(array, array + n); for (; cp.GetCurr() < cp.GetEnd(); cp++) cout << *cp.GetCurr() << " "; cout << endl; for (--cp; cp.GetCurr() > cp.GetBeg(); cp--) cout << *cp.GetCurr() << " "; cout << *cp.GetCurr() << endl; delete[] array; return 0; }