[英]sorting strings largest and smallest
我無法使程序正確地將字符串分類為所有用戶輸入的最大和最小
即時通訊與if語句以及它們如何記錄最小和最大的問題
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
int finish = 0, longest=0, smallest=0, count =0;
char word[20], smallest_word[20], largest_word[20];
while (0 == finish)
{
printf("enter word:");
fflush(stdin);
scanf("%s", &word);
if (count == 0)
{
strcpy(smallest_word, word);
strcpy(largest_word, word);
}
if (strcmp(word, smallest_word)<smallest)
{
strcpy(smallest_word, word);
smallest = strcmp(word, smallest_word);
}
if (strcmp(word, largest_word) > longest)
{
strcpy(largest_word, word);
longest = strcmp(word, largest_word);
}
if (strlen(word) == 4)
{
finish++;
}
count++;
}
printf("smallest word: %s\n", smallest_word);
printf("largest word: %s\n", largest_word);
getch();
return 0;
}
程序運行只是無法正確記錄最大和最小值
您的邏輯和語法都有一些問題。
如果兩個字符串相同,則當字符串分別為src> dst和dst> src時,strcmp(src,dst)返回0。
scanf()需要變量的地址,而數組名本身給出了數組第1個元素的地址,因此無需使用'&'。
對於邏輯部分,應在增加值后繼續循環,以避免造成額外的混亂和比較。
if (count == 0)
{
strcpy(smallest_word, word);
strcpy(largest_word, word);
}
不需要最長和最小的變量,因為您可以使用零來代替它們。
我為您編輯的完全有效的代碼是
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
int finish = 0, longest=0, smallest=0, count =0;
char word[20], smallest_word[20], largest_word[20];
while (0 == finish)
{
printf("enter word:\t");
scanf("%s", word);
if (count == 0)
{
strcpy(smallest_word, word);
strcpy(largest_word, word);
count++;
continue; //To ensure in first time loop ends here
}
if (strcmp(word, smallest_word)<0)
strcpy(smallest_word, word);
else if (strcmp(word, largest_word) > 0)
strcpy(largest_word, word);
if (strlen(word) == 4)
{
finish++;
}
count++;
}
printf("smallest word: %s\n", smallest_word);
printf("largest word: %s\n", largest_word);
getch();
return 0;
}
快樂編碼:)
關於:
if (strcmp(word, smallest_word)<smallest)
和
如果(strcmp(word,maximum_word)>最長)
strcmp()
返回0或> 0或<0。 它不返回指向任何東西的指針。 返回值<0表示第一個參數按字母順序在第二個參數之前。 返回值0表示兩個參數相等。 返回值> 0表示第一個參數按字母順序在第二個參數之后。
您可以使用string.h
庫中的strlen()
查看字符串的大小。
頭文件conio.h
及其getch()
不是標准C的一部分,您應該避免使用它們。
請參閱為什么getch不便攜? 為什么我們不使用conio.h? 它過時了嗎? 。
您的循環似乎一直進行到用戶輸入長度恰好為4的單詞為止。無需為此使用諸如finish
這樣的單獨變量。 只需使用break
語句即可。
即,代替
if (strlen(word) == 4)
{
finish++;
}
做
if (strlen(word) == 4)
{
break;
}
您可以擺脫該finish
變量。
scanf()
需要指向緩沖區的指針,在緩沖區中需要將掃描的輸入作為參數存儲在格式字符串之后。 同
scanf("%s", &word);
您將指針指向指針,因為word
本身就是指針,因為C中的數組名稱會分解為指向其第一個元素的指針。 因此, word
本身指向存儲在word[]
數組中的第一個字符。
采用
scanf("%19s", &word);
代替。 19是寬度說明符。 選擇word
19是因為word
的大小為20
,我們需要1
字符空間來存儲\\0
表示字符串的結尾。 這可以幫助避免緩沖區溢出。
您可能還需要檢查scanf()
的返回值。 它返回成功分配的次數,例如
if( scanf("%19s", &word)!=1 ) {
//Something went wrong.
}
請參見在scanf函數中對字符串使用“&”會發生什么情況? 。
在stdin
上使用fflush()
會導致C語言出現不確定的行為,因為它只能在輸出流上使用。 請參閱使用fflush(stdin) 。
如果第一個參數小於第二個參數, strcmp()
只會返回一個負數,如果第一個參數大於另一個參數,則strcmp()
返回一個正數。 這些數字可以是任何數字。 您無需保存strcmp()
的返回值。
所以代替
if (strcmp(word, smallest_word)<smallest)
{
strcpy(smallest_word, word);
smallest = strcmp(word, smallest_word);
}
if (strcmp(word, largest_word) > longest)
{
strcpy(largest_word, word);
longest = strcmp(word, largest_word);
}
做
if (strcmp(word, smallest_word) < 0)
{
strcpy(smallest_word, word);
}
if (strcmp(word, largest_word) > 0)
{
strcpy(largest_word, word);
}
因此,您可以將程序更改為
char word[20], smallest_word[20], largest_word[20];
for(int count=0; ; ++count)
{
printf("enter word:");
scanf("%19s", word);
if (count == 0)
{
strcpy(smallest_word, word);
strcpy(largest_word, word);
}
if (strcmp(word, smallest_word) < 0)
{
strcpy(smallest_word, word);
}
if (strcmp(word, largest_word) > 0)
{
strcpy(largest_word, word);
}
if (strlen(word) == 4)
{
break;
}
}
printf("smallest word: %s\n", smallest_word);
printf("largest word: %s\n", largest_word);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.