디버깅에 필요한 정보를 저장하는 Trace 클래스를 만들어보자. Trace 클래스를 활용하는 다음 프로그램과 결과를 참고하여 Trace 클래스를 작성하고 전체 프로그램을 완성하라. 디버깅 정보는 100개로 제한한다.
#include // 08.h
#include
using namespace std;
class Trace { // 08.h
static string tag[100]; // tag 배열
static string inform[100]; // inform 배열
static int count;
public:
static void put(string t, string i); // put
static void print(); // 함수 중복 생성
static void print(string s);
};
void f();
#include // 08.cpp
#include
#include"08.h"
using namespace std;
int Trace::count = 0; // static 변수 초기화
string Trace::tag[100] = {};
string Trace::inform[100] = {};
void Trace::put(string t, string i){ // 입력
tag[count] = t;
inform[count] = i;
count++; // count 증가
}
void Trace::print(){ // 모두 출력
cout << "----모든 Trace 정보를 출력합니다.----" << endl;
for (int i = 0; i < count; i++)
cout << tag[i] << ":" << inform[i] << endl;
}
void Trace::print(string s){ // s 태그 출력
cout << "----" << s << "태그의 Trace 정보를 출력합니다.----" << endl;
for (int i = 0; i < count; i++)
if (s == tag[i]) // s 태그와 현재 태그가 같다면 출력
cout << tag[i] << ":" << inform[i] << endl;
}
void f() {
int a, b, c;
cout << "두 개의 정수를 입력하세요 >>";
cin >> a >> b;
Trace::put("f()", "정수를 입력받았음"); // 디버깅 정보 저장
c = a + b;
Trace::put("f()", "합 계산"); // 디버깅 정보 저장
cout << "합은" << c << endl;
}
#include // 08_main.cpp
#include
#include"08.h"
using namespace std;
int main() {
Trace::put("main()", "프로그램 시작합니다"); // 디버깅 정보 저장, tag, inform 순
f();
Trace::put("main()", "종료"); // put()의 첫번째 매개변수는
// 태그이고 두번째는 디버깅정보이다
Trace::print("f()"); //f()태그를 가진 디버깅 정보를 모두 출력한다
Trace::print(); // 모든 디버깅 정보 출력
}