C++ //习题13.4 建立两个磁盘文件f1.dat和f2.dat,编程序实现以下工作:(1) 从键盘输入20个整数,分别存放在两个磁盘文件中(每个文件中放10个整数);(2) 从f1.dat读入

2024-01-03 11:29:25

C++程序设计 (第三版) 谭浩强 习题13.4

习题13.4 建立两个磁盘文件f1.dat和f2.dat,编程序实现以下工作:

(1) 从键盘输入20个整数,分别存放在两个磁盘文件中(每个文件中放10个整数);

(2) 从f1.dat读入10个数,然后存到f2.dat文件原有数据的后面;

(3) 从f2.dat中读入20个整数,对它们按从小到大的顺序存放到f2.dat(不保留原来的数据)。

IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。

?

代码块:
说明:文件f1.txt,f2.txt都在程序源文件同目录下。
#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;
}
结果显示如下:

在这里插入图片描述

文章来源:https://blog.csdn.net/navicheung/article/details/135342928
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。