![](/img/trans.png)
[英]C: parse char array of digits separated by characters and turn digits into integers
[英]Turn a List of digits into an Intenger in C
給定一個數字列表,例如: <1 6 3 2 5> 你如何把它變成一個整數? 在這種情況下,16325。我能夠進行相反的操作,將整數轉換為數字列表,但我堅持使用這個......
這是一個簡短的程序,它接受一個整數數組並將其乘以一個整數:
#include <stdio.h>
#include <math.h>
int main()
{
int output = 0;
int numbers[5] = {1,2,3,4,5};
int size = sizeof numbers / sizeof numbers[0];
for(int i = size; i > 0; i--)
{
output = output + numbers[size-i] * pow(10,i-1);
printf("%d\n",output);
}
return 0;
}
首先,它找到數組的長度。 然后它需要取每個元素並將其乘以 10 的冪,具體取決於它在數組中的位置,添加到輸出以獲得總和。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.