#include

#include

#include


#define SIZE 6



struct Employee

{

    char name[30];

    float sal;

    float rate;

    float raise;

    float newSal;

//declare your struct data members here

};


void load (struct Employee e[], int n);

void ARate(struct Employee e[], int n);

void calcRaise(struct Employee e[], int n);

void sort(struct Employee e[], int n);

void total(struct Employee e[], int n,float *ts,float *tr,float *tn );

void title();

void print(struct Employee e[], int n);

void printTotals(float ts,float tr, float tn);

void savetext(struct Employee e[], int n);

void gettext(struct Employee e[], int n);

void savebn(struct Employee e[], int n);

void getbn(struct Employee e[], int n);

//function prototype here



int main()

{

//declaration of variables

struct Employee e[SIZE];

float ts = 0, tr = 0, tn = 0;


//load data into struct, calculate raises, salaries and new salaries and print the //original array of struct

load(e,SIZE);

ARate(e,SIZE);

calcRaise(e,SIZE);

    total(e,SIZE,&ts,&tr,&tn);

printf("\nOriginal Array of Structure before Sorting\n\n");

title();

print(e,SIZE);

printTotals(ts,tr,tn);


//sort the struct of array and print the sorted struct of array

sort(e,SIZE);

printf("Array of Structure after Sorting\n\n");

title();

print(e,SIZE);

printTotals(ts,tr,tn);


//**************Section for text files*****************

printf("\n\n");

printf("From Save Text file\n\n");

title();


//save the struct of array to a text file

savetext(e,SIZE);


//retrieve the text file and print the data

gettext(e,SIZE);

print(e,SIZE);

printTotals(ts,tr,tn);


//**************Section for binary files*****************

printf("\n\n");

printf("From Save Binary file\n\n");

title();


//save struct of array to a binary file

savebn(e,SIZE);


//retrieve data from binary file and print the data

getbn(e,SIZE);

print(e,SIZE);

printTotals(ts,tr,tn);


return 0;

}



void load (struct Employee e[], int n)

{

    for (int i = 0; i

        {

        printf("Enter the employee's name: ");

        scanf("%s", e[i].name);

        printf("Enter Salary: ");

        scanf("%f", &e[i].sal);

        e[i].rate = 0.0f;

        }

//write the codes to let user enter employee¡¯s name and salary

}



void ARate(struct Employee e[], int n)


{

    for (int i = 0; i

        {

            if (e[i].sal > 0 && e[i].sal

                {

                    e[i].rate = 7.0f;

                }

            else if (e[i].sal >= 30000 && e[i].sal <= 40000)

            {

                e[i].rate = 5.5f;

            }

            else

            {

                e[i].rate = 4.0f;

            }

        }

//assign each employee rate to the struct¡¯s rate data member here by checking salary range

//for example

//        if(e[i].sal >= 0.00 && e[i].sal

// e[i].rate = 7.00;

}



void calcRaise(struct Employee e[], int n)

{

    for (int i = 0; i

        {

            e[i].raise = (e[i].sal*e[i].rate)/100;

            e[i].newSal = e[i].sal + e[i].raise;

        }

//calculate the raise and new salary for each employee and store into the

//struct¡¯s raise and new salary data members

// raise = salary * rate/100

// new salary = salary + raise

}



void sort(struct Employee e[], int n)

{

    for (int i = 1; i

    {

       for (int j = 0; j

        {

        if (strcmp(e[j].name, e[j + 1].name) > 0)

            {

            struct Employee temp = e[j];

            e[j] = e[j + 1];

            e[j + 1] = temp;

            }

        }

    }

//sort the struct in ascending order

}




void total(struct Employee e[], int n,float *ts,float *tr,float *tn )


{

    for (int i = 0; i

        {

            *ts = *ts +e[i].sal;

            *tr = *tr+ e[i].raise;

            *tn = *tn + e[i].newSal;

        }

//ts = total salary(sum the salaries of all 6 employees)

//tr = total raise(sum the raises of all 6 employees)

//tn = total new salary(sum of new salaries of all 6 employees)


}


void title()

{

printf("\t\t\tCalculation of Salary Raises\n\n");


printf("Employee\t");

printf("Salary\t\t");

printf("Rate %\t");

printf("  Raise\t\t");

printf("New Salary\n\n");

}



void print(struct Employee e[], int n)

{

int i;


for(i = 0; i

{

printf("%s\t\t", e[i].name); printf(".2f\t", e[i].sal); printf("%4.2f\t", e[i].rate);

printf("%8.2f\t", e[i].raise); printf("%8.2f\t\n", e[i].newSal);

}

}



void printTotals(float ts,float tr, float tn)

{

printf("Total\t\t"); printf(".2f\t\t", ts); printf("%8.2f", tr); printf(".2f\n\n", tn);

}



void savetext(struct Employee e[], int n)

{

    FILE *outfile;

    outfile = fopen ("employees.txt", "w");

    if (outfile == NULL)

        {

            fprintf (stderr, "\nError opened file\n");

            exit (1);

        }

    for (int i = 0; i

        {

            fwrite (&e[i], sizeof(struct Employee), 1, outfile);

        }

    fclose (outfile);

//save the struct to text file employees.txt

}



void gettext(struct Employee e[], int n)

{

    FILE *infile;

    struct Employee input;

    infile = fopen ("employees.txt", "r");

    if (infile == NULL)

    {

        fprintf(stderr, "\nError opening file\n");

        exit (1);

    }

    for (int i = 0; i

    {

        while(fread(&input, sizeof(struct Employee), 1, infile))

        e[i] = input;

    }

//retrieve the data from employees.txt file

}



void savebn(struct Employee e[], int n)

{

    FILE *outfile;

    outfile = fopen ("employees.bin", "wb");

    if (outfile == NULL)

    {

        fprintf(stderr, "\nError opend file\n");

        exit (1);

    }

    for (int i = 0; i

        {

        fwrite (&e[i], sizeof(struct Employee), 1, outfile);

        }

    fclose (outfile);

//save the struct to a binary file employees.bin

}



void getbn(struct Employee e[], int n)

{

    FILE *infile;

    struct Employee input;

    infile = fopen ("employees.bin", "rb");

    if (infile == NULL)

    {

        fprintf(stderr, "\nError opening file\n");

        exit (1);

    }

    for (int i = 0; i

        {

            while(fread(&input, sizeof(struct Employee), 1, infile))

            e[i] = input;

        }

    fclose (infile);

//retrieve the data from the employees.bin file

}



void savetext함수 이상이 있나요?


왜 파일 생성되는거 보면 글자가 다 깨져서 나올까용?