Chinaunix首页 | 论坛 | 博客
  • 博客访问: 199898
  • 博文数量: 22
  • 博客积分: 641
  • 博客等级: 上士
  • 技术积分: 756
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-27 00:41
文章分类

全部博文(22)

文章存档

2014年(1)

2013年(1)

2012年(20)

分类: C/C++

2012-09-19 11:44:34

题目:

  1. /*
  2. 题目描述(60分):
  3. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
  4. 比如字符串“abacacde”过滤结果为“abcde”。

  5. 要求实现函数:
  6. void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);

  7. 【输入】 pInputStr: 输入字符串
  8.          lInputLen: 输入字符串长度
  9. 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

  10. 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

  11. 示例
  12. 输入:“deefd” 输出:“def”
  13. 输入:“afafafaf” 输出:“af”
  14. 输入:“pppppppp” 输出:“p”
  15. */

  16. /* main函数已经隐藏,这里保留给用户的测试入口,在这里测试你的实现函数,可以调用printf打印输出*/
  17. /* 当前你可以使用其他方法测试,只要保证最终程序能正确执行即可 */
  18. /* 该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响*/

  19. /*
  20. 题目描述(40分):
  21. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
  22. 压缩规则:
  23. 1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
  24. 2. 压缩字段的格式为"字符重复的次数 字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"

  25. 要求实现函数:
  26. void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);

  27. 【输入】 pInputStr: 输入字符串
  28.          lInputLen: 输入字符串长度
  29. 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

  30. 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

  31. 示例
  32. 输入:“cccddecc” 输出:“3c2de2c”
  33. 输入:“adef” 输出:“adef”
  34. 输入:“pppppppp” 输出:“8p”
  35. */

  36. /*
  37. 题目描述(50分):
  38. 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
  39. 输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

  40. 补充说明:
  41. 1. 操作数为正整数,不需要考虑计算结果溢出的情况。
  42. 2. 若输入算式格式错误,输出结果为“0”。

  43. 要求实现函数:
  44. void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);

  45. 【输入】 pInputStr: 输入字符串
  46.          lInputLen: 输入字符串长度
  47. 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

  48. 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

  49. 示例
  50. 输入:“4 7” 输出:“11”
  51. 输入:“4 - 7” 输出:“-3”
  52. 输入:“9 7” 输出:“0” 注:格式错误
  53. */
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. }

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. }

阅读(6191) | 评论(0) | 转发(0) |
0

上一篇:Sed流编辑器学习札记

下一篇:单链表操作

给主人留下些什么吧!~~