目录
1、冒泡排序的练习
2、时钟类的练习
3、时钟类的升级版(具备判断用户输入的时间是否正确的功能)
4、泳池类
5、利用数组统计学生成绩
6、利用数组统计学生成绩(改版1:输入名字查询总分)
7、点类和圆类的练习(判断点是否在圆上)
8、结构体的练习
2022.04.27
1、冒泡排序的练习
#include
using namespace std;
//由高到低
int main()
{
int temp;
int arr[9] = {1,2,3,4,5,6,7,8,9};
for (int i=8;i>=0;i--)
{
for (int j = i - 1; j >= 0; j--)
{
if (arr[j] > arr[i])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
cout << arr[i] << endl;//i是倒着来的,先输出arr【8】后arr【7】...
}
system("pause");
return 0;
}
2、时钟类的练习
#include
using namespace std;
class Clock
{
public:
void settime(int newH, int newM, int newS)
{
hour = newH;
minute = newM;
second = newS;
}
void showtime()
{
cout <<"现在的时间是" <> a;
cout << "请输入分钟" << endl;
cin >> b;
cout << "请输入秒数" << endl;
cin >> c;
myclock.settime(a, b, c);
myclock.showtime();
system("pause");
return 0;
}
3、时钟类的升级版(具备判断用户输入的时间是否正确的功能)
#include
using namespace std;
class Clock
{
public:
void settime(int newH, int newM, int newS)
{
hour = newH;
minute = newM;
second = newS;
}
void showtime()
{
cout << "现在的时间是" << hour << ":" << minute << ":" << second << endl;
}
private:
int hour, minute, second;
};
int main()
{
Clock myclock;
cout << "现在时间为" << endl;
myclock.settime(0, 0, 0);
myclock.showtime();
cout << "第一次调试时间" << endl;
int a, b, c;
cout << "请输入小时(24小时制)" << endl;
cin >> a;
while (a < 0 || a > 23)
{
cout << "请输入正确的小时" << endl;
cin >> a;
}
cout << "请输入分钟" << endl;
cin >> b;
while (b < 0 || b > 59)
{
cout << "请输入正确的分钟" << endl;
cin >> b;
}
cout << "请输入秒数" << endl;
cin >> c;
while (c < 0 || c > 59)
{
cout << "请输入正确的秒数" << endl;
cin >> c;
}
myclock.settime(a, b, c);
myclock.showtime();
system("pause");
return 0;
}
#includeusing namespace std; class Clock { public: void settime(int newH, int newM, int newS) { hour = newH; minute = newM; second = newS; } void showtime() { cout <<"现在的时间是" < > a; cout << "请输入分钟" << endl; cin >> b; cout << "请输入秒数" << endl; cin >> c; myclock.settime(a, b, c); myclock.showtime(); system("pause"); return 0; }
3、时钟类的升级版(具备判断用户输入的时间是否正确的功能)
#include
using namespace std;
class Clock
{
public:
void settime(int newH, int newM, int newS)
{
hour = newH;
minute = newM;
second = newS;
}
void showtime()
{
cout << "现在的时间是" << hour << ":" << minute << ":" << second << endl;
}
private:
int hour, minute, second;
};
int main()
{
Clock myclock;
cout << "现在时间为" << endl;
myclock.settime(0, 0, 0);
myclock.showtime();
cout << "第一次调试时间" << endl;
int a, b, c;
cout << "请输入小时(24小时制)" << endl;
cin >> a;
while (a < 0 || a > 23)
{
cout << "请输入正确的小时" << endl;
cin >> a;
}
cout << "请输入分钟" << endl;
cin >> b;
while (b < 0 || b > 59)
{
cout << "请输入正确的分钟" << endl;
cin >> b;
}
cout << "请输入秒数" << endl;
cin >> c;
while (c < 0 || c > 59)
{
cout << "请输入正确的秒数" << endl;
cin >> c;
}
myclock.settime(a, b, c);
myclock.showtime();
system("pause");
return 0;
}
现在程序有个bug,就是当输入小数的时候会出错。可以在先将a声明为浮点型,在while条件语句中加入abs(a-(int)a!=0)的情况(abs为取绝对值的符号),【但在这里因为a是正数,所以可以不用取绝对值,a恒>=(int)a,】这样就可以解决这一问题。
float a,b,c;
while (a < 0 || a > 23||a-(int)a!=0)
while (b < 0 || b > 59||b-(int)b!=0)
while (c < 0 || c > 59||c-(int)c!=0)
2022.04.28
4、泳池类
#include
using namespace std;//利用类求出泳池、过道的面积
float PI = 3.14;
class circle {
public:
circle(float r);
float C() {
return 2 * PI * radius;//类内可以互访,类外不可以访问private。
}
float S() {
return radius * radius * PI;
}
private:
float radius;
};
circle::circle(float r) {
radius = r;
}
int main()
{
float radius;
float distence;
cout << "请输入大圆的半径" << endl;
cin >> radius;
cout << "请输入大圆与泳池间的距离" << endl;
cin >> distence;
circle pool(radius);
circle middle(radius - distence);
cout << "大圆的周长为" << pool.C()<
5、利用数组统计学生成绩
#include
using namespace std;
#include
int main()
{
int arr[3][3] =
{
{60,60,70},{80,60,90},{85,85,85}
};
int zongfen[3];
string name[3] = {"张三","李四","王五"};//不要忘记包含string头文件
for (int i = 0; i < 3; i++)//求每个人的总分
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += arr[i][j];
}
zongfen[i] = sum;
}
//用冒泡排序对总分进行由高到低排序
for (int a = 2; a >= 0; a--)
{
for (int b = a - 1; b >= 0; b--)
{
if (zongfen[a] > zongfen[b])
{
int temp = zongfen[b];
zongfen[b] = zongfen[a];
zongfen[a] = temp;
}
}
}
for (int i = 0; i <=2; i++)
{
cout << name[i] << "的成绩为" << zongfen[i] << ",排名为 " << i + 1 << endl;
}
system("pause");
return 0;
}
6、利用数组统计学生成绩(改版1:输入名字查询总分)
#include
using namespace std;
#include
int main()
{
int arr[3][3] =
{
{60,60,70},{80,60,90},{85,85,85}
};
int zongfen[3];
string name[3] = {"张三","李四","王五"};
for (int i = 0; i < 3; i++)//求每个人的总分
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += arr[i][j];
}
zongfen[i] = sum;
}
//用冒泡排序对总分进行由高到低排序
for (int a = 2; a >= 0; a--)
{
for (int b = a - 1; b >= 0; b--)
{
if (zongfen[a] > zongfen[b])//gaodao低
{
int temp = zongfen[b];
zongfen[b] = zongfen[a];
zongfen[a] = temp;
}
}
}
cout << "请输入你的名字" << endl;
string x;
cin >> x;
while (x != "张三"&& x != "李四" && x != "王五")
//易错点:是&&而不是||,若是||则至多满足一个条件,至少有两个条件无法满足,则无论你输入啥,都提醒你输错了。
{
cout << "你不是本班同学,或者请你检查输入的名字是否有误" << endl;
cin >> x;
}
if (x == "张三")
{
cout << x << "同学,你的成绩为" << zongfen[1] <<",排名第2" <
缺陷:
1、排名暂时没想到怎么智能给出,虽然人为手输可以在结尾加个排名,但是当一个班有60个学生时,这并不现实。
但是第五个练习(5、利用数组统计学生成绩)也就是未改版的可以智能输出全班(不论多少人,只需导入名字跟成绩即可)的总分和排名。
晚点看看能不能想个办法将二者结合。
2022.04.29-30
7、点类和圆类的练习(判断点是否在圆上)
#include
using namespace std;
class point
{
public:
int newx = 0;
int newy = 0;
void setpoint()//如果这里newx、newy没有赋初值,那么下面调用函数的时候就会提醒说使用的参数太少了。
{
cout << "请输入点的横坐标" << endl;
cin >> newx;
x = newx;
cout << "请输入点的纵坐标" << endl;
cin >> newy;
y = newy;
}
void setcenter()
{
cout << "请输入圆心的横坐标" << endl;
cin >> newx;
x = newx;
cout << "请输入圆心的纵坐标" << endl;
cin >> newy;
y = newy;
}
void showpoint()
{
cout << "该点的坐标为(" << x << "," << y << ")" << endl;
}
private:
int x, y;
};
class circle
{
public:
point center;
int r;
void setr()
{
cout << "请输入圆的半径" << endl;
cin >> r;
R = r;
}
private:
int R;
};
int main()
{
cout << "该程序将判断点与圆的关系" << endl;
point p1;
p1.setpoint();
p1.showpoint();
circle yuan1;
yuan1.center.setcenter ();
yuan1.setr ();
int d = (p1.newx - yuan1.center.newx)*( p1.newx - yuan1.center.newx)+(p1.newy - yuan1.center.newy) * (p1.newy - yuan1.center.newy);
if (d == yuan1.r * yuan1.r)
{
cout << "点在圆上" << endl;
}
else if (d < yuan1.r * yuan1.r)
{
cout << "点在圆内" << endl;
}
else if (d > yuan1.r * yuan1.r)
{
cout << "点在圆外" << endl;
}
system("pause");
return 0;
}
2022.05.01
8、结构体的练习
#include
using namespace std;
#include
struct student
{
string name;
int age;
int sorce;
};
struct teacher
{
string name;
int age;
student s1;
};
void show(teacher * t1)
{
cout << t1->name << "的年龄为" << t1->age << ",他的学生为" << t1->s1.name << ",年龄为" << t1->s1.age << ",成绩为" << t1->s1.sorce << endl;
}
int main()
{
student s1 = { "张三",18,80 };
teacher t1 = { "老王",50,s1 };
show(&t1);
system("pause");
return 0;
}
笔记:1、结构体属于用户自定义的数据类型,指针的语法为【数据类型*指针名】
2、除了结构体定义处struct不可省略,其他地方可以省略,直接输入自定义的数据类型即可使用
3、指针访问要用"->"
4、值传递占用内存空间大(因为值传递拷贝了一个对象的所有数据),而地址传递的指针只占4字节?)但是地址传递有可能会误改原来的值,不想误改的话指针要用常量指针。
5、利用数组统计学生成绩
#include
using namespace std;
#include
int main()
{
int arr[3][3] =
{
{60,60,70},{80,60,90},{85,85,85}
};
int zongfen[3];
string name[3] = {"张三","李四","王五"};//不要忘记包含string头文件
for (int i = 0; i < 3; i++)//求每个人的总分
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += arr[i][j];
}
zongfen[i] = sum;
}
//用冒泡排序对总分进行由高到低排序
for (int a = 2; a >= 0; a--)
{
for (int b = a - 1; b >= 0; b--)
{
if (zongfen[a] > zongfen[b])
{
int temp = zongfen[b];
zongfen[b] = zongfen[a];
zongfen[a] = temp;
}
}
}
for (int i = 0; i <=2; i++)
{
cout << name[i] << "的成绩为" << zongfen[i] << ",排名为 " << i + 1 << endl;
}
system("pause");
return 0;
}
6、利用数组统计学生成绩(改版1:输入名字查询总分)
#include
using namespace std;
#include
int main()
{
int arr[3][3] =
{
{60,60,70},{80,60,90},{85,85,85}
};
int zongfen[3];
string name[3] = {"张三","李四","王五"};
for (int i = 0; i < 3; i++)//求每个人的总分
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += arr[i][j];
}
zongfen[i] = sum;
}
//用冒泡排序对总分进行由高到低排序
for (int a = 2; a >= 0; a--)
{
for (int b = a - 1; b >= 0; b--)
{
if (zongfen[a] > zongfen[b])//gaodao低
{
int temp = zongfen[b];
zongfen[b] = zongfen[a];
zongfen[a] = temp;
}
}
}
cout << "请输入你的名字" << endl;
string x;
cin >> x;
while (x != "张三"&& x != "李四" && x != "王五")
//易错点:是&&而不是||,若是||则至多满足一个条件,至少有两个条件无法满足,则无论你输入啥,都提醒你输错了。
{
cout << "你不是本班同学,或者请你检查输入的名字是否有误" << endl;
cin >> x;
}
if (x == "张三")
{
cout << x << "同学,你的成绩为" << zongfen[1] <<",排名第2" <
缺陷:
1、排名暂时没想到怎么智能给出,虽然人为手输可以在结尾加个排名,但是当一个班有60个学生时,这并不现实。
但是第五个练习(5、利用数组统计学生成绩)也就是未改版的可以智能输出全班(不论多少人,只需导入名字跟成绩即可)的总分和排名。
晚点看看能不能想个办法将二者结合。
2022.04.29-30
7、点类和圆类的练习(判断点是否在圆上)
#include
using namespace std;
class point
{
public:
int newx = 0;
int newy = 0;
void setpoint()//如果这里newx、newy没有赋初值,那么下面调用函数的时候就会提醒说使用的参数太少了。
{
cout << "请输入点的横坐标" << endl;
cin >> newx;
x = newx;
cout << "请输入点的纵坐标" << endl;
cin >> newy;
y = newy;
}
void setcenter()
{
cout << "请输入圆心的横坐标" << endl;
cin >> newx;
x = newx;
cout << "请输入圆心的纵坐标" << endl;
cin >> newy;
y = newy;
}
void showpoint()
{
cout << "该点的坐标为(" << x << "," << y << ")" << endl;
}
private:
int x, y;
};
class circle
{
public:
point center;
int r;
void setr()
{
cout << "请输入圆的半径" << endl;
cin >> r;
R = r;
}
private:
int R;
};
int main()
{
cout << "该程序将判断点与圆的关系" << endl;
point p1;
p1.setpoint();
p1.showpoint();
circle yuan1;
yuan1.center.setcenter ();
yuan1.setr ();
int d = (p1.newx - yuan1.center.newx)*( p1.newx - yuan1.center.newx)+(p1.newy - yuan1.center.newy) * (p1.newy - yuan1.center.newy);
if (d == yuan1.r * yuan1.r)
{
cout << "点在圆上" << endl;
}
else if (d < yuan1.r * yuan1.r)
{
cout << "点在圆内" << endl;
}
else if (d > yuan1.r * yuan1.r)
{
cout << "点在圆外" << endl;
}
system("pause");
return 0;
}
2022.05.01
8、结构体的练习
#include
using namespace std;
#include
struct student
{
string name;
int age;
int sorce;
};
struct teacher
{
string name;
int age;
student s1;
};
void show(teacher * t1)
{
cout << t1->name << "的年龄为" << t1->age << ",他的学生为" << t1->s1.name << ",年龄为" << t1->s1.age << ",成绩为" << t1->s1.sorce << endl;
}
int main()
{
student s1 = { "张三",18,80 };
teacher t1 = { "老王",50,s1 };
show(&t1);
system("pause");
return 0;
}
笔记:1、结构体属于用户自定义的数据类型,指针的语法为【数据类型*指针名】
2、除了结构体定义处struct不可省略,其他地方可以省略,直接输入自定义的数据类型即可使用
3、指针访问要用"->"
4、值传递占用内存空间大(因为值传递拷贝了一个对象的所有数据),而地址传递的指针只占4字节?)但是地址传递有可能会误改原来的值,不想误改的话指针要用常量指针。
#includeusing namespace std; #include int main() { int arr[3][3] = { {60,60,70},{80,60,90},{85,85,85} }; int zongfen[3]; string name[3] = {"张三","李四","王五"}; for (int i = 0; i < 3; i++)//求每个人的总分 { int sum = 0; for (int j = 0; j < 3; j++) { sum += arr[i][j]; } zongfen[i] = sum; } //用冒泡排序对总分进行由高到低排序 for (int a = 2; a >= 0; a--) { for (int b = a - 1; b >= 0; b--) { if (zongfen[a] > zongfen[b])//gaodao低 { int temp = zongfen[b]; zongfen[b] = zongfen[a]; zongfen[a] = temp; } } } cout << "请输入你的名字" << endl; string x; cin >> x; while (x != "张三"&& x != "李四" && x != "王五") //易错点:是&&而不是||,若是||则至多满足一个条件,至少有两个条件无法满足,则无论你输入啥,都提醒你输错了。 { cout << "你不是本班同学,或者请你检查输入的名字是否有误" << endl; cin >> x; } if (x == "张三") { cout << x << "同学,你的成绩为" << zongfen[1] <<",排名第2" < 缺陷:
1、排名暂时没想到怎么智能给出,虽然人为手输可以在结尾加个排名,但是当一个班有60个学生时,这并不现实。
但是第五个练习(5、利用数组统计学生成绩)也就是未改版的可以智能输出全班(不论多少人,只需导入名字跟成绩即可)的总分和排名。
晚点看看能不能想个办法将二者结合。
2022.04.29-30
7、点类和圆类的练习(判断点是否在圆上)
#includeusing namespace std; class point { public: int newx = 0; int newy = 0; void setpoint()//如果这里newx、newy没有赋初值,那么下面调用函数的时候就会提醒说使用的参数太少了。 { cout << "请输入点的横坐标" << endl; cin >> newx; x = newx; cout << "请输入点的纵坐标" << endl; cin >> newy; y = newy; } void setcenter() { cout << "请输入圆心的横坐标" << endl; cin >> newx; x = newx; cout << "请输入圆心的纵坐标" << endl; cin >> newy; y = newy; } void showpoint() { cout << "该点的坐标为(" << x << "," << y << ")" << endl; } private: int x, y; }; class circle { public: point center; int r; void setr() { cout << "请输入圆的半径" << endl; cin >> r; R = r; } private: int R; }; int main() { cout << "该程序将判断点与圆的关系" << endl; point p1; p1.setpoint(); p1.showpoint(); circle yuan1; yuan1.center.setcenter (); yuan1.setr (); int d = (p1.newx - yuan1.center.newx)*( p1.newx - yuan1.center.newx)+(p1.newy - yuan1.center.newy) * (p1.newy - yuan1.center.newy); if (d == yuan1.r * yuan1.r) { cout << "点在圆上" << endl; } else if (d < yuan1.r * yuan1.r) { cout << "点在圆内" << endl; } else if (d > yuan1.r * yuan1.r) { cout << "点在圆外" << endl; } system("pause"); return 0; } 2022.05.01
8、结构体的练习
#includeusing namespace std; #include struct student { string name; int age; int sorce; }; struct teacher { string name; int age; student s1; }; void show(teacher * t1) { cout << t1->name << "的年龄为" << t1->age << ",他的学生为" << t1->s1.name << ",年龄为" << t1->s1.age << ",成绩为" << t1->s1.sorce << endl; } int main() { student s1 = { "张三",18,80 }; teacher t1 = { "老王",50,s1 }; show(&t1); system("pause"); return 0; } 笔记:1、结构体属于用户自定义的数据类型,指针的语法为【数据类型*指针名】
2、除了结构体定义处struct不可省略,其他地方可以省略,直接输入自定义的数据类型即可使用
3、指针访问要用"->"
4、值传递占用内存空间大(因为值传递拷贝了一个对象的所有数据),而地址传递的指针只占4字节?)但是地址传递有可能会误改原来的值,不想误改的话指针要用常量指针。



