Добрый день всем.
C++ |
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
| // 7.5.cpp: ?????????? ????? ????? ??? ??????????? ??????????.
//
#include "iostream"
#include "time.h"
#include "conio.h"
#include <fstream>
using namespace std;
struct ZNAK
{
char family[15];
char name[15];
char znak[11];
int date[3];
};
bool chars(char* char1, char* char2)
{
for(int count=0;count<strlen(char1);count++)
if (char1[count] != char2[count]) return false; //????????? ???????.
return true;
}
void Search(ZNAK *pZNAK, char* family, char* name, int n)
{
bool ok = false;
for (int i = 0; i < n; ++i)
{
if (chars(pZNAK[i].family, family))
{
cout << "Family: " << pZNAK[i].family << endl;
cout << "name: " << pZNAK[i].name << endl;
cout << "ZNAK: " << pZNAK[i].znak << endl;
cout << "Date: " << pZNAK[i].date[0] << ":" << pZNAK[i].date[1] << ":" << pZNAK[i].date[2] << endl;
ok = true;
}
}
if (!ok)
cout << "Family ne naydena!!!!" << endl;
}
void Sort(ZNAK *pZNAK, int n)
{
// A - 65
// z - 122
ZNAK f;
for (int i = 0; i < n - 1; ++i)
{
for (int j = i; j < n; ++j)
{
bool h = true;
int q = 0;
while (h == true && q < 16)
{
if (pZNAK[j].date[2] < pZNAK[i].date[2])
{
h=false;
f = pZNAK[i];
pZNAK[i] = pZNAK[j];
pZNAK[j] = f;
}
else if (pZNAK[j].date[2] > pZNAK[i].date[2])
h = false;
++q;
}
}
}
for ( i = 0; i < n - 1; ++i)
{
for (int j = i; j < n; ++j)
{
bool h = true;
int q = 0;
while (h == true && q < 16)
{
if (pZNAK[j].date[1] < pZNAK[i].date[1])
{
h=false;
f = pZNAK[i];
pZNAK[i] = pZNAK[j];
pZNAK[j] = f;
}
else if (pZNAK[j].date[1] > pZNAK[i].date[1])
h = false;
++q;
}
}
}
for ( i = 0; i < n - 1; ++i)
{
for (int j = i; j < n; ++j)
{
bool h = true;
int q = 0;
while (h == true && q < 16)
{
if (pZNAK[j].date[0] < pZNAK[i].date[0])
{
h=false;
f = pZNAK[i];
pZNAK[i] = pZNAK[j];
pZNAK[j] = f;
}
else if (pZNAK[j].date[0] > pZNAK[i].date[0])
h = false;
++q;
}
}
}
}
int main()
{
ZNAK pZNAK[10];
const int n=8;
ifstream in("myfile.txt");
for (int i = 0; i < n; ++i)
{
in >> pZNAK[i].family;
in >> pZNAK[i].name;
in >> pZNAK[i].znak;
in >> pZNAK[i].date[0] >> pZNAK[i].date[1] >> pZNAK[i].date[2];
}
char family[15];
char name[15];
cout << "Vvedite family dly poiska: ";
cin >> family;
cout << "<<<<<SEARCH RESULT>>>>>" << endl;
Search(&pZNAK[0], &family[0], &name[0], n);
Sort(&pZNAK[0], n);
cout << "<<<<<SORT RESULT>>>>>" << endl;
for ( i = 0; i < n; ++i)
{
cout << "Family: " << pZNAK[i].family << endl;
cout << "ZNAK: " << pZNAK[i].znak << endl;
cout << "Date: " << pZNAK[i].date[0] << ":" << pZNAK[i].date[1] << ":" << pZNAK[i].date[2] << endl;
cout << "________________________________________________" << endl;
}
getch();
return 0;
} |
|
Программа сортирует не правильно. если делать сортировку по дате полностью( день месяц год), если же делать год, месяц, то все окей, или же просто год.
Подскажите как исправить сортировку, чтобы она работала нормально?
Добавлено через 7 часов 24 минуты
Решил сам, если кому надо замените весь void sort на :
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| {
// A - 65
// z - 122
int ololo;int ololo1; bool fs=true;int k=n-1;
while (fs)
{
for (int i = 0; i <k; i++)
{
ololo=pZNAK[i].date[2]*1000+pZNAK[i].date[1]*100+pZNAK[i].date[0];
ololo1=pZNAK[i+1].date[2]*1000+pZNAK[i+1].date[1]*100+pZNAK[i+1].date[0];
if(ololo<ololo1)
{
struct ZNAK f;
f = pZNAK[i];
pZNAK[i] = pZNAK[i+1];
pZNAK[i+1] = f;
fs=true;
}
}k--;
if (k<0)fs=false;
}
} |
|
P.S. задание. Описать структуру с именем ZNAK, содержащую следующие поля, фамилия, имя, знак зодиака, дата рождения.
Ввод с клавиатуры данных в массив, состоящий из восьми элементов типа ZNAK, записи должны быть упорядочены по датам рождения.