QTDay1 图形化界面
C++作业1:
#include <iostream>
#include <string.h>using namespace std;class MyString
{friend ostream &operator<<(ostream &cout,const MyString &q);friend istream &operator>>(istream &cin,MyString &q);
private:char *p;
public:MyString():p(nullptr){}MyString(const char *str){if (str){p = new char[strlen(str) + 1];strcpy(p, str);}else{p = new char[1];*p = '\0';}}MyString(MyString &other){p = new char[strlen(other.p) + 1];strcpy(p, other.p);}~MyString(){delete[] p;}MyString &operator=(const MyString &other){if (this != &other){delete[] p;p = new char[strlen(other.p) + 1];strcpy(p, other.p);}return *this;}MyString &operator+=(const MyString &q){size_t old_len = strlen(p);size_t new_len = old_len + strlen(q.p) + 1;char *new_p = new char[new_len];strcpy(new_p, p);strcat(new_p, q.p);delete[] p;p = new_p;return *this;}int size()const{return strlen(p);}bool operator>(const MyString &q) const{return strcmp(p, q.p) > 0;}
};ostream &operator<<(ostream &cout,const MyString &q)
{cout << q.p;return cout;
}istream &operator>>(istream &cin,MyString &q)
{char buffer[1024]; // 使用缓冲区读取输入cin >> buffer;delete[] q.p;q.p = new char[strlen(buffer) + 1];strcpy(q.p, buffer);return cin;
}int main()
{MyString p1;MyString p2;cout << "input p1:";cin >> p1;cout << "input p2:";cin >> p2;cout << "p1: " << p1 << endl;cout << "p2: " << p2 << endl;cout << "p1.len:" << p1.size() << endl;cout << "p2.len:" << p2.size() << endl;if (p1 > p2){cout << "p1 > p2" << endl;}else{cout << "p1 <= p2" << endl;}p1 += p2;cout << "p1&p2: " << p1 << endl;return 0;
}
作用2:
#include <iostream>
#include<vector>using namespace std;
class Book
{//friend istream &operator>>(istream &cin,Book &p);
public:string bookname;string writer;int count;Book(){}Book(string b,string w,int c):bookname(b),writer(w),count(c){}void sendbook(){if(count==0){cout<<"库存不足"<<endl;}else{--count;}}void backbook(){++count;}void show(){cout<<"书名:"<<bookname<<" 作者:"<<writer<<" 库存:"<<count<<endl;}
};istream &operator>>(istream &cin,Book &p)
{cin>>p.bookname>>p.writer>>p.count;return cin;
}int main()
{vector<Book> v;vector<Book>::iterator iter;BEGIN:cout<<"请选择功能:"<<endl;cout<<"1、添加书籍"<<endl<<"2、借书"<<endl<<"3、还书"<<endl<<"4、查询书籍"<<endl<<"5、退出"<<endl;int choose;cin>>choose;switch(choose){case 1:{cout<<"输入书籍信息:书名 作者 库存"<<endl;Book b;cin>>b;v.push_back(b);}goto BEGIN;case 2:{cout<<"输入需借读书名:"<<endl;string book;cin>>book;int i=0;for(iter=v.begin();iter!=v.end();iter++,i++){if(book==v[i].bookname){v[i].sendbook();}}}goto BEGIN;case 3:{cout<<"输入需归还书名:"<<endl;string book;cin>>book;int i=0;for(iter=v.begin();iter!=v.end();iter++,i++){if(book==v[i].bookname){v[i].backbook();}}}goto BEGIN;case 4:{cout<<"输入需查询书名:"<<endl;string book;cin>>book;int i=0;for(iter=v.begin();iter!=v.end();iter++,i++){if(book==v[i].bookname){v[i].show();}}}goto BEGIN;case 5:break;default:cout<<"请输入正确功能选项"<<endl;goto BEGIN;}return 0;
}