1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | #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("Original 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< n; 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< n; i++) { if (e[i].sal > 0 && e[i].sal < 30000) { 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 < 30000.00) // e[i].rate = 7.00; } void calcRaise(struct Employee e[], int n) { for (int i = 0; i < n; 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 < n; i++) { for (int j = 0; j < n - i; 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 < n; 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 < n; 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 opend file\n"); exit (1); } for (int i = 0; i< n; 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< n; 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< n; 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< n; i++) { while(fread(&input, sizeof(struct Employee), 1, infile)) e[i] = input; } fclose (infile); //retrieve the data from the employees.bin file } | cs |
savetext 함수 보면 employees.txt 여기에 저장하는거잖아
그런데 글자가 다깨져서 나오네
왜일까?
fprintf ㄱㄱ
???????????