【问题标题】:Separating spaced Numbers in C++在 C++ 中分隔间隔的数字
【发布时间】: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。

标签: c++ sorting input output


【解决方案1】:

我试图做到基本和简单,但实际使用 C++(和基本 C 函数)。

  1. 将文件中的所有文本读入std::string。
  2. 通过分隔符“\n”(或“\r\n”,取决于输入)将字符串拆分为“std::vector lines”。 (使用 std::string::find()、std::string::substr())。
  3. 循环行,并将每一行由分隔符' '分割成“std::vector string_numbers”。
  4. 循环“string_numbers”并使用 atoi() 将每个 string_number 插入到 std::vector 数字中。
  5. 排序(网上有很多算法。冒泡排序非常简单,但效率不高 O(n^2)。如果您希望自己实现更高效的方法,请查看合并排序 O(nlogn) 或快速排序平均情况下为 O(nlogn),更难实现,但速度更快)。

【讨论】:

  • 好的,我已经用我的排序功能更新了我的问题和一个新问题
【解决方案2】:

我建议使用标准模板库来帮助您控制数字和排序。另外,我不确定数字 2 和 3 在您的示例输出行中的位置......我假设这是一个错字,您不需要出于某种原因从排序向量中删除这些数字。

无论如何,我会做这样的事情:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

bool mySort(int i, int j) 
{ 
    return (i < j); 
}

void printVector(vector<int> &v)
{
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << " ";

    cout << endl;
}

void getNumbers(string &strNums)
{
    ifstream file("input.txt");
    string line;

    if (file.is_open())
    {
        while (file.good())
        {
            getline(file, line);
            strNums += line + " ";
        }

        file.close();
    }
}

void sortAndPrintNumbers(char *input)
{
    vector<int> vNums, vNumsSorted;
    char * strNum = strtok(input, " ");

    if (strNum)
        vNums.push_back(atoi(strNum));

    while (strNum)
    {
        strNum = strtok(NULL, " ");

        if (strNum)
            vNums.push_back(atoi(strNum));
    }

    // Get first 10 numbers, print them, then sort.
    for (int i = 0; i < 10 && i < vNums.size(); i++)
        vNumsSorted.push_back(vNums[i]);

    printVector(vNumsSorted);
    sort(vNumsSorted.begin(), vNumsSorted.end(), mySort);

    for (int i = 10; i < vNums.size(); i++)
    {
        vNumsSorted.push_back(vNums[i]);
        sort(vNumsSorted.begin(), vNumsSorted.end(), mySort);
        printVector(vNumsSorted);   
    }

    cout << endl;
}

int main()
{
    string strNums;

    getNumbers(strNums);

    char * pNums = new char[strNums.size() + 1];
    strcpy(pNums, strNums.c_str());

    sortAndPrintNumbers(pNums);
    delete [] pNums;
}

字符数组和字符串之间的交换有点混乱,但我不确定你是想使用 Boost 还是只是想看看 strtok 解决方案。此外,您可以指定仅要排序的矢量项的范围,因此可以使用更优雅的解决方案。

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 2013-04-18
    • 2015-07-20
    • 1970-01-01
    • 2016-08-25
    • 2020-06-26
    • 1970-01-01
    相关资源
    最近更新 更多