#C14479. Simulated Chat Server
Simulated Chat Server
Simulated Chat Server
You are tasked with simulating a basic chat server. Unlike a real network server, this simulation will use standard input (stdin) and standard output (stdout).
The server processes a sequence of commands. Each command represents a user's action and can be one of the following types:
- register <username>: Registers a new user with the given username. Upon successful registration, the server should output a broadcast message "username has joined the chat". If the username is already taken, output "error: Username already taken".
- message <username> <message text>: Sends a message from the specified user. The server should broadcast the message in the format "username: message text".
- disconnect <username>: Disconnects the specified user. The server should output a broadcast message "username has left the chat".
The input begins with an integer n representing the number of commands, followed by n lines, each containing one command. Process the commands in order and output the corresponding broadcast messages line by line.
Note: For the purpose of this simulation, assume that every message
command is issued by a user that has been successfully registered.
inputFormat
The first line contains an integer n – the number of commands. Each of the following n lines contains one command in one of the following formats:
register username
message username message_text
disconnect username
Note that message_text
may contain spaces.
outputFormat
For each command, output a broadcast message on a new line. For register
commands, if the username is new, output "username has joined the chat"; if the username already exists, output "error: Username already taken". For message
commands, output "username: message_text". For disconnect
commands, output "username has left the chat".
3
register alice
message alice Hello everyone!
disconnect alice
alice has joined the chat
alice: Hello everyone!
alice has left the chat
</p>