Share Pastebin
Guest
Public paste!

Profas

By: a guest | Dec 15th, 2008 | Syntax: C++ | Size: 12.27 KB | Hits: 47 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. //----------------------------------------------------------------------------------------------------
  2. /*
  3. Programa: Studentu saraso apdorojimas is failo pagal pavarde, varda, grupes sifra,
  4.           pazymius, isvedimas i ekrana ivairiais budais, bei studentu trinimas
  5.           is saraso. Programa su komandiniu interfeisu.        
  6. Grupe: PKV07.  
  7. Dalykas: Strukturinis programavimas ir algoritmai.
  8. Destytojas: Lekt. R.Gaivenis.
  9. Atliko: Andrius Kuznecovas.
  10. */
  11. //----------------------------------------------------------------------------------------------------
  12.  
  13. #include <iostream>
  14. #include <fstream>
  15. #include <string.h>
  16. #include <stdio.h>
  17.  
  18. using namespace std;
  19.  
  20. //----------------------------------------------------------------------------------------------------
  21.  
  22. string ReadWord(int line, int word, string text, string sign, bool only_line);
  23. string ReadFile();
  24. string LineDelete(int nr, string info);
  25. void Menu(string info);
  26. void Line(int nr);
  27. void Write(string info);
  28. void WriteStudent();
  29. void Intro();
  30. void View();
  31. void Delete();
  32. int LineCount(string info);
  33. int Count(int student, int mark, string info);
  34.  
  35. //----------------------------------------------------------------------------------------------------
  36.  
  37. struct student_st
  38. {
  39.     string forename;
  40.     string name;
  41.     string group;
  42.     string marks[6];
  43. };
  44.  
  45. //----------------------------------------------------------------------------------------------------
  46. int main()
  47. {
  48.     Intro();
  49.    
  50.     string info = ReadFile();
  51.    
  52.     Menu(info);
  53.    
  54.     system("pause>nul");
  55.     return 0;
  56. }
  57.  
  58. //----------------------------------------------------------------------------------------------------
  59.  
  60. int Count(int student, int mark, string info)
  61. {
  62.     int howmany=0;
  63.    
  64.     for(int i=4;i<10;i++)
  65.         if(atoi((ReadWord(student,i,info,":",false)).c_str()) == mark)
  66.             howmany++;
  67.            
  68.     return howmany;
  69. }
  70.  
  71. //----------------------------------------------------------------------------------------------------
  72.  
  73. int LineCount(string info)
  74. {
  75.     int line = 0;
  76.  
  77.     for (int i = 0; i < info.length(); i++)
  78.         if (info[i] == '$')
  79.             ++line;
  80.  
  81.     return line;
  82. }
  83.  
  84. //----------------------------------------------------------------------------------------------------
  85.  
  86. void View()
  87. {
  88.      system("CLS");
  89.          
  90.      int type;
  91.      string info = ReadFile();
  92.          
  93.      Line(2);
  94.      cout << "# STUDENTU SARASAI.\n";
  95.      Line(2);
  96.      cout << "# Iveskite saraso tipo numeri:\n";
  97.      cout << "# 1. Nuoseklus sarasas.\n";
  98.      cout << "# 2. Sarasas korteles forma.\n";
  99.      Line(2);
  100.      cout << "# ";
  101.      
  102.      cin >> type;
  103.      
  104.      system("CLS");
  105.      
  106.      if(type == 1)
  107.      {
  108.          Line(2);
  109.          cout << "---------------- STUDENTU SARASAS -----------------\n";
  110.          
  111.          if(info!="")
  112.          {
  113.              int lines = LineCount(info)+1;
  114.              
  115.              if(lines!=1)
  116.              {
  117.                  Line(2);
  118.                  cout << "# NR : PAVARDE : VARDAS : GRUPE : 10 : 9 : 8\n";
  119.                  Line(2);
  120.                  
  121.                  for(int i=1;i<lines;i++)
  122.                  {
  123.                      cout << "# " << i <<" : ";
  124.                      cout << ReadWord(i,1,info,":",false) << " : ";
  125.                      cout << ReadWord(i,2,info,":",false) << " : ";
  126.                      cout << ReadWord(i,3,info,":",false) << " : ";
  127.                      cout << Count(i,10,info) << " : ";
  128.                      cout << Count(i,9,info) << " : ";
  129.                      cout << Count(i,8,info) << "\n";    
  130.                  }          
  131.                  
  132.                  Line(2);
  133.              }      
  134.              else
  135.                  cout << "! STUDENTU SARASE NERA.\n";        
  136.          }
  137.          else
  138.          {
  139.              Line(2);
  140.              cout << "! FAILAS TUSCIAS ARBA FAILO NERA.\n";
  141.          }      
  142.      }
  143.      
  144.      if(type == 2)
  145.      {  
  146.          Line(2);
  147.          cout << "---------------- STUDENTU SARASAS -----------------\n";
  148.      
  149.          if(info!="")
  150.          {
  151.              int lines = LineCount(info)+1;
  152.  
  153.              if(lines!=1)
  154.              {
  155.                  for(int i=1;i<lines;i++)
  156.                  {
  157.                      Line(2);
  158.                      cout << "! STUDENTAS NR: " << i << ".\n";
  159.                      cout << "# PAVARDE: " << ReadWord(i,1,info,":",false) << ".\n";
  160.                      cout << "# VARDAS: " << ReadWord(i,2,info,":",false) << ".\n";
  161.                      cout << "# GRUPE: " << ReadWord(i,3,info,":",false) << ".\n";
  162.                      cout << "# PAZYMIAI: \n";
  163.                      cout << "# - Desimtuku: " << Count(i,10,info) << ".\n";
  164.                      cout << "# - Devintuku: " << Count(i,9,info) << ".\n";
  165.                      cout << "# - Astuntuku: " << Count(i,8,info) << ".\n";                                  
  166.                  }
  167.              
  168.                  Line(2);
  169.              }
  170.              else
  171.                  cout << "! STUDENTU SARASE NERA.\n";
  172.              
  173.          }
  174.          else
  175.          {
  176.              Line(2);
  177.              cout << "! FAILAS TUSCIAS ARBA FAILO NERA.\n";
  178.          }
  179.      }
  180.      
  181.      cin.get();
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------------------
  185.  
  186. void Delete()
  187. {
  188.      system("CLS");
  189.      
  190.      int nr;
  191.      string info = ReadFile();
  192.      
  193.      Line(2);
  194.      cout << "# TRINAMAS STUDENTAS.\n";
  195.      Line(2);
  196.      
  197.      cout<< "# Iveskite studento nr:\n# ";
  198.      cin >> nr;
  199.      
  200.      if((nr>0) && (nr<LineCount(info)+1))
  201.      {
  202.          info = LineDelete(nr-1,info);
  203.          Write(info);
  204.          system("CLS");
  205.          Line(2);
  206.          cout << "! STUDENTAS NR: " << nr << " PASALINTAS\n";
  207.      }
  208.      else
  209.      {
  210.          Line(2);
  211.          cout << "! TOKIOS STUDENTO SARASE NERA.\n";
  212.      }
  213.      
  214.      cin.get();
  215. }
  216.  
  217. //----------------------------------------------------------------------------------------------------
  218.  
  219. string LineDelete(int nr, string info)
  220. {
  221.     int current = 0;
  222.     string buf = "";
  223.  
  224.     if (nr != 0)
  225.     {
  226.         for (int i = 0; i < info.length(); i++)
  227.         {
  228.             if (info[i] == '$')
  229.                 current++;
  230.  
  231.             if (current == nr)
  232.             {
  233.                 while (info[++i] != '$')
  234.                     current++;
  235.             }
  236.  
  237.             if (i <= info.length())
  238.                 buf += info[i];
  239.         }
  240.     }
  241.     else
  242.     {
  243.         int line = 0;
  244.  
  245.         while (info[line++] != '$')
  246.             continue;
  247.  
  248.         for (int i = line; i < info.length(); i++)
  249.             buf += info[i];
  250.      }
  251.  
  252.      return buf;
  253. }
  254.  
  255. //----------------------------------------------------------------------------------------------------
  256.  
  257. void Write(string info)
  258. {
  259.      ofstream file ("duomenys.txt");
  260.      
  261.      file << "";
  262.      
  263.      file << info;
  264.      
  265.      file.close();
  266. }
  267.  
  268. //----------------------------------------------------------------------------------------------------
  269.  
  270. void WriteStudent()
  271. {
  272.      system("CLS");
  273.      
  274.      Line(2);
  275.      cout << "# STUDENTO IRASYMAS I DUOMENU BAZE.\n";
  276.      Line(2);
  277.      
  278.      student_st student;
  279.      
  280.      cout<<"# Iveskite studento pavarde:\n# ";
  281.      getline(cin,student.forename);
  282.      cout<<"# Iveskite studento varda:\n# ";
  283.      getline(cin,student.name);
  284.      cout<<"# Iveskite studento grupe:\n# ";
  285.      getline(cin,student.group);
  286.      cout<<"# Iveskite studento pazymius:\n";
  287.      
  288.      for(int i=0;i<6;i++)
  289.      {
  290.          cout << "# " << i+1 << ": ";
  291.          cin >> student.marks[i];
  292.      }
  293.      
  294.      string st_info = student.forename+":"+student.name+":"+student.group+":";
  295.  
  296.      for(int i=0;i<6;i++)
  297.          if(i!=5)
  298.          {
  299.              st_info+=student.marks[i]+":";
  300.          }
  301.          else
  302.              st_info+=student.marks[i]+"$";
  303.              
  304.      string info = ReadFile();
  305.      
  306.      info+=st_info;
  307.      
  308.      Write(info);
  309.      
  310.      system("CLS");
  311.  
  312.      Line(2);
  313.      cout << "# STUDENTAS IRASYTAS SEKMINGAI.\n";
  314.      Line(2);
  315.      
  316.      cin.get();
  317. }
  318.  
  319. //----------------------------------------------------------------------------------------------------
  320.  
  321. void Intro()
  322. {
  323.      Line(1);
  324.      cout << "############## STUDENTU DUOMENU BAZE ##############\n";
  325.      Line(1);
  326.      cout << "############## SUKURE: Andrius Kuznecovas #########\n";
  327.      cout << "############## GRUPE : PKV07 ######################\n";
  328.      Line(1);
  329. }
  330.  
  331. //----------------------------------------------------------------------------------------------------
  332.  
  333. void Line(int nr)
  334. {
  335.      if(nr==1)
  336.          cout << "###################################################\n";
  337.      if(nr==2)
  338.          cout << "---------------------------------------------------\n";
  339. }
  340.  
  341. //----------------------------------------------------------------------------------------------------
  342.  
  343. void Menu(string info)
  344. {
  345.      string command;
  346.      
  347.      while(command != "iseiti")
  348.      {
  349.           command = "";
  350.          
  351.           Line(2);
  352.           cout << "# KOMANDOS: irasyti, ziureti, istrinti, iseiti.   #\n";
  353.           Line(2);
  354.           cout << "# ";
  355.          
  356.           getline(cin,command);
  357.          
  358.           if(command == "irasyti")
  359.               WriteStudent();
  360.  
  361.           if(command == "istrinti")
  362.               Delete();
  363.              
  364.           if(command == "ziureti")
  365.               View();
  366.      }
  367.      
  368.      system("CLS");
  369.      Line(2);
  370.      cout << "# PROGRAMOS DARBO PABAIGA. VISO GERO.\n";
  371.      Line(2);
  372. }
  373.  
  374. //----------------------------------------------------------------------------------------------------
  375.  
  376. string ReadFile()
  377. {
  378.     string info;
  379.     ifstream file ("duomenys.txt");
  380.    
  381.     if(file.is_open())
  382.         while (! file.eof() )
  383.         {
  384.              string buf;
  385.              getline(file,buf);
  386.              info+=buf;
  387.         }
  388.     else
  389.         return "";
  390.    
  391.     file.close();
  392.    
  393.     return info;
  394. }
  395.  
  396. //----------------------------------------------------------------------------------------------------
  397.  
  398. string ReadWord(int line, int word, string text, string sign, bool only_line)
  399. {
  400.     string buf = "";
  401.     string text_line = "";
  402.     int line_nr = 0;
  403.     int sign_nr = 1;
  404.     bool end = false;
  405.  
  406.     --line;
  407.     if (word > 0)
  408.     {
  409.         for (int i = 0; i < text.length(); i++)
  410.         {
  411.             if (line != 0)
  412.             {
  413.                 if (text[i] == '$')
  414.                     line_nr++;
  415.                 else
  416.                     continue;
  417.  
  418.                 if (line_nr == line)
  419.                 {
  420.                     ++i;
  421.                     while ((i != text.length()) && (text[i] != '$'))
  422.                          text_line += text[i++];
  423.  
  424.                     if (only_line != false)
  425.                         return text_line;
  426.  
  427.                         break;
  428.                 }
  429.             }    
  430.             else
  431.             {
  432.                 while ((i != text.length()) && (text[i] != '$'))
  433.                     text_line += text[i++];
  434.  
  435.                     if (only_line != false)
  436.                         return text_line;
  437.  
  438.                     break;
  439.             }
  440.         }
  441.  
  442.         for (int i = 0; i < text_line.length(); i++)
  443.         {
  444.             if (word == 1)
  445.             {
  446.                 while ((i != text_line.length()) && (text_line[i] != sign[0]) && (text[i] != '$'))
  447.                     buf += text_line[i++];
  448.  
  449.                     return buf;
  450.             }
  451.             else
  452.             {
  453.                 for (int j = 0; j < text_line.length(); j++)
  454.                 {
  455.                     while (sign_nr != word)
  456.                         if (text_line[j++] == sign[0])
  457.                             sign_nr++;
  458.  
  459.                     while ((j != text_line.length()) && (text_line[j] != '$') && (text_line[j] != sign[0]))
  460.                         buf += text_line[j++];
  461.  
  462.                     end = true;
  463.  
  464.                     if (end == true)
  465.                         break;
  466.                 }
  467.             }
  468.             if (end == true)
  469.                 break;
  470.        }
  471.     }
  472.     else
  473.         return NULL;
  474.  
  475.     if (buf != "")
  476.         return buf;
  477.     else
  478.         return NULL;
  479. }
  480.  
  481. //----------------------------------------------------------------------------------------------------