Advertisement
Guest User

Beispielprogramm

a guest
Aug 8th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.37 KB | None | 0 0
  1. #include <stdio.h> //input output
  2. #include <stdlib.h> //malloc, realloc
  3. #include <string.h> //strcmp
  4.  
  5. //Defining Structs Album and Group
  6. typedef struct{
  7.     char album_name[21];
  8.     int release_year;
  9.     long int sales;
  10. }Album;
  11.  
  12. typedef struct{
  13.     int group_id;
  14.     char group_name[21];
  15.     int year;
  16.     Album album[2]; //2 albums to make it easier
  17. }Group;
  18.  
  19. //Function Declaration
  20. Group* load_groups_from_file(Group *group, int *group_count);
  21. void save_groups_to_file(Group *group, int group_count);
  22. Group* add_group_to_struct(Group *group, int *group_count);
  23. void search_group_id(Group *group,int temp, int group_count);
  24. void search_group_name(Group *group, char buf[], int group_count);
  25. void print_groups(Group *group, int group_count);
  26. void find_album_after_year(Group *group, int temp, int search);
  27. void find_best_selling_album(Group *group, int search);
  28. Group* sort_groups(Group *group, int group_count);
  29. int input_group_pos(Group *group, int group_count);
  30.  
  31. int main(){
  32.     int group_count = 0;//group count
  33.     int a;              //switch case variable
  34.     Group *group;       //dynamic array of struct group
  35.     char buf[50];       //for string input
  36.     long int temp;      //for integer input
  37.     int search;         //for searching at case 7 and 8
  38.    
  39.     while(1){   //infinite loop
  40.         system("cls"); //clear the console
  41.         //print menu
  42.         printf("1. Load groups from file\n");
  43.         printf("2. Save groups to file\n");
  44.         printf("3. Add a group\n");
  45.         printf("4. Search/Display a group by group id\n");
  46.         printf("5. Search/Display a group by group name\n");
  47.         printf("6. Display all groups\n");
  48.         printf("7. Find the albums of a given group after a given year\n");
  49.         printf("8. Find the best-selling album of a given group\n");
  50.         printf("9. Sort group array by group name\n");
  51.         printf("0. Exit\n");
  52.        
  53.         printf("Choice: ");
  54.         scanf("%d", &a);       
  55.         switch (a){
  56.             case 0:
  57.                 exit(0); //terminate programm
  58.             case 1: //open file "groups.txt" and copy the groups into the array
  59.                 group = load_groups_from_file(group, &group_count); //because group_count is not a pointer we pass it's address
  60.                 break; 
  61.             case 2: //one file "groups.txt" and save the groups into the file
  62.                 save_groups_to_file(group, group_count);
  63.                 break;                         
  64.             case 3: //add new group into the group array
  65.                 group = add_group_to_struct(group, &group_count);
  66.                 break;                     
  67.             case 4: //search and display group with this specific id
  68.                 printf("Give a ID: ");
  69.                 scanf("%d",&temp);
  70.                 search_group_id(group, temp, group_count);  //sunarthsh poy emfanizei to sugkrothma me ID poy dinei o xrhsths
  71.                 break;         
  72.             case 5: //search and display group with specific name
  73.                 printf("Give a Groupname: ");
  74.                 scanf("%49s",buf);
  75.                 search_group_name(group, buf, group_count);
  76.                 break;
  77.             case 6: //diplay all groups
  78.                 print_groups(group, group_count);
  79.                 break; 
  80.             case 7: //display all album of an group after a specific date
  81.                 search=input_group_pos(group, group_count);
  82.                 if(search == -1){
  83.                     printf("No Groups Added!\n");
  84.                     break;
  85.                 }
  86.                 printf("Give a Year: ");
  87.                 scanf("%d",&temp);
  88.                 find_album_after_year(group, temp, search);
  89.                 break;         
  90.             case 8: //display album of specific group with best sales out of the 2
  91.                 search=input_group_pos(group, group_count);
  92.                 if(search == -1){
  93.                     printf("No Groups Added!\n");
  94.                     break;
  95.                 }
  96.                 find_best_selling_album(group, search);        
  97.                 break;     
  98.             case 9: //sort the groups alphabetically
  99.                 group = sort_groups(group, group_count);
  100.                 break;                     
  101.             default:{
  102.                 printf("You must select something from above!!!\n");
  103.                 break;
  104.             }
  105.         }
  106.         sleep(2); //sleep for 2 seconds
  107.     }      
  108. }
  109.  
  110. //FUNCTIONS
  111. Group* load_groups_from_file(Group *group, int *group_count){
  112.     int i;
  113.     FILE *fp; //file pointer to read from "groups.txt"
  114.     if((fp = fopen("groups.txt", "r")) == NULL){ //checking if file exists
  115.         perror ("groups.txt");
  116.         return group;
  117.     }
  118.     fscanf(fp, "%d", group_count); //read group_count as pointer doesn't need &
  119.     group = (Group*)malloc(*group_count * sizeof(Group)); //we need *group_count cause it's a pointer
  120.     for(i=0; i<*group_count; i++){
  121.         fscanf(fp, "%d %s %d", &group[i].group_id, group[i].group_name, &group[i].year);
  122.         fscanf(fp, "%s %d %ld", group[i].album[0].album_name, &group[i].album[0].release_year, &group[i].album[0].sales);
  123.         fscanf(fp, "%s %d %ld", group[i].album[1].album_name, &group[i].album[1].release_year, &group[i].album[1].sales);
  124.     }
  125.     return group;  
  126. }
  127.  
  128. void save_groups_to_file(Group *group, int group_count){
  129.     int i;
  130.     FILE *fp; //file pointer to write into "groups.txt"
  131.     fp = fopen("groups.txt", "w"); //for saving you don't need to check existance cause it will create it
  132.     fprintf(fp, "%d\n", group_count);
  133.     for(i=0; i<group_count; i++){
  134.         fprintf(fp, "%d %s %d\n", group[i].group_id, group[i].group_name, group[i].year);
  135.         fprintf(fp, "%s %d %ld\n", group[i].album[0].album_name, group[i].album[0].release_year, group[i].album[0].sales);
  136.         fprintf(fp, "%s %d %ld\n", group[i].album[1].album_name, group[i].album[1].release_year, group[i].album[1].sales);
  137.     }          
  138.     fclose(fp);
  139. }
  140.  
  141. Group* add_group_to_struct(Group *group, int *group_count){
  142.     long int temp;
  143.     char buf[50];
  144.     if(*group_count == 0){ //if it was not created it needs malloc
  145.         group = (Group*)malloc( (*group_count + 1) * sizeof(Group) );
  146.     }else{
  147.         group = (Group*)realloc(group, (*group_count + 1) * sizeof(Group));
  148.     }
  149.     printf("Group ID: ");
  150.     scanf("%d",&temp);
  151.     group[*group_count].group_id=temp;
  152.                
  153.     do {
  154.     printf("Groupname: ");
  155.     scanf("%49s",buf);
  156.     } while(strlen(buf)>=20);
  157.     strcpy(group[*group_count].group_name, buf);
  158.                
  159.     printf("Foundation Year: ");
  160.     scanf("%d",&temp);
  161.     group[*group_count].year=temp;
  162.                
  163.     printf("Now give 2 Albums in this Order: Name Release_Date Sale_Count\n");
  164.     printf("Album 1: \n");
  165.     scanf("%19s %d %ld", group[*group_count].album[0].album_name, &group[*group_count].album[0].release_year, &group[*group_count].album[0].sales);
  166.     printf("Album 2: \n");
  167.     scanf("%19s %d %ld", group[*group_count].album[1].album_name, &group[*group_count].album[1].release_year, &group[*group_count].album[1].sales);
  168.     (*group_count)++; //increment group_count
  169.     return group;
  170. }
  171.  
  172. void search_group_id(Group *group, int temp, int group_count){
  173.     int i;
  174.     if(group_count == 0){ //checking emptyness
  175.         printf("No Groups Added!\n");
  176.         return;
  177.     }
  178.     for(i=0; i<group_count; i++){
  179.         if(temp==group[i].group_id){
  180.             printf("%d %s %d\n", group[i].group_id, group[i].group_name, group[i].year);
  181.             printf("%s %d %ld\n", group[i].album[0].album_name, group[i].album[0].release_year, group[i].album[0].sales);
  182.             printf("%s %d %ld\n", group[i].album[1].album_name, group[i].album[1].release_year, group[i].album[1].sales);
  183.             printf("\n");          
  184.             break; 
  185.         }
  186.     }
  187.     if(i==group_count) printf("No Group has this ID!\n");
  188. }
  189. void search_group_name(Group *group, char buf[], int group_count){
  190.     int i;
  191.     if(group_count == 0){ //checking emptyness
  192.         printf("No Groups Added!\n");
  193.         return;
  194.     }
  195.     for(i=0; i<group_count; i++){
  196.         if( strcmp(buf, group[i].group_name)==0 ){
  197.             printf("%d %s %d\n", group[i].group_id, group[i].group_name, group[i].year);
  198.             printf("%s %d %ld\n", group[i].album[0].album_name, group[i].album[0].release_year, group[i].album[0].sales);
  199.             printf("%s %d %ld\n", group[i].album[1].album_name, group[i].album[1].release_year, group[i].album[1].sales);
  200.             printf("\n");
  201.             break;
  202.         }
  203.     }
  204.     if(i==group_count) printf("No Group has this Name!\n");
  205. }
  206. void print_groups(Group *group, int group_count){
  207.     int i;
  208.     if(group_count == 0){ //checking emptyness
  209.         printf("No Groups Added!\n");
  210.         return;
  211.     }
  212.     printf("Groups: \n");
  213.     for(i=0; i<group_count; i++){
  214.         printf("%d %s %d\n", group[i].group_id, group[i].group_name, group[i].year);
  215.         printf("%s %d %ld\n", group[i].album[0].album_name, group[i].album[0].release_year, group[i].album[0].sales);
  216.         printf("%s %d %ld\n", group[i].album[1].album_name, group[i].album[1].release_year, group[i].album[1].sales);
  217.         printf("\n");
  218.     }  
  219. }
  220. void find_album_after_year(Group *group, int temp, int search){
  221.     int i;
  222.     for(i=0; i<2; i++){
  223.         if(group[search].year>=temp){
  224.             printf("%s %d %ld\n", group[search].album[i].album_name, group[search].album[i].release_year, group[search].album[i].sales);
  225.         }
  226.     }
  227. }
  228. void find_best_selling_album(Group *group, int search){
  229.     if(group[search].album[0].sales>group[search].album[1].sales){
  230.         printf("%s %d %ld\n", group[search].album[0].album_name, group[search].album[0].release_year, group[search].album[0].sales);
  231.     }
  232.     else{
  233.         printf("%s %d %ld\n", group[search].album[1].album_name, group[search].album[1].release_year, group[search].album[1].sales);
  234.     }
  235. }
  236. Group* sort_groups(Group *group, int group_count){
  237.     int i,j;
  238.     Group swap;
  239.     if(group_count == 0){ //checking emptyness
  240.         printf("No Groups Added!\n");
  241.         return group;
  242.     }
  243.     for(i=0; i<group_count; i++){
  244.         for(j=0; j<group_count; j++){
  245.             if( strcmp(group[j].group_name, group[i].group_name)>0 ){
  246.                 swap=group[i];
  247.                 group[i]=group[j];
  248.                 group[j]=swap;             
  249.             }
  250.         }
  251.     }
  252.     return group;
  253. }
  254. int input_group_pos(Group *group, int group_count){
  255.     int i;
  256.     int search;
  257.     char buf[50];
  258.     if(group_count == 0){ //checking emptyness
  259.         return -1;
  260.     }
  261.     do {   
  262.         printf("Give Groupname: ");
  263.         scanf("%49s",buf);
  264.         search=0;
  265.         for(i=0; i<group_count; i++){
  266.             if( strcmp(buf, group[i].group_name)==0 ){
  267.                 search=i;
  268.                 break;
  269.             }
  270.         }
  271.         if(search==0) printf("No Group has this Name!\n");
  272.     } while(search==0);
  273.     return search;
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement