当前位置: 首页 > news >正文

C++实验 面向对象编程

一、实验目的:

掌握类中静态成员的定义方法,初始化方法,使用方法;
掌握类的友元说明方法,理解友元的使用特点

二、实验内容:

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;elsecout << "已退出" << 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;
}
http://www.lryc.cn/news/318606.html

相关文章:

  • VC++ 设置网卡接口MTU大小
  • dpdk-19.11 对向量指令的使用情况分析
  • 使用CIP采集欧姆龙EtherNet/IP从入门到精通
  • 企业如何高效管理微信里的客户?
  • 怎么在windows系统上查看kylinos的md5、sha1、sha256值
  • Windows中在C#中使用Dapper和Mysql.Data库连接MySQL数据库
  • 大一专科,物联网专业,变态成长偏方!
  • MyBatis入门(JDBC规范,MyBatis,连接池,Lombok)【详解】
  • Vue3--数据和方法
  • 网络编程面试题
  • 移动端区分点击和长按
  • 虚拟环境的激活
  • 宏集案例 | 风电滑动轴承齿轮箱内多点温度采集与处理
  • linux 16进制写入
  • 代码随想录算法训练营第60天| Leetcode 84.柱状图中最大的矩形
  • 编写一个简单的cmakelist.txt
  • 基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的零售柜商品检测软件(Python+PySide6界面+训练代码)
  • 数据库的学习
  • matlab去除图片上的噪声
  • C++超详细知识点(五):类的友元函数和友元类
  • SOC设计:关于reset的细节
  • 支小蜜AI校园防欺凌系统可以使用在宿舍吗?
  • 外卖平台订餐流程架构的实践
  • [AIGC] Spring Boot中的切面编程和实例演示
  • 各个类型和Json类型的相互转换
  • C语言:操作符详解(下)
  • 电商场景下 ES 搜索引擎的稳定性治理实践
  • jdk8与jdk17的区别。springboot2.x与springboot3.x的区别
  • Pytest测试中的临时目录与文件管理!
  • arduino 编程esp8266