基本的C语言编程 [英] Basic C programming

查看:117
本文介绍了基本的C语言编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写它获得一定数量的输入方案将输出所列的输入(只有0-9会计投入和忽略其他)的产物。例如:输入:345将导致输出:60,或另一个例子是,输入:3T4和输出:12

我已经给了很多的尝试,这就是我坚持用:

 的#include<&stdio.h中GT;
主要(){    INT C,I;    C =的getchar();
    I = 1;
    而(C!='\\ n'){
        如果(c取代; = 48&放大器;&放大器;℃下= 57){
          I = C *我;
          C =的getchar();
        }
    }
    的printf(%D,我);
    }


解决方案

修改

  I = C *我;

  I =(C  - '0')*我;

这里发生的事情是变量 C 包含数量不限数量的ASCII值。因此, 0 48, 1 49等等。所以,当你输入12,你不会得到 1×2 ,但 49x50

看一看ASCII表。 http://www.asciitable.com/

I'm trying to write a program which given a certain number of input will output the product of the listed input (only accounting inputs of 0-9 and ignoring other). For example: input:345 would result output: 60, or another example would be, input: 3t4 and output: 12

I have given it many attempts and this is what I'm stuck with:

#include <stdio.h>
main(){

    int c,i;

    c = getchar();
    i = 1;
    while (c!= '\n'){
        if (c>=48 && c<=57){
          i=c*i;
          c=getchar();
        }
    }
    printf("%d",i);
    }

解决方案

Change

i = c*i;

to

i = (c - '0')*i;

What is happening here is the variable c contains the ascii value of the number not the number. So, '0' is 48, '1' is 49 and so forth. So when you enter 12, you will not get 1x2 but 49x50.

Take a look at the ascii chart. http://www.asciitable.com/

这篇关于基本的C语言编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆