//----------------------------------------------------------------------------------------------------
/*
Programa: Studentu saraso apdorojimas is failo pagal pavarde, varda, grupes sifra,
pazymius, isvedimas i ekrana ivairiais budais, bei studentu trinimas
is saraso. Programa su komandiniu interfeisu.
Grupe: PKV07.
Atliko: Andrius Kuznecovas.
*/
//----------------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
//----------------------------------------------------------------------------------------------------
string ReadWord(int line, int word, string text, string sign, bool only_line);
string ReadFile();
string LineDelete(int nr, string info);
void Menu(string info);
void Line(int nr);
void Write(string info);
void Intro();
void View();
void Delete(string info);
int LineCount(string info);
int Count(int student, int mark, string info);
//----------------------------------------------------------------------------------------------------
int main()
{
Intro();
string info = ReadFile();
Menu(info);
system("pause>nul");
return 0;
}
//----------------------------------------------------------------------------------------------------
int Count(int student, int mark, string info)
{
int howmany=0;
for(int i=4;i<10;i++)
if(atoi((ReadWord(student,i,info,":",false)).c_str()) == mark)
howmany++;
return howmany;
}
//----------------------------------------------------------------------------------------------------
int LineCount(string info)
{
int line = 0;
for (int i = 0; i < info.length(); i++)
if (info[i] == '$')
++line;
return line;
}
//----------------------------------------------------------------------------------------------------
void View()
{
system("CLS");
int type;
string info = ReadFile();
Line(2);
cout << "# STUDENTU SARASAI.\n";
Line(2);
cout << "# Iveskite saraso tipo numeri:\n";
cout << "# 1. Nuoseklus sarasas.\n";
cout << "# 2. Sarasas korteles forma.\n";
Line(2);
cout << "# ";
cin >> type;
system("CLS");
if(type == 1)
{
Line(2);
cout << "---------------- STUDENTU SARASAS -----------------\n";
if(info!="")
{
int lines = LineCount(info)+1;
if(lines!=1)
{
Line(2);
cout << "# NR : PAVARDE : VARDAS : GRUPE : 10 : 9 : 8\n";
Line(2);
for(int i=1;i<lines;i++)
{
cout << "# " << i <<" : ";
cout << ReadWord(i,1,info,":",false) << " : ";
cout << ReadWord(i,2,info,":",false) << " : ";
cout << ReadWord(i,3,info,":",false) << " : ";
cout << Count(i,10,info) << " : ";
cout << Count(i,9,info) << " : ";
cout << Count(i,8,info) << "\n";
}
Line(2);
}
else
cout << "! STUDENTU SARASE NERA.\n";
}
else
{
Line(2);
cout << "! FAILAS TUSCIAS ARBA FAILO NERA.\n";
}
}
if(type == 2)
{
Line(2);
cout << "---------------- STUDENTU SARASAS -----------------\n";
if(info!="")
{
int lines = LineCount(info)+1;
if(lines!=1)
{
for(int i=1;i<lines;i++)
{
Line(2);
cout << "! STUDENTAS NR: " << i << ".\n";
cout << "# PAVARDE: " << ReadWord(i,1,info,":",false) << ".\n";
cout << "# VARDAS: " << ReadWord(i,2,info,":",false) << ".\n";
cout << "# GRUPE: " << ReadWord(i,3,info,":",false) << ".\n";
cout << "# PAZYMIAI: \n";
cout << "# - Desimtuku: " << Count(i,10,info) << ".\n";
cout << "# - Devintuku: " << Count(i,9,info) << ".\n";
cout << "# - Astuntuku: " << Count(i,8,info) << ".\n";
}
Line(2);
}
else
cout << "! STUDENTU SARASE NERA.\n";
}
else
{
Line(2);
cout << "! FAILAS TUSCIAS ARBA FAILO NERA.\n";
}
}
cin.get();
}
//----------------------------------------------------------------------------------------------------
void Delete(string info)
{
system("CLS");
int nr;
Line(2);
cout << "# TRINAMAS STUDENTAS.\n";
Line(2);
cout<< "# Iveskite studento nr:\n# ";
cin >> nr;
if((nr>0) && (nr<LineCount(info)+1))
{
info = LineDelete(nr-1,info);
Write(info);
system("CLS");
Line(2);
cout << "! STUDENTAS NR: " << nr << " PASALINTAS\n";
}
else
{
Line(2);
cout << "! TOKIOS STUDENTO SARASE NERA.\n";
}
cin.get();
}
//----------------------------------------------------------------------------------------------------
string LineDelete(int nr, string info)
{
int current = 0;
string buf = "";
if (nr != 0)
{
for (int i = 0; i < info.length(); i++)
{
if (info[i] == '$')
current++;
if (current == nr)
{
while (info[++i] != '$')
current++;
}
if (i <= info.length())
buf += info[i];
}
}
else
{
int line = 0;
while (info[line++] != '$')
continue;
for (int i = line; i < info.length(); i++)
buf += info[i];
}
return buf;
}
//----------------------------------------------------------------------------------------------------
void Write(string info)
{
ofstream file ("duomenys.txt");
file << "";
file << info;
file.close();
}
//----------------------------------------------------------------------------------------------------
void Intro()
{
Line(1);
cout << "############## STUDENTU DUOMENU BAZE ##############\n";
Line(1);
cout << "############## SUKURE: Andrius Kuznecovas #########\n";
cout << "############## GRUPE : PKV07 ######################\n";
Line(1);
}
//----------------------------------------------------------------------------------------------------
void Line(int nr)
{
if(nr==1)
cout << "###################################################\n";
if(nr==2)
cout << "---------------------------------------------------\n";
}
//----------------------------------------------------------------------------------------------------
void Menu(string info)
{
string command;
while(command != "iseiti")
{
command = "";
Line(2);
cout << "# KOMANDOS: ziureti, istrinti, iseiti. #\n";
Line(2);
cout << "# ";
getline(cin,command);
if(command == "istrinti")
Delete(info);
if(command == "ziureti")
View();
}
system("CLS");
Line(2);
cout << "# PROGRAMOS DARBO PABAIGA. VISO GERO.\n";
Line(2);
}
//----------------------------------------------------------------------------------------------------
string ReadFile()
{
string info;
ifstream file ("duomenys.txt");
if(file.is_open())
while (! file.eof() )
{
string buf;
getline(file,buf);
info+=buf;
}
else
return "";
file.close();
return info;
}
//----------------------------------------------------------------------------------------------------
string ReadWord(int line, int word, string text, string sign, bool only_line)
{
string buf = "";
string text_line = "";
int line_nr = 0;
int sign_nr = 1;
bool end = false;
--line;
if (word > 0)
{
for (int i = 0; i < text.length(); i++)
{
if (line != 0)
{
if (text[i] == '$')
line_nr++;
else
continue;
if (line_nr == line)
{
++i;
while ((i != text.length()) && (text[i] != '$'))
text_line += text[i++];
if (only_line != false)
return text_line;
break;
}
}
else
{
while ((i != text.length()) && (text[i] != '$'))
text_line += text[i++];
if (only_line != false)
return text_line;
break;
}
}
for (int i = 0; i < text_line.length(); i++)
{
if (word == 1)
{
while ((i != text_line.length()) && (text_line[i] != sign[0]) && (text[i] != '$'))
buf += text_line[i++];
return buf;
}
else
{
for (int j = 0; j < text_line.length(); j++)
{
while (sign_nr != word)
if (text_line[j++] == sign[0])
sign_nr++;
while ((j != text_line.length()) && (text_line[j] != '$') && (text_line[j] != sign[0]))
buf += text_line[j++];
end = true;
if (end == true)
break;
}
}
if (end == true)
break;
}
}
else
return NULL;
if (buf != "")
return buf;
else
return NULL;
}
//----------------------------------------------------------------------------------------------------