C语言格式化输入不定长数组

时间:2021-11-11 07:54:49

先随便写写,有空再整理。

直接贴代码

#include <stdio.h>
#include <stdlib.h> //从一行标准输入中格式化输入一个不定长数组
void inputVec (); //读入给定行数的不定长数组
void inputVecs1 (); //读入不确定行数不定长数组
void inputVecs2 (); void main ()
{
inputVecs1 ();
return;
} void inputVecs1 ()
{
while (!feof(stdin))
{
int size = 0;
int i = 0;
int k = 0;
int arr[64] = {0}; i = scanf ("%d", &arr[size]);
k = getchar();
++size;
while (EOF != i && ',' == k && 64 >= size)
{
i = scanf ("%d", &arr[size]);
k = getchar();
++size;
printf ("%d:%d ", i, k);
} printf ("\n%d\n",size); for (i = 0; i < size; ++i)
{
printf ("%d ", arr[i]);
}
printf ("\n"); }
} void inputVecs2 ()
{
int n = 0;
int j = 0;
scanf ("%d", &n);
while (j < n)
{
int size = 0;
int i = 0;
int k = 0;
int arr[64] = {0}; i = scanf ("%d", &arr[size]);
k = getchar();
++size;
while (EOF != i && ',' == k && 64 >= size)
{
i = scanf ("%d", &arr[size]);
k = getchar();
++size;
printf ("%d:%d ", i, k);
} printf ("\n%d\n",size); for (i = 0; i < size; ++i)
{
printf ("%d ", arr[i]);
}
printf ("\n"); ++j;
}
} void inputVec ()
{
int size = 0;
int i = 0;
int k = 0;
int arr[64] = {0}; i = scanf("%d", &arr[size]);
k = getchar();
++size;
while (EOF != i && size <= 64 && ',' == k)
{
i = scanf("%d", &arr[size]);
k = getchar();
++size;
printf ("%d:%d ", i, k);
}
printf ("\n%d\n",size); for (i = 0; i < size; ++i)
{
printf ("%d ", arr[i]);
}
printf ("\n");
}

  测试数据(控制台输入的话就变成输入一行然后一个回车键,可以把所有内容存到txt中然后重定向):

1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20
21,22,23,24,25,26,27,28,29,30