c++编程怎么读取文件时,将一行中未知个数的数字赋值给一个string类型后,再分别赋值给相应个数的int类型

2025-02-23 15:29:21
推荐回答(1个)
回答1:

#include
using namespace std ;

//求一个数有多少位
int GetDigitCount(int n)
{
int c = 0 ;
while (n)
{
n /= 10 ;
++c ;
}
return c ;
}

int main(void)
{
int num = 12345 ;

//取得数字的位数
int c = GetDigitCount(num) ;

//动态分配数组
int* a = new int[c] ;

//用数字各位填充数组,注意倒着填充
for (int i = c - 1; i >= 0; --i)
{
a[i] = num % 10 ;
num /= 10 ;
}

//输出数组中的数据
for (int i = 0; i < c; ++i)
{
cout << a[i] ;
}
cout << endl ;

delete a ;
a = NULL ;

system("pause") ;
return 0 ;
}