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

Program that shows the two previous numbers in a txt file

本文关键字:两个 数字 程序 文件 txt 显示      更新时间:2023-10-16

>我有一个程序,它从用户那里获取一个数字,在文本文件中找到它并在下一行显示数字。我希望该程序也显示前两行中的两个数字。

例子

如果数字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 = '';
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;
}
}
}

有许多可能的解决方案。我将向您展示 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 filen";
}
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 valuen";
}
else {
std::cout << "No previous valuesn";
}
}
}
}
} 
else {
std::cerr << "n*** Error: Could not open source filen";
}
return 0;
}