资源简介
Project 2: Jumping the Queue
The beginning of a winter break near Spring Festival is always the beginning of a peak period of transportation. If you have ever tried to get a train ticket at that time, you must have witnessed the endless queues in front of every ticket box window. If a guy has seen his friend in a queue, then it is very much likely that this lucky guy might go straight to his friend and ask for a favor. This is called "jumping the queue". It is unfair to the rest of the people in the line, but, it is life. Your task is to write a program that simulates such a queue with people jumping in every now and then, assume that, if one in the queue has several friends asking for favors, he would arrange their requests in a queue of his own.
Input Specification:
Your program must read test cases from a file “input.txt”. The input file will contain one or more test cases. Each test case begins with the number of groups n (1<= n <=1000). Then n group descriptions follow, each one consisting of the number of friends belonging to the group and those people's distinct names. A name is a string of up to 4 characters chosen from {A, B, ..., Z, a, b, ..., z}. A group may consist of up to 1000 friends. You may assume that there is no one belong to two different groups.
Finally, a list of commands follows. There are three different kinds of commands:
ENQUEUE X - Mr. or Ms. X goes into the queue
DEQUEUE - the first person gets the ticket and leave the queue
STOP - end of test case
The input will be terminated by a value of 0 for n.
Output Specification:
For each test case, output to a file “output.txt”. First print a line saying "Scenario #k", where k is the number of the test case. Then, for each DEQUEUE command, print the person who just gets a ticket on a single line. Print a blank line between two test cases, but no extra line at the end of output.
Sample Input:
2
3 Ann Bob Joe
3 Zoe Jim Fat
ENQUEUE Ann
ENQUEUE Zoe
ENQUEUE Bob
ENQUEUE Jim
E
代码片段和文件信息
#include
#include
#include
typedef unsigned int Index;
typedef Index Position;
struct HashEntry;
struct HashTbl;
typedef struct HashEntry Cell;
typedef struct HashTbl *HashTable;
Index Hash(const char * keyint Tablesize); /*The hash function*/
void InitializeTable(int TableSize); /*Function to initialize the Hash table*/
void DestroyTable(); /*Function to destroy the Hash table and free the memory*/
Position Find(char * key); /*Find functiong to get the position of KEY*/
void Insert(char * keyint segnumberint FLAG); /*Insert the KEY to Hashtable*/
void Input (char *** Poem int seg int line); /*Read the Poem*/
void Output (char *** Poem int seg int line); /*Output the Poem to an output file*/
struct HashEntry /*The structure of each HashTable element CELL*/
{
char Element[80];/*Used to store the content of each First line or Last line*/
int Head; /*First line TAG of each segment used to store the segment number of each “First line“*/
int Tail; /*Last line TAG of each segment used to store the segment number of each “Last line“*/
};
struct HashTbl
{
int TableSize;
Cell * TheCells;
};
/*******************************Global Variables definition**********************************/
FILE *fp; /*To read a file “input.txt“*/
FILE *fout; /*To output data to the file “output.txt“*/
HashTable H;
int Scenario_number=1; /*variable to record the scenario number Initial value is 1*/
/**************************Functions to operate the Hash table*************************************/
Index Hash(const char * keyint TableSize)
{
unsigned long int HashVal=0;
while(*key!=‘\0‘)
HashVal=(HashVal<<5)+*key++;
return HashVal%TableSize; /*Hash function to return the position of KEY in the Hash table H*/
}
void InitializeTable(int TableSize)
{
int i;
H=malloc( sizeof( struct HashTbl ) ); /*Allocate memory for H*/
if(H==NULL)
printf(“out of space!!“);
H->TableSize=TableSize; /*Initial size*/
H->TheCells=malloc(sizeof( Cell ) * H->TableSize); /*Allocate memory for H->TheCells*/
if(H->TheCells==NULL)
printf( “out of space!!“ );
for(i=0;iTableSize;i++)
{
H->TheCells[i].Element[0]=‘\0‘;
H->TheCells[i].Head=-1; /*Initial value is -1 means it is empty */
H->TheCells[i].Tail=-1; /*Initial value is -1 means it is empty */
}
return;
}
void DestroyTable() /*Destroy the Hash table*/
{
free(H->TheCells); /*Free the memory of H->TheCells*/
free(H); /*Free the memory of H */
return;
}
Position Find(char * key)
{
Position CurrentPos;
int CollisionNum;
CollisionNum=0; /*To record the collision numbers */
CurrentPos=Hash(keyH->TableSize); /*Using Hash function to get the first Position of KEY*/
while( !(H->TheCells[CurrentPos
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 452 2008-10-04 20:38 G06\G06\code\input.txt
文件 6931 2008-10-04 20:25 G06\G06\code\Project2.c
文件 196655 2008-10-04 20:25 G06\G06\code\Project2.exe
文件 496 2008-10-04 20:40 G06\G06\code\Readme.txt
目录 0 2008-10-08 10:04 G06\G06\code
目录 0 2009-03-03 21:43 G06\G06
目录 0 2009-03-03 21:43 G06
----------- --------- ---------- ----- ----
204534 7
- 上一篇:成绩记录簿
- 下一篇:IAR中文版教程 IAR教程
相关资源
- 达内c++pptThe C++ Programming Language
- 利用VC++实现Sutherland-Hodgman算法多边形
- 质子交换膜燃料电池PEMFC仿真模型
- CohenSutherland裁剪算法和中点分割裁剪
- The C++ IO Streams and Locales
- Cohen-Sutherland 算法线段裁剪
- vs code C/C++语法高亮配置文件C/C++ The
- Ethernet帧结构解析程序
- QUEUE.CPP
-
Inside the C++ ob
ject Model(中英文版) - 队列类Queue的C++实现
- The Modern C++ Challenge pdf
- An implementation of the ISO-TP (ISO15765-2)
- FreeRTOS 官方指导 英文(Mastering_the_F
- mlx90614(IR_Thermometer_Sensor_MLX90614)
- The_C_Programming_Language第二版中文版
- The C Programming Language中文版(绝对高清
- Sutherland-Hodgeman算法
- OpenGL多边形裁剪算法
- Weiler-Athenton算法
- C++17 - The Complete Guide
- The C++ Programing Language(特别版)中文
- The C Programing Language(中/英文版)
评论
共有 条评论