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 642319c

Browse files
committed
Lab3 Sum1
1 parent 9b22cf5 commit 642319c

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed

‎OOP1-Lab2/Swapping.cpp‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,32 @@ void SwapWithoutVarInFloat() {
2121

2222
void SwapWithoutVarInRandom() {
2323

24-
std::string str = "123";
25-
try {
26-
double d = std::stod(str);
27-
std::cout << "The string is a floating-point number." <<d<< std::endl;
28-
}
29-
catch (const std::exception& e) {
30-
try {
31-
int i = std::stoi(str);
32-
if (str.find('.') == std::string::npos) {
33-
std::cout << "The string is an integer." << std::endl;
34-
}
35-
else {
36-
std::cout << "The string is not a number." << std::endl;
37-
}
38-
}
39-
catch (const std::exception& e) {
40-
std::cout << "The string is not a number." << std::endl;
41-
}
42-
}
24+
string a, b;
25+
int I_a;
26+
int I_b;
27+
double S_a;
28+
double S_b;
29+
30+
string c = "12.23";
31+
//cin >> a >> b;
32+
33+
try {
34+
35+
I_a = stoi(c);
36+
37+
}
38+
catch(invalid_argument& ia) {
39+
40+
cout << "invalid_argument" << endl;
41+
42+
}
43+
catch (const out_of_range& oor) {
44+
45+
cout << "out_of_range" << endl;
46+
47+
}
48+
49+
cout << I_a << endl;
4350
}
4451

4552
int main()

‎OOP1-Lab3/Sum1.cpp‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,28 @@
44

55
#include <iostream>
66
using namespace std;
7+
8+
int SumFromMtoN() {
9+
10+
cout << "Please enter two number, we will sum them from small to big." << endl;
11+
12+
int a, b, sum=0;
13+
cin >> a >> b;
14+
if (a = b) {
15+
return a;
16+
}
17+
18+
if (a > b) {
19+
while (!(a = b)) {
20+
sum = sum + b;
21+
b++;
22+
}
23+
}
24+
25+
}
26+
727
int main() {
8-
int n;
9-
float sum = 0;
10-
cout << "Enter the value of n: ";
11-
cin >> n;
12-
for (int i = 1;i <= n;i++)
13-
sum += 1 / i;
14-
cout << "Sum is : " << sum << endl;
15-
system("pause");
28+
29+
1630
return 0;
1731
}

0 commit comments

Comments
(0)

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