Lab project 1 -- triple ADT

Source: Internet
Author: User

Lab content
[Problem description]
Design and implement the "triple" of the abstract data type ". Each triple is composed of a sequence of any three real numbers. Basic operations include: Create a triple, take any component of the triple, set any component of the triple, and obtain the maximum component of the triple, calculate the minimum component of the three tuples. The corresponding components of the Two Three tuples are added or subtracted, and each component of the three tuples is multiplied by a proportional factor. The three elements are displayed and the three elements are destroyed.
[Basic requirements]
Create a triple, take any component of the triple, set any component of the triple, obtain the maximum component of the triple, obtain the minimum component of the triple, and display basic operations such as the triple.
[Code]
# Include <iostream>
Using namespace std;
Template <class Elem> class Triple
{
Private:
Elem e1;
Elem e2;
Elem e3;
Public:
Triple (Elem v1, Elem v2, Elem v3)
{
E1 = v1;
E2 = v2;
E3 = v3;
}
Elem Get (int I) // initial condition: the triple already exists, 1 ≤ I ≤ 3; Operation Result: The I component of the triple is returned.
{
If (I> = 1 & I <= 3)
{
Elem a [3] = {e1, e2, e3 };
Return a [I-1];
}
Else
{
Cout <"\ n ";
Cout <"error, input value range: 1 ~ 3 "<endl;
}
}
 
Bool Put (int I, Elem e) // initial condition: the triple already exists, 1 ≤ I ≤ 3;
// Operation result: Assign the I-th component of the triplet to e. true is returned if the operation is successful. Otherwise, false is returned.
{
If (I> = 1 & I <= 3)
{
If (I = 1)
E1 = e;
Else if (I = 2)
E2 = e;
Else if (I = 3)
E3 = e;
Return true;
}
Else
Return false;
}
Elem GetMax () // initial condition: the productkey, devicename, and devicesecret already exist. Operation Result: the maximum value e in the productkey, devicename, and devicesecret is returned.
{
Elem a [3] = {e1, e2, e3 };
Elem max = a [0];
If (a [1]> a [0])
Max = a [1];
If (a [2]> max)
Max = a [2];
Return max;
 
}
 
Elem GetMin () // initial condition: the productkey, devicename, and devicesecret already exist. Operation Result: the smallest possible value e in the productkey, devicename, and devicesecret is returned.
{
Elem a [3] = {e1, e2, e3 };
Elem min = a [0];
If (a [1] <a [0])
Min = a [1];
If (a [2] <min)
Min = a [2];
Return min;
}
 
Void Output () // initial condition: the productkey, devicename, and devicesecret already exist. Operation Result: All the values in the productkey, devicename, and devicesecret are Output.
{
Elem a [3] = {e1, e2, e3 };
Cout <"now all the component values of the three tuples are :";
For (int I = 0; I <3; I ++)
Cout <a [I] <"";
}
 
Elem Sum (int I, int j) // Number of the input number for the addition operation. The result of the addition is returned.
{
Elem a [3] = {e1, e2, e3 };
Return a [I-1] + a [J-1];
}

Void Proportion (float a, float B, float c) // each component of the three tuples is multiplied by a proportional factor.
{
E1 = e1 *;
E2 = e2 * B;
E3 = e3 * c;
}

Void Destroy () // Destroy the triplet
{
E1 = 0;
E2 = 0;
E3 = 0;
Cout <"The productkey, devicename, and devicesecret have been initialized to 0 ";
}
};
// Test
Int main ()
{
Triple <float> t (10, 33,-44 );
Cout <"Test Result:" <endl;
Cout <"the first number of three tuples is:" <t. Get (1) <endl;
T. Output ();
 
Cout <"\ n ";
Cout <"\ n change the first number to 55 ";
T. Put (1, 55 );
T. Output ();
 
Cout <"\ n ";
Cout <"\ n the maximum value of the triple is:" <t. GetMax () <endl;
Cout <"the minimum value of the triple is:" <t. GetMin () <endl;
 
Cout <"\ n ";
T. Output ();
The Sum of the first and third numbers in the cout <"\ n triple:" <t. Sum );
 
Cout <"\ n ";
Cout <"\ n is the elements in the three tuples multiplied by 0.1, 0.4, and 0.5 respectively ";
T. Proportion (0.1, 0.4, 0.5 );
Cout <"\ n ";
T. Output ();
 
Cout <"\ n ";
Cout <"\ n destroy triple" <endl;
T. Destroy ();
Cout <"\ n ";
T. Output ();
System ("pause ");
}
Note: The above content is for reference only. If you have any questions, please correct them.
Author: Xinhai xinhang"

Related Article

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.