?
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int N = 20;
void initialNum(int **num, int n){
*num = new int[n];
}
void freeNum(int **num){
delete[] (*num);
}
void inputNum(int *num, int n){
cout<<"Enter "<<n<<" Numbers: ";
for(int i = 0; i < n; i++){
cin>>num[i];
}
cout<<endl;
}
void inputFile(char *name1, char *name2, int *num, int n){
ofstream outfile1(name1, ios::out | ios::binary);
if(!outfile1){
cerr<<"Open File "<<name1<<" Error!"<<endl;
system("pause");
exit(1);
}
ofstream outfile2(name2, ios::out | ios::binary);
if(!outfile2){
cerr<<"Open File "<<name2<<" Error!"<<endl;
system("pause");
exit(1);
}
for(int i = 0; i < n; i++){
if(i < n / 2){
outfile1<<num[i]<<" ";
}
else{
outfile2<<num[i]<<" ";
}
}
outfile1.close();
outfile2.close();
}
void outputFile(char *name, int *num, int n){
ifstream infile(name, ios::in | ios::binary);
if(!infile){
cerr<<"Open File "<<name<<" Error!"<<endl;
system("pause");
exit(1);
}
for(int i = 0; i < n; i++){
infile>>num[i];
cout<<num[i]<<" ";
}
cout<<endl;
infile.close();
}
void transFile(char *name1, char *name2, int n){
ifstream infile(name1, ios::in | ios::binary);
if(!infile){
cerr<<"Open File "<<name1<<" Error!"<<endl;
system("pause");
exit(1);
}
ofstream outfile(name2, ios::out | ios::binary);
if(!outfile){
cerr<<"Open File "<<name2<<" Error!"<<endl;
system("pause");
exit(1);
}
outfile.seekp(0, ios::end);
int *temp = new int[n/2];
for(int i = 0; i < n / 2; i++){
infile>>temp[i];
outfile<<temp[i]<<" ";
}
cout<<endl;
delete[] temp;
infile.close();
outfile.close();
}
void sortNum(char *name, int *num, int n){
ifstream infile(name, ios::in | ios::binary);
if(!infile){
cerr<<"Open File "<<name<<" Error!"<<endl;
system("pause");
exit(1);
}
for(int i = 0; i < n; i++){
infile>>num[i];
}
int temp;
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(num[i] > num[j]){
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
infile.close();
ofstream outfile(name, ios::out | ios::binary);
if(!outfile){
cerr<<"Open File "<<name<<" Error!"<<endl;
system("pause");
exit(1);
}
for(int i = 0; i < n; i++){
outfile<<num[i]<<" ";
}
outfile.close();
}
int main(){
int *num = NULL;
char *name1 = "f1.dat";
char *name2 = "f2.dat";
initialNum(&num, N);
inputNum(num, N);
inputFile(name1, name2, num, N);
cout<<"File1: ";
outputFile(name1, num, N/2);
cout<<"File2: ";
outputFile(name2, num, N/2);
transFile(name1, name2, N);
cout<<"New File2: ";
outputFile(name2, num, N);
sortNum(name2, num, N);
cout<<"New File2 Sort: ";
outputFile(name2, num, N);
freeNum(&num);
system("pause");
return 0;
}