一、实验目的:
掌握类中静态成员的定义方法,初始化方法,使用方法;
掌握类的友元说明方法,理解友元的使用特点
二、实验内容:
1、编写程序,统计某旅馆住宿客人的总数,要求输入客人姓名,输出客人编号(按先后顺序自动生成),姓名以及总人数。
#include<iostream>
#include<stdlib.h>
#include<iomanip>
using namespace std;
struct ListPeople {
int num;
string name;
struct ListPeople* next;
ListPeople(int num, string name)
{
this->num = num;
this->name = name;
}
};
void sumpeople(ListPeople* m)//输出总人数
{
int sum = 0;
while (m->next != NULL)
{
sum++;
m = m->next;
}
cout <<"总人数为:" << sum << "人" << endl;
}
int main()
{
int num = 0;
string name = "";
ListPeople* head = new ListPeople(num, name);
head->next = NULL;
int number = 0;
cout << "输入0查看客人总数" << endl;
cout << "输入1客人入住" << endl;
cout << "输入2客人退房" << endl;
cout << "输入-1退出" << endl;
while (number != -1)
{
cout << "请输入编号:";
cin >> number;
switch (number)
{
case 0://输出总人数
{
sumpeople(head);
break;
}
case 1://客人入住
{
cout << "请输入入住客人姓名:";
cin >> name;
num++;
ListPeople* q = new ListPeople(num, name);
ListPeople* p = head;
while (p->next != NULL)
p = p->next;
p->next = q;
q->next = NULL;
cout << "客人编号为:" << setw(4) << setfill('0') << q->num << ",客人姓名为:" << q->name << endl;
sumpeople(head);
break;
}
case 2://客人退房
{
ListPeople* m = head;
int flag = 0;
if (m->next == NULL)
{
cout << "目前没有客人入住,无法退房!" << endl;
break;
}
cout << "请输入退订客人姓名:";
cin >> name;
while (m->next != NULL)
{
if (m->next->name == name)
{
ListPeople* d = m->next;
m->next = m->next->next;
delete d;
cout << "客人退订成功" << endl;
sumpeople(head);
flag = 1;
break;
}
m = m->next;
}
if (flag == 0)
cout << "未查找到该客人!" << endl;
break;
}
default:
if (number != -1)
cout << "请重新输入" << endl;
else
cout << "已退出" << endl;
}
}
return 0;
}
2、编写学生类Stu,包含学生姓名,成绩,设计一个友员函数,将学生成绩按大到小排序。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Stu {
private:
string name;
float score;
public:
Stu(string name, float score) : name(name), score(score) {}
friend bool operator<(const Stu& s1, const Stu& s2);
void print() {
cout << "Name: " << name << ", Score: " << score << endl;
}
};
bool operator<(const Stu& s1, const Stu& s2) {
return s1.score > s2.score;
}
int main() {
std::vector<Stu> students;
// 添加学生信息
students.push_back(Stu("Tom", 85.5));
students.push_back(Stu("Alice", 92.0));
students.push_back(Stu("John", 78.5));
students.push_back(Stu("Emma", 95.0));
// 按成绩排序
std::sort(students.begin(), students.end());
// 输出排序结果
for (auto it : students) {
it.print();
}
return 0;
}
1、编写整型数组类 arrow,能创建任意长度数组对象,用深复制
#include <iostream>
#include <cstring>
using namespace std;
class Array {
public:
int* arr;
int size;
Array(int length) {
size = length;
arr = new int[length];
}
Array(const Array &a) {
size = a.size;
arr = new int[size];
memcpy(arr, a.arr, size*sizeof(int));
}
~Array() {
delete[] arr;
}
};
int main() {
Array a(5);
int n;
int x;
cin>>n;
for(int i=0;i<n;i++)
cin>>x,a.arr[i]=x;
Array b(a);
for(int i=0;i<n;i++)
cout << b.arr[i] << " " ;
return 0;
}
2、已知三点座标,求三角形的面积。
#include <iostream>
#include <cmath>
using namespace std;
double getTriangleArea(int x1, int y1, int x2, int y2, int x3, int y3) {
double a = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
double b = sqrt((x3-x2)*(x3-x2) + (y3-y2)*(y3-y2));
double c = sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3));
double s = (a + b + c) / 2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
int main() {
int x1 , y1 ;
int x2 , y2 ;
int x3 , y3 ;
cin>>x1>>y1;
cin>>x2>>y2;
cin>>x3>>y3;
double area = getTriangleArea(x1, y1, x2, y2, x3, y3);
cout << "Triangle area is: " << area << endl;
return 0;
}