Task:
In a container type vector stored Christmas trees (fields: height, price). It is necessary to develop a program for:
1. Search for a minimum value (height, price)
2. Search the maximum value
3. Sorting data descending
4. Sorting data ascending
5. Increasing the values in the container to the specified constant
6. Reducing the values in the container on a given constant
7. Removing elements from the container equal to the desired value
class tree
{
Private:
Float Height;
float Price;
Public:
Tree (Float Height, Float Price): Height (Height), Price (Price) {}
Float GetHeight ()
{
Return Height;
}
Float GetPrice ()
{
Return Price;
}
};
INT MAIN ()
{
Vector & lt; Tree & GT; _Tree;
Vector & lt; Tree & GT; :: ITERATOR IT;
it = _tree.begin ();
Float Height;
float Price;
While (True)
{
System ("CLS");
COUT & LT; & LT; "Menu" & lt; & lt; Endl & lt; & lt; Endl;
COUT & LT; & LT; "1 - Add Tree" & lt; & lt; Endl;
COUT & LT; & LT; "2 - Find the Minimum Value" & lt; & lt; Endl;
COUT & LT; & LT; "3 - Find The Maximum Value" & lt; & lt; Endl;
COUT & LT; & LT; "4 - Sort Data in Descending Order" & lt; & lt; Endl;
COUT & LT; & LT; "5 - Sort Data in Ascending Order" & lt; & lt; Endl;
COUT & LT; & LT; "6 - Increase the Values In The CONTAINER BY A Given Constant" & lt; & lt; Endl;
COUT & LT; & LT; "7 - Decrease Values In The Container By A Given Constant" & lt; & lt; Endl;
COUT & LT; & LT; "8 - Removing Items from the Container Equal To the Desired Value" & lt; & lt; Endl;
COUT & LT; & LT; "9 - EXIT" & lt; & lt; Endl & lt; & lt; Endl;
COUT & LT; & LT; "ENTER:";
INT MENU;
CIN & GT; & GT; Menu;
Switch (Menu)
{
Case 1:
{
System ("CLS");
COUT & LT; & LT; "ENTER HEIGHT:";
CIN & GT; & GT; Height;
COUT & LT; & LT; "ENTER Price:";
CIN & GT; & GT; Price;
Tree T (Height, Price);
_tree.push_back (t);
SLEEP (500 * 1);
Break;
}
Case 2:
{
System ("CLS");
SLEEP (1000 * 1);
Break;
}
}
}
System ("CLS");
Return 0;
}
How can I get through the class to the fields of class, what would work with them?
Answer 1, Authority 100%
Here you have some tips:
class tree
{
Private:
Float Height;
float Price;
Public:
Tree (Float Height, Float Price): Height (Height), Price (Price) {}
Float GetHeight () Const
{
Return Height;
}
Float GetPrice () Const
{
Return Price;
}
Void SETPRICE (Float Newprice)
{
Price = newPrice;
}
};
INT MAIN ()
{
Vector & lt; Tree & GT; Trees;
// Iterator on the Christmas tree with the lowest price
auto min_price = min_element (trees.begin (), trees.end (),
[] (Const Tree & amp; A, const Tree & amp; b)
{
Return a.getPrice () & lt; B.GetPrice ();
});
// Sorting the Christmas Trees in Height
Sort (Trees.begin (), Trees.end (),
[] (Const Tree & amp; A, const Tree & amp; b)
{
Return A.GetHeight () & lt; B.GetHeight ();
});
// increase price in the container 2 times
for_each (trees.begin (), trees.end (), [] (Tree & amp; a) {a.setprice (A.GetPrice () * 2);});
// or
For (AUTO & AMP; T: Trees) T.SetPrice (T.GetPrice () * 2);
}
From Soviets: Do not neglect const
! And do not use the first synchm symbol – such names are reserved with ++ for internal purposes (in fact, the rules are more accurate than just starting with the underline, but it is easier to acquire a habit just not to use the first symbol at all).
Answer 2, Authority 33%
as an option to work with the vector’s objects of any class (access to the methods / object methods / object):
# include & lt; iostream & gt;
#Include & lt; vector & gt;
Class Tree
{
Private:
Float MHEIGHT;
Float MPRICE;
Public:
Tree (Float Height, Float Price)
: MHEIGHT (Height), MPRICE (Price) {}
Float GetHeight ()
{
Return Mheight;
}
Float GetPrice ()
{
Return MPRICE;
}
};
Int Main (Int Argc, Const Char * Argv []) {
STD :: Vector & LT; Tree & GT; Vectree = {
Tree (1.55, 1450.0),
Tree (1.65, 1670.0),
Tree (1.07, 989.9)
};
STD :: COUT & LT; & LT; "All Vector Of Trees:" & lt; & lt; STD :: ENDL;
For (auto iTer = Vectree.begin (); Iter! = Vectree.end (); ++ iTER) {
STD :: COUT & LT; & LT; "Tree (" & lt; & lt; Iter- & gt; getHeight () & lt; & lt; "," & lt; & lt; iTer- & gt; getPrice () & lt; & lt; ");" & lt; & lt; STD :: ENDL;
}
Return 0;
}