Advertisement
Ladies_Man

DZ_1 New (bugfixes, time, memory, shell)

May 31st, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.77 KB | None | 0 0
  1. //ИУ4_какое-то_ДЗ_сем2
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <strings.h>
  6. #include <time.h>
  7. #define MAXLEN 40
  8. #define READLEN 300
  9.  
  10. //changelog:
  11.             //minor bug fixes
  12.             //memory allocated as much as prog needs (prev size:1000000)
  13.             //added time stamps
  14.             //changed bubblesort -> shellsort
  15.  
  16. typedef struct student {
  17.     char first[MAXLEN];
  18.     char second[MAXLEN];
  19.     char univer[MAXLEN];
  20.     char fac[MAXLEN];
  21.     char dep[MAXLEN];
  22.     int age, course, group;
  23.     char city[MAXLEN];
  24. } stud_tag;
  25.  
  26. struct town {
  27.     char name[MAXLEN];
  28.     int stud, bach, mag;
  29. };
  30.  
  31. void swap (stud_tag *i, stud_tag *j) {
  32.     stud_tag temp = *i;
  33.     *i = *j;
  34.     *j = temp;
  35. }
  36.  
  37. int main()
  38. {
  39.     FILE *fp = fopen("students_1.csv", "r");
  40.     FILE *ff = fopen("result_1.txt", "w");
  41.  
  42.     if (NULL == fp) {
  43.         printf("cannot open file\n");
  44.         exit(1);
  45.     }
  46.  
  47.     int i = 0, j = 0, stud_num = 0, memory_size = 0, bachelors = 0, magisters = 0, eldest, city_num = 0, mark, k = 0, gap;
  48.     double start_time, c_t1, c_t2, c_t3, c_t4, c_t5, finish_time;
  49.     char str[READLEN];
  50.  
  51.     while (!feof(fp)) if (fgets(str, READLEN, fp)) memory_size++;  //чтобы не выделять лишней памяти считаем сначала сколько всего строк в файле
  52.     memory_size++;
  53.     rewind(fp);
  54.  
  55.     struct student* man;
  56.     man = (struct student *)malloc(sizeof(struct student)*memory_size);
  57.     printf("memory allocated\n");
  58.  
  59.     start_time = clock();
  60.     printf("\tstart:%.2lfms\n", start_time);  //здесь именно текущее время
  61.  
  62.     printf("reading file: ");
  63.     fscanf (fp, "%*[a-zA-Z];%*[a-zA-Z];%*[a-zA-Z];%*[a-zA-Z];%*[a-zA-Z];%*[a-zA-Z];%*[a-zA-Z];%*[a-zA-Z];%*[a-zA-Z];");
  64.     while (9 == fscanf(fp, "%[^;];%[^;];%[^;];%[^;];%[^;];%d;%d;%d;%[^;];\n", man[i].first, man[i].second, man[i].univer,
  65.                 man[i].fac, man[i].dep, &man[i].age, &man[i].course, &man[i].group, man[i].city)) {
  66.         if (5 > man[i].course) {
  67.             bachelors++;
  68.         } else {
  69.             magisters++;
  70.         }
  71.         i++;
  72.     }
  73.     fclose(fp);
  74.     printf("done\n");
  75.     c_t1 = clock();
  76.     printf("\treading duration:%.2lfms\n", c_t1 - start_time);  //а здесь именно продолжительность процесса
  77.  
  78.     //===========LEVEL1===========
  79.  
  80.     stud_num = i;
  81.     fprintf(ff, "Total number of students:%d\nBachelors:%d / Magisters:%d\n", stud_num, bachelors, magisters);
  82.  
  83.     eldest = man[0].age;
  84.     for (i = 0; i < stud_num; i++) if (man[i].age > eldest) eldest = man[i].age;
  85.     //Bubblesort
  86.     /*for (i = 0; i < stud_num - 1; i++) {
  87.         for (j = i + 1; j < stud_num; j++) {
  88.             if (0 == strcmp(man[i].second, man[j].second) && 0 < strcmp(man[i].first, man[j].first)) {
  89.                 swap(&man[i], &man[j]);
  90.             } else {
  91.                 if (0 < strcmp(man[i].second, man[j].second)) swap(&man[i], &man[j]);
  92.             }
  93.         }
  94.     }*/
  95.     //sHELLsort
  96.     for (gap = stud_num/2; gap > 0; gap /= 2) //wiki
  97.         for (i = gap; i < stud_num; i++)
  98.         for (j = i; j >= gap && ((0 == strcmp(man[j-gap].second, man[j].second) && 0 < strcmp(man[j-gap].first, man[j].first)) || (0 < strcmp(man[j-gap].second, man[j].second))); j -= gap) swap (&man[j], &man[j - gap]);
  99.  
  100.     fprintf(ff, "\nAlphabetical sorting:\n");
  101.     for (i = 0; i < stud_num; i++) fprintf(ff, "\t%s %s\n", man[i].second, man[i].first);
  102.     printf(">>>Level 1 Finished\n");
  103.     printf("sorting done\n");
  104.     c_t2 = clock();
  105.     printf("\tsorting duration:%.2lfms\n", c_t2 - c_t1);
  106.  
  107.     //===========LEVEL2===========
  108.  
  109.     fprintf(ff, "\nEldest student's age:%d\n\nEldest students:\n", eldest);
  110.     for (j = 1, i = 0; i < stud_num; i++) if (man[i].age == eldest) fprintf(ff, "%d)%s %s, %d\n\t%s %s\n\tfaculty:%s course:%d group:%d\n\t%s\n\n",
  111.                                                                             j++ , man[i].second, man[i].first, man[i].age, man[i].univer, man[i].fac,
  112.                                                                             man[i].dep, man[i].course, man[i].group, man[i].city);
  113.     printf(">>>Level 2 Finished\n");
  114.     c_t3 = clock();
  115.     printf("\t'eldest' duration:%.2lfms\n", c_t3 - c_t2);
  116.  
  117.     //===========LEVEL3===========
  118.  
  119.     struct town* cities;
  120.     cities = (struct town *)malloc(sizeof(struct town)*memory_size);
  121.     printf("memory2 allocated\n");
  122.  
  123.     for (i = 0; i < stud_num; i++) {
  124.         for (mark = 0, j = 0; j < i; j++) {
  125.             if (0 == strcmp(man[i].city, man[j].city)) {
  126.                 mark++;
  127.                 break;
  128.             }
  129.         }
  130.         if (0 == mark) {
  131.             strcpy(cities[k].name, man[i].city);
  132.             city_num++;
  133.             k++;
  134.         }
  135.     }
  136.     printf("array filled\n");
  137.     fprintf(ff, "\n\nNumber of cities:%d\n", city_num);
  138.     c_t4 = clock();
  139.     printf("\tfill duration:%.2lfms\n", c_t4 - c_t3);
  140.  
  141.     for (i = 0; i < city_num; i++) { cities[i].stud = 0; cities[i].bach = 0; cities[i].mag = 0; }
  142.  
  143.     for (i = 0; i < city_num; i++) {
  144.         for (j = 0; j < stud_num; j++) {
  145.             if (0 == strcmp(cities[i].name, man[j].city)) {
  146.                 if (5 > man[j].course) {
  147.                     cities[i].bach++;
  148.                 } else {
  149.                     cities[i].mag++;
  150.                 }
  151.                 cities[i].stud++;
  152.             }
  153.         }
  154.     }
  155.     printf("courses distributed\n");
  156.     c_t5 = clock();
  157.     printf("\tdistr duration:%.2lfms\n", c_t5 - c_t4);
  158.     fprintf(ff, "Distribution by cities:\n");
  159.     for (i = 0; i < city_num; i++) fprintf(ff, "%s:\n\ttotal students:%d\n\tbachelors:%d / magisters:%d\n\n", cities[i].name, cities[i].stud, cities[i].bach, cities[i].mag);
  160.  
  161.     int most_stud = cities[0].stud, most_bachs = cities[0].bach, most_mags = cities[0].mag;
  162.  
  163.     for (i = 1; i < city_num; i++) {
  164.         if (cities[i].stud > most_stud) most_stud = cities[i].stud;
  165.         if (cities[i].bach > most_bachs) most_bachs = cities[i].bach;
  166.         if (cities[i].mag > most_mags) most_mags = cities[i].mag;
  167.     }
  168.  
  169.     printf("most_courses counted\n");
  170.     for (i = 0; i < city_num; i++) {
  171.         if (cities[i].stud == most_stud) fprintf(ff, "Most students from %s\n", cities[i].name);
  172.         if (cities[i].bach == most_bachs) fprintf(ff, "Most bachelors from %s\n", cities[i].name);
  173.         if (cities[i].mag == most_mags) fprintf(ff, "Most magisters from %s\n", cities[i].name);
  174.     }
  175.  
  176.     fprintf(ff, "\nAverage num of students:%d\nAvg num of bachelors:%d / magisters:%d\n\n", stud_num / city_num, bachelors / city_num, magisters / city_num);
  177.     printf(">>>Level 3 Finished\n");
  178.     finish_time = clock();
  179.     printf("\tTotal duration:%.2lfms\n", finish_time - start_time);
  180.  
  181.     fclose(ff);
  182.     return 0;
  183. //A.B
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement