Text: |
ФІО = Чтение с файла числовой информации + сортировка + рандом
Запитання:#include <windows.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <time.h>
#define PROGRAM_CONSOLE_IN_MODE
//#define PROGRAM_RANDOM_MODE
//#define PROGRAM_FILE_IN_MODE
//#define DEBUGMODE
using namespace std;
void swap(double *a, double *b)
{
double temp = *a;
*a = *b;
*b = temp;
}
void PrintArray(double *a, int length)
{
for (int i = 0 ; i < length ; i++)
{
cout<<*(a+i)<<"\t";
}
cout<<endl;
}
void SortArray(int count_, double *mas)
{
for (int i = 0 ; i < count_ ; i++)
for (int j = 0 ; j < i ; j++)
{
if ( *(mas+i) < *(mas+j))
{
swap(mas+i,mas+j);
#ifdef DEBUGMODE
cout<<"[DEBUG] ";
PrintArray(mas,count_);
#endif
}
}
}
int main()
{
srand(time(NULL));
int count;
#ifdef PROGRAM_FILE_IN_MODE
ifstream fin;
fin.open("in.txt");
if (fin.is_open())
{
cout<<"File opened!"<<endl;
}
else
{
cout<<"File not found!"<<endl;
system("pause");
return 0;
}
fin>>count;
#endif
#ifdef PROGRAM_CONSOLE_IN_MODE
cout << "Enter array length : ";
cin >> count;
if (count <=1)
{
cout<<"Invalid array length!";
system("pause");
return 0;
}
#endif
#ifdef PROGRAM_RANDOM_MODE
cout << "Enter array length : ";
cin >> count;
#endif
double *mas = new double[count];
#ifdef PROGRAM_FILE_IN_MODE
for (int i = 0 ; i < count ; i++)
{
fin>>*(mas+i);
}
fin.close();
#endif
#ifdef PROGRAM_CONSOLE_IN_MODE
for (int i = 0 ; i < count ; i++)
{
cout<<"Enter [ "<<i<<" ] = ";
cin>>*(mas+i);
}
#endif
#ifdef PROGRAM_RANDOM_MODE
for (int i = 0 ; i < count ; i++)
{
*(mas+i)=rand()%201-100;
}
#endif
cout<<endl;
cout<<endl;
for (int i = 0 ; i < count ; i++)
{
cout<<*(mas+i)<<"\t";
}
cout<<endl;
SortArray(count, mas);
PrintArray(mas, count);
ofstream fout;
fout.open("textt.txt");
for(int i=0;i<count;i++)
{
fout<<*(mas+i)<<' ';
}
fout.close();
system("pause");
return 0;
}
====================================
|