C++ 实验八

编程题1

题目描述
【问题描述】
编程序,对 k=1,2,3,…,14,15,按下式分别计算出 15 组(i,d,c):整数 i=2k-1;实数 d=kk+k-9.8;字符 c=‘H’+k。并通过使用运算符“<<”将这15组数据保存到当前目录下自定义的text型磁盘文件ft.txt之中。

【输入形式】

【输出形式】
文件内容要求:每组占一行,每行中的输出项用一个空格进行分割。

【样例输入】
   无
【样例输出】
文件内容格式如下:
1 -7.8 I
3 -3.8 J
5 2.2 K
7 10.2 L
#include <iostream>
#include <fstream>
using namespace std;
int main(){
	ofstream ft("C:\\Download\\ft.txt",ios::out);
	if(!ft){
		cout<<"不可以打开文件"<<endl;
		exit(1);
	}
	for(int i = 1;i<=15;i++){
		int a = 2*i-1;
		double b = i*i+i-9.8;
		char c = 'H'+i;
		ft<<a<<" "<<b<<" "<<c<<endl; 
	}
	ft.close();
	return 0;
}

编程题2

题目描述
【问题描述】
通过使用运算符“>>”和“<<”,对自定义的text型磁盘文件进行如下的指定操作。
(1)对i=1,2,3,…,15,计算d=ii+0.5,并将15个结果写出到自定义的text型磁盘文件f1.txt中(注意,f1文件中的数据从小到大有序);
(2)对i=1,2,3,…,10,计算d=10
i+0.5,并将10个结果写出到自定义的text型磁盘文件f2.txt中,而后再写出第11个数:357.9(注意,f2文件中的数据也从小到大有序);
(3)读出文件f1.txt以及f2.txt中的数据,用来形成text型磁盘文件f3.txt,但要求存放于f3.txt中的数据仍然是有序的(按照从小到大的顺序);

【输入形式】

【输出形式】
输出到f3.txt文件中(一行,数据间间隔为一个空格)

样例输入】
    无
【样例输出】
1.5 4.5 9.5 10.5 16.5...(以下略)
#include <iostream>
#include <fstream>
using namespace std;
int main(){
	double temp;
	double a[15],b[15],c[30];
	int i = 0;
	int j = 0;
	ofstream f1("C:\\Download\\f1.txt",ios::out);
	if(!f1){
		cout<<"error"<<endl;
		exit(1);
	}
	ofstream f2("C:\\Download\\f2.txt",ios::out);
	if(!f2){
		cout<<"error"<<endl;
		exit(1);
	}
	ofstream f3("C:\\Download\\f3.txt",ios::out);
	if(!f3){
		cout<<"error"<<endl;
		exit(1);
	}
	for(int i = 1;i<=15;i++){
		double d1 = i*i+0.5;
		f1<<d1<<" "; 
	}
	for(int i = 1;i<=10;i++){
		double d2 = 10*i+0.5;
		f2<<d2<<" ";
	}
	f2<<357.9;
	f1.close();
	f2.close();
	ifstream f11("C:\\Download\\f1.txt",ios::in);
	ifstream f22("C:\\Download\\f2.txt",ios::in);
	while(!f11.eof()){
		f11>>temp;
		a[i++] = temp;	
	}
	while(!f22.eof()){
		f22>>temp;
		b[j++] = temp;
	}
	int i1 = 0;
	int j1 = 0;
	int k = 0;
	while(i1<i&&j1<j){
		if(a[i1]<b[j1]){
			c[k++] = a[i1++];
		}
		else{
			c[k++] = b[j1++];
		}
	}
	while(i1<i){
		c[k++] = a[i1++];
	}
	while(j1<j){
		c[k++] = b[j1++];
	}
	f1.close();
	f2.close();
	for(int p = 0;p<k;p++){
		f3<<c[p]<<" ";
	}
	f3.close();
	ifstream f33("C:\\Download\\f3.txt",ios::in);
	while(!f33.eof()){
		f33>>temp;
		cout<<temp<<" ";
	}
	f33.close();
	return 0;
} 

编程题3

题目描述
【问题描述】
使用 getline 成员函数读入某个text文件(f5.txt)中的“一篇文章”(如可以是一个C++源程序,文件名由用户从键盘输入),而后将该文件的各空行以及各程序行中的注解均删除掉(认为该行中从第一个双撇符号“//”开始直至行末的所有符号为注解),并将删除注解后的结果行写出到另一个文件(f6.txt)中。

【输入形式】
逐行读入文件f5.txt的内容。

【样例输入】
文件f5.txt的内容如下:
//This is test
#include <iostream>

int main()
{

cout<<"Hello World"<<endl;
return 0;

}
【样例输出】
文件f6.txt的内容如下:
#include <iostream>
int main()
{
cout<<"Hello World"<<endl;
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
int main() {
	char x[5000];
	ifstream f5("C:\\Download\\f5.txt");
	ofstream f6("C:\\Download\\f6.txt");
	while (f5.getline(x, 5000))
	{
		if (x[0] == '\0' || x[0] == '/')
			continue;
		f6 << x;
		f6 << endl;
	}
	f5.close();
	f6.close();
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值