데이터 비교 프로그램

카테고리 없음

2019. 4. 15. 23:33

자료구조 과제물을 하다가 어떤 데이터가 틀렸는지 단번에 알아내기 위해 간단한 프로그램을 작성하였다.

 

/*
	compare.cpp
*/

#include <bits/stdc++.h>
using namespace std;

int main(int argc, char *argv[]) {

	if(argc != 3) {
		fprintf(stderr, "usage : exec-file-name comp-file-1-name comp-file-2-name\n");
		return -1;
	}

	int cntGood = 0, cntBad = 0;
	string A, B;
	ifstream inp1(argv[1]), inp2(argv[2]);
	while(inp1 >> A && inp2 >> B)
		if(A != B) {
			cout << A << "!=" << B << endl;
			cntBad++;
		}
		else
			cntGood++;
			
	cout << "comparison over." << endl;
	cout << "match : " << cntGood << endl;
	cout << "unmatch : " << cntBad << endl;
	return 0;
}

다음과 같이 비교할 두 파일의 이름과 함께 프로그램을 실행시키면 손쉽게 비교가 가능하다.

행복하다 ^^

$ g++ compare.cpp -o compare
$ ./compare comp1 comp2
7!=3
comparison over.
match : 3
unmatch : 1

 

참고로 comp1 : 1 2 7 4, comp2 : 1 2 3 4 로 작성하였다.