1. 프로그램 개요 |
엑셀 입/출력 라이브러리를 이용하여 학생의 정보를 엑셀에 저장하고 점수를 합산하여 보여주는 프로그램
2. 소스코드 |
#include <iostream>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include<fstream>
#include <regex>
#include "ExcelFormat.h"
using namespace ExcelFormat;
using namespace std;
enum Menu { INPUT_MODE = 1, SEARCH_MODE, EXIT};
enum SearchMenu { PRINT_NAME = 1, PRINT_GRADE, PRINT_AVG, PRINT_TEL, PRINT_ALL };
// 학생클래스
class Student {
private:
string name;
int grade;
float subjectA;
float subjectB;
float subjectC;
float subjectD;
string tel;
float avg;
public:
Student(string _name, int _grade, float _subjectA, float _subjectB, float _subjectC, float _subjectD, string _tel, float _avg)
{
name = _name;
grade = _grade;
subjectA = _subjectA;
subjectB = _subjectB;
subjectC = _subjectC;
subjectD = _subjectD;
tel = _tel;
avg = _avg;
}
string getName() { return name; }
int getGrade() { return grade; }
float getSubjectA() { return subjectA; }
float getSubjectB() { return subjectB; }
float getSubjectC() { return subjectC; }
float getSubjectD() { return subjectD; }
string getTel(){ return tel; }
float getAvg() { return avg; }
};
// 학생관리 클래스
class StudentManager {
private:
string systemEnvFilePath = "C:/Temp/System_env.ini";
string studentXlsFilePath;
list<Student> studentList;
public:
StudentManager() {};
// 메뉴출력
void showMenu() {
cout << "=====================\n";
cout << "1. 입력모드\n";
cout << "2. 검색모드\n";
cout << "3. 종료\n";
cout << "=====================\n";
};
// 검색메뉴출력
void showSearchMenu() {
cout << "=====================\n";
cout << "1. 이름 출력\n";
cout << "2. 학년 출력\n";
cout << "3. 평균점수 출력\n";
cout << "4. 전화번호 출력\n";
cout << "5. 전체 출력\n";
cout << "=====================\n";
};
// 메뉴입력
int inputMenu() {
int menuNum = -1;
cout << "입력 >> ";
cin >> menuNum;
return menuNum;
}
// 입력모드
void doInputMode()
{
// 저장필드 선언
string name;
int grade;
float subjectA;
float subjectB;
float subjectC;
float subjectD;
string tel;
float avg;
// 입력
cout << "-----------------------------------(입력)\n";
cout << "이름 : ";
cin >> name;
cout << "학년 : ";
cin >> grade;
cout << "A과목 : ";
cin >> subjectA;
cout << "B과목 : ";
cin >> subjectB;
cout << "C과목 : ";
cin >> subjectC;
cout << "D과목 : ";
cin >> subjectD;
cout << "전화번호 : ";
cin >> tel;
cout << "-----------------------------------(저장)\n";
// 전화번호 치환
//regex e("-");
// 평균계산
avg = (subjectA + subjectB + subjectC + subjectD) / 4;
// 학생객체 생성
Student student = Student(name, grade, subjectA, subjectB, subjectC, subjectD, replaceAll(tel, "-", ""), avg);
// 엑셀에 학생 저장
addStudentToExcel(student);
// 리스트에 학생 저장
studentList.push_back(student);
cout << "입력이 끝났습니다.\n";
}
// 리플레이스 ALL
string replaceAll(const string& str, const string& pattern, const string& replace)
{
string result = str;
string::size_type pos = 0;
string::size_type offset = 0;
while ((pos = result.find(pattern, offset)) != string::npos)
{
result.replace(result.begin() + pos, result.begin() + pos + pattern.size(), replace);
offset = pos + replace.size();
}
return result;
}
// 검색모드
void doSearchMode()
{
while (true)
{
// 메뉴출력
showSearchMenu();
// 사용자로 부터 메뉴 번호 입력받음.
int menuNum = inputMenu();
// 메뉴에 따른 메소드 실행
switch (menuNum)
{
case PRINT_NAME:
printName();
return;
case PRINT_GRADE:
printGrade();
return;
case PRINT_AVG:
printAvg();
return;
case PRINT_TEL:
printTel();
return;
case PRINT_ALL:
printAll();
return;
default:
break;
}
}
}
void printStudent(list<Student> l)
{
list<Student>::iterator it;
cout << "-----------------------------------(출력)\n";
cout << "이름\t학년\tA과목\tB과목\tC과목\tD과목\t전화번호\t평균\n";
for (Student s : l) {
cout << s.getName() << "\t" << s.getGrade() << "\t" << s.getSubjectA() << "\t"
<< s.getSubjectB() << "\t" << s.getSubjectB() << "\t" << s.getSubjectC() << "\t"
<< s.getSubjectD() << "\t" << s.getTel() << "\t" << s.getAvg() << "\n";
}
cout << "-----------------------------------\n";
}
// 이름 출력
void printName()
{
string search;
cout << "이름 입력 >> ";
cin >> search;
list<Student> l;
for (Student s : studentList) {
if (s.getName() == search || s.getName().find(search) != -1)
{
l.push_back(s);
}
}
printStudent(l);
}
// 학년 출력
void printGrade()
{
int search;
cout << "학년 입력 >> ";
cin >> search;
list<Student> l;
for (Student s : studentList) {
if (s.getGrade() == search)
{
l.push_back(s);
}
}
printStudent(l);
}
// 평균 출력
void printAvg()
{
float search;
cout << "평균 입력 >> ";
cin >> search;
list<Student> l;
for (Student s : studentList) {
if (s.getAvg() >= search)
{
l.push_back(s);
}
}
printStudent(l);
}
// 전화번호 출력
void printTel()
{
string search;
cout << "전화번호 입력 >> ";
cin >> search;
list<Student> l;
for (Student s : studentList) {
if (s.getTel().find(search))
{
l.push_back(s);
}
}
printStudent(l);
}
// 전체 출력
void printAll()
{
printStudent(studentList);
}
// 초기화
void initialize() {
// 엑셀 경로 읽기
ifstream in(systemEnvFilePath);
for (int i = 0; i < 1; i++) {
getline(in, studentXlsFilePath);
}
in.close();
// 학생정보 읽기 엑셀로부터
loadStudent();
while (true)
{
// 메뉴출력
showMenu();
// 사용자로 부터 메뉴 번호 입력받음.
int menuNum = inputMenu();
// 메뉴에 따른 메소드 실행
switch (menuNum)
{
case INPUT_MODE:
doInputMode();
break;
case SEARCH_MODE:
doSearchMode();
break;
case EXIT:
exit(0);
break;
default:
break;
}
}
}
// 엑셀에 학생 로우 추가
void addStudentToExcel(Student student)
{
BasicExcel e;
wstring wide_string = wstring(studentXlsFilePath.begin(), studentXlsFilePath.end());
const wchar_t* result = wide_string.c_str();
e.Load(result);
BasicExcelWorksheet* sheet1 = e.GetWorksheet("Sheet1");
if (sheet1)
{
//sheet1->Cell(5, 2)->SetString("Asdf");
int startRow = 1;
int index = startRow + studentList.size();
size_t maxRows = sheet1->GetTotalRows();
list<Student>::iterator it;
sheet1->Cell(index, 0)->SetString(student.getName().c_str());
sheet1->Cell(index, 1)->SetInteger(student.getGrade());
sheet1->Cell(index, 2)->SetDouble(student.getSubjectA());
sheet1->Cell(index, 3)->SetDouble(student.getSubjectB());
sheet1->Cell(index, 4)->SetDouble(student.getSubjectC());
sheet1->Cell(index, 5)->SetDouble(student.getSubjectD());
sheet1->Cell(index, 6)->SetString(student.getTel().c_str());
}
cout << endl;
e.Save();
}
// 엑셀에서 학생정보 읽기
void loadStudent()
{
BasicExcel e;
wstring wide_string = wstring(studentXlsFilePath.begin(), studentXlsFilePath.end());
const wchar_t* result = wide_string.c_str();
e.Load(result);
BasicExcelWorksheet* sheet1 = e.GetWorksheet("Sheet1");
if (sheet1)
{
int startRow = 1;
size_t maxRows = sheet1->GetTotalRows();
for (size_t r = startRow; r < maxRows; ++r)
{
Student student = Student(
sheet1->Cell(r, 0)->GetString(),
sheet1->Cell(r, 1)->GetInteger(),
sheet1->Cell(r, 2)->GetDouble(),
sheet1->Cell(r, 3)->GetDouble(),
sheet1->Cell(r, 4)->GetDouble(),
sheet1->Cell(r, 5)->GetDouble(),
sheet1->Cell(r, 6)->GetString(),
( sheet1->Cell(r, 2)->GetDouble() +
sheet1->Cell(r, 3)->GetDouble() +
sheet1->Cell(r, 4)->GetDouble() +
sheet1->Cell(r, 5)->GetDouble()
) / 4
);
studentList.push_back(student);
}
}
cout << endl;
}
};
int main()
{
StudentManager studentManager = StudentManager();
studentManager.initialize();
return 0;
}
3. 실행화면 |