您好,我正在开发一个小程序来整理从文件中提取的数字。目前,我目前的难题是如何将文件中的数字作为整数一次接收一个,或者如何将它们与字符串分开。
sample input:
3 4 6 60 9 10 2 20
56 11 18
34
output:
3 4 6 60 9 10 2 20 56 11 /*prints out the first 10 numbers taken in then sorts it*/
3 4 6 9 10 11 18 20 56 60 /* after first 10 it looks at the next input then sorts it again*/
4 6 9 10 11 18 20 34 56 60
void Sortingnums(char *nums,int firsttime)
{
//holds counter and temporary number//
int i, k, temp;
//holds temporary c string//
char* wordnum;
//just take in the first ten numbers and that is it.
if(firsttime == 0)
{
wordnum = strtok(nums," ");
numbers[0] = atoi(wordnum);
//take in the first 10 numbers in the string//
for(i = 1; i < 10; i++)
{
wordnum = strtok(NULL," ");
numbers[i] = atoi(wordnum); //store the number//
}
// output the first 10 numbers//
for(i = 0; i < 10; i++)
{
cout << numbers[i] << " " << endl;
}
firsttime++;
}
while(
下面的示例是我的排序算法,它接受一个 cstring 数组并将其拆分为由空格分隔的整数,但是我遇到的一个问题是前 10 个数字必须在之前打印。
我将如何整理其余的输入?(第一行输入可以有10个以上的数字)