Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string.h>
- #include <fstream>
- using namespace std;
- typedef struct stud{
- char *first_name, *last_name;
- int year, month, day;
- double av_grade;
- struct stud *next;
- };
- void push(stud **head, char *first_name, char *last_name, int year, int month, int day, double av_grade) {
- stud *tmp = (stud*) malloc(sizeof(stud));
- tmp->first_name = (char*) malloc(strlen(first_name));
- strcpy(tmp -> first_name, first_name);
- tmp->last_name = (char*) malloc(strlen(last_name));
- strcpy(tmp -> last_name, last_name);
- tmp->year = year;
- tmp->month = month;
- tmp->day = day;
- tmp->av_grade = av_grade;
- tmp->next = *head;
- *head = tmp;
- }
- void pop(stud **head) {
- stud* prev = NULL;
- if (head == NULL){
- cout << "Node Error" << endl;
- exit(1);
- }
- prev = *head;
- *head = (*head) -> next;
- free(prev);
- }
- void write_f (stud *head){
- const int size = 10;
- cout << "Enter file name" << endl;
- char filename[50];
- cin.getline(filename,50);
- fstream in;
- in.open(filename);
- if (in.is_open()){
- in << head->first_name;
- in << head->last_name;
- in << head->year;
- in << head->month;
- in << head->day;
- in << head->av_grade;
- cout << "Done";
- }
- in.close();
- }
- void pr_b(stud *head) {
- while (head) {
- cout << head->first_name << " " << head->last_name << " ";
- cout << "Date of Birth: " << head->day << "." << head->month << "." << head->year << endl; ;
- cout << "Av_grade: " << head->av_grade << endl;
- head = head->next;
- }
- cout << endl;
- }
- stud* getNth(stud* head, int n) {
- int c = 0;
- while (c < n && head) {
- head = head->next;
- c++;
- }
- return head;
- }
- void deleteNth(stud **head, int n) {
- if (n == 0)
- return pop(head);
- else {
- stud *prev = getNth(*head, n - 1);
- stud *elm = prev -> next;
- prev -> next = elm -> next;
- free(elm);
- }
- }
- int main(){
- stud *head = NULL;
- char first_name[50], last_name[50], name_file[100], buf[10];
- int year, month, day;
- double av_grade;
- int act;
- FILE* source;
- char k[10];
- cout << "Enter way" << endl;
- int n = 0;
- while(n != 3){
- cin >> act;
- switch (act) {
- case 1: {
- cout << "Enter file name:"; //Чтение из файла
- cin >> name_file;
- if ((source = fopen(name_file, "rw")) == NULL)
- cout << "File error!" << endl;
- while (fgets(first_name, 64, source) != NULL) {
- fgets(last_name, 32, source);
- fgets(buf, 10, source);
- year = atoi(buf);
- fgets(buf, 10, source);
- month = atoi(buf);
- fgets(buf, 10, source);
- day = atoi(buf);
- fgets(buf, 10, source);
- av_grade = atof(buf);
- push(&head, first_name, last_name, year, month, day, av_grade);
- }
- fclose(source);
- break;
- }
- case 2: {
- write_f(head);
- break;
- }
- case 3:{
- pr_b(head);
- break;
- }
- case 4: {
- cout << "Surname, first name" << endl;
- cin >> last_name >> first_name;
- cout << "Enter date of birth (DD MM YYYY):" << endl;
- cin >> day >> month >> year;
- cout << "Grade point average:" << endl;
- cin >> av_grade;
- push(&head, first_name, last_name, year, month, day, av_grade);
- break;
- }
- case 5: {
- cout << "Base Plot File:" << endl;
- cin >> name_file;
- source = fopen(name_file, "r");
- while (fgets(first_name, 64, source) != NULL) {
- fgets(last_name, 64, source);
- fgets(buf, 10, source);
- year = atoi(buf);
- fgets(buf, 10, source);
- month = atoi(buf);
- fgets(buf, 10, source);
- day = atoi(buf);
- fgets(buf, 10, source);
- av_grade = atof(buf);
- push(&head, first_name, last_name, year, month, day, av_grade);
- }
- break;
- }
- case 6: {
- int n = 0;
- stud* prev = head;
- cout << "Enter last name:" << endl;
- cin >> last_name;
- sprintf(last_name, "%s%s", last_name, "\n\0");
- while (prev != NULL) {
- if (!strcmp(last_name, prev->last_name)) {
- deleteNth(&head, n);
- break;
- }
- prev = prev->next;
- n++;
- }
- break;
- }
- case 8:
- n = 3;
- return n;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement