【发布时间】:2013-02-02 20:03:54
【问题描述】:
您好,我正在开发一个小程序来整理从文件中提取的数字。目前,我目前的难题是如何将文件中的数字作为整数一次接收一个,或者如何将它们与字符串分开。
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个数字)
【问题讨论】:
-
如果您告诉我们您使用的编程语言可能会有所帮助。 (并添加标签)
-
@AlexKrauss 我将其编辑到标题中
-
第 42 行中的
break;应该是continue;。 -
所以你要预加载 10 个项目,然后 (1) 排序,(2) 打印,(3) 删除第一个项目,(4) 添加一个新项目,否则如果 EOF,最后(5) 转到 (1) 。 that 是否(不好)描述了您正在寻找的算法?
-
我出于好奇尝试了这个,你的第二个输出没有意义,因为我一开始就得到 2。