描述:
实现一个简易的银行排号叫号系统
get 取号 示例:”get”或”get vip”
call 叫号 示例:”call”
delete 删除号码 示例:”delete 5”
count 获取当前排队总人数 示例:”count”
countN 获取号码N以前的排队人数 示例:”countN”
reset 重置排号机 示例:”reset”
quit 退出排号机 示例:”quit”
运行时间限制: 无限制
内存限制: 无限制
输入:
每行只会有一条输入(比如:C语言可使用gets函数获取一行输入命令的字符串)。
1、若输入不符合要求(如:命令字非法,或其他认为输入的错误)均需输出”error”
2、每条输出后使用换行符隔开(如后面示例)
输出:
1)取号。可获取普通号和vip号码。如初始状态,输入”get”,则获取普通号码,执行结果为”1”,如再次输入”get vip”,则获取VIP号码,执行结果为”vip 2”。如果末尾的2号被删除,则再次调用”get”时应输出”2”
VIP号码有绝对的优先级。普通号和vip号码统一编号,取号均为连续号码。号码从1开始编号,最大为100000.
2)叫号。获取当前应该处理用户的号码。例如当前排队号码为1 2 3 4 5 7,当输入”call”,执行结果为”1”,如1为vip号码,则为”vip 1”.如果再连续调用6次,第六次执行结果应为”error”
3)删除号码。客户不想办理时可删除号码,叫号时则跳过此号码。例如当前排队号码为1 2 3 4 5,输入”delete 5”,执行结果为”5”,如果5为vip则显示”vip 5”。再次输出”delete 5”,执行结果为”error”
4)获取当前排队总人数。获取当前排队人数。例如当前排队号码为1 2 3 4 5 6,执行结果为”6”
5)获取在某个号码之前排队的总人数。例如当前排队号码为1 2 3 4 5 7,输入”countN 7”,执行结果为”5”
6、重置排号机。例如输入”reset”,则重置排号机,进入初始状态,无需输出。
7、退出排号机。例如输入”quit”,则退出排号机,无需输出。
样例输入:
get
get
get
get vip
count
countN 1
call
quit
样例输出:
1
2
3
vip 4
4
1
vip 4
代码有错误部分,测试未全部通过,待修改
#include "stdafx.h"
#include <string>
#include <vector>
#include <cstdlib>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAX 100000
int que[MAX];
int vip[MAX];
int main() //排号机
{
int del, vcnt = 0, cnt = 0, amount = 0, vbeg = 0, quebeg = 0, total = 0;
string dele, beg, operate;
getline(cin, beg);
int loc = beg.find(" ");
operate = beg.substr(0, loc);
while (operate != "quit" )
{
if (operate == "get")
{
if (beg.size() >= 7)
{
if (beg.substr(4, 3) == "vip" && beg.size() == 7)
{
amount++; total++;
que[cnt++] = amount;
vip[vcnt++] = amount;
cout << "vip " << amount << endl;
}
}
else
{
if (beg.size() == 3)
{
amount++; total++;
que[cnt++] = amount;
cout << amount << endl;
}
}
}
else if (operate == "call" && total <= amount)
{
if (beg.size() == 4)
{
if (vcnt - vbeg>0)
{
while (vip[vbeg] == 0) vbeg++;
cout << "vip " << vip[vbeg] << endl;
que[vip[vbeg] - 1] = 0;
vip[vbeg++] = 0;
}
else if (cnt - quebeg > vcnt)
{
while (que[quebeg] == 0) quebeg++;
cout << que[quebeg] << endl;
que[quebeg++] = 0;
}
else
{
cout << "error" << endl;
}
}
}
else if (operate == "delete" && cnt>0 )
{
int delflag = 0;
loc = beg.find(" ");
dele = beg.substr(1 + loc);
del = atoi(dele.c_str());
int* Result;
if (vcnt > 0)
{
Result = find(vip, vip + vcnt - 1, del);
if (Result != vip + vcnt - 1 || (Result==vip+vcnt-1 && *Result==del))
{
cout << "vip " << del << endl;
*Result = 0;
delflag = 1;
vcnt--;
Result = find(que, que + cnt - 1, del);
if (Result != que + cnt - 1 || (Result == que + cnt - 1 && *Result == del))
{
*Result = 0;
cnt--;
}
}
}
if (delflag == 0)
{
Result = find(que, que + cnt - 1, del);
if (Result != que + cnt - 1 || (Result == que + cnt - 1 && *Result == del))
{
cout << del << endl;
delflag = 1;
*Result = 0;
cnt--;
}
}
if (delflag == 1)
{
total--;
}
else
{
cout << "error" << endl;
}
}
else if (operate == "count")
{
if (beg.size() == 5)
{
cout << total << endl;
}
else
{
cout << "error" << endl;
}
}
else if (operate == "countN")
{
loc = beg.find(" ");
string temp;
temp = beg.substr(1 + loc);
int num = atoi(temp.c_str());
int i, j = 0;
for (i = 0; i < num; i++)
{
if (que[i] != 0) j++;
}
cout << j << endl;
}
else if (operate == "reset")
{
if (beg.size() == 5)
{
memset(que, 0, sizeof(int)*MAX);
memset(vip, 0, sizeof(int)*MAX);
cnt = 0; vcnt = 0;
}
else
{
cout << "error" << endl;
}
}
else
{
cout << "error" << endl;
}
getline(cin, beg);
loc = beg.find(" ");
operate = beg.substr(0, loc);
}
return 0;
}