虽然都能运行,但是仅供参考!!!
用*//*分割,可自取
*//*
//1.1输入两个整数,计算两个数的和,然后输出计算结果。
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "输入第一个整数: "; cin >> num1;
cout << "输入第二个整数: "; cin >> num2;
int sum = num1 + num2;
cout << "两个数的和为: " << sum << endl;
return 0;
}
*//*
//1.2编写第一个C/C++程序,输出“Hello BUPT”
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello BUPT"<<endl;
}
*//*
//2.1输入3个整数,找出其中的最大值并输出。
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3;
cout << "请输入三个整数" << endl;
cin >> num1 >> num2 >> num3;
int maxNum = num1;
if (num2 > maxNum)maxNum = num2;
if (num3 > maxNum)maxNum = num3;
cout << "最大的是: " << maxNum << endl;
return 0;
}
*//*
//2.2输入一个字符,输出对应的16进制ASCII码
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"请输入一个字符:"<<endl;
cin>>ch;
cout<<"对应的16进制ASCII码为:"<<hex<<(int)ch<<endl;
return 0;
}
*//*
//3.1输入一个0-9的数字k,找出100-500哪些数各位数之和为k,输出。
#include <iostream>
using namespace std;
int main()
{
int tested_num,k;
cin>>k;
int bai,shi,ge,sum;
for(tested_num=100;tested_num<=500;tested_num++)
{
bai=(tested_num-tested_num%100)/100;
shi=(tested_num-bai*100-(tested_num-bai*100)%10)/10;
ge=tested_num-bai*100-shi*10;
sum=bai+shi+ge;
if(sum==k)
cout<<tested_num<<endl;
bai=0;
shi=0;
ge=0;
sum=0;
}
return 0;
}
*//*
//3.2输入3个整数,按从小到大的顺序输出。
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3;
cout << "请输入三个整数:" << endl;
cin >> num1 >> num2 >> num3;
int temp;
if (num1 > num2)
{temp = num1; num1 = num2; num2 = temp;}
if (num1 > num3)
{temp = num1; num1 = num3; num3 = temp;}
if (num2 > num3)
{temp = num2; num2 = num3; num3 = temp;}
cout<<"从小到大的顺序是:"
<<num1<<" "<<num2<<" "<<num3<<endl;
return 0;
}
*//*
//4.1 N位评委(<=10),每位给出1-9之间的整数
//去最高最低,计算平均值并显示,保留两位小数。
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int numJudges;
cout << "请输入评委人数(不超过10人):";
cin >> numJudges;
// 验证评委人数的有效性
if (numJudges < 2 || numJudges > 10) {
cout << "评委人数无效。请重新运行并输入有效的评委人数。" << endl;
return 0;
}
int scores[10];
for (int i = 0; i < numJudges; ++i) {
cout << "请输入第 " << i + 1 << " 位评委的评分:";
cin >> scores[i];
}
// 找到最高分和最低分的索引
int maxIndex = 0, minIndex = 0;
for (int i = 1; i < numJudges; ++i) {
if (scores[i] > scores[maxIndex]) {
maxIndex = i;
}
if (scores[i] < scores[minIndex]) {
minIndex = i;
}
}
// 去掉最高分和最低分,并计算平均值
double sum = 0.0;
for (int i = 0; i < numJudges; ++i) {
if (i != maxIndex && i != minIndex) {
sum += scores[i];
}
}
double average = sum / (numJudges - 2);
// 输出结果,保留两位小数
cout << "去掉一个最高分和一个最低分后的平均分为: " << fixed << setprecision(2) << average << endl;
return 0;
}
*//*
//4.2 计算学生总成绩
#include <iostream>
#include <string>
using namespace std;
struct Student {
int studentId;
string name;
float mathScore;
float chineseScore;
float programmingScore;
};
int main() {
const int NUM_STUDENTS = 5;
Student students[NUM_STUDENTS];
// 输入学生信息
for (int i = 0; i < NUM_STUDENTS; i++)
{
cout << "请输入第" << i + 1 << "个学生的信息:" << endl;
cout << "学号:"; cin >> students[i].studentId;
cout << "姓名:"; cin >> students[i].name;
cout << "数学成绩:";cin >> students[i].mathScore;
cout << "语文成绩:";cin >> students[i].chineseScore;
cout << "程序设计课程成绩:";cin >> students[i].programmingScore;
cout << endl;
}
// 计算并输出每个学生的总成绩
cout << "学生总成绩如下:" << endl;
for (int i = 0; i < NUM_STUDENTS; i++)
{
float totalScore = students[i].mathScore + students[i].chineseScore + students[i].programmingScore;
cout << "学号:" << students[i].studentId
<< " 姓名:" << students[i].name
<< " 总成绩:" << totalScore
<< endl;
}
return 0;
}
*//*
//5.1 递归函数
#include <iostream>
using namespace std;
int f(int x)
{
if (x == 1) {return 10;}
else {return f(x - 1) + 2;}
}
int main() {
int x;
cout << "请输入 x 的值:";
cin >> x;
int y = f(x);
cout << "f(" << x << ") = " << y << endl;
return 0;
}
*//*
//5.2 凯撒密码
#include<iostream>
using namespace std;
int main()
{
string character1 = "abcdefghijklmnopqrstuvwxyz",
character2 = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
string s1, s2;
int key;
cout<<"请输入密码"<<endl; cin >> key;
cout<<"请输入原文字母"<<endl; cin >> s1;
for (int i = 0; i < s1.length(); i++)
{
for (int j = 0; j < 26; j++)
{
if (s1[i] == character1[j])
s2 += character1[(j + key)%26];
if (s1[i] == character2[j])
s2 += character2[(j + key)%26];
}
}
cout << s2 << endl;
}
*//*
//6.1 计算向量距离
#include <iostream>
#include <cmath>
using namespace std;
double calculateEuclideanDistance(const double* vector1, const double* vector2, int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++)
{
double diff = vector1[i] - vector2[i];
sum += diff * diff;
}
return sqrt(sum);
}
int main()
{
int n;
cout << "请输入向量的维度:";
cin >> n;
double* vector1 = new double[n];
double* vector2 = new double[n];
cout << "请输入第一个向量的坐标:" << endl;
for (int i = 0; i < n; i++)
{
cout << "坐标" << i + 1 << ":";
cin >> vector1[i];
}
cout << "请输入第二个向量的坐标:" << endl;
for (int i = 0; i < n; i++)
{
cout << "坐标" << i + 1 << ":";
cin >> vector2[i];
}
double distance = calculateEuclideanDistance(vector1, vector2, n);
cout << "两个向量之间的欧氏距离为:" << distance << endl;
delete[] vector1;
delete[] vector2;
return 0;
}
*//*
//6.2 求数组的平均值
#include <iostream>
using namespace std;
double get_average(int* arr, int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += *(arr + i);
}
return static_cast<double>(sum) / size;
}
double get_average(int** arr, int rows, int cols)
{
int sum = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sum += *(*(arr + i) + j);
}
}
return static_cast<double>(sum) / (rows * cols);
}
int main()
{
int a[5], b[2][3];
cout << "请输入一维数组的值:";
for (int i = 0; i < 5; i++)
cin >> a[i];
// 计算一维数组的平均值并输出
double avg1D = get_average(a, 5);
cout << "一维数组的平均值为:" << avg1D << endl;
// 读取二维数组的值
cout << "请输入二维数组的值:";
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
cin >> b[i][j];
}
// 计算二维数组的平均值并输出
double avg2D = get_average(reinterpret_cast<int**>(b), 2, 3);
cout << "二维数组的平均值为:" << avg2D << endl;
return 0;
}
*//*
//7.1 设计学生类并编程测试
#include <iostream>
using namespace std;
class Student
{
private:
int studentID;
string name;
int age;
float score;
public:
Student(int id = 2021001, string studentName = "Lili", int studentAge = 19, float studentScore = 89.5) {
studentID = id;
name = studentName;
age = studentAge;
score = studentScore;
}
~Student()
{
cout << "学号:" << studentID << ",姓名:" << name << ",~~~" << endl;
}
};
int main()
{
// 创建一个不带参数的对象
Student student1;
// 读取用户输入信息作为构造函数的参数,再创建一个对象
int id;
string name;
int age;
float score;
cout << "请输入学生的学号:";cin >> id;
cout << "请输入学生的姓名:";cin >> name;
cout << "请输入学生的年龄:";cin >> age;
cout << "请输入学生的成绩:";cin >> score;
Student student2(id, name, age, score);
return 0;
}
*//*
//7.2 学生信息输出
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
int studentId;
string name;
int age;
double score;
Student(int id = 2021001, string n = "Lili", int a = 19, double s = 89.5)
{
studentId = id;
name = n;
age = a;
score = s;
}
void displayInfo()
{
cout << "学号:" << studentId << endl;
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "成绩:" << score << endl;
}
};
void printStudentInfo(Student stu)
{
cout << "普通函数输出的学生信息:" << endl;
cout << "学号:" << stu.studentId << endl;
cout << "姓名:" << stu.name << endl;
cout << "年龄:" << stu.age << endl;
cout << "成绩:" << stu.score << endl;
}
int main()
{
Student stu;
cout << "类成员函数输出的学生信息:" << endl;
stu.displayInfo();
cout << endl;
printStudentInfo(stu);
return 0;
}
*//*
//8.1 设计基类和派生类
#include <iostream>
#include <string>
using namespace std;
class Student
{
protected:
int studentId;
string name;
int age;
double score;
public:
Student(int id = 2021001, string n = "Lili", int a = 19, double s = 89.5)
{
studentId = id;
name = n;
age = a;
score = s;
}
~Student()
{
cout << "学号:" << studentId << ",姓名:" << name << ",";
cout << "~~~" << endl;
}
};
class Graduate : public Student {
private:
string researchDirection;
public:
Graduate() : Student() {
cout << "construction" << endl;
cout << "请输入研究方向:";
cin >> researchDirection;
}
~Graduate() {
cout << "学号:" << studentId << ",姓名:" << name << ",研究方向:" << researchDirection << ",";
cout << "~~~" << endl;
}
};
int main()
{
Student stu;
Graduate grad;
return 0;
}
*//*
//8.2 基类指针指向派生类对象
#include <iostream>
#include <string>
using namespace std;
class Student
{
protected:
int studentId;
string name;
int age;
double score;
public:
Student(int id = 2021001, string n = "Lili", int a = 19, double s = 89.5)
{
studentId = id;
name = n;
age = a;
score = s;
}
void pr()
{
cout << "学号:" << studentId << ",姓名:" << name << ",年龄:" << age << ",成绩:" << score << endl;
}
};
class Graduate : public Student
{
private:
string researchDirection;
public:
Graduate() : Student()
{
cout << "construction" << endl;
cout << "请输入研究方向:";
cin >> researchDirection;
}
void pr()
{
cout << "学号:" << studentId << ",姓名:" << name << ",研究方向:" << researchDirection << endl;
}
};
int main()
{
Student* stu = new Graduate();
stu->pr();
delete stu;
return 0;
}
*//*
//9.1 复数相加
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(double r = 1, double i = 1) : real(r), imag(i) {}
Complex operator+(const Complex &other)
{
return Complex(real + other.real, imag + other.imag);
}
void print()
{
cout << real << "+" << imag << "i" << endl;
}
};
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
if (c == -1000)
{
Complex x(a, b);
Complex y; // 默认构造
Complex z = x + y; // 调用 operator+
z.print();
}
else
{
Complex x(a, b);
Complex y(c, d);
Complex z = x + y; // 调用 operator+
z.print();
}
return 0;
}
*//*
//9.2 类的组合应用
#include <iostream>
using namespace std;
class point
{
private:
int x;
int y;
int color;
public:
point(int _x=0, int _y=0, int _color=255):x(_x), y(_y), color(_color) {
cout << "point(" << x << "," << y << "," << color << ")" << endl;
}
~point()
{
cout << "point~~~" << endl;
}
int getx() const { return x; }
int gety() const { return y; }
int getColor() const { return color; }
};
class rectangle
{
private:
point topLeft;
int width;
int height;
public:
rectangle(int x=0, int y=0, int w=1, int h=1):topLeft(x, y), width(w), height(h) {
cout << "rect" << endl;
}
~rectangle()
{
cout << "rect~~~" << endl;
}
void printInfo() const
{
cout << "x: " << topLeft.getx() << endl;
cout << "y: " << topLeft.gety() << endl;
cout << "width: " << width << endl;
cout << "height: " << height << endl;
}
};
int main()
{
int x, y, w, h;
cout << "Please enter the values of x, y, width and height:" << endl;
cin >> x >> y >> w >> h;
rectangle r(x, y, w, h);
r.printInfo();
return 0;
}
*//*
//10.1 函数模板练习
#include <iostream>
using namespace std;
template<typename T>
void bubbleSort(T arr[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
}
}
int main()
{
int intArr[10];
double doubleArr[10];
cout << "Please enter 10 integers:" << endl;
for (int i = 0; i < 10; i++)
cin >> intArr[i];
cout << "Please enter 10 double values:" << endl;
for (int i = 0; i < 10; i++)
cin >> doubleArr[i];
bubbleSort(intArr, 10);
bubbleSort(doubleArr, 10);
cout << "Sorted integer array: ";
for (int i = 0; i < 10; i++)
cout << intArr[i] << " ";
cout << endl;
cout << "Sorted double array: ";
for (int i = 0; i < 10; i++)
cout << doubleArr[i] << " ";
cout << endl;
return 0;
}
*//*
//10.2 虚函数和抽象类练习
#include <iostream>
using namespace std;
class shape
{
protected:
int x;
int y;
public:
shape(int x = 0, int y = 0) : x(x), y(y) {}
virtual ~shape() { cout << "~~~shape" << endl; }
virtual double area() const = 0;
};
class circle : public shape
{
private:
double radius;
public:
circle(double radius, int x = 0, int y = 0) : shape(x, y), radius(radius) {}
virtual ~circle() { cout << "~~~circle" << endl; }
virtual double area() const { return 3.14159 * radius * radius; }
};
int main()
{
shape* p = new circle(5.0);
cout << "Area: " << p->area() << endl;
delete p;
return 0;
}
*/