@Helig

Как найти совпадения в файле спомощью C++?

Необходимо найти совпадения в текстовом файле и вывести их. Параметры float id, float a, float b
Параметры a должны совпадать, а параметры b не должны различаться более чем на 0.5 и id элементов совпадений нужно вывести.
У меня это получилось сделать, но я не знаю как избавиться от повторений и избавиться от лишнего кода, помогите пожалуйста.

#include <iostream> 
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main() {
    setlocale(LC_ALL, "");
    fstream file, file1;
    file.open("text.txt", ios::in); 
    float id, id1, a, b, a1, b1;
    stringstream x1;
    stringstream x;
    string line;
    string line1;

    while(getline(file, line1)){
    	for (size_t j = 0; j < line1.length(); j++) isdigit(line1[j]) || line1[j]=='.' ? x1<<line1[j] : x1<<' ';
	    x1>>id;x1>>a;x1>>b;
		file1.open("text.txt", ios::in); 
		while(getline(file1, line)){
	    	for (size_t j = 0; j < line.length(); j++) isdigit(line[j]) || line[j]=='.' ? x<<line[j] : x<<' ';
		    x>>id1;x>>a1;x>>b1;
			if(a == a1 && id != id1 && ((b - b1) <= 0.5 || (b - b1) >= -0.5)){
				cout<<"Совпадение! " << id << " " << id1 << endl;
			}
    	}
    	file1.close();
    }	
    file.close();
}
  • Вопрос задан
  • 465 просмотров
Решения вопроса 1
@Helig Автор вопроса
Я задачу решил так, вдруг кому пригодится.
Для того, чтобы не было повторений, я записываю в файл out совпадения и после сравниваю.

include <iostream> 
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

bool check_out(float id, float id1){
	fstream file;
	file.open("text.out", ios::in); 
    float a1, b1;
    stringstream x;
    string line;
    bool flag = false;
    while(getline(file, line)){
    	for (size_t j = 0; j < line.length(); j++) isdigit(line[j]) || line[j]=='.' ? x<<line[j] : x<<' ';
	    x>>a1;x>>b1;
		if (b1 == id && a1 == id1){
			flag = true;
		}
	}
	if(flag){
		return false;
	}else{
		return true;
	}
}

void check_file(float id, float a, float b){
	fstream file;
	file.open("text.txt", ios::in); 
    float id1, a1, b1;
    stringstream x;
    string line;
    while(getline(file, line)){
    	for (size_t j = 0; j < line.length(); j++) isdigit(line[j]) || line[j]=='.' ? x<<line[j] : x<<' ';
	    x>>id1;x>>a1;x>>b1;
		//cout << "Найдено! " << "id1: "<< id1 << " a1: "<< a1<<" b1: "<<b1<< endl;
		if (a == a1){
			if(id != id1){
				if((b - b1) <= 0.5 && (b - b1) >= -0.5){
					if (check_out(id, id1)){
					cout << "Совпадение: "<< id << " " << id1<<endl;
					ofstream out("text.out", ios::app);
					if (out.is_open()){
						out << id << " " << id1 << " "<<endl; 
					}
					out.close();
				}
				}
			}
		}
    }
    file.close();
}
int main() {
    setlocale(LC_ALL, "");
    fstream clear_file("text.out", ios::out);//Очищаем файл
	clear_file.close();
    cout << "Программа выполняет поиск совпадений по количеству вещей в багаже и его весу.\n"; 
    fstream file;
    file.open("text.txt", ios::in); 
    float id, id1, a, b, a1, b1;
    stringstream x;
    string line;
    while(getline(file, line)){//пробел после последнего числа обязателен
    	for (size_t j = 0; j < line.length(); j++) isdigit(line[j]) || line[j]=='.' ? x<<line[j] : x<<' ';
	    x>>id;x>>a;x>>b;
		check_file(id, a, b);
    }	
    file.close();
    return 0;
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы