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 ...
随机推荐
- zabbix-4.0-监控服务器的ping告警设置
问题:一直在困惑如果一台服务器的网络发生故障或者断开时,怎么第一时间发现并去排查. 思路:利用zabbix平台监控服务器,监控ping这一项,设置一个报警,并使用脚本去提醒与通知,可使用邮件报警/短信 ...
- Oracle适配问题解决
问题一:SQL 命令未正确结束 问题二:ORA-00907: 缺失右括号 问题三:mysql函数在Oracle中不适用 问题四:ORA-00936: 缺失表达式 问题五:No serializer f ...
- Spring security OAuth2.0认证授权学习第三天(认证流程)
本来之前打算把第三天写基于Session认证授权的,但是后来视屏看完后感觉意义不大,而且内容简单,就不单独写成文章了; 简单说一下吧,就是通过Servlet的SessionApi 通过实现拦截器的前置 ...
- Zabbix Agent日志路径定位
Zabbix Agent的日志一般记录在zabbix_agentd.log中,那么如何定位.找到Zabbix Agent的日志路径呢? 下面从Linux操作系统和Windows系统来简单总结一下,方便 ...
- Java判断一个字符串是否是回文
package com.spring.test; /** * 判断字符串是否为回文 * * @author liuwenlong * @create 2020-08-31 11:33:04 */ @S ...
- Oracle 根据不同成绩,对应不同等级信息
--查询每一颗成绩的:优秀.良好.不良的数量 --校园需要一张各科成绩状态统计表,格式如下: --科目名称 优秀人数 良好人数 不良人数 --高等数学 5 12 2 --英语 8 18 1 先创建一个 ...
- 购书网站前端实现(HTML+CSS+JavaScript)
购书+阅读静态网页设计与实现 一.主页设计HTML 1.效果展示及实现 2.完整代码 二.主页样式布局CSS 三.空间功能实现Javascript 主要功能 Javascript完整代码: 总结 购书 ...
- 执行./install.sh时报错-bash: ./install.sh: /bin/bash^M: 坏的解释器: 没有那个文件或目录
百度解释说是因为这个文件在windows下编辑过,windows下每一行的结尾是\n\r, 而linux下每一行结尾是\n,所以只需要删除这个文件中的\r字符就可以了sed -i 's/\r$//' ...
- 痞子衡嵌入式:IAR在线调试时设不同复位类型可能会导致i.MXRT下调试现象不一致(J-Link / CMSIS-DAP)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是IAR在线调试时设不同复位类型可能会导致i.MXRT下调试现象不一致. 做Cortex-M内核MCU嵌入式软件开发,可用的集成开发环境( ...
- Sql Server中使用特定字符分割字符串
在T-SQL中我们经常批量操作时都会对字符串进行拆分,可是SQL Server中却没有自带Split函数,所以要自己来实现了.这里将字符串分割以table形式输出 语法如下: SET ANSI_NUL ...