For an electronic mail application you are to describe the SMTP-based communication that takes place between pairs of MTAs. The sender’s User Agent gives a formatted message to the sending Message Transfer Agent (MTA). The sending MTA communicates with the receiving MTA using the Simple Mail Transfer Protocol (SMTP). The receiving MTA delivers mail to the receiver’s User Agent. After a communication link is initialized, the sending MTA transmits command lines, one at a time, to the receiving MTA, which returns a three-digit coded response after each command is processed. The sender commands are shown below in the order sent for each message. There is more than one RCPT TO line when the same message is sent to several users at the same MTA. A message to users at different MTAs requires separate SMTP sessions.
HELO myname Identifies the sender to the receiver (yes, there is only one L)
MAIL FROM:< sender > Identifies the message sender
RCPT TO:< user > Identifies one recipient of the message
DATA Followed by an arbitrary number of lines of text comprising the message body, ending with a line containing a period in column one.
QUIT Terminates the communication.
The following response codes are sent by the receiving MTA:
221 Closing connection (after QUIT)
250 Action was okay (after MAIL FROM and RCPT TO specifying an acceptable user, or completion of a message)
354 Start sending mail (after DATA)
550 Action not taken; no such user here (after RCPT TO with unknown user)
具体题目意思可以用翻译软件翻译一下,其实重点就是下面的成功/错误编码和报文类型啦。理解题意可以看下面的输入、输出示例,方便更快理解题目。
The input contains descriptions of MTAs followed by an arbitrary number of messages. Each MTA description begins with the MTA designation and its name (1 to 15 alphanumeric characters). Following the MTA name is the number of users that receive mail at that MTA and a list of the users (1 to 15 alphanumeric characters each). The MTA description is terminated by an asterisk in column 1. Each message begins with the sending user’s name and is followed by a list of recipient identifiers. Each identifier has the form user@mtaname. The message (each line containing no more than 72 characters) begins and terminates with an asterisk in column 1. A line with an asterisk in column 1 instead of a sender and recipient list indicates the end of the entire input.
示例:
MTA London 4 Fiona Paul Heather Nevil
MTA SanFrancisco 3 Mario Luigi Shariff
MTA Paris 3 Jacque Suzanne Maurice
MTA HongKong 3 Chen Jeng Hee
MTA MexicoCity 4 Conrado Estella Eva Raul
MTA Cairo 3 Hamdy Tarik Misa
*
Hamdy@Cairo Conrado@MexicoCity Shariff@SanFrancisco Lisa@MexicoCity
*
Congratulations on your efforts !!
–Hamdy
*
Fiona@London Chen@HongKong Natasha@Paris
*
Thanks for the report! –Fiona
*
*
For each message, show the communication between the sending and receiving MTAs. Every MTA mentioned in a message is a valid MTA; however, message recipients may not exist at the destination MTA. The receiving MTA rejects mail for those users by responding to the RCPT TO command with the 550 code. A rejection will not affect delivery to authorized users at the same MTA. If there is not at least one authorized recipient at a particular MTA, the DATA is not sent. Only one SMTP session is used to send a message to users at a particular MTA. For example, a message to 5 users at the same MTA will have only one SMTP session. Also a message is addressed to the same user only once. The order in which receiving MTAs are contacted by the sender is the same as in the input file. As shown in the sample output, prefix the communication with the communicating MTA names, and indent each non-empty communication line. No innecessary spaces should be printed.
示例:
Connection between Cairo and MexicoCity
HELO Cairo
250
MAIL FROM:< Hamdy@Cairo>
250
RCPT TO:< Conrado@MexicoCity>
250
RCPT TO:< Lisa@MexicoCity>
550
DATA
354
Congratulations on your efforts !!
–Hamdy
.
250
QUIT
221
Connection between Cairo and SanFrancisco
HELO Cairo
250
MAIL FROM:< Hamdy@Cairo>
250
RCPT TO:< Shariff@SanFrancisco>
250
DATA
354
Congratulations on your efforts !!
–Hamdy
.
250
QUIT
221
Connection between London and HongKong
HELO London
250
MAIL FROM:< Fiona@London>
250
RCPT TO:< Chen@HongKong>
250
DATA
354
Thanks for the report! –Fiona
.
250
QUIT
221
Connection between London and Paris
HELO London
250
MAIL FROM:< Fiona@London>
250
RCPT TO:< Natasha@Paris>
550
QUIT
221
该题目属于大模拟题,没有什么算法难度,但是要模拟出整个业务流程是比较繁琐的。根据题意,逻辑如下:获取所有MTA下的所有用户 -> 获取收件人,发件人s -> 按发件人列表的MTA顺序发件,发件时需检测该用户名是否存在于对应MTA下。
编程参考了紫书P130,书上的代码存在几个疑问。首先是”用户名@mta“是用set来存储的,难道输入会出现重复的情况吗?另外就是在获取邮件正文之前还添加了一行getline,作用是吃掉*后的回车?这个不太明白。希望大佬能在评论区讨论。
另外,输入方面我是完全参考了紫书上的流处理方法,书上对流的处理远比我目前掌握的方法还要灵活,是我需要重点理解和掌握的地方。否则真正比赛或考试时连输入数据都处理不好就尴尬了。
#include
#include
#include
#include