C++面试题--(六)

该博客转载了一篇文章,原文链接为https://www.cnblogs.com/Grisson/archive/2005/08/21/219728.html ,标签涉及面试和C/C++。
Write C/C++ program which will repeatedly prompt for and read command from the terminal and perform desired action.
The commands to be supported are:

Add an item into the queue at a given priority, e.g.
Priority: 1
Item:item 1
Print the current contents of the queue.
1: item 1, next item, …

9: my item, …
Reverse – toggle the priority order of the queue, e.g.
9: my item, …

1: item 1, next item, …
Remove the highest priority item form the queue and print it, e.g.
Removed: my item

Specifications of the priority queue:
·Items added into queue may have priority in range 0 to 9
·Initially, 0 is the highest priority.
·Within one priority level, items should be maintained as FIFO.
Hints:
· It is not imperative that you complete application.
· Parts of application you did not finished comment:
1.Why did you not implement?
2.How would you implement?
===========================================
None.gif #include  < stdio.h >  
None.gif#include 
< stdlib.h >  
None.gif#include 
< string .h >  
None.gif
None.gif
#define  FALSE   0 
None.gif
#define  TRUE    1 
None.gif
None.giftypedef 
int      BOOL; 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
struct  item  dot.gif
InBlock.gif        
char    *info; 
InBlock.gif        
struct item     *next; 
ExpandedBlockEnd.gif}

None.gif
None.gif
struct  item      * queue[ 10 ]; 
None.gif
None.gifBOOL swcmd(
int ); 
None.gif
void  add_item( int  priority,  char   * buf); 
None.gif
void  print_item( void ); 
None.gif
void  rm_item( void ); 
None.gif
struct  item      * get_last_item( int  priority);            //  get the last item with the priority. 
None.gif
void  free_item( struct  item  * tmp); 
None.gif
void  flush_in( void );                     //  to flush input buffer. 
None.gif

ExpandedBlockStart.gifContractedBlock.gif
int  main( void dot.gif
InBlock.gif        
int     ch, loop = TRUE; 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (loop) dot.gif
InBlock.gif                printf(
"type a command to run the program(h for help): "); 
InBlock.gif                ch 
= getchar(); 
InBlock.gif                flush_in(); 
InBlock.gif                loop 
= swcmd(ch); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        exit(
0); 
ExpandedBlockEnd.gif}
 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* to process the different command. 
InBlock.gif
ExpandedBlockEnd.gif
*/
 
ExpandedBlockStart.gifContractedBlock.gifBOOL swcmd(
int  ch)  dot.gif
InBlock.gif        BOOL    ret 
= TRUE; 
InBlock.gif        
int     priority; 
InBlock.gif        
char    buf[256]; 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
switch (ch) dot.gif
InBlock.gif                
case 'a' : 
InBlock.gif                        printf(
"add an item.\n"); 
InBlock.gif                        printf(
"priority: "); 
InBlock.gif
InBlock.gif                        scanf(
"%d"&priority); 
InBlock.gif                        flush_in(); 
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
if ((priority < 0|| (priority > 9)) dot.gif
InBlock.gif                                printf(
"priority must be in 0 to 9.\n"); 
InBlock.gif                                
break
ExpandedSubBlockEnd.gif                        }
 
InBlock.gif
InBlock.gif                        printf(
"item: "); 
InBlock.gif                        scanf(
"%s", buf); 
InBlock.gif                        flush_in(); 
InBlock.gif                        add_item(priority, buf);        
// maybe unsucessful. 
InBlock.gif
                        break
InBlock.gif                
case 'p' : 
InBlock.gif                        printf(
"print the items.\n"); 
InBlock.gif                        print_item(); 
InBlock.gif                        
break
InBlock.gif                
case 'o' : 
InBlock.gif                        rm_item(); 
InBlock.gif                        
break
InBlock.gif                
case 'h' : 
InBlock.gif                        printf(
"a -- add an item to the queue.\n"); 
InBlock.gif                        printf(
"p -- print the queue.\n"); 
InBlock.gif                        printf(
"o -- take out the highest priority item.\n"); 
InBlock.gif                        printf(
"h -- show this help information.\n"); 
InBlock.gif                        printf(
"q -- quit this program.\n"); 
InBlock.gif                        
break
InBlock.gif                
case 'q' : 
InBlock.gif                        ret 
= FALSE; 
InBlock.gif                        
break
InBlock.gif                
default : 
InBlock.gif                        printf(
"an unrecgnized command!\n"); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
return ret; 
ExpandedBlockEnd.gif}
 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* add an item to the date structure. 
InBlock.gif
ExpandedBlockEnd.gif
*/
 
ExpandedBlockStart.gifContractedBlock.gif
void  add_item( int  priority,  char   * buf)  dot.gif
InBlock.gif        
struct item     *tmp, *last; 
InBlock.gif
InBlock.gif        tmp 
= (struct item *)malloc(sizeof(struct item)); 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (tmp == NULL) dot.gif
InBlock.gif                perror(
"add_item::malloc"); 
InBlock.gif                exit(
1); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        tmp
->info = strdup(buf); 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (tmp->info == NULL) dot.gif
InBlock.gif                perror(
"add_item::strdup"); 
InBlock.gif                exit(
1); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        tmp
->next = NULL; 
InBlock.gif        last 
= get_last_item(priority); 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (last == NULL) dot.gif
InBlock.gif                queue[priority] 
= tmp; 
InBlock.gif                
return
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        last
->next = tmp; 
InBlock.gif        
return
ExpandedBlockEnd.gif}
 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* to show the items on the screen. 
InBlock.gif
ExpandedBlockEnd.gif
*/
 
ExpandedBlockStart.gifContractedBlock.gif
void  print_item( void dot.gif
InBlock.gif        
int     i; 
InBlock.gif        
struct item     *tmp; 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (i = 0; i < 10; i++dot.gif
InBlock.gif                printf(
"%d: ", i); 
InBlock.gif                tmp 
= queue[i]; 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
while (tmp != NULL) dot.gif
InBlock.gif                        printf(
"%s, ", tmp->info); 
InBlock.gif                        tmp 
= tmp->next; 
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                printf(
"-\n"); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
return ; 
ExpandedBlockEnd.gif}
 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* get the last item position with the same priority. 
InBlock.gif
ExpandedBlockEnd.gif
*/
 
ExpandedBlockStart.gifContractedBlock.gif
struct  item  * get_last_item( int  priority)  dot.gif
InBlock.gif        
struct item     *tmp; 
InBlock.gif        tmp 
= queue[priority]; 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (tmp == NULL) dot.gif
InBlock.gif                
return tmp; 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (tmp->next != NULL) dot.gif
InBlock.gif                tmp 
= tmp->next; 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
return tmp; 
ExpandedBlockEnd.gif}
 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* output the highest priority item,and remove it from the queue. 
InBlock.gif
ExpandedBlockEnd.gif
*/
 
ExpandedBlockStart.gifContractedBlock.gif
void  rm_item( void dot.gif
InBlock.gif        
int     i; 
InBlock.gif        
struct item     *tmp; 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (i = 9; i >= 0; i--dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (queue[i] == NULL) dot.gif
InBlock.gif                        
continue
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                tmp 
= queue[i]; 
InBlock.gif                queue[i] 
= tmp->next; 
InBlock.gif                
break
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (i < 0dot.gif{            // a trap to remove nothing. 
InBlock.gif
                return
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        printf(
"the highest priority item: [%d] %s\n", i, tmp->info); 
InBlock.gif        free_item(tmp); 
InBlock.gif        print_item(); 
InBlock.gif        
return
ExpandedBlockEnd.gif}
 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* to free the struct item. 
InBlock.gif
ExpandedBlockEnd.gif
*/
 
ExpandedBlockStart.gifContractedBlock.gif
void  free_item( struct  item  * tmp)  dot.gif
InBlock.gif        free(tmp
->info); 
InBlock.gif        free(tmp); 
ExpandedBlockEnd.gif}
 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* to flush the input buffer. 
InBlock.gif
ExpandedBlockEnd.gif
*/
 
ExpandedBlockStart.gifContractedBlock.gif
void  flush_in( void dot.gif
InBlock.gif        
int     ch; 
InBlock.gif        ch 
= getchar(); 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (ch != '\n'dot.gif
InBlock.gif                ch 
= getchar(); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
return ; 
ExpandedBlockEnd.gif}
posted on 2005-08-21 20:34 海盗 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Grisson/archive/2005/08/21/219728.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值