C++ 文件题(编程)


一、编写程序,创建文件mul.txt,其内容为九九乘法表,其格式为:

      1     2    3    4    5    6    7    8     9
1    1    2    3    4    5    6    7    8     9
2    2    4    6    8    10  12  14  16  18
3    .........    
4    .........
5    .........
6    .........
7    .........
8    .........
9    9    18   27   36   45   54  63  72  81
要求通过循环自动生成该表,其中每个数据占4个字符位置,右对齐。

实现代码:

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
int main(void){
	int i,j,n,count;
	ofstream out;
	out.open("mul.txt");//打开mul.txt文件 
	if(!out){//判读是否打开 
		cerr<<"can't open the file"<<endl;
		return -1; 
	}
	out<<"  ";//目的是为了对齐
	for(i=1;i<10;i++)//输入到文件的第一层  1 2 3 .....9 
		out<<right<<setw(7)<<i; 
	out<<endl; 
	for(i=1;i<10;i++){
		out<<i;//自第一行后,每行前的 1 2 3.....9 
		for(j=1;j<10;j++){//控制域宽 
		 n=i*j;
		 count=0;
		 while(n){
		 	n=n/10;
		 	count++;
		 }
		 out<<right<<setw(8-count)<<i*j;//计算口诀表 
		}
		out<<endl;
	}
	out.close();
}

二、用编辑器(记事本等)产生一个文本文件data.txt,其内容为若干实数,数据之间以空白字符分割。编程从该文件中读入这些实数,求出这些实数的平均值,在程序中创建并产生一个文本文件result.txt,内容为data.txt中的全体实数,每行5个数,最后一行是求出的平均值。

实现代码:

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<fstream>
using namespace std;
int main(void){
	srand((unsigned)time(NULL));//设置随机种子,目的是每次生成的随机数不同 
	ofstream out1;
	out1.open("data.txt");//打开data.txt文件 
	if(!out1){//判读是否打开 
		cerr<<"can not open the file of data.txt!\n";
		return -1;
	}
	int i;
	for(i=0;i<1000;i++){//随机生成1000个100以内的随机数,输入到data中 
		out1<<rand()%100<<" ";
	}
	out1.close();//关闭文件对象 
	ofstream out2; 
	ifstream in1;
	int a[1000],sum=0;
	out2.open("result.txt");//打开result.txt 
	if(!out2){//判读是否打开 
		cerr<<"can not open the file of result.txt!\n";
		return -1;
	}
	in1.open("data.txt");// 打开data.txt
	if(!in1){//判读是否打开 
		cerr<<"can not open the file of data.txt!\n";
		return -1;
	} 
    for(i=0;i<1000;i++){//把1000个数从data流入result 
    	in1>>a[i];
    	out2<<a[i]<<" ";
    	if((i+1)%5==0)//五个数据一行 
    	 out2<<endl;
    	sum+=a[i];//求和 
	}
	out2<<sum/1000;//求平均 
	
	return 0;
} 

三、定义一个学生类,然后根据学生类创建一个对象,接着将该对象的数据成员的值输出到文件中,并将该数据读入到内存以检查文件的读写是否有误。

实现代码:

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

class student{
	   char name[20];
	   char num[20];
	   int age;
	   bool initialized;//表示学生数据是否被成功读入  
	public:
	   student( ){
	   	  initialized=false; 
	   } 
	   bool const data_is_ok(){
	   	  return initialized;
	   }
	   friend istream &operator>>(istream &in,student &x){
	   	  if(&in==&cin)//从键盘输入时给出提示
		        cout<<"请输入学号,姓名,年龄(以学号为'E'结束):\n";
		  in>>setw(11)>>x.num;//读入学号 
		  if(in.eof()||x.num[0]=='E'){
		   	    x.initialized=false;
			    return in;
		   } 
		   in>>setw(9)>>x.name;//读入姓名
		   in>>setw(9)>>x.age;//读入年龄
		   x.initialized=true;
		   return in; 
	   }
	   friend ostream &operator<<(ostream &out,const student &x){
	   	   out<<x.num<<' ';//输出学号
		   out<<x.name<<' ';//输出姓名
		   out<<x.age<<endl;//输出年龄
		   return out; 
	   }
};
int main(void){
	ofstream out;
	out.open("student.txt");
	if(!out){//判断是否打开 
		cerr<<"can not open the file!\n";
		return -1;
	}
	student s;
	cin>>s;
	while(s.data_is_ok()){//判断输入是否结束
	     out<<s;
		 cin>>s; 
    }
    out.close();
	
	return 0;
}

 

实验目的: 通过编写C++程序建文mul.txt,生成九九乘法表,并对实验结果进行分析和总结,加深对文件处理的理解和掌握。 实验过程: 1. 首先,我们需要用ofstream对象文件mul.txt,用于存储九九乘法表的结果。代码如下: ```c++ #include <iostream> #include <fstream> using namespace std; int main() { ofstream fout("mul.txt"); // 建文mul.txt if (!fout.is_open()) // 判断文件是否打开成功 { cout << "文件打开失败!" << endl; return 0; } // 将九九乘法表写入文件 for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { fout << j << "*" << i << "=" << i*j << "\t"; } fout << endl; } fout.close(); // 关闭文件 return 0; } ``` 2. 然后,我们打开mul.txt文件,查看生成的九九乘法表是否正确。代码如下: ```c++ #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin("mul.txt"); // 打开文件mul.txt if (!fin.is_open()) // 判断文件是否打开成功 { cout << "文件打开失败!" << endl; return 0; } char ch; while (fin.get(ch)) // 逐字符读取文件内容并输出到屏幕 { cout << ch; } fin.close(); // 关闭文件 return 0; } ``` 实验结论: 通过本次实验,我们成功地建了文件mul.txt,并且用C++程序生成了九九乘法表,最后通过打开文件查看,证明了程序的正确性。同时,本次实验还深化了对文件处理的理解和掌握,加强了对C++编程语言的应用能力。 实验总结: 本次实验通过实践操作,加深了对C++文件处理的理解和掌握,同时也提高了对C++编程语言的应用能力。在实验过程中,我们需要注意以下几点: 1. 建文时需要使用ofstream对象,并通过判断文件是否打开成功来确保文件建成功。 2. 写入文件时需要使用fout对象的输出函数,可以根据需求选择使用<<运算符或者write()函数。 3. 关闭文件时需要使用fout.close()函数,以释放系统资源。 4. 打开文件时需要使用ifstream对象,并通过判断文件是否打开成功来确保文件打开成功。 5. 读取文件时可以使用fin对象的get()函数逐字符读取文件内容,也可以使用getline()函数逐行读取文件内容。 6. 关闭文件时需要使用fin.close()函数,以释放系统资源。 在实验中,我们还需要注意代码的规范性和可读性,尽量遵循编程规范和注重代码格式的美观性。同时,我们也需要注重实验总结和实验报告的撰写,以便更好地总结和分享经验。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值