Petro_zzz

studentmain_3110

Oct 31st, 2022
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <Windows.h>
  4. #include "Student.h"
  5.  
  6. using namespace std;
  7.  
  8. void init_student(Student table[]) {
  9.     ifstream fin;
  10.     fin.open("journal.txt");
  11.     string str;
  12.     int counter = 0;
  13.     if (fin.is_open()) {
  14.         while (!fin.eof()) {
  15.             getline(fin, str);
  16.             if (str.size() > 0) {
  17.                 table[counter].name = str;
  18.                 counter++;
  19.             }
  20.         }
  21.         fin.close();
  22.     }
  23.     cout << counter << " студентов зарегистрировано." << endl;
  24. }
  25.  
  26. void save_sudent(Student table[]) {
  27.     ofstream fout("journal_new.xls");
  28.     if (fout.is_open()) {
  29.         fout << "Ф.И.О.";
  30.         for (int k = 1; k <= 31; ++k) {
  31.             fout << "\t" << k;
  32.         }
  33.         fout << endl;
  34.         for (int n = 0; n < 10; n++) {
  35.             fout << table[n].name;
  36.             for (int k = 0; k < 31; ++k) {
  37.                 fout << "\t" << table[n].mark[k];
  38.             }
  39.             fout << endl;
  40.         }
  41.         fout.close();
  42.         cout << "Студенты сохранены." << endl;
  43.     }  
  44. }
  45.  
  46. void load_sudent(Student table[]) {
  47.     ifstream fin("journal_new.xls");
  48.     if (fin.is_open()) {
  49.         string str;
  50.         int counter = 0;
  51.         while (!fin.eof()) {
  52.             getline(fin, str);         
  53.             if (counter > 0) {
  54.                 table[counter - 1].init(str);
  55.             }
  56.             counter++;
  57.         }
  58.         fin.close();
  59.     }
  60. }
  61.  
  62. Student parse_str(string str) {
  63.     Student st;
  64.     int id_st = 0;
  65.     int id_en = 0;
  66.     string small_str;
  67.     int counter = 0;
  68.     for (int k = 0; k < str.length(); ++k) {
  69.         if (str[k] == '\t') {
  70.             id_en = k;
  71.             small_str = str.substr(id_st, id_en - id_st);
  72.             if (counter == 0) {
  73.                 st.name = small_str;
  74.             }
  75.             else {
  76.                 st.mark[counter - 1] = atoi(small_str.data());
  77.             }
  78.             //cout << small_str << endl;
  79.             id_st = id_en + 1;
  80.             counter++;
  81.         }      
  82.     }
  83.     return st;
  84. }
  85.  
  86. void main() {
  87.     setlocale(LC_ALL, "Ru");
  88.     SetConsoleCP(1251);
  89.     /*
  90.     // это проверка функции, написанной отдельно, для разбора строки в поля структуры
  91.     Student st = parse_str("Иванов И.П. 5   4   0   3   2   1   0   0   0   0   0   0   0   0   0   0   0");
  92.     cout << st.name << endl;
  93.     for (int k = 0; k < 31; k++) {
  94.         cout << st.mark[k] << " ";
  95.     }
  96.     cout << endl;
  97.     */
  98.     Student table[10];
  99.     /*
  100.     // это проверка функции, встроенной в качестве метода в структуру, для разбора строки в поля этой структуры
  101.     table[0].init("Иванов И.П.  2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0");
  102.     cout << table[0].name << endl; 
  103.     cout << table[0].mark[0] << endl;
  104.     */
  105.  
  106.     //init_student(table); // это начальная заготовка для создания файла
  107.     load_sudent(table);
  108.     //table[0].mark[0] = 5; // так поставить оценку
  109.     cout << table[0].mark[0] << endl; // так посмотреть оценку
  110.     save_sudent(table);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment