描述: | 实现一个简易的银行排号叫号系统 get 取号 示例:"get"或"get vip" quit 退出排号机 示例:"quit" |
输入: | 每行只会有一条输入(比如:C语言可使用gets函数获取一行输入命令的字符串)。 |
输出: | 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 |
1、C语言链表实现
/************
*
* 自动银行排队系统
*/
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct Note {
int data;
int vip;
Note *next;
Note():data(0),vip(0),next(NULL) {};
};
static Note* head = NULL;
static int num = 0;
static int deleNum = 0;
static bool Flag = 0; // Flag == 0 表示没有删除队尾 , Flag == 1 表示有队尾的删除操作
void get(int n)
{
if(head == NULL) {
Note* p = new Note;
head = p;
p->data = n+1;
cout << n+1 << endl;
}
else {
Note* q = head;
Note* p = new Note;
// while(q != NULL) { //严重 但是又很难发现的错误
// q = q->next;
// cout << "morning" << endl;
// }
Note* t = NULL;
while(q != NULL) {
t=q;
q = q->next;
}
t->next = p;
p->data =(Flag ? deleNum : n+1);
cout << p->data << endl;
Flag = 0;
}
}
void getVip(int n)
{
if(head == NULL) {
Note* p = new Note;
head = p;
p->data = 1;
p->vip =1;
cout << "vip " << n+1 << endl;
}
else {
Note* q = head;
Note* p = new Note;
while(q != NULL) {
q = q->next;
}
p->data = n+1;
p->vip =1;
p->next = head;
head = p;
cout << "vip " << n+1 << endl;
}
}
void call()
{
if(head) {
if(head->vip == 1)
cout << "vip" << ' ' << head->data << endl;
else
cout << head->data << endl;
Note* p = head;
head = p->next;
delete p;
}
else
cout << "error" << endl;
}
void dele(int a)
{
Note* p = head;
Note* tmp = NULL;
while(p) {
if(p->data == a && p->vip == 1) {
if(p == head) {
call();
return;
}
else {
if(p->next == NULL) { //判断是否删除的是队尾
Flag = 1;
deleNum = a;
}
tmp->next = p->next;
delete p;
cout << "vip" << ' ' << a << endl;
return;
}
}
else if(p->data == a) {
if(p == head) {
call();
return;
}
else {
if(p->next == NULL) { //判断是否删除的是队尾
Flag = 1;
deleNum = a;
}
tmp->next = p->next;
delete p;
cout << a << endl;
return;
}
}
tmp = p;
p = p->next;
}
if(p == NULL)
cout << "error" << endl;
}
void count()
{
Note* p = head;
int n = 0;
while(p) {
p = p->next;
n++;
}
cout << n << endl;
}
void countN(int a)
{
Note* p = head;
int n = 0;
while(p) {
if(p->data != a)
n++;
else
cout << n << endl;
p = p->next;
} // 未判断是否已经遍历完
}
void reset()
{
num = 0;
while(head) {
Note* p = head;
head = p->next;
delete p;
}
}
int main()
{
int n = 0;
string str;
do {
getline(cin, str);
if(str == "get") {
int realnum = Flag ? deleNum : num;
num = Flag ? num : (num+1);
get(realnum);
}
else if(str == "get vip") {
getVip(num);
num++;
}
else if(str == "call")
call();
else if(str == "count")
count();
else if(str == "reset")
reset();
else if(str.substr(0, 7) == "countN ") {
string s = str.substr(7);
int m = atoi(s.c_str());
countN(m);
}
else if(str.substr(0, 7) == "delete ") {
string s = str.substr(7);
int m = atoi(s.c_str());
dele(m);
}
else
cout << "error" << endl;
}while(str != "quit");
getchar();
return 0;
}
C++ STL list 容器实现
#include <iostream>
#include <string>
#include <cstring>
#include <list>
using namespace std;
static int num = 1;
static int deleNum = 0;
typedef list<int> ListInt;
typedef list<int>::iterator Listptr;
static bool Flag = 0; // 标志 是否删掉了最后一个
static ListInt queue;
void get()
{
int realnum = Flag ? deleNum : num; //
num = Flag ? num : num+1;
Flag = 0;
queue.push_back(realnum);
cout << realnum << endl;
}
void getVip()
{
int realnum = Flag ? deleNum : num; //
num = Flag ? num : num+1;
Flag = 0;
Listptr ptr; //未判断是否是空的队列
for(ptr = queue.begin(); ptr != queue.end() && *ptr<0; ptr++); //如果对头有会员,则排在会员的后面
if(ptr == queue.end())
queue.push_back(0 - realnum);
else
queue.insert(ptr, 0-realnum);
cout << "vip" << " " << realnum << endl;
}
void call()
{
if(queue.empty())
cout << "error" << endl;
else {
int a;
if ((a=queue.front()) < 0)
cout << "vip" << " " << abs(a) << endl;
else if (a > 0)
cout << a << endl;
queue.pop_front();
}
}
void dele(int a)
{
if(queue.empty())
cout << "error" << endl;
else {
Listptr ptr;
for(ptr = queue.begin(); ptr != queue.end(); ptr++) {
if(*ptr == a) { // 普通客户删除
if(a == queue.back()) { //判断删除的是否是最后一个客户
Flag = 1;
deleNum = a;
}
cout << a << endl;
queue.erase(ptr);
return;
}
else if(*ptr == 0-a) { // 会员的删除
if(0-a == queue.back()) { //判断删除的是否是最后一个客户
Flag = 1;
deleNum = abs(a);
}
cout << "vip" << abs(a) <<endl;
queue.erase(ptr);
return;
}
}
if(ptr == queue.end())
cout << "error" << endl;
}
}
void countN(int a)
{
if(queue.empty()) {
cout << "error" << endl;
return;
}
int countn = 0;
Listptr ptr;
for(ptr = queue.begin(); ptr != queue.end(); ptr++) {
countn++;
if(abs(*ptr) == a) {
cout << countn-1 << endl;
return;
}
}
if(ptr == queue.end())
cout << "error" << endl;
}
void count()
{
cout << queue.size() << endl;
}
void reset()
{
num = 1;
Flag = 0;
queue.clear();
}
int main()
{
string str;
do {
getline(cin, str);
if(str == "get") {
get();
}
else if(str == "get vip") {
getVip();
}
else if(str == "call")
call();
else if(str == "count")
count();
else if(str == "reset")
reset();
else if(str.substr(0, 7) == "countN ") {
string s = str.substr(7);
int m = atoi(s.c_str());
countN(m);
}
else if(str.substr(0, 7) == "delete ") {
string s = str.substr(7);
int m = atoi(s.c_str());
dele(m);
}
else
cout << "error" << endl;
}while(str != "quit");
getchar();
return 0;
}