Untitled

 avatar
unknown
c_cpp
3 years ago
6.3 kB
8
Indexable
#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);

void insertCompany(Company *c, int n);

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 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");
}


int main(){
    int n;
    printf("Enter the number of companies\n");
    scanf("%d",&n);
    Company c[n];
    insertCompany(c, n);
    int choice;
    char buf[100];
    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);
            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("This company not found.\n");
                else {
                    printf("Enter the new Average Salary\n");
                    scanf("%f", &newSalary);
                    updateSalary(&c[index], newSalary);
                }
                break;
            default: 
                break;
        }
    } while(choice != 8);


    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 this domain.\n");
}

void dispNoofEmployees(struct Company *c, char *name, int n){
    int found = getCompanyByName(c, name, n);
    if(found == -1) printf("This company not found.\n");
    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("This domain not found.\n");
    else {
        printf("Details of company offering highest salary in domain Software\n");
        dispCompanyDetails(*(c+index));
    }
}

void dispParticularCompanyDetails(struct Company *c, char *name, int n){
    int found = getCompanyByName(c, name, n);
    if(found == -1) printf("Company by name is not found.\n");
    else {
        printf("Details of company %s\n", name);
        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));
    }
}
Editor is loading...