2022/3/20
1.设计一个矩形类Rect,类数据成员有左上角的坐标值x,y,宽w,高h,要求有下述成员函数。
(1)move():从一个位置移动到另一个位置。
(2)size():改变矩形的大小。
(3)where():返回矩形右下角的坐标值。
(4)area():计算矩形的面积。
#pragma once
class Rect {
public:
Rect(double a, double b, double c, double d) {
x = a;
y = b;//左上角坐标值x,y
w = c;//宽w
h = d;//高h
}
void move(double a, double b);//移动到a,b
void size(double a, double b);//改变矩形大小
void where();//返回右下角坐标值
void area();//计算矩形面积
private:
double x;
double y;
double w;
double h;
};
#include<iostream>
#include"1.h"
using namespace std;
void Rect::move(double a, double b) {
x = a;
y = b;
}//移动到a,b
void Rect::size(double a,double b) {
w = a;
h = b;
}//改变矩形大小
void Rect::where() {
cout << "(" << x + w << "," << y - h << ")" << endl;
}//输出右下角坐标值
void Rect::area() {
cout << w * h;
}//计算矩形面积
int main() {
double x, y, w, h;
cout << "请输入矩形坐标x,y 高h 宽w:";
cin >> x >> y >> w >> h;
Rect X(x, y, w, h);
cout << "请输入移动位置坐标x,y:";
cin >> x >> y;
X.move(x, y);
cout << "请输入改变的大小w,h:";
cin >> w >> h;
X.size(w, h);
cout << "矩形右下角坐标值为:";
X.where();
cout << "矩形面积为:";
X.area();
return 0;
}
2. 定义有理数类Rational,该类存放分数形式的有理数。要求如下:
(1)该类具有默认参数的构造函数,默认值为1.
(2)定义变量x和y,分别存放分子和分母,同时分数要以最简形式存放。例如:分数2/4应简化为1/2.
(3)定义成员函数Add, Sub, Mul和Div。计算结果仍以最简形式存放。
(4)以x/y的形式打印分数。
(5)以浮点数形式打印分数。
#pragma once
class Rational{
public:
Rational(int a = 1, int b = 1) {
x = a;
y = b;
}//构造函数
void Add(int a, int b);//加
void Sub(int a, int b);//减
void Mul(int a, int b);//乘
void Div(int a, int b);//除
void hj();//化简
void out1();//以x/y形式输出
void out2();//以浮点数形式输出
private:
int x;//分子
int y;//分母
};
#include<iostream>
#include"2.h"
using namespace std;
void Rational::Add(int a, int b) {
x = x * b + y * a;
y = y * b;
hj();
}
void Rational::Sub(int a, int b) {
x = x * b - y * a;
y = y * b;
hj();
}
void Rational::Mul(int a, int b) {
x = x * a;
y = y * b;
hj();
}
void Rational::Div(int a, int b) {
x = x * b;
y = y * a;
hj();
}
void Rational::hj() {
int min = x < y ? x : y;
for (int i = 2; i <= min; i++) {
if (x % i == 0 && y % i == 0) {
x = x / i;
y = y / i;
}
}
}
void Rational::out1() {
hj();
cout << x << "/" << y << endl;
}
void Rational::out2() {
hj();
double ans = (double)x / y;
cout << ans << endl;
}
int main() {
int x, y;
cout << "请输入分子x和分母y:";
cin >> x >> y;
Rational f(x, y);
cout << "请输入加上的分数的分子和分母:";
cin >> x >> y;
f.Add(x, y);
cout << "结果为:";
f.out1();
cout << "请输入减去的分数的分子和分母:";
cin >> x >> y;
f.Sub(x, y);
cout << "结果为:";
f.out1();
cout << "请输入乘上的分数的分子和分母:";
cin >> x >> y;
f.Mul(x, y);
cout << "结果为:";
f.out1();
cout << "请输入除去的分数的分子和分母:";
cin >> x >> y;
f.Div(x, y);
cout << "结果为:";
f.out1();
cout << "以浮点数输出为:";
f.out2();
return 0;
}
3. 声明一个用于检查用户合法性及用户级别的类CheckUser,具体要求如下:
1)该类包含UserName(用户名)和UsePwd(密码)2个string 类型的属性。
2)包含一个带有两个string 类型参数的构造函数。
3)包含一个返回值为int 类型的UserLevel()方法,返回值为0 表示高级用户,为1 表示普通用户,为2 表示用户不存在。若用户名为“Administrator”,密码”123456”,则为高级用户。若用户名为“user”,密码为”000000”,则为普通用户。所有其他用户均为不合法用户。
#pragma once
#include <string>
using namespace std;
class CheckUser {
public:
CheckUser(string x, string y) {
Username = x;
UsePwd = y;
}//构造函数
int UserLevel();//检查用户合法性及用户级别
private:
string Username;//用户名
string UsePwd;//密码
};
#include<iostream>
#include<string>
#include"3.h"
using namespace std;
int CheckUser::UserLevel () {
if (Username == "Administrator" && UsePwd == "123456") {
return 0;//高级用户
}
else if (Username == "user" && UsePwd == "000000") {
return 1;//普通用户
}
else {
return 2;//用户不存在
}
}
int main() {
string x, y;
while (1) {
cout << "请输入用户名:";
cin >> x;
cout << "请输入密码:";
cin >> y;
CheckUser a(x, y);
cout << "用户级别为:" << a.UserLevel() << endl;
}
return 0;
}
4.建立一个对象数组,内放5个学生的数据(学号、总成绩)、设计一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
提示:
1)学生信息应该设置为私有,应合理设置类的成员函数。
2)将max函数定义为非成员函数。
3)使用const来限制对s的修改
#pragma once
#include <iostream>
using namespace std;
class Student {
public:
Student(string a, int b) {
num = a;
score = b;
}//构造函数
int cj()const;//返回成绩
void out()const;//输出学号
private:
string num;//学号
int score;//成绩
};
#include <iostream>
#include "4.h"
using namespace std;
int Student::cj()const{
return score;
}
void Student::out()const{
cout << num << endl;
}
void max(const Student* p1){
const Student* p2 = p1;
for (int i = 1; i < 5; i++) {
if ((p1 + i)->cj() > p2->cj()) {
p2 = p1 + i;
}
}
p2->out();
}
int main(){
const Student stu[5] = {
Student("1001",18),
Student("1002",23),
Student("1003",95),
Student("1004",83),
Student("1005",21)
};//存储学生数据
max(stu);
return 0;
}
5.输入若干个学生的姓名、英语和数学成绩,求出总分, 并按总分从高到低排序。要求设计一个学生类 Student,所有学生对象存放在一个 Student 对象数组中,最后输出排序后的结果。
提示:学会生的信息应设置为私有数据成员
#pragma once
#include<string>
using namespace std;
class Student{
public:
void setname();//输入姓名
void setscore();//输入成绩
void out();//输出
int score();//求总分
private:
string name;//姓名
int english;//英语
int math;//数学
int sum;//总分
};
#include<iostream>
#include<string>
#include"5.h"
using namespace std;
void Student::setname() {
cin >> name;
}
void Student::setscore() {
cin >> english >> math;
}
void Student::out() {
cout << name << " " << english << " " << math << " " << sum << endl;
}
int Student::score() {
sum = english + math;
return sum;
}
int main() {
Student s[100];
int n = 0;
cout << "输入学生组数:";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "请输入学生姓名:";
s[i].setname();
cout << "请输入学生英语和数学成绩:";
s[i].setscore();
s[i].score();
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (s[j].score() < s[j + 1].score()) {
Student tmp = s[j];
s[j] = s[j + 1];
s[j + 1] = tmp;
}
}
}//排序
for (int i = 0; i < n; i++) {
s[i].out();
}
return 0;
}
实验总结
- 注意私有数据和公有数据的区别和使用
- 注意析构函数的定义
- 注意类的排序和交换
- 注意函数的头文件是否添加