C 需要帮助理解while循环吗

C 需要帮助理解while循环吗,c,C,假设我们有一个非常简单的代码,开头如下: #include <stdio.h> int main() { char c; int x; printf("Num here: "); c = getchar(); x = 0; while (c!=EOF) { x = c - 48; //to convert x from ASCII #包括 int main(){ 字符c; int x; printf(“Num he

假设我们有一个非常简单的代码,开头如下:

#include <stdio.h>
int main() {
    char c;
    int x;

    printf("Num here: ");
    c = getchar();
    x = 0;

    while (c!=EOF) {
        x = c - 48; //to convert x from ASCII
#包括
int main(){
字符c;
int x;
printf(“Num here:”);
c=getchar();
x=0;
而(c!=EOF){
x=c-48;//从ASCII转换x
在这个程序中,我试图让用户键入一个数字(例如42),并尝试将一位数添加到十位数(以及百位数等);我很难理解如何让while循环返回到循环,直到数字结束

因此,我需要很多帮助来理解如何让循环一直读到字符的末尾,读取用户输入的字符作为数字(42),然后使用getchar()单独处理这些数字。

您的代码:

#include <stdio.h> 
#include<ctype.h>
int main() {
    int c;//getchar() returns integer
    int x;

    printf("Num here: ");

    x=0;    
    //As @Jonathan Leffler  suggested ,  
    //usage of while loop like this is very helpful the moment you press Enter loop breaks.  

    while (isdigit(c = getchar())) //isdigit is a function from ctype.h checks for Entered character is digit or not
        x = x*10 + c - 48; //here '0'==48   
    printf("%d",x);  
    }
若要将一位数字添加到十位,然后再添加到百位…若要在输入数字中添加数字,请使用下面的while循环

int sum=0,num;
//read num
while(num>0)
{
sum=sum+num%10; //get the last digit and add to sum
num=num/10; //reduce last digit of num
}  
通常,您会使用:

int c;  // Because getchar() returns an int, not a char
int x = 0;

while ((c = getchar()) != EOF)
{
    if (isdigit(c))
        x = x * 10 + (c - '0');
    else
        ...
}
每次到达循环顶部时,它都会读取一个字符。您可以通过在循环末尾运行大括号(或者偶尔使用
continue
语句)使循环返回。例如,如果您读取的字符不能是数字的一部分,您可以使用
break
退出循环

如果用户键入
42
(后跟Enter),然后先读
c=='4'
,然后读
c=='2'
,然后读换行符
'\n'
。对于从
'0'
'9'
的每一个数字,
数字-'0'
产生与该数字对应的数字。换行符不能是该数字的一部分,因此您可以使用
ungtc,stdin
或在阅读后中断循环

如果用户键入的是43219876543,而您预期的是42(并且
int
是32位的数量),请注意溢出

您可以将循环条件写为:

while ((c = getchar()) != EOF && isdigit(c))
甚至:

while (isdigit(c = getchar()))
我非常不愿意将后者实际应用到生产代码中,但从理论上讲,它是安全的


我如何单独处理每个数字,以便以后使用整个数字?这样,如果用户键入
102030
,我可以将
10
乘以
20
,然后(
10*20
)乘以
30

轮子中的轮子-或循环中的循环。您需要稍微指定您的标准。如果用户键入
1
您想要答案
1
;如果他们键入
12
,您想要
2
;如果他们键入
13
,您想要
6
;等等(这些都是一行输入上的数字)。您需要一个外循环,该循环跳过空白和制表符,然后使用内循环读取一个数字,然后将当前产品(初始值
1
)乘以新的数字,在外循环之后,您将打印产品。这将打印一个空行的
1
;可能这并不重要(可能确实如此)

下面是一些近似合适的代码:

#include <ctype.h>
#include <stdio.h>

int main(void)
{
    int c;
    while ((c = getchar()) != EOF && c != '\n')
    {
        int product = 1;
        while (c != EOF && c != '\n')
        {
            while (isspace(c))
                c = getchar();
            int number = 0;
            while (isdigit(c))
            {
                number = number * 10 + (c - '0');
                c = getchar();
            }
            printf("Number:  %d\n", number);
            product *= number;
        }
        printf("Product: %d\n", product);
    }
    return 0;
}

这两种输入都可以正常工作。空行被视为输入的结尾;包含空格的行则不能。如果使用第二个条件输入
1a2b3c
,您将得到输出
0
;使用第一个条件,您将得到一个无限循环。没有溢出保护;不要尝试执行阶乘20,并期望得到正确的答案(带32位
int
)。调整你的心意。

逐字阅读,并使用乔纳森答案中的while循环将其转换为数字。每次阅读数字时,只需将当前总和乘以10,然后将数字相加。这样,当你阅读最后一个数字并将其相加时,你就得到了正确的数字。

有时当考虑到所有语言的能力时,我们认为应该解决问题的方式可以用不同的方法来解决

#include <stdio.h>
int main() {
    int x;

    printf("Num here: ");
    scanf("%d", x);

}
#包括
int main(){
int x;
printf(“Num here:”);
scanf(“%d”,x);
}

实现与程序相同的功能。

getchar()
不会返回
char
。您的代码将无法正常工作。@IgnacioVazquez Abrams但是
char
也是一种整数类型,最终它将只存储整数,就ASCII而言
getchar()也是如此
不会比返回127太远,所以存储起来也很安全。不是吗?@PHI:谁说的
getchar()
仅限于ASCII?@IgnacioVazquez Abrams我是在这里说的。在这里,用户使用ASCII。当我们只关心ASCII时,它真的有区别吗?假设一个程序只接收程序员希望它接收的东西,这是创建脆弱垃圾的第一步。但字符也是一个关键整数类型,最终它将只存储整数,而且就ASCII而言,getchar()不会返回127,因此存储起来也很安全。不是吗?存储
getchar()的结果不安全
char
中,因为它返回的是
int
,而不是
char
。准确地说,
getchar()
可以返回与
无符号char
对应的每个值,再加上一个保证为负值的额外的不同值EOF。如果存储
getchar()的结果
在普通
char
中,然后与EOF比较,出现两个错误中的一个。普通
char
无符号且
c==EOF
条件从不为真,或者普通
char
有符号,然后是一个有效字符(通常为ÿ,y-umlaut,U+00FF,拉丁文小写字母y,带DIAERESIS)被误解为EOF.EOF是在
stdio.h
中定义的
-1
(至少C primer是这么说的),并且getchar()在到达文件末尾时返回EOF……如果我们现在不讨论unicode,而只讨论ASCII,那么负值不会造成问题?
            while (c != EOF && c != '\n' && !isdigit(c))
                c = getchar();
#include <stdio.h>
int main() {
    int x;

    printf("Num here: ");
    scanf("%d", x);

}