[英]scanf each digit of integer AND store the whole number in C
輸入示例:12345
如您所見,我可以通過scanf存儲每個數字“ 1”,“ 2”,“ 3”,“ 4”,“ 5”,但是如何存儲整數“ 12345”? 它可以在同一scanf行中發生嗎?
#include <stdio.h>
#include <math.h>
int main(){
int wholeNumber = 0;
int i1,i2,i3,i4,i5 = 0;
printf("\nPlease enter a five digit integer value.\n");
scanf("%1d%1d%1d%1d%1d",&i,&i2,&i3,&i4,&i5); //scanning each digit but
//how do I store the whole #?
return 0;
}
我將嘗試回答您的第一個問題。 您可以簡單地將輸入的數字乘以10的倍數。讓我們通過計算來顯示它:
5 * 1 + 4 * 10 + 3 * 100 + 2 * 1000 + 1 * 10000 = 12345
因此,這是手動使用的代碼:
...
scanf("%1d%1d%1d%1d%1d",&i,&i2,&i3,&i4,&i5);
i*=10000;
i2*=1000;
i3*=100;
i4*=10;
i5*=1;
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.