热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

某公司C笔试题[个人解法]

原题出处:http:topic.csdn.netu20081011159ee842e0-9c0d-4804-8376-42abdfe80698.html1.编写函数,检查给定字符串是否

 原题出处:http://topic.csdn.net/u/20081011/15/9ee842e0-9c0d-4804-8376-42abdfe80698.html

 

1.编写函数,检查给定字符串是否整数,如果是,返回其整数值(注:不允许使用某种特定的库函数)。
2.有两个无序链表lsit1和list2,编写函数把list1和list2合并成一个递增的链表。
3.编写一段高效的代码,从排序的后的整数中找出指定整数n的个数。如找出[1,1,3,3,3,
    5,5,5,9,9,9,9]中的5的个数(为3个)。
4. 编写函数实现字符串分割。SourceStr为源串,splitStr为分割字符串,SplitStr中的每一个字符在SourceStr被看作是分 
  割符。函数需要返回被分割符分割好的字符串链表。
  例如: 如果
      SourceStr为“this is pen ,that is a ball.”。
      SplitStr  为“, .”
 
  则返回的字符串链表每一项值分别为:“this” “is” “ pen”…
5.某银行有N个业务办理窗口,每一个窗口能办理各种业务, 银行客户分为VIP可与和普通客户,VIP客户有优先办理权,请你结合实际的应用场景为该银行设计排队机系统。
  1)设计关键的数据结构和算法并用代码表示。
  2)用自己的方式表达出系统的运转流程。

 

这里只做下第一题和题四题,第五题等以后有空再想想。

 

  1. /****************************************************************************
  2. 题目要求 :编写函数,检查给定字符串是否整数,如果是,返回其整数值(注:不允许使用某种特定的库函数)。 
  3. 用C描述的算法,注释略
  4. ****************************************************************************/
  5. int is_int(const char *str, int *num)
  6. {
  7.     char *p = (char *)str;
  8.     int temp = 0;
  9.     char sign = '+';
  10.     if ( !str )
  11.     {
  12.         assert(0);
  13.         return 0;
  14.     }
  15.     do 
  16.     {
  17.         if ( *p == ' ' )
  18.         {
  19.             p++;
  20.         }
  21.         else
  22.         {
  23.             break;
  24.         }
  25.     } while( *p != '/0' );
  26.     if ( *p == '-' || *p == '+' )
  27.     {
  28.         sign = *p++;
  29.     }
  30.     while ( *p != '/0' ) 
  31.     {
  32.         if ( (*p >= '0') && (*p <= '9') )
  33.         {
  34.             temp = temp * 10 + (*p - '0');
  35.         }
  36.         else
  37.         {
  38.             return 0;
  39.         }
  40.         p++;
  41.     }
  42.     *num = sign == '-' ? -temp : temp;
  43.     return 1;
  44. }
  45. /****************************************************************************
  46. 题目要求 :编写函数实现字符串分割。SourceStr为源串,splitStr为分割字符串,SplitStr中的每一个字符在SourceStr被看作是分 
  47. 割符。函数需要返回被分割符分割好的字符串链表。 
  48. 例如: 如果 
  49. SourceStr为“this is pen ,that is a ball.”。 
  50. SplitStr 为“, .”
  51. 用C描述的算法,注释略
  52. ****************************************************************************/
  53. struct node{
  54. char *str;
  55. struct node *next;
  56. };
  57. node* split_string(const char *str, const char *token)
  58. {
  59.     char *p = (char *)str;
  60.     char *ch = (char *)token;
  61.     char map[128];
  62.     char *pos = p;
  63.     char *substr = NULL;
  64.     node l;
  65.     node *plist = &l;
  66.     node *pnode = NULL;
  67.     int loop = 0;
  68.     if ( str == NULL || token == NULL )
  69.     {
  70.         assert(0);
  71.         return NULL;
  72.     }
  73.     l.next = NULL;
  74.     while ( *ch != '/0' )
  75.     {
  76.         map[*ch] = *ch++;
  77.     }
  78.     while ( *p != '/0' )
  79.     {
  80.         if ( map[*p] == *p )
  81.         {
  82.             pnode = (node *)malloc(sizeof(node));
  83.             substr = (char *)malloc(sizeof(char) * (p - pos + 1));
  84.             while ( pos != p )
  85.             {
  86.                 substr[loop++] = *pos++;
  87.             }
  88.             substr[loop] = '/0';
  89.             pnode->next = NULL;
  90.             pnode->str = substr;
  91.             plist->next = pnode;
  92.             plist = pnode;
  93.             loop = 0; 
  94.             pos = ++p;
  95.         }
  96.         else
  97.         {
  98.             ++p;
  99.         }
  100.     }
  101.     return l.next; 
  102. }
  103. void print_list(node *l)
  104. {
  105.     while ( l != NULL )
  106.     {
  107.         printf("%s|", l->str);
  108.         l = l->next;
  109.     }
  110. }
  111. void free_memory(node *l)
  112. {
  113.     node *p = l;
  114.     if ( l == NULL )
  115.     {
  116.         return;
  117.     }
  118.     do 
  119.     {
  120.         p = l->next;
  121.         free(l->str);
  122.         free(l);
  123.         l = p;
  124.     } while( l != NULL );
  125. }
  126. /*两算法简单测试*/
  127. #include 
  128. #include 
  129. int main(int argc, char* argv[])
  130. {
  131.     int num = 0;
  132.     char *strtest = " this is pen , that is a bal. ";
  133.     node *l = NULL;
  134.     is_int(" +51111", &num);
  135.     printf("%d/n", num);
  136.     cout << endl << endl;
  137.     cout << strtest << endl; 
  138.     l = split_string(strtest, ", .");
  139.     print_list(l);
  140.     free_memory(l);
  141.     l = NULL; 
  142.     system("pause");
  143.     return 0;
  144. }

推荐阅读
author-avatar
老黑_微笑
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有