华为2013上机题

  1. /*
  2. 题目描述(60分):
  3. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
  4. 比如字符串“abacacde”过滤结果为“abcde”。
  5.  
  6. 要求实现函数: 
  7. void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
  8.  
  9. 【输入】 pInputStr: 输入字符串
  10.          lInputLen: 输入字符串长度 
  11. 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
  12.  
  13. 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
  14.  
  15. 示例 
  16. 输入:“deefd” 输出:“def”
  17. 输入:“afafafaf” 输出:“af”
  18. 输入:“pppppppp” 输出:“p”
  19. */
  20.  
  21. /* main函数已经隐藏,这里保留给用户的测试入口,在这里测试你的实现函数,可以调用printf打印输出*/
  22. /* 当前你可以使用其他方法测试,只要保证最终程序能正确执行即可 */
  23. /* 该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响*/
  24.  
  25. /*
  26. 题目描述(40分):
  27. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
  28. 压缩规则:
  29. 1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
  30. 2. 压缩字段的格式为"字符重复的次数 字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"
  31.  
  32. 要求实现函数: 
  33. void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
  34.  
  35. 【输入】 pInputStr: 输入字符串
  36.          lInputLen: 输入字符串长度 
  37. 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
  38.  
  39. 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
  40.  
  41. 示例 
  42. 输入:“cccddecc” 输出:“3c2de2c”
  43. 输入:“adef” 输出:“adef”
  44. 输入:“pppppppp” 输出:“8p”
  45. */
  46.  
  47. /*
  48. 题目描述(50分): 
  49. 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
  50. 输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。
  51.  
  52. 补充说明:
  53. 1. 操作数为正整数,不需要考虑计算结果溢出的情况。
  54. 2. 若输入算式格式错误,输出结果为“0”。
  55.  
  56. 要求实现函数: 
  57. void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);
  58.  
  59. 【输入】 pInputStr: 输入字符串
  60.          lInputLen: 输入字符串长度 
  61. 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
  62.  
  63. 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
  64.  
  65. 示例 
  66. 输入:“4 7” 输出:“11”
  67. 输入:“4 - 7” 输出:“-3”
  68. 输入:“9 7” 输出:“0” 注:格式错误
  69. */
1. stringFilter函数

  1. void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
  2. {
  3.   int i,j,flag,k=0;
  4.   pOutputStr[0]=pInputStr[0];
  5.   for(i=1;i<lInputLen;i++)
  6.      {
  7.          flag=1;
  8.          for(j=0;j<=k;j++)
  9.             {
  10.               if(pOutputStr[j]==pInputStr[i])
  11.                     flag=0;
  12.             }
  13.          if(flag==1)
  14.          {
  15.            k=k+1;
  16.            pOutputStr[k]=pInputStr[i];
  17.          }
  18.      }
  19.    pOutputStr[k+1]='\0';
  20. }
  21.  
2.stringZip函数


  1. void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
  2. {
  3.   int i,count=1;
  4.   pOutputStr[0]='\0';
  5.   for(i=0;i<lInputLen;i++)
  6.    {
  7.      if(pInputStr[i]==pInputStr[i+1])
  8.         {
  9.           count++;
  10.         }
  11.      else
  12.         {
  13.           sprintf(pOutputStr strlen(pOutputStr),"%d%c",count,pInputStr[i]);
  14.          // *pOutputStr =count+'0';
  15.          // *pOutputStr =pInputStr[i];
  16.           count=1;
  17.         }
  18.    }
  19. // *pOutputStr='\0';
  20. }

3. arithmetic函数


  1. void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
  2. {
  3.     char* pTemp = (char*)malloc(lInputLen);
  4.     char* pLeft = pTemp;
  5.     const char* pRight = pInputStr;
  6.     int L,R;
  7.     int result;
  8.     while(*pInputStr != '\0')
  9.     {
  10.         if(*pInputStr == '+' || *pInputStr == '-')
  11.         {
  12.             pRight = pInputStr+2;
  13.             break;
  14.         }
  15.         else
  16.         {
  17.             *pTemp = *pInputStr++;
  18.         }
  19.     }
  20.     *pTemp = '\0';
  21.     if (pRight == pLeft || *pRight == '+' || *pRight == '-')
  22.     {
  23.         *pOutputStr++='0';
  24.         *pOutputStr = '\0';
  25.         return;
  26.     }
  27.     puts(pRight);
  28.     puts(pLeft);
  29.     L = atoi(pLeft);
  30.     R = atoi(pRight);
  31.     
  32.     switch (*pInputStr)
  33.     {
  34.         case '+':
  35.             result = L + R;
  36.             break;
  37.         case '-':
  38.             result = L - R;
  39.         break;
  40.         default:
  41.             result = 0;
  42.         break;
  43.     }
  44.     itoa(result, pOutputStr, 10);
  45.  
  46. }
posted on 2013-07-28 12:07  Revolutionist  阅读(180)  评论(0)    收藏  举报