题目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;
}