Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> //input output
- #include <stdlib.h> //malloc, realloc
- #include <string.h> //strcmp
- //Defining Structs Album and Group
- typedef struct{
- char album_name[21];
- int release_year;
- long int sales;
- }Album;
- typedef struct{
- int group_id;
- char group_name[21];
- int year;
- Album album[2]; //2 albums to make it easier
- }Group;
- //Function Declaration
- Group* load_groups_from_file(Group *group, int *group_count);
- void save_groups_to_file(Group *group, int group_count);
- Group* add_group_to_struct(Group *group, int *group_count);
- void search_group_id(Group *group,int temp, int group_count);
- void search_group_name(Group *group, char buf[], int group_count);
- void print_groups(Group *group, int group_count);
- void find_album_after_year(Group *group, int temp, int search);
- void find_best_selling_album(Group *group, int search);
- Group* sort_groups(Group *group, int group_count);
- int input_group_pos(Group *group, int group_count);
- int main(){
- int group_count = 0;//group count
- int a; //switch case variable
- Group *group; //dynamic array of struct group
- char buf[50]; //for string input
- long int temp; //for integer input
- int search; //for searching at case 7 and 8
- while(1){ //infinite loop
- system("cls"); //clear the console
- //print menu
- printf("1. Load groups from file\n");
- printf("2. Save groups to file\n");
- printf("3. Add a group\n");
- printf("4. Search/Display a group by group id\n");
- printf("5. Search/Display a group by group name\n");
- printf("6. Display all groups\n");
- printf("7. Find the albums of a given group after a given year\n");
- printf("8. Find the best-selling album of a given group\n");
- printf("9. Sort group array by group name\n");
- printf("0. Exit\n");
- printf("Choice: ");
- scanf("%d", &a);
- switch (a){
- case 0:
- exit(0); //terminate programm
- case 1: //open file "groups.txt" and copy the groups into the array
- group = load_groups_from_file(group, &group_count); //because group_count is not a pointer we pass it's address
- break;
- case 2: //one file "groups.txt" and save the groups into the file
- save_groups_to_file(group, group_count);
- break;
- case 3: //add new group into the group array
- group = add_group_to_struct(group, &group_count);
- break;
- case 4: //search and display group with this specific id
- printf("Give a ID: ");
- scanf("%d",&temp);
- search_group_id(group, temp, group_count); //sunarthsh poy emfanizei to sugkrothma me ID poy dinei o xrhsths
- break;
- case 5: //search and display group with specific name
- printf("Give a Groupname: ");
- scanf("%49s",buf);
- search_group_name(group, buf, group_count);
- break;
- case 6: //diplay all groups
- print_groups(group, group_count);
- break;
- case 7: //display all album of an group after a specific date
- search=input_group_pos(group, group_count);
- if(search == -1){
- printf("No Groups Added!\n");
- break;
- }
- printf("Give a Year: ");
- scanf("%d",&temp);
- find_album_after_year(group, temp, search);
- break;
- case 8: //display album of specific group with best sales out of the 2
- search=input_group_pos(group, group_count);
- if(search == -1){
- printf("No Groups Added!\n");
- break;
- }
- find_best_selling_album(group, search);
- break;
- case 9: //sort the groups alphabetically
- group = sort_groups(group, group_count);
- break;
- default:{
- printf("You must select something from above!!!\n");
- break;
- }
- }
- sleep(2); //sleep for 2 seconds
- }
- }
- //FUNCTIONS
- Group* load_groups_from_file(Group *group, int *group_count){
- int i;
- FILE *fp; //file pointer to read from "groups.txt"
- if((fp = fopen("groups.txt", "r")) == NULL){ //checking if file exists
- perror ("groups.txt");
- return group;
- }
- fscanf(fp, "%d", group_count); //read group_count as pointer doesn't need &
- group = (Group*)malloc(*group_count * sizeof(Group)); //we need *group_count cause it's a pointer
- for(i=0; i<*group_count; i++){
- fscanf(fp, "%d %s %d", &group[i].group_id, group[i].group_name, &group[i].year);
- fscanf(fp, "%s %d %ld", group[i].album[0].album_name, &group[i].album[0].release_year, &group[i].album[0].sales);
- fscanf(fp, "%s %d %ld", group[i].album[1].album_name, &group[i].album[1].release_year, &group[i].album[1].sales);
- }
- return group;
- }
- void save_groups_to_file(Group *group, int group_count){
- int i;
- FILE *fp; //file pointer to write into "groups.txt"
- fp = fopen("groups.txt", "w"); //for saving you don't need to check existance cause it will create it
- fprintf(fp, "%d\n", group_count);
- for(i=0; i<group_count; i++){
- fprintf(fp, "%d %s %d\n", group[i].group_id, group[i].group_name, group[i].year);
- fprintf(fp, "%s %d %ld\n", group[i].album[0].album_name, group[i].album[0].release_year, group[i].album[0].sales);
- fprintf(fp, "%s %d %ld\n", group[i].album[1].album_name, group[i].album[1].release_year, group[i].album[1].sales);
- }
- fclose(fp);
- }
- Group* add_group_to_struct(Group *group, int *group_count){
- long int temp;
- char buf[50];
- if(*group_count == 0){ //if it was not created it needs malloc
- group = (Group*)malloc( (*group_count + 1) * sizeof(Group) );
- }else{
- group = (Group*)realloc(group, (*group_count + 1) * sizeof(Group));
- }
- printf("Group ID: ");
- scanf("%d",&temp);
- group[*group_count].group_id=temp;
- do {
- printf("Groupname: ");
- scanf("%49s",buf);
- } while(strlen(buf)>=20);
- strcpy(group[*group_count].group_name, buf);
- printf("Foundation Year: ");
- scanf("%d",&temp);
- group[*group_count].year=temp;
- printf("Now give 2 Albums in this Order: Name Release_Date Sale_Count\n");
- printf("Album 1: \n");
- scanf("%19s %d %ld", group[*group_count].album[0].album_name, &group[*group_count].album[0].release_year, &group[*group_count].album[0].sales);
- printf("Album 2: \n");
- scanf("%19s %d %ld", group[*group_count].album[1].album_name, &group[*group_count].album[1].release_year, &group[*group_count].album[1].sales);
- (*group_count)++; //increment group_count
- return group;
- }
- void search_group_id(Group *group, int temp, int group_count){
- int i;
- if(group_count == 0){ //checking emptyness
- printf("No Groups Added!\n");
- return;
- }
- for(i=0; i<group_count; i++){
- if(temp==group[i].group_id){
- printf("%d %s %d\n", group[i].group_id, group[i].group_name, group[i].year);
- printf("%s %d %ld\n", group[i].album[0].album_name, group[i].album[0].release_year, group[i].album[0].sales);
- printf("%s %d %ld\n", group[i].album[1].album_name, group[i].album[1].release_year, group[i].album[1].sales);
- printf("\n");
- break;
- }
- }
- if(i==group_count) printf("No Group has this ID!\n");
- }
- void search_group_name(Group *group, char buf[], int group_count){
- int i;
- if(group_count == 0){ //checking emptyness
- printf("No Groups Added!\n");
- return;
- }
- for(i=0; i<group_count; i++){
- if( strcmp(buf, group[i].group_name)==0 ){
- printf("%d %s %d\n", group[i].group_id, group[i].group_name, group[i].year);
- printf("%s %d %ld\n", group[i].album[0].album_name, group[i].album[0].release_year, group[i].album[0].sales);
- printf("%s %d %ld\n", group[i].album[1].album_name, group[i].album[1].release_year, group[i].album[1].sales);
- printf("\n");
- break;
- }
- }
- if(i==group_count) printf("No Group has this Name!\n");
- }
- void print_groups(Group *group, int group_count){
- int i;
- if(group_count == 0){ //checking emptyness
- printf("No Groups Added!\n");
- return;
- }
- printf("Groups: \n");
- for(i=0; i<group_count; i++){
- printf("%d %s %d\n", group[i].group_id, group[i].group_name, group[i].year);
- printf("%s %d %ld\n", group[i].album[0].album_name, group[i].album[0].release_year, group[i].album[0].sales);
- printf("%s %d %ld\n", group[i].album[1].album_name, group[i].album[1].release_year, group[i].album[1].sales);
- printf("\n");
- }
- }
- void find_album_after_year(Group *group, int temp, int search){
- int i;
- for(i=0; i<2; i++){
- if(group[search].year>=temp){
- printf("%s %d %ld\n", group[search].album[i].album_name, group[search].album[i].release_year, group[search].album[i].sales);
- }
- }
- }
- void find_best_selling_album(Group *group, int search){
- if(group[search].album[0].sales>group[search].album[1].sales){
- printf("%s %d %ld\n", group[search].album[0].album_name, group[search].album[0].release_year, group[search].album[0].sales);
- }
- else{
- printf("%s %d %ld\n", group[search].album[1].album_name, group[search].album[1].release_year, group[search].album[1].sales);
- }
- }
- Group* sort_groups(Group *group, int group_count){
- int i,j;
- Group swap;
- if(group_count == 0){ //checking emptyness
- printf("No Groups Added!\n");
- return group;
- }
- for(i=0; i<group_count; i++){
- for(j=0; j<group_count; j++){
- if( strcmp(group[j].group_name, group[i].group_name)>0 ){
- swap=group[i];
- group[i]=group[j];
- group[j]=swap;
- }
- }
- }
- return group;
- }
- int input_group_pos(Group *group, int group_count){
- int i;
- int search;
- char buf[50];
- if(group_count == 0){ //checking emptyness
- return -1;
- }
- do {
- printf("Give Groupname: ");
- scanf("%49s",buf);
- search=0;
- for(i=0; i<group_count; i++){
- if( strcmp(buf, group[i].group_name)==0 ){
- search=i;
- break;
- }
- }
- if(search==0) printf("No Group has this Name!\n");
- } while(search==0);
- return search;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement