Skip to content

Commit fcd155a

Browse files
authored
Modify C and C++ Testing (#5540)
* Update GCC to 15.2 * Reformat all C and C++ samples
1 parent f8631ae commit fcd155a

75 files changed

Lines changed: 1749 additions & 1478 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

archive/c/c-plus-plus/binary-search.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
#include <iostream>
21
#include <bits/stdc++.h>
2+
#include <iostream>
33

44
using namespace std;
55

66
void handle_error()
77
{
8-
cout << "Usage: please provide a list of sorted integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")" << endl;
8+
cout
9+
<< "Usage: please provide a list of sorted integers (\"1, 4, 5, 11, "
10+
"12\") and the integer to find (\"11\")"
11+
<< endl;
912
exit(0);
1013
}
1114

@@ -32,22 +35,16 @@ int check(string s)
3235
}
3336

3437
for (int i = x1; i <= x2; i++)
35-
{
3638
if (s[i] == ' ')
37-
{
3839
handle_error();
39-
}
40-
}
4140

4241
return stoi(s);
4342
}
4443

4544
vector<int> convert(string s)
4645
{
4746
if (s.size() == 0)
48-
{
4947
handle_error();
50-
}
5148
vector<int> v;
5249
string num = "";
5350
for (int i = 0; i < s.size(); i++)
@@ -68,30 +65,22 @@ vector<int> convert(string s)
6865
}
6966

7067
if (num.size() > 0)
71-
{
7268
v.push_back(check(num));
73-
}
7469

7570
return v;
7671
}
7772

7873
int main(int argc, char *argv[])
7974
{
8075
if (argc < 3)
81-
{
8276
handle_error();
83-
}
8477

8578
vector<int> v = convert(argv[1]);
8679
int num = check(argv[2]);
8780

8881
for (int i = 0; i < v.size() - 1; i++)
89-
{
9082
if (v[i] > v[i + 1])
91-
{
9283
handle_error();
93-
}
94-
}
9584

9685
int start = 0, end = v.size();
9786
string ans = "false";
@@ -115,9 +104,7 @@ int main(int argc, char *argv[])
115104
}
116105

117106
if (start > end)
118-
{
119107
ans = "false";
120-
}
121108

122109
cout << ans << endl;
123110
}

archive/c/c-plus-plus/bubble-sort.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,17 @@ void bubbleSort(std::vector<int> &v, int n)
2828
}
2929

3030
if (swapped == false)
31-
{
3231
break;
33-
}
3432
}
3533
}
3634

3735
void print(std::vector<int> v, int size)
3836
{
3937
for (int i = 0; i < size; i++)
4038
if (i == size - 1)
41-
{
4239
std::cout << v[i] << std::endl;
43-
}
4440
else
45-
{
4641
std::cout << v[i] << ", ";
47-
}
4842
}
4943

5044
int main(int argc, char *argv[])
@@ -75,22 +69,19 @@ int main(int argc, char *argv[])
7569
if (commaSeparated == true)
7670
{
7771
for (int i = 0; i < strlen(characters); i++)
78-
{
7972

8073
if (characters[i] != ',' && characters[i] != ' ')
81-
{
8274
numbers.push_back(atoi(&characters[i]));
83-
}
84-
}
8575
}
8676
}
8777

8878
int size = numbers.size();
8979
if (size < 2)
9080
{
91-
std::cout << "Usage: please provide a list of at least two integers to "
92-
"sort in the format \"1, 2, 3, 4, 5\""
93-
<< std::endl;
81+
std::cout
82+
<< "Usage: please provide a list of at least two integers to "
83+
"sort in the format \"1, 2, 3, 4, 5\""
84+
<< std::endl;
9485
}
9586
else
9687
{
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <iostream>
21
#include <cstring>
2+
#include <iostream>
33

44
int main(int argc, const char *argv[])
55
{
@@ -10,10 +10,8 @@ int main(int argc, const char *argv[])
1010
}
1111

1212
for (int j = 0; j < (int)std::strlen(argv[1]); j++)
13-
{
1413
if (j == 0)
1514
std::cout << (char)toupper(argv[1][j]);
1615
else
1716
std::cout << *(argv[1] + sizeof(char) * j);
18-
}
1917
}

archive/c/c-plus-plus/convex-hull.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ struct Point
1717
bool operator<(const Point &other) const
1818
{
1919
if (x != other.x)
20-
{
2120
return x < other.x;
22-
}
2321
return y < other.y;
2422
}
2523
};
@@ -41,9 +39,7 @@ std::vector<int> parseList(const std::string &input)
4139
{
4240
auto start = token.find_first_not_of(" \t");
4341
if (start == std::string::npos)
44-
{
4542
usage();
46-
}
4743

4844
const auto end = token.find_last_not_of(" \t");
4945
token = token.substr(start, end - start + 1);
@@ -57,9 +53,7 @@ std::vector<int> parseList(const std::string &input)
5753
const int value = std::stoi(token, &pos);
5854

5955
if (pos != token.size())
60-
{
6156
usage();
62-
}
6357

6458
out.push_back(value);
6559
}
@@ -79,9 +73,7 @@ std::vector<Point> parseCoordinates(const std::string &xs,
7973
auto y = parseList(ys);
8074

8175
if (x.size() != y.size() || x.size() < 3)
82-
{
8376
usage();
84-
}
8577

8678
std::vector<Point> pts;
8779
pts.reserve(x.size());
@@ -100,9 +92,7 @@ long long cross(const Point &a, const Point &b, const Point &c)
10092
std::vector<Point> convexHull(std::vector<Point> pts)
10193
{
10294
if (pts.size() < 3)
103-
{
10495
usage();
105-
}
10696

10797
std::sort(pts.begin(), pts.end());
10898

@@ -115,8 +105,8 @@ std::vector<Point> convexHull(std::vector<Point> pts)
115105
{
116106
const auto &p = *it;
117107

118-
while (hull.size() >= 2 &&
119-
cross(hull[hull.size() - 2], hull.back(), p) <= 0)
108+
while (hull.size() >= 2
109+
&& cross(hull[hull.size() - 2], hull.back(), p) <= 0)
120110
{
121111
hull.pop_back();
122112
}
@@ -140,14 +130,10 @@ std::vector<Point> convexHull(std::vector<Point> pts)
140130
int main(int argc, char *argv[])
141131
{
142132
if (argc != 3)
143-
{
144133
usage();
145-
}
146134

147135
const auto hull = convexHull(parseCoordinates(argv[1], argv[2]));
148136

149137
for (const auto &[x, y] : hull)
150-
{
151138
std::cout << '(' << x << ", " << y << ")\n";
152-
}
153139
}

archive/c/c-plus-plus/depth-first-search.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ vector<int> g[N];
77

88
void handle_error()
99
{
10-
cout << "Usage: please provide a tree in an adjacency matrix form (\"0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0\") together with a list of vertex values (\"1, 3, 5, 2, 4\") and the integer to find (\"4\")\n";
10+
cout << "Usage: please provide a tree in an adjacency matrix form (\"0, 1, "
11+
"1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, "
12+
"0\") together with a list of vertex values (\"1, 3, 5, 2, 4\") "
13+
"and the integer to find (\"4\")\n";
1114
exit(0);
1215
}
1316

@@ -34,12 +37,8 @@ int check(string s)
3437
}
3538

3639
for (int i = x1; i <= x2; i++)
37-
{
3840
if (s[i] == ' ')
39-
{
4041
handle_error();
41-
}
42-
}
4342

4443
return stoi(s);
4544
}
@@ -48,9 +47,7 @@ vector<int> convert(string s)
4847
{
4948

5049
if (s.size() == 0)
51-
{
5250
handle_error();
53-
}
5451
vector<int> v;
5552
string num = "";
5653
for (int i = 0; i < s.size(); i++)
@@ -67,9 +64,7 @@ vector<int> convert(string s)
6764
}
6865

6966
if (num.size() > 0)
70-
{
7167
v.push_back(check(num));
72-
}
7368

7469
return v;
7570
}
@@ -95,7 +90,8 @@ int main(int argc, char *argv[])
9590
if (argc <= 1)
9691
handle_error();
9792

98-
vector<int> bin = convert(argv[1]), nodes = convert(argv[2]), t = convert(argv[3]);
93+
vector<int> bin = convert(argv[1]), nodes = convert(argv[2]),
94+
t = convert(argv[3]);
9995
if (bin.size() == 0 || nodes.size() == 0 || t.size() == 0)
10096
handle_error();
10197

@@ -120,9 +116,11 @@ int main(int argc, char *argv[])
120116
int src = nodes[0];
121117
dfs(src);
122118
if (vis[t[0]])
123-
cout << "true"
124-
<< "\n";
119+
cout
120+
<< "true"
121+
<< "\n";
125122
else
126-
cout << "false"
127-
<< "\n";
123+
cout
124+
<< "false"
125+
<< "\n";
128126
}

archive/c/c-plus-plus/dijkstra.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ vector<int> dis(N, INT_MAX);
99

1010
void handle_error()
1111
{
12-
cout << "Usage: please provide three inputs: a serialized matrix, a source node and a destination node";
12+
cout << "Usage: please provide three inputs: a serialized matrix, a source "
13+
"node and a destination node";
1314
exit(0);
1415
}
1516

@@ -36,12 +37,8 @@ int check(string s)
3637
}
3738

3839
for (int i = x1; i <= x2; i++)
39-
{
4040
if (s[i] == ' ')
41-
{
4241
handle_error();
43-
}
44-
}
4542

4643
return stoi(s);
4744
}
@@ -50,9 +47,7 @@ vector<int> convert(string s)
5047
{
5148

5249
if (s.size() == 0)
53-
{
5450
handle_error();
55-
}
5651
vector<int> v;
5752
string num = "";
5853
for (int i = 0; i < s.size(); i++)
@@ -113,7 +108,8 @@ int main(int argc, char *argv[])
113108

114109
if (argc <= 1)
115110
handle_error();
116-
vector<int> bin = convert(argv[1]), nodes = convert(argv[2]), t = convert(argv[3]);
111+
vector<int> bin = convert(argv[1]), nodes = convert(argv[2]),
112+
t = convert(argv[3]);
117113
if (bin.size() == 0 || nodes.size() == 0 || t.size() == 0)
118114
handle_error();
119115
int des = t[0];

archive/c/c-plus-plus/duplicate-character-counter.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,19 @@ int main(int argc, char *argv[])
1212
{
1313

1414
if (argc != 2)
15-
{
1615
handle_error();
17-
}
1816
string inputStr(argv[1]);
1917

2018
if (inputStr.size() == 0)
21-
{
2219
handle_error();
23-
}
2420

2521
unordered_map<char, int> m1;
2622

2723
for (auto x : inputStr)
28-
{
2924
if (m1.find(x) == m1.end())
30-
{
3125
m1.insert({x, 1});
32-
}
3326
else
34-
{
3527
m1[x]++;
36-
}
37-
}
3828
int flag = 1;
3929
for (int i = 0; i < inputStr.length(); i++)
4030
{

archive/c/c-plus-plus/even-odd.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@ using namespace std;
66

77
int main(int argc, char **argv)
88
{
9-
if (argc == 1 || argv[1][0] == '\0' || (atoi(argv[1]) == 0 && strcmp(argv[1], "0") != 0))
9+
if (argc == 1
10+
|| argv[1][0] == '\0'
11+
|| (atoi(argv[1]) == 0 && strcmp(argv[1], "0") != 0))
1012
{
1113
cout << "Usage: please input a number\n";
1214
}
1315
else
1416
{
1517
int input = atoi(argv[1]);
1618
if (input % 2 == 0)
17-
{
1819
cout << "Even\n";
19-
}
2020
else
21-
{
2221
cout << "Odd\n";
23-
}
2422
}
2523

2624
return 0;

0 commit comments

Comments
 (0)