Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdio_ext.h>
- #include <stdlib.h>
- #include <string.h>
- #define SIZE 2
- struct Person
- {
- char name[16], dep[16];
- float cyi, ra, rp, npa, tyi, tra, tpa;
- };
- void load(struct Person s[], int n)
- {
- int i;
- for (i = 0; i < n; i++)
- {
- printf("Enter your name ");
- fgets(s[i].name, 16, stdin);
- printf("Enter your department ");
- fgets(s[i].dep, 16, stdin);
- printf("Enter your current yearly income $");
- scanf("%f", &s[i].cyi);
- printf("Enter your raise percentage ");
- scanf("%f", &s[i].rp);
- s[i].ra = (s[i].cyi * s[i].rp) / (float)100;
- s[i].npa = (s[i].cyi) + (s[i].ra);
- printf("\n");
- __fpurge(stdin);
- //fflush(stdin);
- }
- }
- void sort(struct Person s[], int n)
- {
- int i, j;
- struct Person t;
- for (i = 0; i < n - 1; i++)
- for (j = 0; j < n - 1; j++)
- if (strcmp(s[j].name, s[j + 1].name) > 0)
- {
- t = s[j];
- s[j] = s[j + 1];
- s[j + 1] = t;
- }
- }
- void print(struct Person s[], int n)
- {
- int i;
- printf("\n\n");
- for (i = 0; i < n; i++)
- {
- printf("%s in department %s\n", s[i].name, s[i].dep);
- printf("The current yearly income is $%0.2f the raise percentage is %0.2f%%\n", s[i].cyi, s[i].rp);
- printf("The raise amount is $%0.2f, the new pay amount is $%0.2f\n\n", s[i].ra, s[i].npa);
- }
- }
- void calc(struct Person s[], int n)
- {
- int i;
- float tyi = 0, tra = 0, tpa = 0;
- for (i = 0; i < n; i++)
- {
- tyi += s[i].cyi;
- tra += s[i].ra;
- tpa += s[i].npa;
- }
- printf("The total current yearly income is $%0.2f\n", tyi);
- printf("The total raise amount is $%0.2f\n", tra);
- printf("The total new pay amount is $%0.2f\n", tpa);
- }
- void savetext(struct Person s[], int n)
- {
- int i;
- FILE *f;
- f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.txt", "w");
- for (i = 0; i < n; i++)
- {
- fprintf(f, "%s\n", s[i].name);
- fprintf(f, "%s\n", s[i].dep);
- fprintf(f, "%f %f %f\n", s[i].cyi, s[i].rp, s[i].npa);
- }
- fclose(f);
- }
- void retrievetext(struct Person s[], int n)
- {
- int i;
- FILE *f;
- f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.txt", "r");
- for (i = 0; i < n; i++);
- {
- fgets(s[i].name, sizeof(s[i].name), f);
- fgets(s[i].dep, sizeof(s[i].dep), f);
- fscanf(f, "%f%f%f\n", &s[i].cyi, &s[i].rp, &s[i].npa);
- }
- fclose(f);
- }
- void savebin(struct Person s[], int n)
- {
- FILE *f;
- f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.bin", "wb");
- fwrite(&s, sizeof(s[0]), n, f);
- fclose(f);
- }
- void retrievebin(struct Person s[], int n)
- {
- FILE *f;
- f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.bin", "rb");
- fread(&s, sizeof(s[0]), n, f);
- fclose(f);
- }
- int main()
- {
- struct Person s[SIZE];
- load(s, SIZE);
- sort(s, SIZE);
- print(s, SIZE);
- calc(s, SIZE);
- savetext(s, SIZE);
- retrievetext(s, SIZE);
- printf("\nAfter the text file is retrieved\n");
- print(s, SIZE);
- savebin(s, SIZE);
- retrievebin(s, SIZE);
- printf("\nAfter the binary file is retrieved\n");
- print(s, SIZE);
- system("PAUSE");
- return 0;
- }
- /* Output */
- /*
- Enter your name Joe
- Enter your department Sales
- Enter your current yearly income $1000
- Enter your raise percentage 10
- Enter your name Alex
- Enter your department IT
- Enter your current yearly income $2000
- Enter your raise percentage 12
- Alex
- in department IT
- The current yearly income is $2000.00 the raise percentage is 12.00%
- The raise amount is $240.00, the new pay amount is $2240.00
- Joe
- in department Sales
- The current yearly income is $1000.00 the raise percentage is 10.00%
- The raise amount is $100.00, the new pay amount is $1100.00
- The total current yearly income is $3000.00
- The total raise amount is $340.00
- The total new pay amount is $3340.00
- After the text file is retrieved
- Alex
- in department IT
- The current yearly income is $2000.00 the raise percentage is 12.00%
- The raise amount is $240.00, the new pay amount is $2240.00
- Joe
- in department Sales
- The current yearly income is $1000.00 the raise percentage is 10.00%
- The raise amount is $100.00, the new pay amount is $1100.00
- After the binary file is retrieved
- Alex
- in department IT
- The current yearly income is $2000.00 the raise percentage is 12.00%
- The raise amount is $240.00, the new pay amount is $2240.00
- Joe
- in department Sales
- The current yearly income is $1000.00 the raise percentage is 10.00%
- The raise amount is $100.00, the new pay amount is $1100.00
- //Cat Output
- $ cat G\:\\College\\CS\ 36\\Projects\\Final\ Program\\FinalHomework\\info.txt
- Alex
- IT
- 2000.000000 12.000000 2240.000000
- Joe
- Sales
- 1000.000000 10.000000 1100.000000
- */
Add Comment
Please, Sign In to add comment