Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct students {
- int flag, age;
- float GradePointAverage;
- char *surname, *name;
- union {
- char *region, *city, *street;
- int house, flat;
- };
- };
- char* EnterInformation(char* st) {
- int i = 0, sch = 0;
- while (1) {
- st[i++] = (char)getchar();
- if (st[i - 1] == '\n') {
- st[i - 1] = '\0';
- break;
- }
- if (!(st = (char*)realloc(st, i + 2)))
- st = (char*)realloc(st, i + 2);
- }
- i = 0;
- if (st[0] == '\0') {
- printf("Incorrect information,reenter!\n");
- rewind(stdin);
- EnterInformation(st);
- }
- while (st[i] != '\0') {
- if (!(st[i] >= 'a' && st[i] <= 'z') && !(st[i] >= 'A' && st[i] <= 'Z'))
- sch++;
- i++;
- }
- if (sch > 0) {
- printf("Incorrect data,reenter!\n");
- rewind(stdin);
- EnterInformation(st);
- }
- return st;
- }
- students *GetStudents(int &n) {
- int i, j = 0, typeofinformation;
- students *p;
- if (!(p = (struct students*)malloc(sizeof(students)*n)))
- p = (students*)realloc(p, n + 1);
- for (i = 0; i < n; i++) {
- if (!(p[i].surname = (char*)malloc(sizeof(char))))
- p[i].surname = (char*)realloc(p[i].surname, 2);
- if (!(p[i].name = (char*)malloc(sizeof(char))))
- p[i].name = (char*)realloc(p[i].name, 2);
- printf("Enter student surname\n");
- rewind(stdin);
- p[i].surname = EnterInformation(p[i].surname);
- printf("Enter student name\n");
- rewind(stdin);
- p[i].name = EnterInformation(p[i].name);
- printf("Enter age of student\n");
- while (!(scanf_s("%d", &p[i].age)) || p[i].age <= 0 || p[i].age > 113) {
- rewind(stdin);
- printf("Incorrect age,reenter!\n");
- }
- printf("Enter student grade point average\n");
- while (!(scanf_s("%f", &p[i].GradePointAverage)) || p[i].GradePointAverage <= 0 || p[i].GradePointAverage > 10) {
- rewind(stdin);
- printf("Incorrect grade point average,reenter!\n");
- }
- printf("What type of information you prefer to put?\n1.Refion\n2.City\n3.Street\n4.House\n5.Flat\n");
- while (!(scanf_s("%d", &typeofinformation)) || typeofinformation <= 0 || typeofinformation > 5) {
- rewind(stdin);
- printf("Incorrect number,reenter!\n");
- }
- rewind(stdin);
- switch (typeofinformation) {
- case 1: {
- printf("Enter region where the student live\n");
- if (!(p[i].region = (char*)malloc(sizeof(char))))
- p[i].region = (char*)realloc(p[i].region, 2);
- p[i].region = EnterInformation(p[i].name);
- p[i].flag = 1;
- break;
- }
- case 2: {
- printf("Enter city where the student live\n");
- if (!(p[i].city = (char*)malloc(sizeof(char))))
- p[i].city = (char*)realloc(p[i].city, 2);
- p[i].city = EnterInformation(p[i].city);
- p[i].flag = 2;
- break;
- }
- case 3: {
- printf("Enter street where the student live\n");
- if (!(p[i].street = (char*)malloc(sizeof(char))))
- p[i].street = (char*)realloc(p[i].street, 2);
- p[i].street = EnterInformation(p[i].street);
- p[i].flag = 3;
- break;
- }
- case 4: {
- printf("Enter number of house where the student live\n");
- while (!(scanf_s("%d", &p[i].house)) || p[i].house <= 0) {
- rewind(stdin);
- printf("Incorrect number of house,reenter\n");
- }
- p[i].flag = 4;
- break;
- }
- case 5: {
- printf("Enter number of flat where the student live\n");
- while (!(scanf_s("%d", &p[i].flat)) || p[i].flat <= 0) {
- rewind(stdin);
- printf("Incorrect number of flat,reenter\n");
- }
- p[i].flag = 5;
- break;
- }
- }
- }
- return p;
- }
- int GetDefiner(int &definer, char** argv) {
- if (!(strcmp(argv[1], "Street")) || !(strcmp(argv[1], "street"))) {
- return 3;
- }
- if (!(strcmp(argv[1], "Region")) || !(strcmp(argv[1], "region"))) {
- return 1;
- }
- if (!(strcmp(argv[1], "City")) || !(strcmp(argv[1], "city"))) {
- return 2;
- }
- if (!(strcmp(argv[1], "House")) || !(strcmp(argv[1], "house"))) {
- return 4;
- }
- if (!(strcmp(argv[1], "Flat")) || !(strcmp(argv[1], "flat"))) {
- return 5;
- }
- else return 0;
- }
- int SearchInformationAboutStudents(int p, int definer) {
- if (p == definer)
- return 1;
- else return 0;
- }
- void ShowInformationAboutStudents(students p, int definer) {
- if (definer == 1) {
- printf("------------------\n");
- printf("Surname : %s\n", p.surname);
- printf("Name : %s\n", p.name);
- printf("Age : %d\n", p.age);
- printf("Grade point average : %.1f\n", p.GradePointAverage);
- printf("Region : %s\n", p.region);
- printf("------------------\n");
- }
- if (definer == 2) {
- printf("------------------\n");
- printf("Surname : %s\n", p.surname);
- printf("Name : %s\n", p.name);
- printf("Age : %d\n", p.age);
- printf("Grade point average : %.1f\n", p.GradePointAverage);
- printf("City : %s\n", p.city);
- printf("------------------\n");
- }
- if (definer == 3) {
- printf("------------------\n");
- printf("Surname : %s\n", p.surname);
- printf("Name : %s\n", p.name);
- printf("Age : %d\n", p.age);
- printf("Grade point average : %.1f\n", p.GradePointAverage);
- printf("Street : %s\n", p.street);
- printf("------------------\n");
- }
- if (definer == 4) {
- printf("------------------\n");
- printf("Surname : %s\n", p.surname);
- printf("Name : %s\n", p.name);
- printf("Age : %d\n", p.age);
- printf("Grade point average : %.1f\n", p.GradePointAverage);
- printf("House number : %d\n", p.house);
- printf("------------------\n");
- }
- if (definer == 5) {
- printf("------------------\n");
- printf("Surname : %s\n", p.surname);
- printf("Name : %s\n", p.name);
- printf("Age : %d\n", p.age);
- printf("Grade point average : %.1f\n", p.GradePointAverage);
- printf("Flat number : %d\n", p.flat);
- printf("------------------\n");
- }
- }
- char* EnterInformation(char*);
- students *GetStudents(int&);
- int GetDefiner(int&, char**);
- int SearchInformationAboutStudents(int, int);
- int SearchInformationAboutStudents(int, int);
- int main(int argc, char** argv) {
- int n, sch = 0, i, definer, value;
- students *p;
- printf("Enter number of students\n");
- while (!(scanf_s("%d", &n)) || n < 1) {
- rewind(stdin);
- printf("Incorrect number,reenter!\n");
- }
- p = GetStudents(n);
- definer = GetDefiner(definer, argv);
- if (definer == 0) {
- printf("Wrong data in command line,reenter!\n");
- free(p);
- return 1;
- }
- int(*SearchInf)(int p, int definer);
- for (i = 0; i < n; i++) {
- SearchInf = SearchInformationAboutStudents;
- if ((value = SearchInf(p[i].flag, definer)) == 1) {
- ShowInformationAboutStudents(p[i], definer);
- sch++;
- }
- }
- if (sch == 0)
- printf("No students with this information\n");
- free(p);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement