struct menu

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
6.8 kB
34
Indexable
Never
#include <stdio.h>
#include <string.h>

typedef struct Company{
    char name[100];
    char domain[100];
    int noOfEmployees;
    float averageSalary;
}Company;

void dispCompanyDetailsinDomain(struct Company *c, char *domain, int n);
void dispNoofEmployees(struct Company *c, char *name, int n);
void dispCompanyOfferingHighestAvgSal(struct Company *c, int n);
void dispCompanyOfferingHighestAvgSalinDomain(struct Company *c, char *domain, int n);
void dispParticularCompanyDetails(struct Company *c, char *name, int n);
void dispCompanyDetails(struct Company c);
void updateSalary(struct Company *c, float newSalary);
void dispAll(Company *c, int n);

// utility functions
void insertCompany(Company *c, int n);
int getCompanyByName(Company *c, char * name, int n);
void sortCompany(Company *c, int n);
void showMenu();

int main(){
    int n;
    printf("Enter the number of companies\n");
    scanf("%d",&n);
    Company c[n];
    insertCompany(c, n); // input
    sortCompany(c, n);  // sort
    int choice;
    char buf[100];  // buffer for name or domain
    float newSalary;   
    do {
        showMenu();
        scanf("%d", &choice);
        switch(choice) {
            case 1:
                printf("Enter the domain name\n");
                scanf("%s", buf);
                dispCompanyDetailsinDomain(c, buf, n);
                break;
            case 2:
                printf("Enter the company name\n");
                scanf("%s", buf);
                dispNoofEmployees(c, buf, n);
                break;
            case 3:
                dispCompanyOfferingHighestAvgSal(c, n);
                break;
            case 4:
                printf("Enter the domain name\n");
                scanf("%s", buf);
                dispCompanyOfferingHighestAvgSalinDomain(c, buf, n);
                break;
            case 5:
                printf("Enter the company name\n");
                scanf("%s", buf);
                dispParticularCompanyDetails(c, buf, n);
                break;
            case 6:
                dispAll(c, n);
                break;
            case 7:
                printf("Enter the company name\n");
                scanf("%s", buf);
                int index = getCompanyByName(c, buf, n);
                if(index == -1) printf("%s company not found.\n", buf);
                else {
                    printf("Enter the new Average Salary\n");
                    scanf("%f", &newSalary);
                    updateSalary(&c[index], newSalary);
                }
                break;
            default: 
                break;
        }
    } while(choice != 8);
    printf("Closing the program\n");

    return 0;
}

void dispCompanyDetailsinDomain(struct Company *c, char *domain, int n){
    int found = 0;
    for(int i = 0; i < n; i++){
        if(strcmp((c+i)->domain, domain) == 0 ){
            found++;
            if(found == 1) printf("Companies working in domain %s", domain);
            printf("%s\n", (c+i)->name);
        }
    }
    if(found == 0) printf("No company found in %s domain.\n", domain);
}

void dispNoofEmployees(struct Company *c, char *name, int n){
    int found = getCompanyByName(c, name, n);
    if(found == -1) printf("%s company not found.\n", name);
    else {
        printf("Employee count in %s is \n", name);
        printf("%d\n", (c+found)->noOfEmployees);
    }
}

void dispCompanyOfferingHighestAvgSal(struct Company *c, int n){
    float highest = 0.0;
    int index;
    for(int i=0; i<n; i++){
        if((c+i)->averageSalary > highest){
            highest = (c+i)->averageSalary;
            index = i;
        }
    }
    printf("Details of company offering highest salary\n");
    dispCompanyDetails(*(c+index));
}
void dispCompanyOfferingHighestAvgSalinDomain(struct Company *c, char *domain, int n){
    float highest = 0.0;
    int index, found = 0;
    for(int i=0; i<n; i++){
        if(strcmp((c+i)->domain, domain) == 0 ){
            found++;
            if((c+i)->averageSalary > highest){
                highest = (c+i)->averageSalary;
                index = i;
            }
        }
    }
    if(found == 0) printf("%s domain not found.\n", domain);
    else {
        printf("Details of company offering highest salary in domain %s\n", domain);
        dispCompanyDetails(*(c+index));
    }
}

void dispParticularCompanyDetails(struct Company *c, char *name, int n){
    int found = getCompanyByName(c, name, n);
    printf("Details of company %s\n", name);
    if(found == -1) printf("Company by name %s is not found\n", name);
    else {
        dispCompanyDetails(*(c+found));
    }
}

void dispCompanyDetails(struct Company c){
    printf("Name : %s\n", c.name);
    printf("Domain : %s\n", c.domain);
    printf("Number of Employees : %d\n", c.noOfEmployees);
    printf("Average Salary : %.2f\n", c.averageSalary);
}

void updateSalary(struct Company *c, float newSalary){
    c->averageSalary = newSalary;
}

void dispAll(Company *c, int n){
    for(int i = 0; i < n; i++){
        dispCompanyDetails(*(c+i));
    }
}


void insertCompany(Company *c, int n){
    for(int i = 0; i < n; i++){
        printf("Enter the details of company %d\n", i+1);
        printf("Enter name\n");
        scanf("%s", (c+i)->name);
        printf("Enter domain\n");
        scanf("%s", (c+i)->domain);
        printf("Enter number of employees\n");
        scanf("%d", &(c+i)->noOfEmployees);
        printf("Enter average salary\n");
        scanf("%f", &(c+i)->averageSalary);
    }
}

int getCompanyByName(Company *c, char * name, int n){
    for(int i = 0; i < n; i++){
        if(strcmp((c+i)->name, name) == 0){
            return i;
        }
    }
    return -1;
}

void sortCompany(Company *c, int n){
    for(int i=0;i<n-1; i++){
        for(int j=i; j<n; j++){
            if(strcmp((c+i)->name, (c+j)->name) > 0){
                Company temp = *(c+j);
                *(c+j) = *(c+i);
                *(c+i) = temp;
            }
        }
    }
}

void showMenu(){
    printf("Menu\n");
    printf("1)Display names of companies working in a particular domain\n");
    printf("2)Display number of employees in a particular company\n");
    printf("3)Display all details of the company offering the highest average salary\n");
    printf("4)Display all details of the company from a particular domain offering the highest average salary\n");
    printf("5)Display all details of a particular company (Search by name)\n");
    printf("6)Display all details of all companies\n");
    printf("7)Update the average salary of a company\n");
    printf("8)Exit\n");
    printf("Enter your choice\n");
}