当然,我可以帮你整理一下你提供的C++代码,并为每个代码片段添加相应的标题。请看下面的整理:
1. 计算两点之间的距离
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int x1, x2, y1, y2;
cout << "请输入x1, x2, y1, y2的值"; cin >> x1 >> x2 >> y1 >> y2;
cout << "两点之间的距离为: " << sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
return 0;
}
2. 成绩评定
#include <iostream>
using namespace std;
int main() {
float score;
char grade;
cout << "输入分数"; cin >> score;
while (score > 100 || score < 0) {
cout << "数据错误" << endl;
cin >> score;
}
switch (int(score / 10)) {
case 10:
case 9: grade = 'A'; break;
case 8: grade = 'B'; break;
case 7: grade = 'C'; break;
case 6: grade = 'D'; break;
default: grade = 'E'; break;
}
cout << "分数等级: " << grade;
return 0;
}
3. 完数
#include <iostream>
using namespace std;
int main() {
int m, i, s;
for (m = 2; m < 100; m++) {
s = 0;
for (i = 1; i < m; i++)
if (m % i == 0)
s += i;
if (s == m) cout << m << "是完数" << endl;
}
return 0;
}
4. 最大、最小、平均值
#include <iostream>
using namespace std;
int main() {
int a, max, min, s;
cin >> a;
max = min = s = a;
for (int i = 1; i < 20; i++) {
cin >> a;
if (max < a) max = a;
if (min > a) min = a;
s += a;
}
cout << "最大值: " << max << ", 最小值: " << min << ", 平均值: " << s / 20 << endl;
return 0;
}
5. 正数、负数、零的个数
#include<iostream>
using namespace std;
int main() {
int n = 0, m = 0;
for (int i = 0; i < 20; i++) {
int a;
cin >> a;
if (a > 0) n++;
else if (a < 0) m++;
}
cout << "正数个数: " << n << " 负数个数: " << m << endl;
return 0;
}
6. 奇数和、偶数和
#include<iostream>
using namespace std;
int main() {
int n = 0, m = 0;
for (int i = 1; i < 51; i++)
if (i % 2) n += i;
else m += i;
cout << "奇数和: " << n << " 偶数和: " << m;
return 0;
}
7. 素数
#include <iostream>
using namespace std;
bool isprime(int num) {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
int main() {
int count = 0;
for (int i = 2; i <= 1000; i++)
if (isprime(i)) {
cout << i << '\t';
count++;
if (count % 5 == 0) cout << endl;
}
cout << endl;
return 0;
}
8. 找最大值
#include <iostream>
using namespace std;
int max(int a, int b, int c = 0) {
if (a < b) a = b;
if (a < c) a = c;
return a;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
cout << "max(a, b, c): " << max(a, b, c) << endl;
cout << "max(a, b): " << max(a, b) << endl;
return 0;
}
9. 类与对象 - 计算体积
#include <iostream>
using namespace std;
class Box {
int length, width, height;
public:
void get_value() {
cout << "输入长度 宽度 高度: " << endl;
cin >> length >> width >> height;
}
float display() {
cout << length * width * height << endl;
}
};
int main() {
Box box1, box2, box3;
box1.get_value();
cout << "box1的体积为 ";
box1.display();
box2.get_value();
cout << "box2的体积为 ";
box2.display();
box3.get_value();
cout << "box3的体积为 ";
box3.display();
return 0;
}
10. 类与对象 - 时间显示
#include <iostream>
using namespace std;
class Time {
int hour, minute, second;
public:
Time(int h, int m, int s) {
hour = h; minute = m; second = s;
}
void show_time() {
cout << hour << ":" << minute << ":" << second << endl;
}
};
int main() {
Time t(9, 10, 0);
t.show_time();
return 0;
}
11. 类与对象 - 长方体比较
#include <iostream>
using namespace std;
class Cuboid {
int length, width, height;
public:
Cuboid(int l = 3, int w = 4, int h = 5) {
length = l; width = w; height = h;
}
int getL() { return length; }
int getW() { return width; }
int getH() { return height; }
int getArea() {
return length * width * height;
}
bool isEqual(Cuboid &b) {
return length == b.getL() && width == b.getW() && height == b.getH();
}
};
12. 类与对象 - 矩形面积和周长
#include <iostream>
using namespace std;
class Rectangle {
float length, width;
public:
Rectangle(float l = 20, float w = 30) { length = l; width = w; }
float Area() {
return length * width;
}
float Perimeter() {
return (length + width) * 2;
}
};
13. 类与对象 - 日期
#include <iostream>
using namespace std;
class CDate {
int Year, Month, Day;
public:
CDate() { Year = 2024; Month = 1; Day = 5; }
CDate(int y = 2024, int m = 1, int d = 1) { Year = y; Month = m; Day = d; }
void PrintDate() { cout << Year << "-" << Month << "-" << Day; }
void SetDate(int sy, int sm, int sd) { Year = sy; Month = sm; Day = sd; }
};
14. 类与对象 - 日期
#include <iostream>
using namespace std;
class Date {
int Month, Day, Year;
public:
Date(int y = 2019, int m = 1, int d = 1) { Year = y; Month = m; Day = d; }
void Display() { cout << Month << '/' << Day << '/' << Year; }
};
15. 类与对象 - 复数相加
#include <iostream>
using namespace std;
class Complex {
double real, image;
public:
Complex(double r, double i) { real = r; image = i; }
void Show() { cout << real << ',' << image << endl; }
Complex operator+(Complex &c2) {
return Complex(real + c2.real, image + c2.image);
}
};
16. 类与对象 - 继承
#include <iostream>
using namespace std;
class rectangle {
double length, width;
public:
rectangle(double l, double w) { length = l; width = w; }
float Area() { return length * width; }
};
class rectangular : public rectangle {
double height;
public:
rectangular(double l, double w, double h) : rectangle(l, w) { height = h; }
double volume() { return Area() * height; }
};
int main() {
rectangle obj1(2, 8);
rectangular obj2(3, 4, 5);
cout << "长方形的面积为: " << obj1.Area() << endl;
cout << "长方体的体积为: " << obj2.volume() << endl;
return 0;
}
这样应该更容易理解了。如果有任何问题或需要进一步解释,请随时告诉我。
标签:std,cout,int,编程,namespace,c++,期末,using,include From: https://www.cnblogs.com/chatBlog/p/17948180