问答题
已知检查括号匹配及注释、字符串处理的C源程序如下:
#include
<stdio.h>
int brace, brack, paren;
void
in_quote(int c);
void in_comment(void)
void
search(int c)
/* rudimentary syntax checker for C program
*/
int main()
{
Int
c;
extern int brace, brack, paren;
while((c=getchar())!=EOF){
if(c=='/'){
if((c=getchar())=='*')
in_comment(); /* inside comment
*
/
else
search(c);
}else if(c=='"||c==" ")
in_quote(c); /* inside quote
*/
else
search(c);
if(brace<0){ /* output errors */
printf("Unbalanced
braces/n");
brace=0;
}else
if(brack<0){
printf("Unbalanced brackets/n");
brack=0;
}else if (paren<0){
printf("Unbalanced parentheses/n");
paren=0;
}
}
if(brace>0) /* output errors */
printf("Unbalanced braees/n");
if(brack>0)
printf("Unbalanced brackets/n");
if(paren>0)
printf("Unbalanced parentheses/n");
return 0;
}
/* search: search for rudimentary syntax errors
*/
void seareh(int c)
{
extern int brace, brack, paren; [
if(c=='{')
++brace;
else if(c=='}')
--brace;
else if(c=='[')
++brack;
else
if(c==']')
--brack;
else if(c=='(')
++paren;
else if(c==')')
--paren;
}
/* in_comment: inside of a valid
comment */
Void in_comment(void)
{
int c, d;
c=getchar();
d=getchar(); /*
curr character */
while(c!='*'||d!='/'){ /* search for end
*/
c=d
d=getchar();
}
}
/* in_quote; inside quote */
void in_quote(int c)
{
int d;
while((d=getchar())!=c) /* search end quote */
if(d=='/')
getchar(); /* ignore escape seq */
}
问答题
画出程序中main函数的控制流程图;
【正确答案】main函数的控制流程图如下:
[*]
【答案解析】
问答题
设计一组测试用例,使该程序所有函数的语句覆盖率和分支覆盖率均能达到100%。如果认为该程序的语句覆盖率或分支覆盖率无法达到100%,需说明为什么。
【正确答案】测试用例:
①依次输入:c==EOF
②依次输入:c=='/', c=='*', c=='*', d=='/'
③依次输入:c=='/', c=='*', c=='*', d=='{', d=='/'
④依次输入:c=='/', c=-'*', c=='/', d=='/', d=='/'
⑤依次输入:c=='/', c=='/'
⑥依次输入:c=='/', c=='{'
⑦依次输入:c=='/', c=='}'
⑧依次输入:c=='/', c=='['
⑨依次输入:c=='/', c==']'
⑩依次输入:c=='/', c=='('
[*]依次输入:c=='/', c==')'
[*]依次输入:c=='/', d=='/'
[*]依次输入:c=='/', d=='*'
[*]依次输入:c=='/', d=='/'
[*]依次输入:c==''', d=='/'
[*]依次输入:c==''', d=='*'
[*]依次输入:c==''', d=='/'
[*]依次输入:c=='{'
[*]依次输入:c=='}'
[*]依次输入:c=='['
[*]依次输入:c==']'
[*]依次输入:c=='('
[*]依次输入:c=='/'
该程序的语句覆盖率和分支覆盖率无法达到100%。因为该程序中有好几个判断语句,对于有的判断语句,若取真,则取假无法执行;若取假,则取真无法执行。所以,语句覆盖率和分支覆盖率无法达到100%。
【答案解析】