25.9.11 QTday1作业
一、思维导图
二、自由发挥一个应用程序的登录界面,使用ui界面实现,基本功能都需要实现。
三、
#include <iostream>
#include <cstring>
using namespace std;class Mystring
{
private:char *data;public:Mystring(){data = new char[1];data[0] = '\0'; // 空字符串}Mystring(const char* str){if (str == nullptr) {data = new char[1];data[0] = '\0';} else {int len = strlen(str);data = new char[len + 1];strcpy(data, str);}}// 从单个字符构造Mystring(char c){data = new char[2];data[0] = c;data[1] = '\0';}// 拷贝构造函数Mystring(const Mystring &other){int len = strlen(other.data);data = new char[len + 1];strcpy(data, other.data);}// 析构函数~Mystring(){delete[] data;data = nullptr;}bool operator==(const Mystring &my) const;Mystring& operator=(const Mystring &my);Mystring operator+(const Mystring &my) const;friend ostream& operator<<(ostream &os, const Mystring &my);friend istream& operator>>(istream &is, Mystring &my);
};// 输出流运算符重载
ostream& operator<<(ostream &os, const Mystring &my)
{os << my.data;return os;
}// 输入流运算符重载
istream& operator>>(istream &is, Mystring &my)
{char buffer[256];if (is >> buffer) {delete[] my.data;int len = strlen(buffer);my.data = new char[len + 1];strcpy(my.data, buffer);}return is;
}// ==运算符重载
bool Mystring::operator==(const Mystring &my) const
{return strcmp(data, my.data) == 0;
}// =运算符重载
Mystring& Mystring::operator=(const Mystring &my)
{if (this == &my)return *this;delete[] data;int len = strlen(my.data);data = new char[len + 1];strcpy(data, my.data);return *this;
}// +运算符重载
Mystring Mystring::operator+(const Mystring &my) const
{int len1 = strlen(data);int len2 = strlen(my.data);char* temp = new char[len1 + len2 + 1];strcpy(temp, data);strcat(temp, my.data);Mystring result(temp);delete[] temp;return result;
}int main()
{Mystring a("hello");Mystring b;b = a;cout << "a: " << a << endl;cout << "b: " << b << endl;cout << "a + b: " << (a + b) << endl;cout << "a == b: " << (a == b) << endl;Mystring c;cin >> c;cout << "b + c: " << (b + c) << endl;return 0;
}
四、
#include <iostream>
#include <vector>
#include <string>using namespace std;// 定义图书类
class Book {
private:string title; // 书名string author; // 作者static int totalBooks; // 静态成员:所有图书的总库存数量public:// 默认构造函数Book() : title(""), author("") {}// 带参构造函数Book(string t, string a, int q) : title(t), author(a) {totalBooks += q;}// 获取书名string getTitle() const { return title; }// 获取作者string getAuthor() const { return author; }// 静态方法:获取总库存static int getTotalBooks() { return totalBooks; }// 静态方法:增加库存static void addBooks(int q) { totalBooks += q; }// 静态方法:减少库存static void removeBook() { if (totalBooks > 0) totalBooks--; }// 显示图书信息void display() const {cout << "《" << title << "》- " << author << endl;}
};// 初始化静态成员
int Book::totalBooks = 0;// 图书馆类
class Library {
private:vector<Book> books; // 使用vector存储图书public:// 添加图书void addBook() {string title, author;int quantity;cout << "请输入书名: ";getline(cin, title);cout << "请输入作者: ";getline(cin, author);cout << "请输入数量: ";cin >> quantity;cin.ignore(); // 清除缓冲区// 检查图书是否已存在for (auto it = books.begin(); it != books.end(); ++it) {if (it->getTitle() == title && it->getAuthor() == author) {Book::addBooks(quantity);cout << "库存更新成功!当前总库存: " << Book::getTotalBooks() << endl;return;}}// 使用push_back添加新书books.push_back(Book(title, author, quantity));cout << "添加成功!当前总库存: " << Book::getTotalBooks() << endl;}// 借书void borrowBook() {string title, author;cout << "请输入书名: ";getline(cin, title);cout << "请输入作者: ";getline(cin, author);// 遍历查找图书for (auto it = books.begin(); it != books.end(); ++it) {if (it->getTitle() == title && it->getAuthor() == author) {if (Book::getTotalBooks() > 0) {Book::removeBook();cout << "借书成功!当前总库存: " << Book::getTotalBooks() << endl;} else {cout << "库存不足!" << endl;}return;}}cout << "未找到该图书!" << endl;}// 还书void returnBook() {string title, author;cout << "请输入书名: ";getline(cin, title);cout << "请输入作者: ";getline(cin, author);// 遍历查找图书for (auto it = books.begin(); it != books.end(); ++it) {if (it->getTitle() == title && it->getAuthor() == author) {Book::addBooks(1);cout << "还书成功!当前总库存: " << Book::getTotalBooks() << endl;return;}}// 如果图书不存在,使用push_back添加books.push_back(Book(title, author, 1));cout << "新书入库!当前总库存: " << Book::getTotalBooks() << endl;}// 查询图书void searchBook() {string keyword;bool found = false;cout << "请输入书名或作者关键字: ";getline(cin, keyword);cout << "查询结果:" << endl;// 使用迭代器遍历查询for (auto it = books.begin(); it != books.end(); ++it) {if (it->getTitle().find(keyword) != string::npos || it->getAuthor().find(keyword) != string::npos) {it->display();found = true;}}if (!found) {cout << "未找到相关图书!" << endl;}}// 显示所有图书(使用vector的size()和遍历)void showAllBooks() {if (books.empty()) {cout << "图书馆为空!" << endl;return;}cout << "所有图书 (" << books.size() << " 种):" << endl;// 使用索引遍历for (int i = 0; i < books.size(); i++) {cout << i + 1 << ". ";books[i].display();}cout << "总库存: " << Book::getTotalBooks() << " 本" << endl;}
};// 主函数
int main() {Library lib;int choice;// 使用vector构造函数创建初始图书vector<Book> initialBooks = {Book("C++ Primer", "Stanley Lippman", 5),Book("数据结构", "严蔚敏", 3)};// 使用vector的insert函数添加初始数据lib = Library(); // 简化处理cout << "=== 简易图书管理系统 ===" << endl;while (true) {cout << "\n1. 添加图书" << endl;cout << "2. 借书" << endl;cout << "3. 还书" << endl;cout << "4. 查询图书" << endl;cout << "5. 显示所有图书" << endl;cout << "0. 退出" << endl;cout << "请选择: ";cin >> choice;switch (choice) {case 1: lib.addBook(); break;case 2: lib.borrowBook(); break;case 3: lib.returnBook(); break;case 4: lib.searchBook(); break;case 5: lib.showAllBooks(); break;case 0: cout << "谢谢使用!" << endl;return 0;default:cout << "无效选择!" << endl;}}
}