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

C++/QT 1

题目1:自定义实现字符串类

题目描述:
实现一个简单的自定义字符串类 MyString,要求: 
1】支持构造函数、拷贝构造函数、赋值运算符
2】实现基本的字符串操作:长度、拼接、比较
3】 实现对MyString对象的输入、输出
4】 正确处理内存管理

#include <iostream>
#include <cstring>using namespace std;class MyString
{friend istream &operator>>(istream &cin, MyString &s);friend ostream &operator<<(ostream &cout, const MyString &s);
private:char *str;size_t length;
public://无参构造函数MyString():str(new char[1]), length(0){str[0] = '\0';}//有参构造函数MyString(const char *s){if(s==nullptr){str = new char[1];str[0] = '\0';length = 0;}else{length = strlen(s);str = new char[length+1];strcpy(str,s);}}//拷贝构造函数MyString(const MyString &other){length = other.length;str = new char[length+1];strcpy(str, other.str);}//赋值MyString &operator=(const MyString &other){if(this != &other){delete[] str;length = other.length;str = new char[length+1];strcpy(str, other.str);}return *this;}MyString &operator=(const char *s){if(s==nullptr){delete[] str;length = 0;str = new char[1];str[0] = '\0';}else{delete[] str;length = strlen(s);str = new char[length+1];strcpy(str,s);}return *this;}//长度size_t size() const{return length;}//拼接MyString operator+(const MyString &other){char *new_str = new char[length+other.length+1];strcpy(new_str, str);strcat(new_str, other.str);MyString result(new_str);delete[] new_str;return result;}//比较bool operator<(const MyString& other) const{return strcmp(str, other.str) < 0;}bool operator>(const MyString& other) const{return strcmp(str, other.str) > 0;}bool operator==(const MyString& other) const{return strcmp(str, other.str) == 0;}bool operator!=(const MyString& other) const{return strcmp(str,other.str)!=0;}//析构函数~MyString(){delete[] str;}};//对象的输入
istream &operator>>(istream &cin, MyString &s)
{char buf[1024]={0};cin>>buf;s = MyString(buf);return cin;
}//对象的输出
ostream &operator<<(ostream &cout, const MyString &s)
{cout << s.str;return cout;
}int main()
{MyString s1;MyString s2 = "hello";MyString s3 = s2;cout << s3 << endl;s3 = " world";cout << s3 << endl;MyString s4;s4 = s2+s3;cout << s4 << endl;if(s1<s2){cout << "s1<s2" << endl;}cout << s4.size() << endl;return 0;
}


题目2:智能图书管理系统

题目描述:
实现一个简单的图书管理系统,要求
1】使用类和对象来管理图书
2】每本图书有书名、作者、和库存数量
3】实现添加图书、借书、还书和查询
4】使用STL容器存储图书信息

#include <iostream>
#include <vector>using namespace std;class Library
{friend void show2(vector<Library> &v);friend ostream &operator<<(ostream &cout, const Library &a);friend void fun(string a, vector<Library> &v);friend void fun2(string a, vector<Library> &v);friend void show(vector<Library> &v);
private:string name;string writer;int number;static int left;
public:Library(){}Library(string name, string writer, int number):name(name), writer(writer), number(number){}//添加void add(){string n;cout << "输入书名:";getline(cin, n);name = n;cout << "输入作者:";getline(cin, n);writer = n;int a;cout << "输入添加数量:";cin >> a;number = a;left+=number;}//借书void borrow(){if(number==0){cout << "当前无该图书" << endl;}if(number>0){cout << "允许借书" << endl;number--;left--;}}//还书void send_back(){number++;left++;cout << "已送还" << endl;}//查询
//    void show()
//    {
//        cout <<"书名:" << name << " 作者:"<< writer << " 剩余数量:" << number << endl;
//    }};int Library::left=0;ostream &operator<<(ostream &cout, const Library &a)
{cout <<"书名:" << a.name << "\t作者:"<< a.writer << "\t剩余数量:" << a.number << endl;return cout;
}void show(vector<Library> &v){vector<Library>::iterator p;for(p=v.begin(); p!=v.end(); p++){cout << *p;}cout << "书库当前一共有:" << Library::left << "本书" << endl;}void fun(string a, vector<Library> &v)
{vector<Library>::iterator p;for(p=v.begin(); p!=v.end(); p++){if(p->name == a){p->number++;Library::left++;cout << "已送还" << endl;}}
}void fun2(string a, vector<Library> &v)
{vector<Library>::iterator p;for(p=v.begin(); p!=v.end(); p++){if(p->name == a){if(p->number==0){cout << "当前无该图书" << endl;}if(p->number>0){cout << "允许借书" << endl;p->number--;Library::left--;}}}
}int main()
{vector<Library> v;int choice=-1;while(choice!=5){cout << "1.添加" << endl;cout << "2.借书" << endl;cout << "3.还书" << endl;cout << "4.查询" << endl;cout << "5.退出" << endl;cin >> choice;getchar();if(choice ==1){Library l1;l1.add();v.push_back(l1);}if(choice ==2){string a;cout << "输入书名:";getline(cin, a);fun2(a, v);}if(choice ==3){string a;cout << "输入书名:";getline(cin, a);fun(a, v);}if(choice ==4){show(v);}}return 0;
}


文章转载自:

http://TNbcN4bM.zxcny.cn
http://0VD9seAe.zxcny.cn
http://XBUJAVZD.zxcny.cn
http://7tB5gu5y.zxcny.cn
http://ON8Qquke.zxcny.cn
http://3Voht9bn.zxcny.cn
http://bpLTzjdM.zxcny.cn
http://8pkELY5P.zxcny.cn
http://W33fZoJT.zxcny.cn
http://1SEAUi6D.zxcny.cn
http://tAJyrIOU.zxcny.cn
http://ZpSfcxpx.zxcny.cn
http://Cwd9uGxt.zxcny.cn
http://ppGsA2J6.zxcny.cn
http://XnrNPQGS.zxcny.cn
http://wtxchLiK.zxcny.cn
http://ilpte0vZ.zxcny.cn
http://4EFn7tVL.zxcny.cn
http://NHIr7sR5.zxcny.cn
http://37dKzsWz.zxcny.cn
http://v1NTT6uM.zxcny.cn
http://xYyoO2Je.zxcny.cn
http://lE8d4a17.zxcny.cn
http://nrJ1OJl3.zxcny.cn
http://eu7tnWCX.zxcny.cn
http://Rw0WLxpb.zxcny.cn
http://I5JSzRBQ.zxcny.cn
http://QjtsawQr.zxcny.cn
http://YfyiUJI9.zxcny.cn
http://qf5TGufV.zxcny.cn
http://www.dtcms.com/a/377908.html

相关文章:

  • 软件测试用例详解
  • 【ROS2】基础概念-进阶篇
  • 三甲地市级医院数据仓湖数智化建设路径与编程工具选型研究(上)
  • 利用Rancher平台搭建Swarm集群
  • BRepMesh_IncrementalMesh 重构生效问题
  • VRRP 多节点工作原理
  • 运行 Ux_Host_HUB_HID_MSC 通过 Hub 连接 U 盘读写不稳定问题分析 LAT1511
  • Oracle体系结构-控制文件(Control Files)
  • 0303 【软考高项】项目管理概述 - 组织系统(项目型组织、职能型组织、矩阵型组织)
  • Spark-SQL任务提交方式
  • 10、向量与矩阵基础 - 深度学习的数学语言
  • 开发避坑指南(45):Java Stream 求两个List的元素交集
  • React19 中的交互操作
  • 阿里云ECS vs 腾讯云CVM:2核4G服务器性能实测对比 (2025)
  • 网络编程;TCP多进程并发服务器;TCP多线程并发服务器;TCP网络聊天室和UDP网络聊天室;后面两个还没写出来;0911
  • STM32项目分享:基于stm32的室内环境监测装置设计与实现
  • 利用归并算法对链表进行排序
  • GPU 服务器压力测试核心工具全解析:gpu-burn、cpu-burn 与 CUDA Samples
  • Power Automate List Rows使用Fetchxml查询的一个bug
  • Zynq开发实践(FPGA之ddr sdram读写)
  • LeetCode 热题 160.相交链表(双指针)
  • 西门子 S7-200 SMART PLC 编程:转换 / 定时器 / 计数器指令详解 + 实战案例(案例篇)
  • SAM-Med3D:面向三维医疗体数据的通用分割模型(文献精读)
  • 考研复习-计算机网络-第五章-传输层
  • win11安装jdk8-u211-windows
  • 从传统到智能:3D 建模流程的演进与 AI 趋势 —— 以 Blender 为例
  • 开发避坑指南(46):Java Stream 对List的BigDecimal字段进行求和
  • 在OpenHarmony上适配图形显示【3】——添加显示接口类型
  • Node.js 模块化规范详解
  • Neo4j--登录及简单操作