下列给定程序中,函数fun的功能是:用冒泡法对6个字符串进行升序排列。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include <stdlib.h > #include <string.h> #include <conio.h> #include <stdio.h> #define MAXLINE 20 fun (char * pstr[6]) int i, j; char * p; for(i=0; i<5; i++) /********** found********** / for(j=i+1, j<6, j++) if (strcmp (* (pstr+i), (pstr+ j))>0) p=* (pstr+i); /********** found********** / * (pstr+i)=pstr+j; * (pstr+j)=p; void main () int i; char * pstr[6], str[6] [MAXLINE]; system ("CLS"); for(i=0; i<6; i++) pstr[i]=str[i]; printf("nEnter 6 string(1 string at each line) :n "); for(i=0; i<6; i++) scanf("% s", pstr[i]); fun (pstr); printf (" The strings after sor- ting:n "); for (i=0; i<6; i++) printf ("% sn ", pstr[i]);
下列给定程序中,函数fun的功能是:用冒泡法对6个字符串进行升序排列。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include <stdlib.h > #include <string.h> #include <conio.h> #include <stdio.h> #define MAXLINE 20 fun (char * pstr[6]) int i, j; char * p; for(i=0; i<5; i++) /********** found********** / for(j=i+1, j<6, j++) if (strcmp (* (pstr+i), (pstr+ j))>0) p=* (pstr+i); /********** found********** / * (pstr+i)=pstr+j; * (pstr+j)=p; void main () int i; char * pstr[6], str[6] [MAXLINE]; system ("CLS"); for(i=0; i<6; i++) pstr[i]=str[i]; printf("\nEnter 6 string(1 string at each line) :\n "); for(i=0; i<6; i++) scanf("% s", pstr[i]); fun (pstr); printf (" The strings after sor- ting:\n "); for (i=0; i<6; i++) printf ("% s\n ", pstr[i]);
题目解答
答案
for (j = i + 1; j < 6; j++)(2) *(pstr + i) = *(pstr + j) ;
解析
在给定的程序中,有两个错误需要修正。第一个错误在for循环的条件部分,第二个错误在交换字符串指针的代码部分。
步骤 2:修正第一个错误
在for循环的条件部分,逗号应该被替换为分号,以正确地表示循环的条件。
步骤 3:修正第二个错误
在交换字符串指针的代码部分,需要使用指针的解引用操作符来正确地交换指针的值。