在txt文件中显示前两个数字的程序

哈西

我有一个程序可以从用户那里获得一个号码,然后在文本文件中找到它,并在下一行显示该号码。我希望该程序还显示前两行中的两个数字。

例子

如果数字是1.29

输出应为:

next number is: 9 and previous numbers are 6 and 08

next number is: 4.33 and previous numbers are 1 and 1.73

next number is: 7 and previous numbers are 4 and 73

file.txt内容是:

79
08
08
6
1.29
9
87
57
098
1.73
1
1.29
4.33
76
73
4
1.29
7

这是我到目前为止所拥有的:

#include <fstream>
#include <iomanip>
#include <iostream>
#include <term.h>
#include <unistd.h>
using namespace std;
int main() {
  int x = 0;
  int tt = 0;
  int jj = 0;
  float number;
  char t = '\0';
  int temp = 0;
  ifstream inFile;
  inFile.open("file.txt");
  if (!inFile) {
    cout << "Unable to open file";
    exit(1); // terminate with error
  }
  cout << "Please enter a number: ";
  cin >> number;
  {
    string line;
    ifstream myfile("file.txt");
    if (myfile.is_open()) {
      while (getline(myfile, line)) {
        temp = temp + 1;
        string s = line;
        double d;
        d = stof(s.c_str());
        {
        Loop:
          if (d == number) {
            {
              x = x + 1;
            }
            for (int i = 1; i < 2; i++) {
              getline(myfile, line);
              cout << "\n"
                   << "next number is: " << line;
              string h = line;
              double z;
              z = stof(h.c_str());
              if (z <= 1.79) {
                tt = tt + 1;
                cout << " red";
              };
              if (z > 1.79) {
                cout << " blue";
                jj = jj + 1;
                if (z == d) {
                  goto Loop;
                };
              }
            }
            temp = temp + 1;
          }
        }
      }
      cout << "\n";
      cout << "\n"
           << "number repeat: " << x << t << "  red: " << tt << "  blue: " << jj
           << "   Dataset: " << temp << "\n";

      int jk = (tt * 100) / (tt + jj);
      int kl = (jj * 100) / (tt + jj);

      cout << "accur:       " << jk << "%"
           << "       " << kl << "%"
           << "\n";
      return 0;
    }
  }
}
阿明·蒙蒂尼(Armin Montigny)

有许多可能的解决方案。我将向您展示2种不同的实现。

棘手的部分始终是“旧”值和“下一个”值。因此,我们绝不能超出界限。

在第一个解决方案中,我们逐行读取。解决方案是推迟评估。因此,我们首先阅读一些行,然后进行评估。


#include <iostream>
#include <fstream>
#include <string>

int main() {
    // Try to open the file with the source data and check, if it succeeded
    if (std::ifstream file("r:\\file.txt"); file) {

        // Give instructions to the user
        std::cout << "\nWhat are you searching for?\n";

        // Get the search value from the user
        if (std::string toSearch{}; std::cin >> toSearch) {

            // Output values
            std::string previousPrevious{"*"}, previous{"*"}, current{}, next{"*"};

            // Search loop
            bool doSearch{ true };
            while (doSearch) {

                // Take care of old and new values
                previousPrevious = previous;
                previous = current;
                current = next;

                // Read value from file
                if (!std::getline(file, next)) doSearch = false;

                // And check, if we could find the searched value
                if (current == toSearch) {

                    // Show result
                    std::cout << previousPrevious << " " << previous << "   --> " << current << " <--   " << next << "\n";
                }
            }
        }
    }
    else {
        std::cerr << "\n*** Error: Could not open source file\n";
    }
    return 0;
}

第二种解决方法是将整个文件读入向量,然后将索引与向量一起使用。这是许多可能的解决方案中的另一个。

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

int main() {
    // Try to open the file with the source data and check, if it succeeded
    if (std::ifstream file("r:\\file.txt"); file) {

        // Give instructions to the user
        std::cout << "\nWhat are you searching for?\n";

        // Get the search value from the user
        if (std::string toSearch{}; std::cin >> toSearch) {

            // To make our life easier, we read the complete source file into a vector
            std::vector data(std::istream_iterator<std::string>(file), {});

            // Now we iterate through the string and search data
            for (int i = 0; i < data.size(); ++i) {

                // Comparison
                if (toSearch == data[i]) {
                    // Some feedback:
                    std::cout << "Found value '" << toSearch << "' at position: " << i + 1 << "\n";

                    // Check for boundaries and show result
                    if (i < data.size() - 2) std::cout << "Next value is: " << data[i + 1] << ". "; else std::cout << "No next value. ";

                    if (i > 0) {
                        std::cout << "Previous value: " << data[i - 1] << ". ";
                        if (i > 1) std::cout << "Previous previous value: " << data[i - 2] << ". "; else std::cout << "no previous previous value\n";
                    }
                    else {
                        std::cout << "No previous values\n";
                    }
                }
            }
        }
    } 
    else {
        std::cerr << "\n*** Error: Could not open source file\n";
    }
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章