已知C源程序如下: /**************************************************/ /* 功能:检查输入的标识符是否符合C语言规则 */ /**************************

admin2011-01-12  28

问题 已知C源程序如下:
   /**************************************************/
   /*  功能:检查输入的标识符是否符合C语言规则  */
   /**************************************************/
   #include<stdio.h>
   #include<string.h>
   #include<ctype.h>
   #include<conio.h>
   #include<malloc.h>
   char*IsLegal(char*CheckWorD) ;
   const int MaxWordLen=32;
   char*ErrorMessages[]={    /*错误信息列表*/
   "合法!",
   "首字符只能是字母或下划线!",
   "常、变量只能由字母、下划线和数字构成!",
   "常、变量标识不能用C语言关键字!"。
   "常、变量标识不能用C语言预定义函数名!",
   "内存不够!"
   };
   int main()
   {
   char*Prompt="C语言标识符的命名要遵守以下原则:";
   char*TestWord;
   int i;
   TestWord=(char*)malloc(sizeof(char)*MaxWordLen);
   /*TestWord存放用户输入*/
   if(!TestWorD)
   return 1;
   /*显示部分提示信息*/
   puts(Prompt);
   for(i=1;i<=4;i++){
   puts(ErrorMessages);
   }
   while(1){
   printf("\n\n请输入一个标识符(大写的Q退出):");/*提示*/
   scanf("%s",TestWorD) ;                         /*得到用户输入*/
   if(toupper)(TestWord[0]))==’0’)
   break;                                         /*循环出口*/
   prind("\n%s  %s",TestWord,IsLegal(TestWorD) );/*判定标识符的合法性*/
   }
   free(TesfWorD) ;
   return 0;
   }
   /*此函数检验标识符命名的合法性*/
   char*IsLegal(ehar*CheckWorD)
   {
   char*KeyWords[]={"auto","break","case","char","continue","const","default",
   "do","double","else","enum",extern","float","for","goto",
   "if","int","long","noalias","register","return","short",
   "signed","sizeof","static","struct","switch","typedef",
   "union","unsigned","void","volatile","while","defined",
   "define","undef","include","ifdef","ifndef","endif","line",
   "error","elif","pragma"};    /*C关键字列表*/
   char*Functions[]={"close","crest","eof","fclose","fcloseall","feof","fopen",
   "ferror","fgetchar","fgets","fprintf","fputc","fputchar",
   "fseek","get","putch","putc","printf","open","putchar",
   "puts","read","scanf","abs","acos","asin","math","atan",
   "atan2","atof","atoi","atol","ceil","dos","cosh",ecvt",
   "exp","fabs","floor","fmod","frexp","itoa","labs","ldexp",
   "log","log10","modf","pow","rand","sin","sqrt","srand",
   "strtod","strlol","tan","tanh","ultoa","memset","strcpy",
   "strcat","strchr","strcrap","calloc","isalnum","isalpha",
   "toascii","tolower","tollpper","exit");  /*C主要预定义函数列表*/
   char*Others="_";    /*’_’也可以用于关键字,但我们不推荐您使用!*/
   int WordLength,i;
   char*WordTemp;
   WordLength=strlen(CheckWorD) ;
   /*检查标识符命名原则1*/
   if((isalpha(CheckWord[0])==0)&&(CheckWord[0]!=Others[0]))
   return ErrorMessages[1];
   /*检查标识符命名原则2*/
   for(i=0;i<WordLength;i++)
   if((isalnum(CheekWord==0)&&(CheckWord!=Others[0]))
   return ErrorMessages[2];
   /*检查标识符命名原则3*/
   for(i=0;i<44;i++)
   {
   if(!strcmp(CheckWord,KeyWords))
   return ErrorMessages[3];
   }
   /*检查标识符命名原则4 */
   for(i=0;i<69;i++)
   {
   if(!stremp(CheckWord,Functions))
   return ErrorMessages[4];
   }
   return ErrorMessages[0];
   }

设计一组测试用例,使该程序所有函数的语句覆盖率和分支覆盖率尽量达到最大。如果认为该程序的语句覆盖率或分支覆盖率无法达到100%,需说明为什么。

选项

答案测试用例: ①输入:bookCount,输出:合法! ②输入:puts,输出:常、变量标识不能用C语言预定义函数名! ③输入:5student,输出:首字符只能是字母或下划线! ④输入:stu+name,输出:常、变量只能由字母、下划线和数字构成! ⑤输入:float,输出:常、变量标识不能用C语言关键字! ⑥输入:putsfloatstudentsnamechinachineseenglishchinachinesechinachina,输出:内存不够! ⑦输入:_StuId,输出:合法! 该程序的语句覆盖率或分支覆盖率均无法达到100%,因为该程序允许中间返回值。如果if条件中任意一个成立就立即返回,那么剩下的语句就无法执行;即使所有的if条件均不成立。运行到最后,那么if条件成立时的语句就无法执行,其覆盖率始终无法达到100%。

解析
转载请注明原文地址:https://kaotiyun.com/show/pwNZ777K
0

相关试题推荐
最新回复(0)