Sie sind auf Seite 1von 9

// 21104123-9F9C5F3F8B4C6D1D7A9A4E9D

//============================================================================
// Name : lib.cpp
// Author : Trinh Van Giang
// Version : 1.0.1
// Description : Initial code for Assignment 2
// Created on : Mar 16, 2014
// Copyright (C) 2014, HCMUT
//============================================================================
#include "predefine.h"
using namespace std;
ACCOUNT tt[1500];
DOCBORROW borrow[5000];
DOCUMENT report[10000];
struct quahan
{
char date[3];
char month[3];
char year[5];
} qh[5000];
int x1;
int i = 0;
int t = 0;
int q=0;
void read_input(char* filename);
void read_inputDC(char* filename);
void read_inputBR(char* filename);
void write_outputID(char* filename);
void write_outputDC(char* filename);
void write_outputBR(char* filename);
void reportCV(char* filename, int a);
void tra(char* filename);
void write_output1(char* filename, char* year) ;
void phan1()
{
//int dem = 0;
int id ;
bool p1 = false;
do{
cin >> x1;
switch (x1)
{
case 0:
p1 = true;
break;
case 1:
id = 0;
cin.ignore();
cin.getline (tt[i].IDAccount,256);
cin.getline(tt[i].fullName,256);
cin >> tt[i].type;
for(int j = 0; j < i; j++)
if (strcmp(tt[j].IDAccount,tt[i].IDAccount) == 0
)
{
i--;
break;
}
else id++;
if (id == i)
for (int j = 0; j < i; j++)
if (strcmp(tt[j].IDAccount,"x") == 0)
{
strcpy_s (tt[j].IDAccount,tt[i].IDAccoun
t);
strcpy_s(tt[j].fullName,tt[i].fullName);
tt[j].type = tt[i].type;
i--;
break;
}
i++;
break;
case 2:
char x[256];
cin.ignore();
cin.getline(x,256);
for (int j = 0; j < i; j++)
if (strcmp(tt[j].IDAccount,x) == 0)
{
strcpy_s(tt[j].IDAccount,"x");
strcpy_s(tt[j].fullName, "x");
cout << tt[j].IDAccount;
}
/*else
continue;
if (dem == i)
cout << "error" << endl;*/
break;
case 3:
reportCV("report.csv", 1);
break;
case 4:
reportCV("report.csv", 2);
break;
default:
p1=true;
break;
}
}while (!p1);
}
void phan2()
{ int de = 0;
//int count = 0;
bool p2 = false;
do {
cin>>x1;
switch (x1)
{
case 1:
cin.ignore();
cin.getline(report[q].IDDoc,256);
cin.getline(report[q].ISBN,256);
cin.getline(report[q].title,256);
cin.getline(report[q].author,256);
cin.getline(report[q].year,5);
for (int j = 0; j < q; j++)
if (strcmp(report[j].IDDoc,repor
t[q].IDDoc) == 0)
{
q--;
break;
}
else de++;
if (de == q)
for (int j = 0; j < q; j++)
if (strcmp(report[j].IDDoc,"x")
== 0)
{
strcpy_s(report[j].IDDoc
,report[q].IDDoc);
strcpy_s(report[j].ISBN,
report[q].ISBN);
strcpy_s(report[j].title
,report[q].title);
strcpy_s(report[j].autho
r,report[q].author);
strcpy_s(report[j].year,
report[q].year);
q--;
break;
}
q++;
break;
case 2:
char d[256];
cin.ignore();
cin.getline(d,256);
for ( int j=0; j<q ; j++)
if (strcmp(report[j].IDDoc,d) ==
0)
{
strcpy_s(report[j].IDDoc
, "x");
strcpy_s(report[j].ISBN
, "x");
strcpy_s(report[j].title
, "x");
strcpy_s(report[j].autho
r , "x");
strcpy_s(report[j].year
, "x");
}
/*else
continue;
/*if (count == q)
cout<<"Error"<<endl;*/
break;
case 3:
char year1[5];
cin.ignore();
cin.getline(year1,5);
write_output1("report.csv", year1);
break;
case 0:
p2 = true;
break;
default:
p2=true;
break;
}
}while (!p2);
}
void phan3()
{
int j=0;
bool p3 = false;
do{
cin>>x1;
switch(x1)
{
case 0:
p3 = true;
break;
case 1:
{
//int dem = 0;
cin.ignore();
cin.getline(borrow[t].IDAccount,256);
for (j = 0; j < i; j++)
if (strcmp(borrow[t].IDAccount,tt[j].IDAccount)
== 0)
{
cin.getline(borrow[t].IDDoc,256)
;
cin.getline(borrow[t].date,256);
}
else
continue;
/*if (dem == i)
cout << "error"<<endl;*/
t++;
break;
}
case 2:
{
//int dem = 0;
char tra[256];
char sachtra[256];
cin.ignore();
cin.getline(tra,256);
cin.getline(sachtra,256);
for (j = 0; j < t; j++)
if (strcmp(borrow[j].IDAccount,tra) == 0 && strc
mp(borrow[j].IDDoc,sachtra)== 0)
{
strcpy_s(borrow[j].IDAccount,"x");
strcpy_s(borrow[j].IDDoc, "x");
strcpy_s(borrow[j].date,"x");
}
else
continue;
/*if (dem == t)
cout << "error" << endl;*/
break;
}
case 3:
tra("report.csv");
break;
case 4:
char date[3];
char month[3];
char year[5];
cout<<"nhap ngay hien tai";
cin.ignore();
cin.getline(date,3,'/');
cin.getline(month,3,'/');
cin.getline(year,5);
for (int i=0;i<t;i++)
{
}
//case 5:
default:
p3=true;
break;
}
}while (!p3);
}
int main()
{
bool found = false;
read_input("Account.csv");
read_inputDC("Documents.csv");
read_inputBR("Borrow.csv");
ofstream("report.csv");
ofstream("Borrow_H.csv");
// redirect cin to input file
std::ifstream in("key.txt");
std::streambuf *cinbuf = std::cin.rdbuf(); // save old buf
std::cin.rdbuf(in.rdbuf()); // redirect std::cin to key.txt!
do
{
cin >> x1;
switch (x1)
{
case 0:
write_outputID("Account.csv");
write_outputDC("Documents.csv");
write_outputBR("Borrow.csv");
found=true;
break;
case 1:
phan1();
break;
case 2:
phan2();
break;
case 3:
phan3();
break;
default:
found=true;
break;
}


}while (!found);
std::cin.rdbuf(cinbuf); // reset to standard input again
in.close();
system("pause");
return 0;
}
void write_outputID(char* filename)
{
// open file for output
ofstream fout(filename);
// write score
for (int j = 0; j < i; j++)
if (strcmp(tt[j].IDAccount,"x") != 0)
fout << tt[j].IDAccount<< ","<<tt[j].fullName<<","<<tt[j
].type<<endl;
// close file
fout.close();
}
void write_outputDC(char* filename)
{
// open file for output
ofstream fout(filename);
// write score
for (int j = 0; j < q; j++)
if (strcmp(report[j].IDDoc,"x") != 0)
fout << report[j].IDDoc<< ","<< report[j].ISBN<<","<<rep
ort[j].title<<","<<report[j].author<<","<<report[j].year<<endl;
// close file
fout.close();
}
void write_outputBR(char* filename)
{
// open file for output
ofstream fout(filename);
// write score
for (int j = 0; j < t; j++)
if (strcmp(borrow[j].IDAccount,"x") != 0)
{
fout << borrow[j].IDAccount<< ","<< borrow[j].IDDoc<<","
<<borrow[j].date<<endl;
}
// close file
fout.close();
}
void reportCV(char* filename, int a)
{
// open file for output
ofstream fout(filename);
// write score
for (int j = 0; j < i; j++)
if (strcmp(tt[j].IDAccount,"x") != 0 && tt[j].type == a)
{
fout << tt[j].IDAccount<<","<< tt[j].fullName<<","<< tt[
j].type<<endl;
}
// close file
fout.close();
}

void write_output1(char* filename, char* year)
{
// open file for output
ofstream fout(filename);
// write report
for (int j = 0; j < q; j++)
if(strcmp(report[j].year,year) == 0)
{
fout << report[j].IDDoc << "," << report[j].ISBN<<"," <<
report[j].title<<","<<report[j].author<<endl;
}
// close file
fout.close();
}
void tra(char* filename)
{
ofstream fout(filename);
for (int j = 0; j < t; j++)
if (strcmp(borrow[j].IDDoc,"x") != 0)
{
fout << borrow[j].IDAccount<<",";
for (int f = 0; f < i; f++)
if (strcmp(borrow[j].IDAccount,tt[f].IDAccount)
== 0)
{
fout << tt[f].fullName << ",";
break;
}
for (int f = 0; f < q; f++)
if (strcmp(borrow[j].IDDoc,report[f].IDDoc) == 0
)
{
fout << report[f].IDDoc<<","<<report[f].
title<<","<<report[f].author<<",";
break;
}
fout << borrow[j].date<<endl;
}
fout.close();
}
void read_input(char* filename) {
char buffer[256];
int type;
// open file to read
ifstream fin(filename);
i=0;
while(!fin.eof())
{
fin.getline(buffer,256,',');
if (buffer[0] == NULL)
break;
strcpy_s(tt[i].IDAccount,buffer);
fin.getline(buffer,256,',');
strcpy_s(tt[i].fullName,buffer);
fin>>type;
fin.ignore(1);
tt[i].type = type;
i++;
}
fin.close();
}
void read_inputDC(char* filename) {
char buffer[256];
char a[256];
char b[256];
char c [256]= {'"'};
// open file to read
ifstream fin(filename);
q=0;
while(!fin.eof())
{
fin.getline(buffer,256,',');
if (buffer[0] == NULL)
break;
strcpy_s(report[q].IDDoc,buffer);
fin.getline(buffer,256,',');
strcpy_s(report[q].ISBN,buffer);
fin.getline(buffer,256,',');
if (buffer[0] == '"')
{
strcpy_s(a,buffer);
strcat_s(a,",");
fin.getline(buffer,256,'"');
strcpy_s(b,buffer);
strcat_s(a,b);
strcat_s(a,c);
strcpy_s(report[q].title,a);
fin.getline(buffer,256,',');
}
else
{
strcpy_s(report[q].title,buffer);
}
fin.getline(buffer,256,',');
if (buffer[0] == '"')
{
strcpy_s(a,buffer);
strcat_s(a,",");
fin.getline(buffer,256,'"');
strcpy_s(b,buffer);
strcat_s(a,b);
strcat_s(a,c);
strcpy_s(report[q].author,a);
fin.getline(buffer,256,',');
}
else
{
strcpy_s(report[q].author,buffer);
}
fin.getline(buffer,5,'\n');
strcpy_s(report[q].year,buffer);
q++;
}
fin.close();
}
void read_inputBR(char* filename)
{
char buffer[256];
ifstream fin(filename);
t = 0;
while (!fin.eof())
{
fin.getline(buffer,256,',');
if (buffer[0] == NULL)
break;
strcpy_s(borrow[t].IDAccount,buffer);
fin.getline(buffer,256,',');
strcpy_s(borrow[t].IDDoc,buffer);
fin.getline(buffer,3,'/');
strcpy_s(qh[t].date,buffer);
fin.getline(buffer,3,'/');
strcpy_s(qh[t].month,buffer);
fin.getline(buffer,5,'\n');
strcpy_s(qh[t].year,buffer);
strcpy_s(borrow[t].date,strcat(strcat(strcat(strcat(qh[t].date,
"/"),qh[t].month),"/"),qh[t].year));
t++;
}
fin.close();
}

Das könnte Ihnen auch gefallen