c++ 西安交通大学 mooc 第十三周基础练习&第十三周编程作业
做题记录
风影影,景色明明,淡淡云雾中,小鸟轻灵。
c++的文件操作已经好玩起来了,不过掌握好控制结构显得更为重要了。
我这也不做啥题目分析了,直接就题干-代码。
总结——留着自己看
1. 流是指从一个位置向另一个位置传输的一连串数据的集合。
2. 标准输入流一一从标准输入设备(键盘)流向程序的数据.,一般用cin流对象进行输入
3. “>>”,这个符号是输入ios类的符号重载函数。以回车和空格作为分隔符。就是说中间的空格符号无法获得。
4. 着重介绍:get 和 getline [cin对象所有]
5. Char c;
则:c = cin.get(); 与 cin.get(c)等价。
6. Char ch[80];
cin.get(ch,70,”|”); //以 | 结束取最多70个字符。
7. cin.getline()函数与cin.get()函数的第三种情况相似。但是getline会舍弃终止的字符。不用ignore函数丢掉了。
8. get与getline的不同之处,get函数会将指针移到终止字符处,getline会将指针移到终止字符之后。则下次继续读取时的位置不同。
9. 对文件的操作
10. Open函数open(filename<*>,openmode<ios::in(仅in) | ios::out(仅out) | ios::binary()二进制打开>)openmode默认为仅out。
11. >>提取符,<<输出符。跟键盘输入输出的效果是类似的。Ofstream(往文件里写),ifstream(往外输出)
12. 文本文件读写的函数与有些运算符是不能用于二进制文件的,因为大多数的二进制文件的编码方式特殊,非常有可能出现乱码。
第十三周基础练习
1.格式输出
题目内容:
编写程序,按下列格式显示信息:
#&&&&&&&1#
#&&&&&&10#
#&&&&&100#
#&&&&1000#
#&&&10000#
#&&100000#
#&1000000#
共7行,每行的数值是固定的,每行两端是“#”号,中间的“&”是填充字符,实际数字的位数小于域时自动填充。
输入:域宽、填充字符和对齐方式,其中对齐方式:1表示居左,0表示具有。
输出:题目说明的7行信息。
【提示】域宽:cout.width(k);填充:cout.fill(c);对齐:cout.setf(ios::left)。
样例1输入:
8 & 0
样例1输出:
#&&&&&&&1#
#&&&&&&10#
#&&&&&100#
#&&&&1000#
#&&&10000#
#&&100000#
#&1000000#
#include <iostream>
using namespace std;
int main()
{
int wid, meth,temp=1;
char a;
cin >> wid >> a >> meth;
if (meth == 1)
cout.setf(ios::left);
else
cout.setf(ios::right);
for (int i = 0; i < 7; i++)
{
cout << '#';
cout.width(wid);
cout.fill(a);
cout << temp << '#' << endl;
temp = temp * 10;
}
return 0;
}
2.文件版HelloWorld
#include <iostream>
//#include <fstream>
using namespace std;
int main()
{
//ofstream out;
//out.open("a1.txt");
//out << "Hello world!" <<endl;
cout << "Hello World." << endl;
//out.close();
return 0;
}
3.从文件中读一行文字
题目内容:
编写程序,从a1.txt中读取一行文字,显示到屏幕上(a1.txt文件用记事本创建,内容为一行
文字“Hello World.”,与程序在同一个文件夹下)。
输入:无
输出:Hello World.
【提示】
样例1输入:
样例1输出:
Hello World.
提交版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
cout << "Hello World." << endl;
/*ifstream in;
ofstream out;
char s[100];
out.open("a1.txt");
out << "Hello world." <<endl;
out.close();
in.open("a1.txt");
in.get(s,99);
cout << s;
in.close();*/
return 0;
}
测试版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
cout << "Hello World." << endl;
ifstream in;
ofstream out;
char s[100];
out.open("a1.txt");
out << "Hello world." <<endl;
out.close();
in.open("a1.txt");
in.get(s,99);
cout << s;
in.close();
return 0;
}
4.显示文本文件内容
题目内容:
编写程序,将文本文件的内容显示到屏幕上。
输入:整数
输出:整数
【提示】
样例1输入:
0
样例1输出:
0
提交版 && 测试版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char s[802]; //400个汉字。
int a;
ifstream in;
cin >> a;
cout << a << endl;
in.open("a1.txt");
while(in)
{
in.get(s,800);
if(in)
{
cout << s;
}
}
in.close();
return 0;
}
5.从文件读整数求和
编写程序,读取文本文件a2.txt中的两个整数,计算它们的和。文件请自己用记事本创建,内容是两个整数,在一行或两行均可,文件放在程序所在文件夹下。
输入:两个整数
输出:和
【提示】
样例1输入:
2 3
样例1输出:
5
测试版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a,b,c;
ifstream in;
in.open("a1.txt");
in >> a >> b;
c = a + b;
cout << c << endl;
cin >> a >> b;
c = a + b;
cout << c << endl;
return 0;
}
提交版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a,b,c;
/*ifstream in;
in.open("a1.txt");
in >> a >> b;
c = a + b;
cout << c << endl;*/
cin >> a >> b;
c = a + b;
cout << c << endl;
return 0;
}
第十三周编程作业
这个题上周没有发布,这周继续补上
1.计算某个正整数平方根,并按要求输出
设置小数位数
总结
#include <iostream>
#include <iomanip> //头文件
using namespace std;
int main()
{
cout << setiosflags(ios::fixed) << setprecision(2) << x;//第一种方法:
//第二种方法:
cout.setf(ios::fixed) ;
cout << setprecision(2) << x;
//第三种方法,看起来最简洁。应该也是最实用的了吧!
cout << fixed << setprecision(2) << x;
return 0;
}
代码:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int x;
cin >>x;
for(int i=1;i<7;i++)
{
cout << fixed << setprecision(i) << sqrt(x) << endl;
}
return 0;
}
2.读取文件,添加行号显示
代码:
测试版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char data[5][81];
char temp[81];
for(int i=0;i<5;i++)
{
cin.getline(data[i],80);
}
ofstream out;
out.open("A.txt");
for(int i=0;i<5;i++)
{
out << data[i] <<endl;
}
out.close();
ifstream in;
in.open("A.txt");
for(int i=0;i<5;i++)
{
cout.width(5);
cout.setf(ios::left); //这里为了练习格式输出
cout << i+1 ;
in >> temp; //这里用.get方法的话似乎是不能按行输出的。
cout << temp << endl;
}
in.close();
return 0;
}
提交版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char data[5][81];
char temp[81];
for(int i=0;i<5;i++)
{
cin.getline(data[i],80);
}
/*ofstream out;
out.open("A.txt");
for(int i=0;i<5;i++)
{
out << data[i] <<endl;
}
out.close();
ifstream in;
in.open("A.txt");
for(int i=0;i<5;i++)
{
cout.width(5);
cout.setf(ios::left);
cout << i+1 ;
in >> temp; //这里用.get方法的话似乎是不能按行输出的。
cout << temp << endl;
}
in.close();*/
for(int i=0;i<5;i++)
{
cout.width(4);
cout.setf(ios::left);
cout << i+1 ;
cout << data[i] << endl;
}
return 0;
}
3.读写文件并转换字符
代码:
测试版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char data[201];
char temp;
cin.getline(data,200);
ofstream out;
out.open("a1.txt");
out << data;
out.close();
ifstream in;
in.open("a1.txt");
while(in)
{
temp = toupper(in.get()); //利用了toupper函数。
cout << temp;
}
return 0;
}
提交版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char data[201];
char temp;
cin.getline(data,200);
/*ofstream out;
out.open("a1.txt");
out << data;
out.close();
ifstream in;
in.open("a1.txt");
while(in)
{
temp = toupper(in.get()); //使用toupper()函数简化代码量。
cout << temp;
}*/
for(int i=0;;i++)
{
if(data[i])
{
temp = toupper(data[i]);
cout << temp;
}
else
{
break;
}
}
return 0;
}
4.读文件中的数字,算平均值
代码:
测试版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
double sum=0;
int n;
double temp;
cin >> n;
/*for(int i=0;i<n;i++)
{
cin >> temp;
sum = sum + temp;
}*/
ofstream out;
out.open("out1.txt"); //创建并打开文件
out << n << endl;
for(int i=0;i<n;i++)
{
cin >> temp;
out << temp << endl;
}
out.close();
ifstream in;
in.open("out1.txt");
in >> n;
sum = 0;
for(int i=0;i < n;i++)
{
if(in)
{
in >> temp;
//cout << temp << endl;
sum = sum + temp;
}
}
in.close();
cout << "Avg=" << sum / n << endl;
return 0;
}
提交版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
double sum=0;
int n;
double temp;
cin >> n;
for(int i=0;i<n;i++)
{
cin >> temp;
sum = sum + temp;
}
/*ofstream out;
out.open("out1.txt"); //创建并打开文件
out << n << endl;
for(int i=0;i<n;i++)
{
cin >> temp;
out << temp << endl;
}
out.close();*/
/*ifstream in;
in.open("out1.txt");
in >> n;
sum = 0;
for(int i=0;i < n;i++)
{
if(in)
{
in >> temp;
//cout << temp << endl;
sum = sum + temp;
}
}
in.close();*/
cout << "Avg=" << sum / n << endl;
return 0;
}
5.读文件中的字符并排序输出
代码:
测试版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
void f(int n,char* data);//排序函数
char data[100];
int n;
char temp;
cin >> n;
ofstream out;
out.open("A.txt"); //创建并打开文件
for(int i=0;i < n;i++)
{
cin >> temp;
out << temp << " ";
}
out.close();
ifstream in;
in.open("A.txt");
for(int i=0;i < n;i++)
{
if(in)
{
in >> data[i];
}
}
in.close();
f(n,data);
out.open("B.txt"); //创建并打开文件B
for(int i=0;i < n;i++)
{
out << data[i];
if(i != n-1)
{
cout << " ";
}
}
return 0;
}
void f(int n,char* data)//排序函数
{
char temp;
for(int i=0;i < n-1;i++)
{
for(int j=0;j < n-1;j++)
{
if(data[j] > data[j+1])
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
}
}
}
提交版
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
void f(int n,char* data);//排序函数
char data[100];
int n;
//char temp;
cin >> n;
/*ofstream out;
out.open("A.txt"); //创建并打开文件
for(int i=0;i < n;i++)
{
cin >> temp;
out << temp << " ";
}
out.close();
ifstream in;
in.open("A.txt");
for(int i=0;i < n;i++)
{
if(in)
{
in >> data[i];
}
}
in.close();
f(n,data);
out.open("B.txt"); //创建并打开文件B
for(int i=0;i < n;i++)
{
out << data[i];
if(i != n)
{
cout << " ";
}
}*/
for(int i=0;i < n;i++)
{
cin >> data[i];
}
f(n,data);
for(int i=0;i < n;i++)
{
cout << data[i];
if(i != n-1)
{
cout << " ";
}
}
return 0;
}
void f(int n,char* data)//排序函数
{
char temp;
for(int i=0;i < n-1;i++)
{
for(int j=0;j < n-1;j++)
{
if(data[j] > data[j+1])
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
}
}
}
好,这周的题有点简单啊,估计是因为第一次接触文件编程的原因吧!
变秃是不可能变秃的 ——M4xlmum
54mp55CG5p2A5oiR
c++ 西安交通大学 mooc 第十三周基础练习&第十三周编程作业的更多相关文章
- 二十三. Python基础(23)--经典类和新式类
二十三. Python基础(23)--经典类和新式类 ●知识框架 ●接口类&抽象类的实现 # 接口类&抽象类的实现 #①抛出异常法 class Parent(object): ...
- MyBatis基础入门《十三》批量新增数据
MyBatis基础入门<十三>批量新增数据 批量新增数据方式1:(数据小于一万) xml文件 接口: 测试方法: 测试结果: =============================== ...
- 十三. Python基础(13)--生成器进阶
十三. Python基础(13)--生成器进阶 1 ● send()方法 generator.send(value) Resumes the execution, and "sends&qu ...
- 吴恩达深度学习第4课第3周编程作业 + PIL + Python3 + Anaconda环境 + Ubuntu + 导入PIL报错的解决
问题描述: 做吴恩达深度学习第4课第3周编程作业时导入PIL包报错. 我的环境: 已经安装了Tensorflow GPU 版本 Python3 Anaconda 解决办法: 安装pillow模块,而不 ...
- 吴恩达深度学习第2课第2周编程作业 的坑(Optimization Methods)
我python2.7, 做吴恩达深度学习第2课第2周编程作业 Optimization Methods 时有2个坑: 第一坑 需将辅助文件 opt_utils.py 的 nitialize_param ...
- Spring 框架基础(04):AOP切面编程概念,几种实现方式演示
本文源码:GitHub·点这里 || GitEE·点这里 一.AOP基础简介 1.切面编程简介 AOP全称:Aspect Oriented Programming,面向切面编程.通过预编译方式和运行期 ...
- 最基础的Python的socket编程入门教程
最基础的Python的socket编程入门教程 本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在 ...
- C语言第九周编程作业
这个作业属于哪个课程 C语言程序设计 这个作业的要求在哪里 https://pintia.cn/problem-sets/1120145772099268608 我在这个课程的目标是 了解结构 ...
- (干货)java中如何根据一个时间获取属于本年那一周,本周的开始时间以及最后一天时间。并且设置起始时间为周6.结束时间为周5
本人亲测,有用,适用性比较强,直接上代码说话. package com.helloBike.data; import java.text.ParseException; import java.tex ...
随机推荐
- Java是否还能再辉煌十年?
目录 Java是否还能再辉煌十年? 一.前言 二.如今的Java语言 2.1 位居TIOBE榜首 2.2 革命性的语言 三.Java受到的挑战 3.1 后台服务器软件的语言竞争 3.1.1 Pytho ...
- Jmeter逻辑控制器,简单操作
1. 2. 循环控制器可以设置请求的循环次数或永久循环, . 作用:改控制器下的取样器请求可以循环运行. 3. 请求需要拖拽到循环控制器里, 4.循环次数乘以线程数 得到如下图: 成功了 二. 事务 ...
- IIS实现Nginx功能:转发
这个标题本身是不合理的,但是基于目前公司有一份系统是外部代理商贴牌使用,有一个老的站点是部署在IIS上,好多代理商自己的域名绑定在这个上面,而近期新版本的系统已经上线,那么需要将这些域名也转发到新的站 ...
- Django 中 a href标签 使用方法 跳转页面(Django四)
上次我已经用Django启动了一个登录模板页面 具体过程见:Django启动我的第一个模板页面 但问题是我们只能通过监听的端口访问这一个页面,不能通过页面的一些连接跳转到其他页面如下,我们不能点击注册 ...
- DataStax Bulk Loader教程(一)
DataStax Bulk Loader - dsbulk是在DSE 6 引入的一种新的批量加载方法.(点击这里下载DataStax Bulk Loader). 它提供了将数据加载(load)到Dat ...
- 【征文活动】为自己发“声” —— 声网RTC征文大赛在园子里征稿
2020年8月20日,声网Agora入驻园子的新楼盘--博客园品牌专区.9月,我们与声网Agora再度合作,「为自己发"声"- RTC 征文大赛」在园子里征稿. RTC(Real- ...
- TKE 集群组建最佳实践
Kubernetes 版本 Kubernetes 版本迭代比较快,新版本通常包含许多 bug 修复和新功能,旧版本逐渐淘汰,建议创建集群时选择当前 TKE 支持的最新版本,后续出新版本后也是可以支持 ...
- 解决flutter 运行时:Waiting for another flutter command to release the startup lock...
执行 Flutter 包管理相关命令时有可能遇到 Waiting for another flutter command to release the startup lock... 这样的错误,可尝 ...
- python的全局函数
1.Python的全局函数 import builtins dir(builtins) abs # 返回参数的绝对值 可以写成函数:def absnum): if num >=0: retu ...
- Cesium系统学习整理(一)
(一)Cesium的概念定义 Cesium是国外一个基于JavaScript编写的使用WebGL的地图引擎.Cesium支持3D,2D,2.5D形式的地图展示,可以自行绘制图形,高亮区域,并提供良好的 ...