C ++ study Note 2

Source: Internet
Author: User

Implement and test an intset class. each object of this class will represent a set of integers, storing the different elements as integers. provide constructors and some operations, such as empty (), isempty (), ismemberof (INT), isequal (intset &), add (INT ),
Sub (INT), intersection (intset &), merge (intset &) and print (), and so on.

 

 

// BookProgramWritten in VC-SP6, debugging through

# Include <iostream>
# Include <iomanip>
Using namespace STD;

Const int size = 20;

Class intest
{
Public:
Void reset ()
{
Top = 0;
}
Void empty ();
Int isempty ();
Int ismemberof (INT );
Int isequal (intest &);
Int add (INT );
Int sub (INT );
Intest intersection (intest &);
Intest Merge (intest &);
Void print ();
PRIVATE:
Int A [size];
Int top;
};

 

Void intest: Empty ()
{
Top = 0;
Cout <"The intest is empty." <Endl;

}

Int intest: isempty ()
{
If (Top = 0)
 
Return 1;
Else
Return 0;
}

Int intest: add (INT element)
{Int I;
If (top> 0)
{
For (I = 0; I <= Top-1; I ++)
{
If (A [I] = element)
 
{
Return 0;

Break;

}


}
If (I = top & Top <= size)
{A [I] = element;
Top ++;
Return 1;
}
If (I = top & top> size)
{Cout <"The intest is full." <Endl;
Return 0;
}
}

Else
{
A [0] = element;
Top ++;
Return 1;
}

}

Int intest: ismemberof (INT element)
{Int I;
For (I = 0; I <= top; I ++)
If (A [I] = element)
{Return 1;
Break;

}
If (I = Top + 1)
Return 0;
}

Int intest: Sub (INT element)
{
 
Int I;
For (I = 0; I <= Top-1; I ++)
If (A [I] = element)
{
For (Int J = I + 1; j <= top; j ++)
A [J-1] = A [J];
Top --;
Return 1;
}
If (I = top)
Return 0;
 
}

Void intest: Print ()
{
Int I;
If (! Isempty ())
{Cout <"{";
For (I = 0; I <= Top-1; I ++)
Cout <A [I] <SETW (3 );
Cout <"}" <Endl;
}
Else
Cout <"The intest is empty." <Endl;
}

 

Int intest: isequal (intest & temp)
{Int I;
If (temp. Top = top)
{
For (I = 0; I <= temp. Top; I ++)
{
If (! Ismemberof (temp. A [I])
Return 0;
}
}
Else
Return 0;
Return 1;
}

intest: intersection (intest & temp)
{< br> intest temper;
temper. reset ();
for (Int J = 0; j <= temp. top-1; j ++)
{< br> If (ismemberof (temp. A [J])
{< br> temper. add (temp. A [J]);
}< BR >}< br> return temper;
}

Intest: Merge (intest & temp)
{// Merge the two sets and put the results into the collection
Int COUNT = 0;
Intest temper (temp );
For (INT I = 0; I <= temper. Top-1; I ++)
{
If (! Ismemberof (temper. A [I])
Count ++;
}
Count + = top;
If (count> 20)
{// If the union of the two sets exceeds the storage range, 0 is returned.
Cout <"the merge of two intests too big" <Endl;
Return temp;
}
Else
{
For (I = 0; I <= Top-1; I ++)
{
If (! Temper. ismemberof (A [I])
Temper. Add (A [I]);
}
Return temper;
}
}

 

 

Int main ()
{
Class intest number1, number2;
Int type, element;
Intest * temp_intest_1, * temp_intest_2;
Number1.reset ();
Number2.reset ();
Cout <"Please choice a command for number1:" <Endl;
Cout <"***********************************" <Endl;
Cout <"1. Empty the intest." <Endl;
Cout <"2. To judge an element is in the intest." <Endl;
Cout <"3. to judge the intest is empty." <Endl;
Cout <"4. Add an element to the intest." <Endl;
Cout <"5. delete an element in the intest." <Endl;
Cout <"6. Print the intest." <Endl;
Cout <"0. Exit" <Endl;
Cout <"***********************************" <Endl;
Cin> type;
While (type! = 0)
{
Switch (type)
{
Case 1: number1.empty ();
Break;

Case 2: cout <"Please enter an element." <Endl;
Cin> element;
If (number1.ismemberof (element ))
Cout <"Yes, it is a member of the intest." <Endl;

Else
Cout <"Sorry, it is not in the intest." <Endl;
Break;

Case 3: If (number1.isempty ())
Cout <"Yes, It is empty." <Endl;
Else
Cout <"no, it is not empty." <Endl;
Break;

Case 4:
Cout <"Please enter an element. (Only numbers)" <Endl;
Cin> element;

If (number1.add (element ))
Cout <"add sucessfully." <Endl;

Else
Cout <"The intest is full or the element is already in it." <Endl;
Break;

Case 5:
Cout <"Please enter an element. (Only numbers)" <Endl;
Cin> element;

If (number1.sub (element ))
Cout <"delete sucessfully." <Endl;

Else
Cout <"the element is not in the intest." <Endl;
Break;

Case 6:
Number1.print ();
Break;

}
Cin> type;
}
Cout <"Please choice a command for number2:" <Endl;
Cout <"***********************************" <Endl;
Cout <"1. Empty the intest." <Endl;
Cout <"2. To judge an element is in the intest." <Endl;
Cout <"3. to judge the intest is empty." <Endl;
Cout <"4. Add an element to the intest." <Endl;
Cout <"5. delete an element in the intest." <Endl;
Cout <"6. Print the intest." <Endl;
Cout <"0. Exit" <Endl;
Cout <"***********************************" <Endl;
Cin> type;
While (type! = 0)
{
Switch (type)
{
Case 1: number2.empty ();
Break;

Case 2: cout <"Please enter an element." <Endl;
Cin> element;
If (number2.ismemberof (element ))
Cout <"Yes, it is a member of the intest." <Endl;

Else
Cout <"Sorry, it is not in the intest." <Endl;
Break;

Case 3: If (number2.isempty ())
Cout <"Yes, It is empty." <Endl;
Else
Cout <"no, it is not empty." <Endl;
Break;

Case 4:
Cout <"Please enter an element. (Only numbers)" <Endl;
Cin> element;

If (number2.add (element ))
Cout <"add sucessfully." <Endl;

Else
Cout <"The intest is full or the element is already in it." <Endl;
Break;

Case 5:
Cout <"Please enter an element. (Only numbers)" <Endl;
Cin> element;

If (number2.sub (element ))
Cout <"delete sucessfully." <Endl;

Else
Cout <"the element is not in the intest." <Endl;
Break;

Case 6:
Number2.print ();
Break;


}
Cin> type;
}
Cout <"now, please input a command for the two intests:" <Endl;
Cout <"************************************ * *********** "<Endl;
Cout <"1. merge the two intests." <Endl;
Cout <"2. intersection for the two intests." <Endl;
Cout <"3. Whether the two intests are equal." <Endl;
Cout <"0. Exit." <Endl;
Cout <"************************************ * *********** "<Endl;
Cin> type;
While (type! = 0)
{
Switch (type)
{
Case 1:
Temp_intest_1 = new intest (number1.merge (number2 ));
(* Temp_intest_1). Print ();
Break;

Case 2:
Temp_intest_2 = new intest (number1.intersection (number2 ));
(* Temp_intest_2). Print ();
Break;

Case 3:
If (number1.isequal (number2 ))
Cout <"the two intests are equal" <Endl;
Else
Cout <"the two intests are not equal" <Endl;

Break;

}
Cin> type;
}
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.