第13周编程作业(dheuiwbf)

第13周编程作业

1.计算某个正整数平方根,并按要求输出(20分)

题目内容:

输入一个正整数。计算其平方根(用sqrt函数),并将结果按取1~6位小数分六行显示出来。

输入格式:

输入一个正整数。

输出格式:

计算结果分6行显示,小数位数依次取1~6位。

输入样例:

2

输出样例:

1.4

1.41

1.414

1.4142

1.41421

1.414214

//计算某个正整数平方根,并按要求输出
#include<iostream>
using namespace std;
#include<cmath>
#include<fstream>
class test
{
public:
	void f()
	{
		int a;
		cin >> a;
		double b = sqrt(a);
		for (int i = 1; i < 7; i++)
		{
			cout.setf(ios::fixed);
			cout.precision(i);
			cout << b << endl;
		}
	}
};
int main()
{
	test a;
	a.f();
	return 0;
}

2.读取文件,添加行号显示(20分)

题目内容:

输入5行信息,生成文件A.txt。然后再次打开该文件,为每一行前面加一个行号后显示在屏幕上,行号占据4个字符宽,行号左对齐显示。

输入格式:

5行字符,每行字符中可以有空格,长度小于80

输出格式:

行号占据4个字符宽,行号左对齐显示。

输入样例:

AB

CD

EF

UV

XY

输出样例:

1 AB

2 CD

3 EF

4 UV

5 XY

//读取文件,添加行号显示
#include<iostream>
using namespace std;
#include<fstream>
class test
{
public:
	void f()
	{
		fstream out;
		out.open("A.txt", ios::out);
		char s[6][20];
		for (int i = 0; i < 5; i++)
		{
			cin.getline(s[i], 100);
			out << s << endl;
		}
		out.close();
		fstream in;
		in.open("A.txt", ios::out);
		for (int i = 0; i < 5; i++)
		{
			in << i + 1 << "   ";
			in << s[i] << endl;
		}
	}
};
int main()
{
	//test a;
	//a.f();
	char s[6][20];
	for (int i = 0; i < 5; i++)
	{
		cin.getline(s[i], 100);
	}
	for (int i = 0; i < 5; i++)
	{
		cout<< i + 1 << "   ";
		cout<< s[i] << endl;
	}
	return 0;
}

3.读写文件并转换字符(20分)

题目内容:

编写一个程序,从键盘输入一行字符(可包含各种字符,遇回车符结束),写入到文件a1.txt中;再从a1.txt中读出文件内容,将其中的小写字母转换成大写字母后显示在屏幕上。

提示:输入带空格的字符串,用cin.getline()。

输入格式:

一个字符串,中间可能有大写或小写字母、空格、数字以及其他标点符号。长度小于80。

输出格式:

输出同输入内容相同的字符串,其中小写字母变成大写字母。

输入样例:

We are students from Xi’an Jiaotong University.

输出样例:

WE ARE STUDENTS FROM XI’AN JIAOTONG UNIVERSITY.

注:提交时,注释文件操作语句。

//读写文件并转换字符
#include<iostream>
using namespace std;
#include<fstream>
void convert(char* p)
{
	while (*p)
	{
		if (*p >= 'a' && *p <= 'z')
			*p = *p + 'A' - 'a';
		p++;
	}
}
class test
{
public:
	void f()
	{
		fstream out;
		out.open("A.txt", ios::out);//打开文件,输出方式
		char s[90], ss[90];
		cin.getline(s, 81);
		out << s << endl;//输出到文件
		out.close();
		out.clear();
		out.open("A.txt", ios::in);//打开文件,从文件输入
		out.getline(ss, 81);//从文件读入到ss中
		convert(ss);//转换大小写
		cout << ss << endl;
		
	}
};
int main()
{
	//test a;
	//a.f();
	char s[90], ss[90];
	cin.getline(s, 81);
	convert(s);
	cout << s << endl;
	return 0;
}

4.读文件中的数字,算平均值(20分)

题目内容:

输入n个数字(实数),将他们写入文件out1.txt,数字之间用空格分开。然后再次打开该文件,读出全部数字,计算他们平均值并输出在屏幕上。

输入格式:

第一行输入数字个数n,以后的n行每行输入一个实数。

输出格式:

输出n个数平均值。格式:Avg=平均值

输入样例:

5

1.0

2.0

3.0

4.0

5.0

输出样例:

Avg=3

//读文件中的数字,算平均值
#include<iostream>
using namespace std;
#include<fstream>
#include<vector>
class test
{
public:
	void f()//练习版
	{
		fstream out;
		out.open("out1.txt", ios::out);//打开文件,输出方式
		int n;
		double a;
		double avg = 0;
		cin >> n;
		vector<double> array;//用vectro容器写吧
		for (int i = 0; i < n; i++)
		{
			cin >> a;
			array.push_back(a);	
;		}			
		for (vector<double>::iterator it = array.begin(); it < array.end(); ++it)
			out << *it << " ";
		out.clear();//指针归位
		out.close();
		out.open("out1.txt", ios::in);
		for (int i = 0; i < n; i++)
		{
			out >> a;
			avg += a / n * 1.0;
		}
		cout<<"Avg="<<avg<<endl;
	}
	void g()//提交版
	{
		int n;
		double a;
		double avg = 0;
		cin >> n;
		vector<double> array;//用vectro容器写吧
		for (int i = 0; i < n; i++)
		{
			cin >> a;
			array.push_back(a);
		}
		for (vector<double>::iterator it = array.begin(); it < array.end(); ++it)
		{
			avg += *it / n * 1.0;
		}
		cout << "Avg=" << avg << endl;
	}
};
int main()
{
	test a;
	a.g();
	return 0;
}

5.读文件中的字符并排序输出(20分)

题目内容:

输入n个字符写入文本文件A.txt,字符间用空格分开。在打开该文件,读取所有字符并排序后,按从小到大顺序写入B.txt(字符间用空格分开),同时将文件B的内容显示在屏幕上。

输入格式:

第一行输入数字个数n(100>n>=1),第2行输入n个字符(以空格分开)。

输出格式:

按从小到大顺序输出n个字符(以空格分开)

输入样例:

5

Z Y X A C

输出样例:

A C X Y Z

//读文件中的字符并排序输出
#include<iostream>
using namespace std;
#include<fstream>
#include<vector>
class test
{
public:
	void f()//练习版
	{
		char s[20];
		int n;
		cin >> n;
		fstream out("a5.txt", ios::out);
		for (int i = 0; i < n; i++)
		{
			cin >> s[i];
		}
		sort(s, n);
		for (int i = 0; i < n; i++)
			out << s[i] << " ";

	}
	void g()//提交版
	{
		char s[100];
		int n;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			cin >> s[i];
		}
		sort(s, n);
		for (int i = 0; i < n-1; i++)
			cout << s[i] << " ";
		cout << s[n - 1] << endl;		
	}
	void sort(char* p,int n)//冒泡排序
	{
		for(int i=0;i<n-1;i++)
			for (int j = 0; j < n - 1 - i; j++)
			{
				if (p[j] > p[j + 1])
				{
					char temp = p[j];
					p[j] = p[j + 1];
					p[j + 1] = temp;
				}
			}
	}
};
int main()
{
	test a;
	a.g();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值