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{
- char* name;
- char* surname;
- char* patronymic;
- int unit;
- union {
- char* region;
- char* borough;
- char* city;
- char* street;
- int house;
- int flat;
- }placeOfLiving;
- };
- char* charStringInput(){
- char *string = (char*)malloc(sizeof(char)* 50);
- if (!string){
- printf("Memory error\n");
- return 0;
- }
- fflush(stdin);
- gets(string);
- fflush(stdin);
- int i = 0;
- while (string[i] != '\0'){
- if (!('a' <= string[i]) && (string[i] <= 'z') || (('A' <= string[i] ) && (string[i] <= 'Z'))){
- printf("This string cannot contain any numbers or symbols besides letters!\n Please,input write string : ");
- charStringInput();
- }
- i++;
- }
- string = (char*)realloc(string, sizeof(char)*i);
- if (!string){
- printf("Memory error\n");
- return 0;
- }
- string[i] = '\0';
- return string;
- }
- int integerInput(){
- int number;
- if (!scanf("%d", &number) || (number < 1)){
- printf("Please,enter number once more\n");
- fflush(stdin);
- }
- fflush(stdin);
- return number;
- }
- struct students* initStruct(){
- struct students *oneStudent = (struct students*)malloc(sizeof(struct students));
- if (!oneStudent){
- printf("Memory error\n");
- return 0;
- }
- printf("Enter the name of student: ");
- oneStudent->name = charStringInput();
- printf("Enter the surname of student: ");
- oneStudent->surname = charStringInput();
- printf("Enter the patronymic of student: ");
- oneStudent->patronymic = charStringInput();
- printf("What information about a student do you want to record?\n");
- printf("1 - region\n2 - borough\n3 - city\n4- street\n5-number of a house\n6-number of a flat\n Choice: ");
- while (!(scanf_s("%d", &oneStudent->unit))){
- rewind(stdin);
- printf("Error: cannot use symbols and numbers greater than 3 and less than 1!\nOption: ");
- }
- switch (oneStudent->unit) {
- case 1:
- printf("Enter the region of this student : ");
- oneStudent->placeOfLiving.region = charStringInput();
- break;
- case 2:
- printf("Enter the borough where the student lives: ");
- oneStudent->placeOfLiving.borough = charStringInput();
- break;
- case 3:
- printf("Enter city where the student lives: ");
- oneStudent->placeOfLiving.city = charStringInput();
- break;
- case 4:
- printf("Enter street where the student lives: ");
- oneStudent->placeOfLiving.street = charStringInput();
- break;
- case 5:
- printf("Enter the number of house where the student lives: ");
- oneStudent->placeOfLiving.house = integerInput();
- break;
- case 6:
- printf("Enter the number of flat where the student lives: ");
- oneStudent->placeOfLiving.flat = integerInput();
- break;
- }
- return oneStudent;
- }
- void printStudent(struct students** table, int i){
- printf("Name of %d student is %s\n", i + 1, table[i]->name);
- printf("Surname of %d student is %s\n", i + 1, table[i]->surname);
- printf("Patrinymic of %d student is %s\n", i + 1, table[i]->patronymic);
- switch (table[i]->unit) {
- case 1:
- printf("The region of this student is %s\n", table[i]->placeOfLiving.region);
- break;
- case 2:
- printf("The borough of this student is %s\n", table[i]->placeOfLiving.borough);
- break;
- case 3:
- printf("The city of this student is %s\n", table[i]->placeOfLiving.city);
- break;
- case 4:
- printf("The street of this student is %s\n", table[i]->placeOfLiving.street);
- break;
- case 5:
- printf("The hosue of this student is %d\n", table[i]->placeOfLiving.house);
- break;
- case 6:
- printf("The flat of this student is %d\n", table[i]->placeOfLiving.flat);
- break;
- }
- }
- struct students** createTable(struct students** table, int* numberOfStudents){
- table = (struct students**)malloc(sizeof(struct students*));
- int size = 0;
- char answer;
- bool statement = true;
- do {
- printf("\nDo you want add students?(enter 'y' or 'n'):");
- answer = getchar();
- fflush(stdin);
- switch (answer) {
- case 'y': {
- table = (struct students**)realloc(table, sizeof(struct students*)*(*numberOfStudents + 1));
- if (!table){
- printf("Memory error\n");
- return 0;
- }
- table[*numberOfStudents] = (struct students*)malloc(sizeof(struct students*));
- if (!table[*numberOfStudents]){
- printf("Memory error\n");
- return 0;
- }
- table[*numberOfStudents] = initStruct();
- (*numberOfStudents)++;
- break;
- }
- case 'n':{
- statement = false;
- break;
- }
- default:{
- printf("\nError,please enter 'y' or 'n'");
- break;
- }
- }
- } while (statement);
- return table;
- }
- int searchRightStringInCommandLine(struct students** table, char* str){
- int i = 0;
- if ((str[i] <= 'Z') && (str[i] >= 'A')){
- str[i] = char(((int)str[i] + 32));
- }
- if (strcmp(str, "region") == 0 ) {
- return 1;
- }
- if (strcmp(str, "borough") == 0 ) {
- return 2;
- }
- if (strcmp(str, "city") == 0) {
- return 3;
- }
- if (strcmp(str, "street")) {
- return 4;
- }
- if (strcmp(str, "house")) {
- return 5;
- }
- if (strcmp(str, "flat")) {
- return 6;
- }
- return 0;
- }
- int main(int argc, char **argv) {
- if (!argv[1]){
- printf("Command line is empty!\n");
- return 0;
- }
- int numberOfStudents = 0;
- struct students ** table = (students**)malloc(sizeof(students*));
- students**(*pointerToCreating)(students** table, int* numberOfStudents);
- pointerToCreating = createTable;
- table = pointerToCreating(table, &numberOfStudents);
- int choice;
- bool exitVariable = true;
- do{
- printf("1-to print table\n2-to search students for a given item\n3-to exit\n Choice: ");
- scanf("%d", &choice);
- fflush(stdin);
- switch (choice){
- case 1:{
- for (int i = 0; i < numberOfStudents; i++){
- printStudent(table, i);
- }
- break;
- }
- case 2:{
- bool flag = false;
- int j = 1;
- char* str;
- for (int i = 0; i < numberOfStudents; i++){
- while (argv[j]){
- int number = searchRightStringInCommandLine(table, argv[j]);
- if (number == table[i]->unit){
- flag = true;
- printStudent(table, i);
- }
- j++;
- }
- j = 1;
- }
- if (flag == false){
- printf("There no students suitable to arguments of command line\n");
- }
- break;
- }
- case 3:{
- exitVariable = false;
- break;
- }
- default:{
- printf("Please,enter your choice!\n");
- }
- }
- } while (exitVariable);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement