1.第一题错在哪里了?
-
#include <stdio.h>
-
#include <string.h>
-
#define LEN 100
-
#define LENGTH 20
-
int main()
-
{
-
char word[LENGTH];
-
char words[LEN +
1][LENGTH +
1];
-
scanf(
"%s",word);
-
char (*p)[LENGTH +
1];
-
p = words;
-
char *smallest =
"";
-
char *biggest =
"";
-
while(
strlen(word) !=
4)
-
{
-
strcpy(*p ++,word);
-
if(
strcmp(smallest,word) >
0)
-
smallest = word;
-
if(
strcmp(biggest,word) <
0)
-
biggest = word;
-
scanf(
"%s",word);
-
}
-
-
printf(
"%s\n",biggest);
-
printf(
"%s\n",smallest);
-
return
0;
-
}
改正:
-
//方法1:smallest和biggest定义为char *
-
#include <stdio.h>
-
#include <string.h>
-
#include <stdbool.h>
-
-
/*LEN是words的单词个数,LENGTH是words每个单词的预留长度*/
-
#define LEN 100
-
#define LENGTH 20
-
-
int init(char (*)[LENGTH + 1],int );
-
void find_big_small(char (*)[LENGTH + 1]);
-
void print(char (*p)[LENGTH + 1]);
-
int main()
-
{
-
char words[LEN +
1][LENGTH +
1];
-
init(words,LEN +
1);
-
print(words);
-
find_big_small(words);
-
-
return
0;
-
}
-
-
/* initial the words */
-
int init(char (*p)[LENGTH + 1],int n)
-
{
-
char word[LENGTH +
1];
-
scanf(
"%s",word);
-
int cnt =
0;
-
while(
strlen(word) !=
4 && (cnt < n))
-
{
-
strcpy(*p ++,word);
-
scanf(
"%s",word);
-
++ cnt;
-
}
-
if(cnt == n)
-
strcpy(*p,
"");
-
else
-
{
-
strcpy(*p ++ ,word);
-
strcpy(*p,
"");
-
++ cnt;
-
}
-
return cnt;
-
}
-
/* find smallest and biggest word */
-
void find_big_small( char (*p)[LENGTH + 1])
-
{
-
//small和big是拷贝,这两个赋值都得拷贝
-
char small[LENGTH +
1];
-
char big[LENGTH +
1];
-
strcpy(small,*p);
-
strcpy(big,*p);
-
-
for(;
strcmp(*p,
"") !=
0;++p)
-
{
-
if(
strcmp(small,*p) >
0)
-
strcpy(small,*p);
-
if(
strcmp(big,*p) <
0)
-
strcpy(big,*p);
-
}
-
printf(
"small:%s\n",small);
-
printf(
"big :%s\n",big);
-
}
-
-
/* print the words */
-
void print(char (*p)[LENGTH + 1])
-
{
-
while(
strcmp(*p,
"") !=
0)
-
{
-
printf(
"%s\n",*p ++);
-
}
-
}
自己评注:什么都好,就是在find_big_small中small和big都执行拷贝,下面这个程序不执行字符串拷贝,
下面是执行结果:
-
dog
-
zebra
-
rabbit
-
catfish
-
walrus
-
cat
-
fish
-
----------------------
begin print -----------------------
-
dog
-
zebra
-
rabbit
-
catfish
-
walrus
-
cat
-
fish
-
--------------------------
end-----------------------------
-
small:cat
-
big
:zebra
把程序中查找中的small和big换成指针后,程序如下:
-
-
//方法1:smallest和biggest定义为char *
-
#include <stdio.h>
-
#include <string.h>
-
#include <stdbool.h>
-
#define LEN 100
-
#define LENGTH 20
-
-
int init(char (*)[LENGTH + 1],int );
-
void find_big_small(char (*)[LENGTH + 1]);
-
void print(char (*p)[LENGTH + 1]);
-
int main()
-
{
-
char words[LEN +
1][LENGTH +
1];
-
init(words,LEN +
1);
-
print(words);
-
find_big_small(words);
-
-
-
return
0;
-
}
-
-
int init(char (*p)[LENGTH + 1],int n)
-
{
-
char word[LENGTH +
1];
-
scanf(
"%s",word);
-
int cnt =
0;
-
while(
strlen(word) !=
4 && (cnt < n))
-
{
-
strcpy(*p ++,word);
-
scanf(
"%s",word);
-
++ cnt;
-
}
-
if(cnt == n)
-
strcpy(*p,
"");
-
else
-
{
-
strcpy(*p ++ ,word);
-
strcpy(*p,
"");
-
++ cnt;
-
}
-
return cnt;
-
}
-
-
void find_big_small( char (*p)[LENGTH + 1])
-
{
-
char (*small)[LENGTH +
1];
-
char (*big)[LENGTH +
1];
-
small = p;
-
big = p;
-
-
for(;
strcmp(*p,
"") !=
0;++p)
-
{
-
//small big is pointer which pointed to array
-
if(
strcmp(*small,*p) >
0)
-
small = p;
-
if(
strcmp(*big,*p) <
0)
-
big = p;
-
}
-
printf(
"small:%s\n",*small);
-
printf(
"big :%s\n",*big);
-
}
-
-
void print(char (*p)[LENGTH + 1])
-
{
-
while(
strcmp(*p,
"") !=
0)
-
{
-
printf(
"%s\n",*p ++);
-
}
-
}
转载:https://blog.csdn.net/digitalkee/article/details/113796572
查看评论