Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 405e7b1

Browse files
committed
GreatestNum
- Try to write a function to solve finding the max value in random value array .
1 parent cb36e80 commit 405e7b1

File tree

14 files changed

+73
-17
lines changed

14 files changed

+73
-17
lines changed

‎.vs/ProjectSettings.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"CurrentProjectSetting": "无配置"
3+
}

‎.vs/VSWorkspaceState.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"ExpandedNodes": [
3+
""
4+
],
5+
"SelectedNode": "\\OOP1-Lab2",
6+
"PreviewInSolutionExplorer": false
7+
}
715 KB
Binary file not shown.

‎.vs/cpp-programming/FileContentIndex/read.lock‎

Whitespace-only changes.

‎.vs/cpp-programming/v17/.suo‎

31 KB
Binary file not shown.

‎.vs/cpp-programming/v17/Browse.VC.db‎

16.7 MB
Binary file not shown.
32.9 MB
Binary file not shown.
32.9 MB
Binary file not shown.

‎.vs/slnx.sqlite‎

116 KB
Binary file not shown.

‎OOP1-Lab2/Greatestnumber.cpp‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
// U1910049
22
// Lab assignment #3
33
// Program to find greatest in 3 numbers
4+
// Program to find greatest in n numbers, the user offer requirement amount, and get the greatest number
45

56
#include <iostream>
7+
#include <string>
8+
#include <vector>
9+
#include <algorithm> // °üo¬ max_element() o ̄Êý
610
using namespace std;
7-
intmain2()
8-
{
9-
float a, b, c;
11+
12+
voidFindGreatestNumInThreeNum() {
13+
1014
cout << "Enter the 3 numbres: ";
15+
float a, b, c;
1116
cin >> a >> b >> c;
1217
if (a >= b && a >= c)
1318
cout << "The greatest number is " << a << ";" << endl;
@@ -17,6 +22,55 @@ int main2()
1722
cout << "The greatest number is " << c << ";" << endl;
1823
else if (a == b == c)
1924
cout << "All numbers are equal." << endl;
20-
system("pause");
25+
26+
}
27+
28+
void FindGreatestNumInRandomNum() {
29+
30+
cout << "Please enter number what you like, when you want to stop enter the number just enter QUIT or quit, we will return the greatest value" << endl;
31+
32+
vector<double> Numlist;
33+
string S_Num;
34+
double D_Num;
35+
36+
do {
37+
cin >> S_Num;
38+
39+
try {
40+
41+
D_Num = stod(S_Num);
42+
43+
}
44+
catch (const std::invalid_argument& ia) {
45+
46+
if (S_Num == "QUIT" || S_Num == "quit") {
47+
48+
auto MaxValue = max_element(Numlist.begin(), Numlist.end());//what is Iterator?
49+
50+
cout << "The Greatest Value is " <<*MaxValue<< endl;//What is a dereference?
51+
return;
52+
}
53+
54+
cout << "Your input is invalid, please enter again or send quit" << endl;
55+
56+
continue;
57+
}
58+
catch (const std::out_of_range& oor) {
59+
60+
std::cerr << "Out of range: " << oor.what() << endl <<"Your input is invalid, please enter again or send quit" << endl;
61+
continue;
62+
}
63+
64+
Numlist.push_back(D_Num);
65+
66+
} while (1);
67+
68+
}
69+
70+
71+
int main()
72+
{
73+
FindGreatestNumInRandomNum();
74+
2175
return 0;
2276
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /