我想接受如下輸入:
寫:1 5 3
或者
寫:1 2
或者
寫:2 4 9 4
或者
寫:(直接回車)
但是,問題是我想取最大 5 個整數或更少。因此,我寫了這個運算式:
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
當我這樣做時,如果我寫整數,例如 1 4 6,程式會繼續需要整數,直到我完成整數個數為 5。
此外,有時,我需要通過按 Enter 鍵跳過這一部分而不寫入整數。但是,我無法實作。
我也這樣做了:
scanf("%d%c", &firstStore,&control);
if(control==' ')
{
scanf("%d%c", &secondStore,&control);
if(control==' ')
{
scanf("%d%c", &thirdStore,&control);
if(control==' ')
{
scanf("%d%c", &fourthStore,&control);
if(control==' ')
{
scanf("%d", &fifthStore);
}
}
}
}
這段代碼的問題是,如果我愿意,我不能跳過這部分。我的意思是這段代碼總是希望至少取一個整數。
規則:
禁止使用陣列。
uj5u.com熱心網友回復:
我建議獲取一整行然后決議它:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char line[1024];
if (fgets(line, 1024, stdin) == NULL) {
perror("Error occured");
return 2;
}
if (line[strlen(line)-1] != '\n') {
while (getchar() != '\n');
} else {
line[strlen(line)-1] = '\0';
}
int counter = 0;
for (char * word = strtok(line, " "); word; word = strtok(NULL, " ")) {
int number = atoi(word);
// do something with your number
counter ;
}
if (counter == 0) {
return 1;
}
return 0;
}
uj5u.com熱心網友回復:
檢查 的回傳值scanf()。
從手冊頁scanf:
成功時,這些函式回傳成功匹配和分配的輸入項的數量;如果早期匹配失敗,這可能會小于規定的值,甚至為零。
所以你的代碼應該是這樣的:?
#include <stdio.h>
int main(void)
{
int n;
while ((scanf("%d")) == 1) {
/* perform actions */
}
return 0;
}
要實作不鍵入任何內容的部分,您必須使用除scanf(). 考慮fgets()或getchar()。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n;
char buff[500];
while (fgets(buff, sizeof buff, stdin) != NULL) {
if (buff[0] == '\n') { /* empty - just newline character */
continue;
}
if (sscanf(buff, "%d", &n) != 1) { /* unsuccessful conversion */
exit(1);
}
}
return 0;
}
考慮到每行都有一個數字,或者您每次都輸入一個數字,這將起作用。如果您的行有多個數字,您將不得不以不同方式決議它,可能通過使用strtok()或修改sscanf().
您可以使用%n格式和回圈,這樣您就可以使用sscanf().
從手冊頁%n:
沒有任何期望;相反,到目前為止從輸入消耗的字符數通過 next 指標存盤,該指標必須是指向 int 的指標,或者其大小與(可選)提供的整數長度修飾符匹配的變體。
因此,您可以將 if 塊替換為以下內容:
int char_read;
int start = 0;
while (start < sizeof(buff) &&
sscanf(&buff[start], "%d%n", &n, &char_read) == 1) { /* 1 successful conversion */
printf("%d\n", n);
start = char_read;
}
作業示例
uj5u.com熱心網友回復:
與所有其他答案一樣,以下解決方案違反了不使用陣列的規則,因為我想不出任何不使用陣列的干凈解決方案。
使用的問題scanf是它會平等地對待空格和換行符。這不是你想要的。
對于基于行的用戶輸入,始終一次讀取一行并單獨處理每一行更有意義。為了將一行用戶輸入讀取為字串,您可以使用函式fgets. sscanf然后,您可以使用函式或將此字串轉換為數字strtol。
這是使用該功能的解決方案sscanf:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
for (;;) //infinite loop
{
char line[200];
int numbers[5];
int num_numbers;
//prompt user for input
printf( "Please enter up to 5 numbers: " );
//attempt to read one line of input from user
if ( fgets( line, sizeof line, stdin ) == NULL )
break;
//verify that line was not too long for input buffer
if ( strchr( line, '\n' ) == NULL && !feof( stdin ) )
{
printf( "Line too long for input buffer!\n" );
exit( EXIT_FAILURE );
}
//attempt to convert string to numbers
num_numbers = sscanf(
line,
"%d %d %d %d %d",
&numbers[0], &numbers[1], &numbers[2],
&numbers[3], &numbers[4]
);
//print result
if ( num_numbers == 0 || num_numbers == EOF )
{
printf( "Was not able to match any numbers.\n" );
}
else
{
printf( "Successfully matched %d numbers:\n", num_numbers );
for ( int i = 0; i < num_numbers; i )
{
printf( "%d\n", numbers[i] );
}
}
printf( "\n" );
}
}
該程式具有以下行為:
Please enter up to 5 numbers: 20 30 35 40
Successfully matched 4 numbers:
20
30
35
40
Please enter up to 5 numbers:
Was not able to match any numbers.
Please enter up to 5 numbers: 12 17
Successfully matched 2 numbers:
12
17
Please enter up to 5 numbers: 5
Successfully matched 1 numbers:
5
這是一個使用該函式strtol代替的解決方案sscanf:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUMBERS 5
int main( void )
{
for (;;) //infinite loop
{
char line[200], *p = line, *q;
long numbers[MAX_NUMBERS];
int num_numbers = 0;
//prompt user for input
printf( "Please enter up to 5 numbers: " );
//attempt to read one line of input from user
if ( fgets( line, sizeof line, stdin ) == NULL )
break;
//verify that line was not too long for input buffer
if ( strchr( line, '\n' ) == NULL && !feof( stdin ) )
{
printf( "Line too long for input buffer!\n" );
exit( EXIT_FAILURE );
}
//convert as many numbers as possible
for ( int i = 0; i < MAX_NUMBERS; i )
{
numbers[i] = strtol( p, &q, 10 );
if ( p == q )
break;
p = q;
num_numbers ;
}
//print result
if ( num_numbers == 0 )
{
printf( "Was not able to match any numbers.\n" );
}
else
{
printf( "Successfully matched %d numbers:\n", num_numbers );
for ( int i = 0; i < num_numbers; i )
{
printf( "%ld\n", numbers[i] );
}
}
printf( "\n" );
}
}
第二個程式的輸出與第一個程式完全相同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/460881.html
下一篇:JS頁面加載除錯
