实验二 数组、指针与C++标准库

实验任务五:

info.hpp

#include<string>
#include <iostream>
using namespace std;

class Info{
    public:
            Info(string name="xxx",string contact="xxx",string city="xxx",int n=1):nickname(name),contact(contact),city(city),n(n){};
             void print() const {
                 cout<<"称呼:    "<<nickname<<endl;
                 cout<<"联系方式: "<<contact<<endl;
                 cout<<"所在城市: "<<city<<endl;
                 cout<<"预订人数: "<<n<<endl;
             }
    const string &getNickname() const {
        return nickname;
    }

    void setNickname(const string &nickname) {
        Info::nickname = nickname;
    }

    const string &getContact() const {
        return contact;
    }

    void setContact(const string &contact) {
        Info::contact = contact;
    }

    const string &getCity() const {
        return city;
    }

    void setCity(const string &city) {
        Info::city = city;
    }

    int getN() const {
        return n;
    }

    void setN(int n) {
        Info::n = n;
    }
private:
    std::string nickname;
    string contact;
    string city;
    int n;

};

main:

#include "Info.hpp"
#include<iostream>
#include <vector>

int main(){
    using namespace std ;


    vector<Info> audience_info_list;
    cout<<"录入信息"<<endl<<endl;
    cout<<"称呼/昵称, 联系方式(邮箱/手机号)所在城市,预定参加人数  是否继续录入(y)";

    const int capacity=100;
    int num=0;
    char flag='y';
    while( flag=='y'){
      Info info;

      string name;
      cin>>name;
      info.setNickname(name);

      string contact;
      cin>>contact;
      info.setContact(contact);

      string city;
      cin>>city;
      info.setCity(city);

      int n;
      cin>>n;
      if(num+n>capacity) {
          cout << "对不起,只剩" << capacity - num << "个位置" << endl;
          cout << "1.输入u,更新(update)预订信息" << endl;
          cout << "2.输入q,退出预定" << endl;
          cout << "你的选择:";
          char choice;
          cin >> choice;
          if (choice == 'q')
              break;
          else if (choice == 'u')
              continue;
      }
        else
            info.setN(n);
        num+=n;
        audience_info_list.push_back(info);
        if(num==100)
            break;
        cin>>flag;
    }
    cout<<"截止目前,一共有"<<num<<"位观众预定参与。预定听众信息如下"<<endl;
    for(int i = 0; i<audience_info_list.size();i++){
        audience_info_list[i].print();
    }
}

运行截图

 

实验二 数组、指针与C++标准库

 

实验任务六:

TextCoder.hpp

 

#include <string>
using namespace std;
class TextCoder{
public:
    TextCoder(string text):text(text){};
    string encoder();
    string decoder();

private:
    string text;
};
string TextCoder::encoder() {
    string s;
    for(int i=0;i<text.size();i++){
        if('a'<=text[i]&&text[i]<='z')
            s.push_back((text[i]-'a'+5)%26+'a');
        else if('A'<=text[i]&&text[i]<='Z')
            s.push_back( text[i]=(text[i]-'A'+5)%26+'A');
        else
            s.push_back(text[i]);
    }
    return s;
}
string TextCoder::decoder() {
    string s;
    for(int i=0;i<text.size();i++){
        if('a'<=text[i]&&text[i]<='z')
            s.push_back(text[i]<='e'?'v'+text[i]-'a':text[i]-5);
        else if('A'<=text[i]&&text[i]<='Z')
            s.push_back( text[i]<='e'?'v'+text[i]-'a':text[i]-5);
        else
            s.push_back(text[i]);
    }
    return s;
}

 

main:

#include <string>
using namespace std;
class TextCoder{
public:
    TextCoder(string text):text(text){};
    string encoder();
    string decoder();

private:
    string text;
};
string TextCoder::encoder() {
    string s;
    for(int i=0;i<text.size();i++){
        if('a'<=text[i]&&text[i]<='z')
            s.push_back((text[i]-'a'+5)%26+'a');
        else if('A'<=text[i]&&text[i]<='Z')
           s.push_back( text[i]=(text[i]-'A'+5)%26+'A');
        else
            s.push_back(text[i]);
    }
    return s;
}
string TextCoder::decoder() {
    string s;
    for(int i=0;i<text.size();i++){
        if('a'<=text[i]&&text[i]<='z')
            s.push_back(text[i]<='e'?'v'+text[i]-'a':text[i]-5);
        else if('A'<=text[i]&&text[i]<='Z')
            s.push_back( text[i]<='e'?'v'+text[i]-'a':text[i]-5);
        else
            s.push_back(text[i]);
    }
    return s;
}

运行截图:

 

 

 

 

实验二 数组、指针与C++标准库

 

上一篇:数据库实验一:创建CAP数据库


下一篇:实验二 数组、指针和C++标准库