例题5-11 UVA 814 The Letter Carrier’s Rounds邮件传输代理的交互

书中的大体思路就是:

建立一个set<string>addr用来存放邮箱的全称,用在后面检测是否输出(检测是否存在!)

vector<string>mta用来存放需要连接的MTA,按照先后顺序push_back,

set<string>vis则是用在输入时检测是否存在重复的收件人。

map<string ,vector<string> >dest则是用来与前面的vector<string>mta相匹配,这个是mta里面的指定用户,存放的是邮箱全称

之所以后面有个getline(cin,t)是因为要吃掉最后输入*的回车,因为输入完*后,则要输入邮件内容,如果不吃掉的话,那么内容的第一行便是空行,这里换成getchar()也行。

在输出时要输出很多空格,所以不妨把5个空格作为一个常量space来记录。


代码如下:

#include<cstdio>
#include<iostream>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
const string space = "     ";
void deal(const string &s,string &user,string &mta){
    int k = s.find("@");
    user = s.substr(0,k);
    mta = s.substr(k+1);
}
int main()
{
    //freopen("out.txt","w",stdout);
    string user1,user2,mta1,mta2,s,t;
    set <string> addr;// check MTA;
    while(cin >> s && s != "*"){
        int k;
        cin >> s >> k;
        while(k--){
            cin >> t;
            addr.insert(t + "@" + s);
        }
    }
    while(cin >> s && s != "*"){
        deal(s,user1,mta1);
        vector<string>mta;// MTA in order;
        set<string>vis;// check2;
        map<string ,vector<string> >dest;// receiver;
        while(cin >> t && t != "*"){
            deal(t,user2,mta2);
            if (vis.count(t))continue;
            vis.insert(t);
            if (!dest.count(mta2)){
                mta.push_back(mta2);
                dest[mta2] = vector<string>();
            }
            dest[mta2].push_back(t);
        }
        getchar();
        string data = "";
        while(getline(cin,t) && t != "*")data += space + t + "\n";
        data += space + ".\n";
        for (int i = 0; i < mta.size(); ++i){
            string mta2 = mta[i];
            vector<string>users = dest[mta2];
            cout << "Connection between " + mta1 + " and " + mta2 << endl;
            cout << space + "HELO " + mta1 << endl;
            cout << space + "250\n";
            cout << space + "MAIL FROM:<" + s + ">\n";
            cout << space + "250\n";
            bool ok = false;
            for (int i = 0; i < users.size(); ++i){
                cout << space + "RCPT TO:<" + users[i] << ">\n";
                cout << space;
                if (addr.count(users[i])){cout << "250\n";ok = true;}
                else cout << "550\n";
            }
            if (ok)cout << space + "DATA\n" + space + "354\n" + data << space + "250\n";
            cout << space + "QUIT\n" + space + "221\n";
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值