Advertisement
Guest User

SUKAAAAAAAAAAAAAAAAAAAAAA

a guest
Dec 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. typedef struct stud{
  8.    char *first_name, *last_name;
  9.    int year, month, day;
  10.    double av_grade;
  11.    struct stud *next;
  12. };
  13.  
  14. void push(stud **head, char *first_name, char *last_name, int year, int month, int day, double av_grade) {
  15.     stud *tmp = (stud*) malloc(sizeof(stud));
  16.     tmp->first_name = (char*) malloc(strlen(first_name));
  17.     strcpy(tmp -> first_name, first_name);
  18.     tmp->last_name = (char*) malloc(strlen(last_name));
  19.     strcpy(tmp -> last_name, last_name);
  20.     tmp->year = year;
  21.     tmp->month = month;
  22.     tmp->day = day;
  23.     tmp->av_grade = av_grade;
  24.     tmp->next = *head;
  25.     *head = tmp;
  26. }
  27. void pop(stud **head) {
  28.     stud* prev = NULL;
  29.     if (head == NULL){
  30.         cout << "Node Error" << endl;
  31.         exit(1);
  32.     }
  33.     prev = *head;
  34.     *head = (*head) -> next;
  35.     free(prev);
  36. }
  37. void write_f (stud *head){
  38.     const int size = 10;
  39.     cout << "Enter file name" << endl;
  40.     char filename[50];
  41.     cin.getline(filename,50);
  42.     fstream in;
  43.     in.open(filename);
  44.     if (in.is_open()){
  45.         in << head->first_name;
  46.         in << head->last_name;
  47.         in << head->year;
  48.         in << head->month;
  49.         in << head->day;
  50.         in << head->av_grade;
  51.         cout << "Done";
  52.     }
  53.     in.close();
  54.  
  55. }
  56. void pr_b(stud *head) {
  57.     while (head) {
  58.         cout << head->first_name << " " << head->last_name << " ";
  59.         cout << "Date of Birth: " << head->day << "." << head->month << "." << head->year << endl; ;
  60.         cout << "Av_grade: " <<  head->av_grade << endl;
  61.         head = head->next;
  62.     }
  63.     cout << endl;
  64. }
  65. stud* getNth(stud* head, int n) {
  66.     int c = 0;
  67.     while (c < n && head) {
  68.         head = head->next;
  69.         c++;
  70.     }
  71.     return head;
  72. }
  73.  
  74. void deleteNth(stud **head, int n) {
  75.     if (n == 0)
  76.         return pop(head);
  77.     else {
  78.         stud *prev = getNth(*head, n - 1);
  79.         stud *elm = prev -> next;
  80.         prev -> next = elm -> next;
  81.         free(elm);
  82.     }
  83. }
  84.  
  85. int main(){
  86.     stud *head = NULL;
  87.     char  first_name[50], last_name[50], name_file[100], buf[10];
  88.     int year, month, day;
  89.     double av_grade;
  90.     int act;
  91.     FILE* source;
  92.     char k[10];
  93.    cout << "Enter way" << endl;
  94.    int n = 0;
  95.    while(n != 3){
  96.     cin >> act;
  97.      switch (act) {
  98.  
  99.             case 1: {
  100.                 cout << "Enter file name:";                    //Чтение из файла
  101.                 cin >>  name_file;
  102.                 if ((source = fopen(name_file, "rw")) == NULL)
  103.                    cout << "File error!" << endl;
  104.  
  105.                 while (fgets(first_name, 64, source) != NULL) {
  106.                     fgets(last_name, 32, source);
  107.                     fgets(buf, 10, source);
  108.                     year = atoi(buf);
  109.                     fgets(buf, 10, source);
  110.                     month = atoi(buf);
  111.                     fgets(buf, 10, source);
  112.                     day = atoi(buf);
  113.                     fgets(buf, 10, source);
  114.                     av_grade = atof(buf);
  115.  
  116.                     push(&head, first_name, last_name, year, month, day, av_grade);
  117.  
  118.                 }
  119.                 fclose(source);
  120.                 break;
  121.             }
  122.             case 2: {
  123.                 write_f(head);
  124.                 break;
  125.             }
  126.  
  127.             case 3:{
  128.                 pr_b(head);
  129.                 break;
  130.             }
  131.             case 4: {
  132.                 cout << "Surname, first name" << endl;
  133.                 cin >> last_name >> first_name;
  134.                 cout << "Enter date of birth (DD MM YYYY):" << endl;
  135.                 cin >> day >> month >> year;
  136.                 cout << "Grade point average:" << endl;
  137.                 cin >> av_grade;
  138.                 push(&head, first_name, last_name, year, month, day, av_grade);
  139.                 break;
  140.             }
  141.             case 5: {
  142.                 cout << "Base Plot File:" << endl;
  143.                 cin >> name_file;
  144.                 source = fopen(name_file, "r");
  145.  
  146.                 while (fgets(first_name, 64, source) != NULL) {
  147.                     fgets(last_name, 64, source);
  148.                     fgets(buf, 10, source);
  149.                     year = atoi(buf);
  150.                     fgets(buf, 10, source);
  151.                     month = atoi(buf);
  152.                     fgets(buf, 10, source);
  153.                     day = atoi(buf);
  154.                     fgets(buf, 10, source);
  155.                     av_grade = atof(buf);
  156.  
  157.                     push(&head, first_name, last_name, year, month, day, av_grade);
  158.                 }
  159.                 break;
  160.             }
  161.  
  162.             case 6: {
  163.                 int n = 0;
  164.                 stud* prev = head;
  165.                 cout << "Enter last name:" << endl;
  166.                 cin >> last_name;
  167.                 sprintf(last_name, "%s%s", last_name, "\n\0");
  168.                 while (prev != NULL) {
  169.                     if (!strcmp(last_name, prev->last_name)) {
  170.                         deleteNth(&head, n);
  171.                         break;
  172.                     }
  173.                     prev = prev->next;
  174.                     n++;
  175.                 }
  176.                 break;
  177.             }
  178.             case 8:
  179.                 n = 3;
  180.                 return n;
  181.             }
  182.         }
  183.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement