在 txt 文件中显示前两个数字的程序
Program that shows the two previous numbers in a txt file
>我有一个程序,它从用户那里获取一个数字,在文本文件中找到它并在下一行显示数字。我希望该程序也显示前两行中的两个数字。
例子
如果数字
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;
}
相关文章:
- C++通过数字比较两个数字
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 在 txt 文件中显示前两个数字的程序
- 形成一个斐波那契三角形,使得每个数字是上面左对角线或右对角线上两个数字的总和
- 两个数字的对称配对功能
- 查找中间两个数字的正则表达式的匹配项
- 使用C++具有两个数字的最短路径算法.(C++)
- 将两个数字添加为链表
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 我应该如何使用remove_if删除两个数字范围内的元素
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- 子数组中两个数字的相同出现(连续)
- 如何使用位操作在单个整数中编码和解码两个数字
- 在 c++ 程序中调试链表以添加两个数字: 有人可以解释为什么 l3=l3->next;导致错误?
- 钻头以两个数字替换位
- C 如何在小数系统中添加两个数字
- 为什么我的代码不给最后两个数字
- 如何在给出前两个数字的级数中找到大于 x 的第 n 个最小子数组总和?
- 输入两个数字后程序关闭
- 代码是否交换数组的两个数字