问答题
已知C源程序如下:
/
************************************************** /
/*
功能:检查输入的标识符是否符合C语言规则 */
/
************************************************** /
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<condo.h>
#include<malloc.h>
char * IsLegal(char * CheckWor
D.;
const int MaxWordLen=32;
char *
ErrorMessages[]={/* 错误信息列表 */
"合法!",
"首字符只能是字母或下划线!",
"常、变量只能由字母、下划线和数字构成!",
"常、变量标识不能用C语言关键字!",
"常、变量标识不能用C语言预定义函数名!",
"内存不够!"
};
int main()
{
char * Prompt="C语言标识符的命名要遵守以下原则.";
char *
TestWord
inti;
TestWord=(char
*)malloc(sizeof(char) * MaxWordLen);
/* TestWord存放用户输入
*/
if(!TestWor
D.
return
1;
/* 显示部分提示信息 */
puts(Prompt);
for(i=1; i<=4; i++){
puts(ErrorMessages [i]);
}
while(1) {
printf("/n/n请输入一个标识符(大写的Q退出))."); /* 提示 */
scanf("% s",
TestWor
D.; /* 得到用户输入 */
if(toupper)(TestWord[0]))==0)
breaks/* 循环出口 */
printf("/n % s % s", TestWord, IsLegal(TestWor
D.); /*
判定标识符的合法性 */
}
free(TestWor
D.;
return 0;
}
/*
此函数检验标识符命名的合法性 */
char * IsLegal(char * CheckWor
D.
{
char * KeyWords []={"auto",
"break", "case", "char", "continue", "const", "default",
"do",
"double", "else", "ehum", 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", "creat", "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", "ato1" ,"ceil", "dos", "cosh", ecvt",
"exp", "fabs", "floor", "fmod", "frexp", "itoa", "labs",
"idexp",
"log", "log10", "modf", "pow", "rand", "sin", "sqrt",
"srand",
"strtod", "strlol", "tan", "tanh", "ultoa", "memset",
"strcpy",
"strcat", "strchr", "strcmp", "calloc", "isalnum",
"isalpha",
"toascii", "tolower", "tollpper", "exit"}; /*
C主要预定义函数列表* /
char * Others="_'; /* "_"也可以用于关键字,但我们不推荐您使用!
*/
int WordLength, i;
char *
WordTemp;
WordLength=strlen(CheckWor
D.;
/* 检查标识符命名原则1 */
if((isalpha(CheckWord[0])==0) && (CheckWord[0]!=Others[0]))
return ErrorMessages[1];
/* 检查标识符命名原则2 */
for(i=0; i<WordLength; i++)
if((isalnum(CheckWord[i]==0)
&& (CheckWord[i]!=Others[0]))
return
ErrorMessages[2];
/* 检查标识符命名原则3 */
for(i=0;
i<44; i++)
{
if(!strcmp(CheckWord,
KeyWords[i]))
return ErrorMessages[3];
}
/* 检查标识符命名原则4 */
for(i=0; i<69;
i++)
{
if (!strcmp(CheckWord,
Functions[i]))
return ErrorMessages[4];
}
return ErrorMessages[0];
}
问答题
参照QESAT/C软件分析与测试工具的规定,画出程序中所有函数的控制流程图;
问答题
设计一组测试用例,使该程序所有函数的语句覆盖率和分支覆盖率尽量达到最大。如果认为该程序的语句覆盖率或分支覆盖率无法达到100%,需说明为什么。
【正确答案】测试用例:
①输入:bookCount,输出:合法!
②输入:puts,输出:常、变量标识不能用C语言预定义函数名!
③输入:5student,输出:首字符只能是字母或下划线!
④输入:stu+name,输出:常、变量只能由字母、下划线和数字构成!
⑤输入:float,输出:常、变量标识不能用C语言关键字!
⑥输入:putsfloatstudentsnamechinachineseenglishchin achinesechinachina,输出:内存不够!
⑦输入:_StuId,输出:合法!
该程序的语句覆盖率或分支覆盖率均无法达到100%,因为该程序允许中间返回值。如果if条件中任意一个成立就立即返回,那么剩下的语句就无法执行;即使所有的if条件均不成立,运行到最后,那么if条件成立时的语句就无法执行,其覆盖率始终无法达到100%。
【答案解析】