1.
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class MyString
{
friend MyString &operator+=(MyString &L,const MyString &R);
friend ostream &operator<<(ostream &cout,const MyString &p);
friend istream &operator>>(istream &cin,MyString &p);
friend bool operator==(MyString &L,const MyString &R);
private:
char* arr;
char* brr;
size_t arr_len;
size_t brr_len;
void copyString(char* &dest,const char* src,size_t &len)
{
if(src && src[0] != '\0')
{
len = strlen(src);
dest = new char[len + 1];
strcpy(dest,src);
}
else
{
dest = nullptr;
len = 0;
}
}
void freeString(char* &str)
{
if(str)
{
delete[] str;
str = nullptr;
}
}
public:
MyString():arr(nullptr),brr(nullptr),arr_len(0),brr_len(0){}
MyString(const char* a,const char* b)
{
copyString(arr,a,arr_len);
copyString(brr,b,brr_len);
}
MyString(const MyString &other)
{
copyString(arr,other.arr,arr_len);
copyString(brr,other.brr,brr_len);
}
~MyString()
{
freeString(arr);
freeString(brr);
}
MyString &operator=(const MyString &other)
{
if(this != &other)
{
freeString(arr);
freeString(brr);
copyString(arr,other.arr,arr_len);
copyString(brr,other.brr,brr_len);
}
return *this;
}
void size() const
{
cout << "First length:" << arr_len << endl;
cout << "Second length:" << brr_len << endl;
}
void cmp() const
{
if(strcmp(arr,brr) == 0)
{
cout << "equal" << endl;
}
else
{
cout << "not equal" << endl;
}
}
void show() const
{
cout << "First:" << arr << endl;
cout << "Second:" << brr << endl;
}
void cat()
{
if(!brr) return;
size_t new_len = arr_len + brr_len;
char* new_arr = new char[new_len + 1];
new_arr[0] = '\0';
if(arr)
{
strcpy(new_arr,arr);
}
if(brr)
{
strcat(new_arr,brr);
}
freeString(arr);
arr = new_arr;
arr_len = new_len;
cout << "new:" << arr << endl;
cout << "new length:" << arr_len << endl;
}
};
MyString &operator+=(MyString &L,const MyString &R)
{
if(R.arr)
{
size_t new_len = L.arr_len + strlen(R.arr);
char* new_arr = new char[new_len + 1];
new_arr[0] = '\0';
if(L.arr)
{
strcpy(new_arr,L.arr);
}
strcat(new_arr,R.arr);
L.freeString(L.arr);
L.arr = new_arr;
L.arr_len = new_len;
}
return L;
}
ostream &operator<<(ostream &cout,const MyString &p)
{
cout << "First:" << p.arr << " Second:" << p.brr << endl;
return cout;
}
istream &operator>>(istream &cin,MyString &p)
{
p.freeString(p.arr);
p.freeString(p.brr);
char buff1[256],buff2[256];
cout << "input First:";
cin >> buff1;
cout << "input Second:";
cin >> buff2;
p.copyString(p.arr,buff1,p.arr_len);
p.copyString(p.brr,buff2,p.brr_len);
return cin;
}
bool operator==(MyString &L,const MyString &R)
{
if(L.arr && R.brr)
{
return false;
}
return true;
}
int main()
{
MyString a;
cin >> a;
cout << "============" << endl;
a.show();
a.size();
a.cmp();
a.cat();
return 0;
}
2.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//图书类
class Book
{
private:
string title;
string author;
int stock;
public:
Book(string t,string a,int s):title(t),author(a),stock(s)
{}
//获取图书信息
string getTitle() const
{
return title;
}
string getAuthor() const
{
return author;
}
int getStock() const
{
return stock;
}
//借书
bool borrowBook()
{
if(stock > 0)
{
stock--;
return true;
}
return false;
}
//还书
void returnBook()
{
stock++;
}
//显示图书信息
void display() const
{
cout << "书名: " << title << " 作者: " << author << " 库存: " << stock << endl;
}
};
//图书管理系统类
class LibrarySystem
{
private:
vector<Book> books;
public:
//添加图书
void addBook(string title,string author,int stock)
{
books.push_back(Book(title,author,stock));
cout << "添加成功" << endl;
}
//借书
void borrowBook(string title)
{
for(auto& book : books)
{
if(book.getTitle() == title)
{
if(book.borrowBook())
{
cout << "借书成功" << endl;
}
else
{
cout << "借书失败,库存不足" << endl;
}
return;
}
}
cout << "未找到该图书" << endl;
}
//还书
void returnBook(string title)
{
for(auto& book : books)
{
if(book.getTitle() == title)
{
book.returnBook();
cout << "还书成功" << endl;
return;
}
}
cout << "未找到该图书" << endl;
}
//查询图书
void searchBook(string keyword)
{
bool found = false;
cout << "查询结果:" << endl;
for (const auto& book : books)
{
string title = book.getTitle();
string author = book.getAuthor();
//检查书名是否包含关键字
bool titleMatch = false;
for(size_t i = 0;i <= title.length() - keyword.length();i++)
{
if(title.substr(i, keyword.length()) == keyword)
{
titleMatch = true;
break;
}
}
//检查作者是否包含关键字
bool authorMatch = false;
for(size_t i = 0;i <= author.length() - keyword.length();i++)
{
if (author.substr(i,keyword.length()) == keyword)
{
authorMatch = true;
break;
}
}
if(titleMatch || authorMatch)
{
book.display();
found = true;
}
}
if(!found)
{
cout << "未找到相关图书!" << endl;
}
}
//显示所有图书
void displayAllBooks()
{
if(books.empty())
{
cout << "图书馆暂无图书" << endl;
return;
}
cout << "所有图书信息:" << endl;
for(const auto& book : books)
{
book.display();
}
}
};
//显示菜单
void displayMenu()
{
cout << "\n===== 图书管理系统 =====" << endl;
cout << "1. 添加图书" << endl;
cout << "2. 借书" << endl;
cout << "3. 还书" << endl;
cout << "4. 查询图书" << endl;
cout << "5. 显示所有图书" << endl;
cout << "0. 退出系统" << endl;
cout << "请选择操作: ";
}
//清除
void clearInputBuffer()
{
//读取并清除缓冲区中的所有字符,直到遇到换行符
while (cin.get() != '\n')
{
continue;
}
}
int main()
{
LibrarySystem library;
int choice;
string title, author, keyword;
int stock;
do
{
displayMenu();
cin >> choice;
clearInputBuffer();
switch (choice)
{
case 1:
cout << "请输入书名: ";
getline(cin,title);
cout << "请输入作者: ";
getline(cin,author);
cout << "请输入库存数量: ";
cin >> stock;
clearInputBuffer();
library.addBook(title,author,stock);
break;
case 2:
cout << "请输入要借的书名: ";
getline(cin,title);
library.borrowBook(title);
break;
case 3:
cout << "请输入要还的书名: ";
getline(cin,title);
library.returnBook(title);
break;
case 4:
cout << "请输入书名或作者关键字: ";
getline(cin,keyword);
library.searchBook(keyword);
break;
case 5:
library.displayAllBooks();
break;
case 0:
cout << "感谢使用图书管理系统" << endl;
break;
default:
cout << "无效的选择,请重新输入" << endl;
cin.clear();
clearInputBuffer();
}
}while(choice != 0);
return 0;
}
3.
4.