mirror of
https://github.com/Kaehvaman/OAIP.git
synced 2025-02-22 17:36:22 +04:00
finish task5
This commit is contained in:
parent
f2c98c712d
commit
b3cf77045e
2
lab19/lab19/in5.txt
Normal file
2
lab19/lab19/in5.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
10
|
||||||
|
13 16 5 7 8 9 11 1 2 4
|
@ -134,8 +134,10 @@
|
|||||||
<Text Include="data.txt" />
|
<Text Include="data.txt" />
|
||||||
<Text Include="in3.txt" />
|
<Text Include="in3.txt" />
|
||||||
<Text Include="in4.txt" />
|
<Text Include="in4.txt" />
|
||||||
|
<Text Include="in5.txt" />
|
||||||
<Text Include="out3.txt" />
|
<Text Include="out3.txt" />
|
||||||
<Text Include="out4.txt" />
|
<Text Include="out4.txt" />
|
||||||
|
<Text Include="out5.txt" />
|
||||||
<Text Include="tasks12.txt" />
|
<Text Include="tasks12.txt" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
@ -38,5 +38,11 @@
|
|||||||
<Text Include="out4.txt">
|
<Text Include="out4.txt">
|
||||||
<Filter>Файлы ресурсов</Filter>
|
<Filter>Файлы ресурсов</Filter>
|
||||||
</Text>
|
</Text>
|
||||||
|
<Text Include="in5.txt">
|
||||||
|
<Filter>Файлы ресурсов</Filter>
|
||||||
|
</Text>
|
||||||
|
<Text Include="out5.txt">
|
||||||
|
<Filter>Файлы ресурсов</Filter>
|
||||||
|
</Text>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -197,6 +197,96 @@ void task4() {
|
|||||||
free(pout);
|
free(pout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void swap(int* arr, int i, int j) {
|
||||||
|
int temp = arr[i];
|
||||||
|
arr[i] = arr[j];
|
||||||
|
arr[j] = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bubbleSort(int arr[], int n) {
|
||||||
|
for (int i = 0; i < n - 1; i++) {
|
||||||
|
|
||||||
|
// Last i elements are already in place, so the loop
|
||||||
|
// will only num n - i - 1 times
|
||||||
|
for (int j = 0; j < n - i - 1; j++) {
|
||||||
|
if (arr[j] > arr[j + 1])
|
||||||
|
swap(arr, j, j + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void task5() {
|
||||||
|
int* pdata;
|
||||||
|
int len = load(&pdata, "in5.txt");
|
||||||
|
|
||||||
|
int oddCount = 0;
|
||||||
|
int evenCount = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
if (pdata[i] % 2 == 0) {
|
||||||
|
evenCount++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
oddCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oddCount == 0 || evenCount == 0) {
|
||||||
|
puts("No odds or evens, exiting task5()");
|
||||||
|
free(pdata);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int* peven = (int*)malloc(sizeof(int) * (evenCount + 2));
|
||||||
|
if (peven == NULL) {
|
||||||
|
puts("Out of memory");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int* podd = (int*)malloc(sizeof(int) * (oddCount + 2));
|
||||||
|
if (podd == NULL) {
|
||||||
|
puts("Out of memory");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ieven = 0, iodd = 0;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
if (pdata[i] % 2 == 0) {
|
||||||
|
peven[ieven] = pdata[i];
|
||||||
|
ieven++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
podd[iodd] = pdata[i];
|
||||||
|
iodd++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bubbleSort(peven, evenCount);
|
||||||
|
bubbleSort(podd, oddCount);
|
||||||
|
|
||||||
|
FILE* file = fopen("out5.txt", "w");
|
||||||
|
if (file == NULL) {
|
||||||
|
puts("Failed to create file");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf_s(file, "%d\n", len);
|
||||||
|
|
||||||
|
for (int i = 0; i < evenCount; i++) {
|
||||||
|
fprintf_s(file, "%d ", peven[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = oddCount - 1; i >= 0; i--) {
|
||||||
|
fprintf_s(file, "%d ", podd[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
free(pdata);
|
||||||
|
free(peven);
|
||||||
|
free(podd);
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
//saveRandomArray();
|
//saveRandomArray();
|
||||||
|
|
||||||
@ -217,5 +307,7 @@ int main() {
|
|||||||
|
|
||||||
//task4();
|
//task4();
|
||||||
|
|
||||||
|
task5();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
2
lab19/lab19/out5.txt
Normal file
2
lab19/lab19/out5.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
10
|
||||||
|
2 4 8 16 13 11 9 7 5 1
|
Loading…
x
Reference in New Issue
Block a user