实验二

info.hpp

 

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 class Info{
 5     
 6     public:
 7         Info(string name,string phone,string place,string number){
 8             nickname = name;
 9             contact = phone;
10             city = place;
11             n = number;
12         }
13         void print();
14     private:
15         string nickname;    
16         string contact;
17         string city;
18         string n;
19 };
20 void Info::print(){
21     std::cout<<"称呼:\t\t\t"<<nickname<<endl;
22     std::cout<<"联系方式:\t\t"<<contact<<endl;
23     std::cout<<"所在城市:\t\t"<<city<<endl;
24     std::cout<<"预定人数:\t\t"<<n<<endl;    
25 }

task5.cpp

 

#include<iostream>
#include<vector>
#include<string>
#include "Info.hpp"
using namespace std;
const int capacity = 100;//场地容量 
int main(){
    cout<<"录入信息:\n\n";
    cout<<"称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数\n";
    vector<Info> list;
    string s1[4];//保存一个用户的四个输入 
    int number = 0;//记录人数
    string s;
    while(cin>>s1[0]>>s1[1]>>s1[2]>>s1[3]){
/*使用getline获取每一行拆解的写法:    
//        int xb = 0;//用于指示字符串位置 
//        while(s[xb]==' '||s[xb]=='\t'){//防止用户在第一次输入时使用空格或制表符 
//            xb++;
//        }
//        for(int j=0;j<4;j++){
//            if(xb>=s.length()) break;
//            while(s[xb]!=' ' && s[xb]!='\t' && xb<s.length()){
//                s1[j].push_back(s[xb]);
//                xb++;
//            }
//            if(j==3) break;//收集到四个字符串时即可退出 
//            while(s[xb] ==' ' || s[xb] =='\t'){//过滤中间的空格和制表符 
//                xb++;
//            }
//        }
*/
        int yuding = 0;
        for(int i=0;i<s1[3].length();i++){
            yuding = yuding*10 + s1[3][i]-'0';
        }
        number += yuding;
        if(number > capacity){//人数判断 
            number -= yuding;
            cout<<"对不起,只剩"<<capacity-number<<"个位置.\n"; 
            cout<<"1.输入u,更新(update)预定信息\n";
            cout<<"2.输入q,退出预定\n";
            cout<<"你的选择:";
            char option;
            cin>>option;
        //    getchar();//使用getline时要用getchar读取多余的回车 
            if(option == 'q'){
                break;
            }
            else if(option !='u'){//防止用户输入其他字符 
                break;
            }        
        } 
        else{
            list.push_back(Info(s1[0],s1[1],s1[2],s1[3]));    
        } 
            
            
        for(int i=0;i<4;i++){
            s1[i].clear();//清空string的内容 
        }    
    
    }
    cout<<"\n截至目前,一共有"<<number<<"位听众预定参加。预定听众信息如下:\n";    
    for(auto user:list){
        user.print();
    }
                    
    return 0;            
}

运行结果:

 

 

 

 

Textcoder.hpp

 

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

class TextCoder{
public:
    TextCoder(string x):text(x){}
    string encoder();
    string decoder();
private:
    string text;    
};

string TextCoder::encoder() 
{
    for(int i=0;i<text.length();++i)
    {
        if((text[i]>='A'&&text[i]<='U')||(text[i]>='a'&&text[i]<='u'))
        text[i]=text[i]+5;
        else if((text[i]>='V'&&text[i]<='Z')||(text[i]>='v'&&text[i]<='z'))
        text[i]=text[i]-21; 
    }
    return text; 
}

string TextCoder::decoder() 
{
    for(int i=0;i<text.length();i++)
    {
        if((text[i]>='f'&&text[i]<='z')||(text[i]>='F'&&text[i]<='Z'))
        text[i]=text[i]-5;
        else if((text[i]>='a'&&text[i]<='e')||(text[i]>='A'&&text[i]<='E')) 
        text[i]=text[i]+21;
    }
    return text; 
}

 

task6.cpp

 

#include "TextCoder.hpp"
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string text, encoded_text, decoded_text;
    cout << "输入英文文本: ";
    while (getline(cin, text))
    {
        encoded_text = TextCoder(text).encoder(); // 这里使用的是临时无名对象
        cout << "加密后英文文本:\t" << encoded_text << endl;
        
        decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象
        cout << "解密后英文文本:\t" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    }
}

 

运行结果:

 

posted @ 2021-11-02 17:57  悟道树  阅读(20)  评论(3编辑  收藏  举报