Загрузка данных


#include <iostream>
#include <fstream>
using namespace std;

int main() {
    // Создаём тестовый файл f.txt
    ofstream f("f.txt");
    f << "3 7 -1 5 -4 2 -8 9 -6 1 -2 4";
    f.close();

    // Читаем из f.txt, раскидываем по массивам
    int pos[100], neg[100];
    int posCount = 0, negCount = 0;

    ifstream fin("f.txt");
    int x;
    while (fin >> x) {
        if (x > 0) pos[posCount++] = x;
        else       neg[negCount++] = x;
    }
    fin.close();

    // Сохраняем отрицательные во вспомогательный файл h.txt
    ofstream h("h.txt");
    for (int i = 0; i < negCount; i++) h << neg[i] << " ";
    h.close();

    // ===== ЗАДАНИЕ А: чередуем +, -, +, -, ... =====
    ofstream gA("gA.txt");
    for (int i = 0; i < posCount; i++) {
        gA << pos[i] << " ";
        gA << neg[i] << " ";
    }
    gA.close();

    // ===== ЗАДАНИЕ Б: сначала все +, потом все - =====
    ofstream gB("gB.txt");
    for (int i = 0; i < posCount; i++) gB << pos[i] << " ";
    for (int i = 0; i < negCount; i++) gB << neg[i] << " ";
    gB.close();

    // ===== ЗАДАНИЕ В: два +, два -, два +, два -, ... =====
    ofstream gC("gC.txt");
    int pi = 0, ni = 0;
    while (pi < posCount || ni < negCount) {
        if (pi < posCount) gC << pos[pi++] << " ";
        if (pi < posCount) gC << pos[pi++] << " ";
        if (ni < negCount) gC << neg[ni++] << " ";
        if (ni < negCount) gC << neg[ni++] << " ";
    }
    gC.close();

    cout << "Готово! Проверь gA.txt, gB.txt, gC.txt" << endl;

    return 0;
}