uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

bintree.c (3091B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 struct Student {
      6         char name[50];
      7         float grade;
      8         int id;
      9         struct Student *left;
     10         struct Student *right;
     11 };
     12 
     13 static struct Student *
     14 createstud(struct Student *root)
     15 {
     16         struct Student *s;
     17 
     18         if ((s = malloc(sizeof(struct Student))) == NULL) {
     19                 fputs("malloc failed\n", stderr);
     20                 exit(EXIT_FAILURE);
     21         }
     22 
     23         printf("Name: ");
     24         fgets(s->name, sizeof(s->name), stdin);
     25         s->name[strlen(s->name) - 1] = '\0';
     26         printf("ID: ");
     27         scanf("%d", &s->id);
     28         getchar();
     29         printf("Grade: ");
     30         scanf("%f", &s->grade);
     31         getchar();
     32 
     33         s->left = NULL;
     34         s->right = NULL;
     35 
     36         return s;
     37 
     38 }
     39 
     40 static void
     41 addinorder(struct Student *root, struct Student *s)
     42 {
     43         if (s->id < root->id) {
     44                 if (root->left == NULL)
     45                         root->left = s;
     46                 else
     47                         addinorder(root->left, s);
     48         } else {
     49                 if (root->right == NULL)
     50                         root->right = s;
     51                 else
     52                         addinorder(root->right, s);
     53         }
     54 }
     55 
     56 static void
     57 printinorder(struct Student *root)
     58 {
     59         if (root != NULL) {
     60                 printinorder(root->left);
     61                 printf("Name: %s | ID: %d | Grade: %.2f\n", root->name,
     62                         root->id, root->grade);
     63                 printinorder(root->right);
     64         }
     65 }
     66 
     67 static float
     68 maxgrade(struct Student *root)
     69 {
     70         float max = -1, maxright = -1;
     71 
     72         if (root != NULL) {
     73                 max = maxgrade(root->left);
     74                 if (root->grade > max)
     75                         max = root->grade;
     76                 maxright = maxgrade(root->right);
     77                 if (maxright > max)
     78                         max = maxright;
     79                 return max;
     80         }
     81 
     82         return max;
     83 }
     84 
     85 int
     86 main(int argc, char *argv[])
     87 {
     88         struct Student *root = NULL, *stud;
     89         float max;
     90         int ch = -1;
     91 
     92         while (ch != 0) {
     93                 puts("1. Add student in order");
     94                 puts("2. Print students in order");
     95                 puts("3. Show maximum grade");
     96                 puts("0. Exit");
     97                 printf("\nYour choice: ");
     98                 scanf("%d", &ch);
     99                 getchar();
    100 
    101                 switch (ch) {
    102                 case 1:
    103                         stud = createstud(root);
    104                         if (root == NULL)
    105                                 root = stud;
    106                         else
    107                                 addinorder(root, stud);
    108                         break;
    109                 case 2:
    110                         printinorder(root);
    111                         break;
    112                 case 3:
    113                         if ((max = maxgrade(root)) < 0)
    114                                 puts("Empty tree.");
    115                         else
    116                                 printf("Max grade: %.2f\n", max);
    117                         break;
    118                 }
    119         }
    120 
    121         return 0;
    122 }