Перевод целых чисел в бинарную систему счисления
Требуется создать и заполнить 15-ю числами 2 бинарных файла, а в 3 бинарный файл занести сумму четных чисел из этих 2 файлов. Помогите, пожалуйста.
- Не получается реализовать функцию
void findEvenSumBin();, для нахождения суммы. - В бинарных файлах находятся значения в шестнадцатеричной системе счисления, как изменить на двоичную?
#include <iostream>
#include <fstream>
using namespace std;
void createAndFillBin(int numbers_1, int numbers_2);
void findEvenSumBin(int numbers_1, int numbers_2);
int main() {
int numbers_1 = 0;
int numbers_2 = 0;
createAndFillBin(numbers_1, numbers_2);
findEvenSumBin(numbers_1, numbers_2);
return 0;
}
void createAndFillBin(int numbers_1, int numbers_2) {
//creating binary files and filling them with numbers from 0 to 30
ofstream file_1("file_1.bin", ios_base::binary);
ofstream file_2("file_2.bin", ios_base::binary);
srand(time(0));
for (int i = 0; i < 15; i++) {
numbers_1 = rand() % 31;
file_1.write((char*)&numbers_1, sizeof(numbers_1));
numbers_2 = rand() % 31;
file_2.write((char*)&numbers_2, sizeof(numbers_2));
}
file_1.close();
file_2.close();
}
void findEvenSumBin(int numbers_1, int numbers_2) {
//finding sum of even elements in 2 binary files, then writing sum into 3rd file
ifstream file_1("file_1.bin", ios_base::binary);
ifstream file_2("file_2.bin", ios_base::binary);
int sum = 0;
if (file_1.is_open() && file_2.is_open()) {
while (file_1 >> numbers_1)
if (numbers_1 % 2 == 0)
sum += numbers_1;
while (file_2 >> numbers_2)
if (numbers_2 % 2 == 0)
sum += numbers_2;
file_1.close();
file_2.close();
ofstream file_3("file_3.bin", ios_base::binary);
file_3.write((char*)&sum, sizeof(sum));
file_3.close();
cout << "All files was created! Check them in the folder." << endl;
}
else
cout << "Error with opening files." << endl;
}
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Вот, смотрите сами, что не так, как у вас.
#include <iostream>
#include <fstream>
using namespace std;
void createAndFillBin();
void findEvenSumBin();
int main()
{
srand(time(0));
createAndFillBin();
findEvenSumBin();
return 0;
}
void createAndFillBin()
{
//creating binary files and filling them with numbers from 0 to 30
ofstream file_1("file_1.bin", ios_base::binary);
ofstream file_2("file_2.bin", ios_base::binary);
for (int n, i = 0; i < 15; i++)
{
n = rand() % 31;
file_1.write((char*)&n, sizeof(n));
n = rand() % 31;
file_2.write((char*)&n, sizeof(n));
}
}
void findEvenSumBin()
{
//finding sum of even elements in 2 binary files, then writing sum into 3rd file
ifstream file_1("file_1.bin", ios_base::binary);
ifstream file_2("file_2.bin", ios_base::binary);
int sum = 0, n;
if (file_1.is_open() && file_2.is_open())
{
while(file_1.read((char*)&n, sizeof(n)))
if (n%2 == 0) sum += n;
while(file_2.read((char*)&n, sizeof(n)))
if (n%2 == 0) sum += n;
ofstream file_3("file_3.bin", ios_base::binary);
file_3.write((char*)&sum, sizeof(sum));
cout << "All files was created! Check them in the folder." << endl;
}
else
cout << "Error with opening files." << endl;
}
Или даже так:
void createAndFillBin(const char * fname);
int findEvenSumBin(const char * fname);
int main()
{
srand(time(0));
createAndFillBin("file_1.bin");
createAndFillBin("file_2.bin");
int sum = findEvenSumBin("file_1.bin") + findEvenSumBin("file_2.bin");
ofstream file_3("file_3.bin", ios_base::binary);
file_3.write((char*)&sum, sizeof(sum));
cout << "All files was created! Check them in the folder." << endl;
}
void createAndFillBin(const char * fname)
{
//creating binary files and filling them with numbers from 0 to 30
ofstream file(fname, ios_base::binary);
for (int n, i = 0; i < 15; i++)
{
n = rand() % 31;
file.write((char*)&n, sizeof(n));
}
}
int findEvenSumBin(const char * fname)
{
//finding sum of even elements in 2 binary files, then writing sum into 3rd file
ifstream file(fname, ios_base::binary);
int sum = 0, n;
if (file.is_open())
{
while(file.read((char*)&n, sizeof(n))) if (n%2 == 0) sum += n;
}
else
{
cout << "Error opening file." << endl;
abort();
}
return sum;
}