C++ DLUT operation (III)
I'm here again
1. Myset operator overload
/Define an integer collection class MySet, which can store up to 10 non repeating integers
class MySet{
int s[10]; // Integer set
int size;
pu blic:
MySet( int num =0);
...
};
Define the following member functions to implement the relevant operations of the collection:
1) Add an integer element to ensure that there is no duplicate element data in the collection. The successful addition returns true: bool add(int data)
2) Delete the specified element and find the element. If data is in the collection, delete the element from the collection; otherwise, return false; bool del(int data)
3) Overload the operator "+", which realizes the merging operation of two set objects. After merging, there is no duplicate data in the set, making the expression s1= s2+s3 true;
4) Overload operator "" to find the intersection of two set objects. The elements in the new set are the same elements in the two sets, making the expression s1= s2s3 true/
#include <iostream> using namespace std; class Myset { private: int s[10]; int size; public: Myset(); Myset(int num); bool add(int data); bool del(int data); Myset operator+(Myset& p); Myset operator*(Myset& p); void show(); }; Myset::Myset(int num) { int i; for(i=0;i<10;i++) { s[i]=num; } size=1; } Myset::Myset() { int i; for(i=0;i<10;i++) { s[i]=-1; } size=1; } bool Myset::add(int data) { int i; for(i=0;i<10;i++) { if(s[i]==data) return false; } s[size++]=data; return true; } bool Myset::del(int data) { int k,i,j; for(i=0;i<size;i++) { if(data==s[i]) { k=i; break; } } if(i>=size) { return false; } else { for(j=k;j<size-1;j++) { s[j]=s[j+1]; } size--; return true; } } Myset Myset::operator+(Myset& p) { Myset temp; int i,j; temp.size=size; for(i=0;i<size;i++) temp.s[i]=s[i]; for(i=0;i<p.size;i++) { for(j=0;j<size;j++) { if(p.s[i]==s[j]) break; } if(j==size) { temp.s[temp.size]=p.s[i]; temp.size++; } } return temp; } Myset Myset::operator*(Myset& p) { int i,j=0,k; Myset temp; for(i=0;i<size;i++) { for(k=0;k<p.size;k++) { if(s[i]==p.s[k]) break; } if(k<p.size) { temp.s[j++]=s[i]; } } temp.size=j; return temp; } void Myset::show() { int i; cout<<"Number of effective elements:"<<size<<endl; cout<<"Effective element:"; for(i=0;i<size;i++) { cout<<s[i]<<" "; } cout<<endl; } int main() { Myset s1(1),s2(5); for(int i=2;i<8;i++) { s1.add(i); } s1.show(); s2.add(7); s2.show(); Myset s3=s1+s2; cout<<"Union:"<<endl; s3.show(); Myset s4=s1*s2; cout<<"intersection:"<<endl; s4.show(); }
2. Mystring operator overload
/Improve the string class MyString designed in the previous chapter, and the private data members include char p, int size// String length
Define relevant constructors, copy constructors, destructors and member functions, and overload operators to achieve the following functions:
1) Overload "+" to realize the string connection function and make the expression s1= s2+s3 true;
2) Overload "= =" to judge whether two strings are equal, and return true/false according to the dictionary order of string content;
3) Overload "< <" and "> >", and realize the input and output operation of string, such as CIN > > S3; cout<<s2<<endl;
4) Overloaded operator '()' to return a substring from a string object. For example, s1(2,4) indicates that the slave substring is returned, that is, from s 2 Starting substring (including s1[2], s1[3] and s1[4]).
5) Overload "=" to make the expression s1= s2 true*/
#include<iostream> #include<cstring> using namespace std; class Mystring{ private: char* p; int size;//String length public: Mystring(); Mystring(const char* n); Mystring(const Mystring& s); ~Mystring(); Mystring operator()(int a,int b); Mystring operator+(const Mystring &s); bool operator==(const Mystring &s); friend ostream& operator<<(ostream& out,Mystring &s); friend istream& operator>>(istream& in,Mystring &s); Mystring &operator=(const Mystring &s); }; Mystring::Mystring() { p=new char[1]; p[0]='\0'; // strcpy(p,'\0'); size=0; } Mystring::Mystring(const char *n) { size=strlen(n); p=new char[size+1]; strcpy(p,n); p[size]='\0'; } Mystring::Mystring(const Mystring& s) { size=strlen(s.p); p=new char[size+1]; strcpy(p,s.p); p[size]='\0'; } Mystring::~Mystring() { if(p) delete [] p; size=0; } Mystring Mystring::operator+(const Mystring& s) { int length=size+s.size; char* newp=new char[length+1]; strcpy(newp,p); strcat(newp,s.p); newp[length+1]='\0'; return Mystring(newp); } bool Mystring::operator==(const Mystring &s) { if(strcmp(p,s.p)) return true; return false; } ostream& operator<<(ostream& out,Mystring &s) { out<<s.p<<endl; return out; } istream& operator>>(istream& in,Mystring &s) { in>>s.p; s.size=strlen(s.p); return in; } Mystring &Mystring::operator=(const Mystring& s) { strcpy(p,s.p); size=s.size; } Mystring Mystring::operator()(int a,int b) { Mystring r1; r1.p = new char[b-a+1]; strncpy(r1.p,p+a,b-a+1); return r1; /*char *q=NULL; int i,j=0; for(i=a;i<=b;i++) { q[j++]=p[i]; } q[j]='\0'; return Mystring(q);*/ } int main() { Mystring s1,s2; cin>>s1; cin>>s2; Mystring s3=s1+s2; cout<<s3; Mystring s4; cin>>s4; if(s3==s4) cout<<"s3 and s4 Unequal"<<endl; else cout<<"s3 and s4 equal"<<endl; Mystring s5; s5=s1; cout<<s5; Mystring s6; s6=s1(2,4); cout<<"String 1 (2),4)"<<endl; cout<<s6<<endl; }
3. Time operator overload
/Define the Time class describing Time, including the data members hour, minute and second. Define related functions to achieve the following operations:
1) Overloaded operators "+" and "-" can realize the addition and subtraction of time objects and integer seconds.
2) Overloaded operator "< <" outputs the time object, which can display the time in the way of "hour: minute: second".
3) Overloaded operators "+ +" and "–" are required to realize the reasonable self increasing and self decreasing function of time (increase or decrease of seconds)/
#include<iostream> using namespace std; class Time{ private: int hour; int minute; int second; public: Time(int h=0,int m=0,int s=0); Time operator+(int);//Time addition Time operator-(int);//Time subtraction friend ostream& operator<<(ostream&, Time&);//Output time object Time &operator++();//Self increasing Time &operator--();//Self subtraction }; Time::Time(int h,int m,int s) { hour=h; minute=m; second=s; } Time Time::operator+(int x) { second+=x; int m=second/60; second%=60; minute+=m; int h=minute/60; minute%=60; hour+=h; hour%=24; return *this; } Time Time::operator-(int n) { int i,j,k; i=second-n%60; if(i>=0) { second=i; } else { second=i+60; minute--; } j=minute-n/60; if(j>=0) { minute=j; } else { minute=j+60; hour--; } k=hour-n/60/60; if(k>=0) { hour=k; } else { hour=k+24; } return *this; } ostream& operator<<(ostream&out,Time &a) { out <<a.hour<<" "<<a.minute<<" "<<a.second<<endl; return out; } Time &Time::operator++() { second++; if(second==60) { minute++; second=0; if(minute==60) { hour++; minute=0; if(hour==24) { hour=0; } } } return *this; } Time &Time::operator--() { second--; if(second==-1) { second=59; minute--; if(minute==-1) { minute=59; hour--; if(hour==-1) { hour=23; } } } return *this; } int main() { Time s(23,59,59); ++s; cout<<s<<endl; --s; cout<<s<<endl; Time s1=s+3; cout<<s1<<endl; s1=s-3; cout<<s1<<endl; }
4. Overloaded symbolic fractional class
/Define Rational classes that describe Rational numbers
class Rational{
int m; // denominator
int n; // molecule
double d; // n/m
void simple(); // Divide
public:
Rational(int nn=1,int mm=1);
......
};
Please give the definition form of the function that overloads the following operators
1) Overloaded arithmetic operator '' to multiply fractions
2) Overloaded comparison operator "<" >, implementation of fraction comparison operator "< >
3) Overload the unary operator "-" to realize the inverse operation of scores
4) Overloaded binocular operator "-" to realize the subtraction of a double data and a fraction, and the subtraction of two fractions*/
#include<iostream> using namespace std; class Rational{ private: int m;//denominator int n;//molecule double d; //n/m void simple(); //Divide public: Rational(int nn=1,int mm=1); bool setM(int); void setN(int); double setD(); Rational operator*(const Rational &A);//Overload *, realize multiplication bool operator>(const Rational &A); bool operator<(const Rational &A);//Score comparison Rational operator-();// Inverse score void print(); }; int flag=0; int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } void Rational::simple() { int GCD=gcd(n,m); m/=GCD; n/=GCD; } Rational::Rational(int nn,int mm) { setN(nn); flag=setM(mm); } bool Rational::setM(int mm) { if(mm!=0) { m=mm; return true; } return false; } void Rational::setN(int nn) { n=nn; } double Rational::setD() { return (double)n/(double)m; } Rational Rational::operator*(const Rational& A) { Rational c; c.n=A.n*n; c.m=A.m*m; c.simple(); return c; } Rational Rational::operator-() { Rational c; c.m=n; c.n=m; c.simple(); return c; } bool Rational::operator>(const Rational& A) { if(n*A.m-m*A.n>0) return true; return false; } bool Rational::operator<(const Rational& A) { if(n*A.m-m*A.n>0) return false; return true; } void Rational::print() { cout<<"The calculation result is:"<<endl; if(n%m!=0) cout<<n<<"/"<<m<<endl; else cout<<n/m<<endl; } double operator-(double &dou,Rational &A) { return dou-A.setD(); } int main() { int nn,mm; double d; cout<<"Input molecule:"<<endl; cin>>nn; cout<<"Enter denominator:"<<endl; cin>>mm; if(flag==1) { cout<<"Error with zero denominator"<<endl; return 0; } Rational a,b(2,5),c; a.setN(nn); if(!a.setM(mm)) { cout<<"Denominator is zero, error"<<endl; return 0; } c=a*b; c.print(); c=-a; c.print(); if(a>b) { cout<<"fraction a greater than b"<<endl; } else { cout<<"fraction a less than b"<<endl; } if(c>b) { cout<<"fraction a Inverse greater than b"<<endl; } else { cout<<"fraction a Inverse less than b"<<endl; } double dd; cout<<"input double data:"<<endl; cin>>dd; cout<<"double Data and scores a The result of subtraction is:"<<endl; cout<<dd-a; }
It's not well written. Please correct it more.
Slip away, slip away......