C primer plus 第六版 第七章 第五题 编程练习答案

Github 地址:这里这里φ(>ω<*)

/* 本程序为习题四的修改版。

     题目要求: 以 switch 重新写一遍习题四。

*/ 
// 为了让读者明白逻辑。注释多。如果明白代码设立意图请勿观看注释 -- 感谢你对双眼的爱护和节约时间。


#define _CRT_SECURE_NO_WARNINGS  // 别诧异。我用的是VS2017社区版。貌似VS对于C标准不是很友好。 


#include<stdio.h>


#define ture 1


  int main(void)
  {
  int i=0;//循环用。 
  int j=0;//双感叹号替换计数。 
  int p=0;//单感叹号替换计数。 
 
  void deal(void);
 
  char file[200];
 
  printf("Input strat :\n");
 
for (i = 0; scanf("%c", &file[i]) == 1; i++)
{
switch (file[i])
{
case '#':break;
default:continue;
}break;
   
  
i = 0; //置零。


while (file[i] != '#')
{
switch (file[i]) /* switch 总*/
{
case '!': 
/* 在当前元素内容为感叹号的情况下。 */


switch ( file[i+1] ) 
/* switch 1*/
{
case '!':
/* 此种情况为 双感叹号 替换。*/
j++;
i++;
continue;


default:
/* 在当前元素内容为感叹号的情况下。 */
/* 此 default 意为当前下一个元素不为感叹号,则对上一个元素内容判定。 */


switch (file[i - 1]) 
/* switch 2*/
{
case '!': 
// 此种情况为: 当前元素内容和上一个元素内容都为感叹号。 
// 即为 双感叹号 替换。在 switch 1中已判断过。故进入迭代。
i++;
continue;

default:
// 此种情况则为:当前元素内容为感叹号,但是上一个元素内容为其他字符不为感叹号的情况。
// 即为 单感叹号 替换。 故进行 计数 并进入迭代。
p++;
i++;
continue;
}
}

default: 
i++;
continue;
}
}

  printf("\n");
  printf("\n感叹号替换次数是%d。单感叹号替换次数是%d, 双感叹号替换次数是%d。", p+j, p, j );

getchar();
getchar();
 
  return 0;
 
  } 

猜你喜欢

转载自blog.csdn.net/lth_1571138383/article/details/80480725