-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
132 lines (122 loc) · 3.61 KB
/
Copy pathmain.c
File metadata and controls
132 lines (122 loc) · 3.61 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct staff {
char surname[30], name[30], patronymic[30];
int salary[12];
};
void display(struct staff *person, int records)
{
int i = 0, j;
for (i = 0; i < records; i++) {
printf("\n%s%s%s", person[i].surname, person[i].name,
person[i].patronymic);
for (j = 0; j < 12; j++)
printf("\nmonth[%d]=%d", j + 1, person[i].salary[j]);
}
}
char *delete_space(char *string)
{
int i = 0, j, k = 0;
while (string[i++] == ' ' && string[i]);
if (i < 30) {
for (j = i - 1; j < 30; j++)
string[k++] = string[j];
}
return string;
}
char *check_input(struct staff *person, int records)
{
char *string = (char *) calloc(30, sizeof(char));
do {
fgets(string, 30, stdin);
if (strlen(string) == 29)
while (getchar() != '\n');
}
while (string[0] == '\n');
return string;
}
void input(struct staff *person, char *string, int records)
{
int i = 0;
puts("enter surname ");
string = check_input(person, records);
strcpy(person[records].surname, delete_space(string));
puts("enter name ");
string = check_input(person, records);
strcpy(person[records].name, delete_space(string));
puts("enter patronymic ");
string = check_input(person, records);
strcpy(person[records].patronymic, delete_space(string));
printf
("enter salary per month\nATTENCION:maximum length of salary cannot exceed 14 symbols\n");
for (i = 0; i < 12; i++) {
printf("\n month[%d]=", i + 1);
string = check_input(person, records);
person[records].salary[i] = atoi(string);
}
}
void sorting(struct staff *person, int records)
{
int i, j;
struct staff exchange;
for (i = 0; i < records - 1; i++) {
for (j = i + 1; j < records; j++) {
if (strcmp((person + i)->surname, (person + j)->surname) > 0) {
exchange = person[i];
person[i] = person[j];
person[j] = exchange;
}
if ((strcmp((person + i)->surname, (person + j)->surname)) ==
0) {
int compare_name =
strcmp((person + i)->name, (person + j)->name),
compare_patronymic =
strcmp((person + i)->patronymic,
(person + j)->patronymic);
if ((compare_name > 0) || (compare_name == 0
&& compare_patronymic > 0)) {
exchange = person[i];
person[i] = person[j];
person[j] = exchange;
}
}
}
}
}
void info(void)
{
printf("\nEnter fullname and salary per month");
}
int main( int argc, char *argv[])
{
if(argc>1)
{
if(!strcmp(argv[1],"-h"))
info();
}
int records = 0, i;
char *string;
struct staff *person;
do {
string = (char *) calloc(30, sizeof(char));
person = (struct staff *) calloc(25, sizeof(struct staff));
}
while (!person && !string);
puts("ATTENCION: maximum length of record cannot exceed 29 symbols");
while (records < 25) {
input(person, string, records);
records++;
puts("enter stop to quit");
fgets(string, 5, stdin);
if (strcmp(string, "stop") == 0)
break;
}
if (records == 24)
puts("you can enter only 25 persons");
else
person = (struct staff *) realloc(person, records);
sorting(person, records);
display(person, records);
return 0;
}