做题记录

风影影,景色明明,淡淡云雾中,小鸟轻灵。

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 第十三周基础练习&第十三周编程作业的更多相关文章

  1. 二十三. Python基础(23)--经典类和新式类

    二十三. Python基础(23)--经典类和新式类 ●知识框架   ●接口类&抽象类的实现 # 接口类&抽象类的实现 #①抛出异常法 class Parent(object):    ...

  2. MyBatis基础入门《十三》批量新增数据

    MyBatis基础入门<十三>批量新增数据 批量新增数据方式1:(数据小于一万) xml文件 接口: 测试方法: 测试结果: =============================== ...

  3. 十三. Python基础(13)--生成器进阶

    十三. Python基础(13)--生成器进阶 1 ● send()方法 generator.send(value) Resumes the execution, and "sends&qu ...

  4. 吴恩达深度学习第4课第3周编程作业 + PIL + Python3 + Anaconda环境 + Ubuntu + 导入PIL报错的解决

    问题描述: 做吴恩达深度学习第4课第3周编程作业时导入PIL包报错. 我的环境: 已经安装了Tensorflow GPU 版本 Python3 Anaconda 解决办法: 安装pillow模块,而不 ...

  5. 吴恩达深度学习第2课第2周编程作业 的坑(Optimization Methods)

    我python2.7, 做吴恩达深度学习第2课第2周编程作业 Optimization Methods 时有2个坑: 第一坑 需将辅助文件 opt_utils.py 的 nitialize_param ...

  6. Spring 框架基础(04):AOP切面编程概念,几种实现方式演示

    本文源码:GitHub·点这里 || GitEE·点这里 一.AOP基础简介 1.切面编程简介 AOP全称:Aspect Oriented Programming,面向切面编程.通过预编译方式和运行期 ...

  7. 最基础的Python的socket编程入门教程

    最基础的Python的socket编程入门教程 本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在 ...

  8. C语言第九周编程作业

        这个作业属于哪个课程 C语言程序设计 这个作业的要求在哪里 https://pintia.cn/problem-sets/1120145772099268608 我在这个课程的目标是 了解结构 ...

  9. (干货)java中如何根据一个时间获取属于本年那一周,本周的开始时间以及最后一天时间。并且设置起始时间为周6.结束时间为周5

    本人亲测,有用,适用性比较强,直接上代码说话. package com.helloBike.data; import java.text.ParseException; import java.tex ...

随机推荐

  1. Codeforces1348 题解

    AC代码连接 A Phoenix and Balance 显而易见,将前\(\frac{n}{2}-1\)个和最后1个分为1组,剩下的1组即为最优方案. B Phoenix and Science 这 ...

  2. Spark应用开发-关联分析

    在机器学习中,常用的主题有分类,回归,聚类和关联分析.而关联分析,在实际中的应用场景,有部分是用于商品零售的分析.在Spark中有相应的案例 在关联分析中,有一些概念要熟悉. 频繁项集,关联规则,支持 ...

  3. Java通过http协议发送Get和Post请求

    https://blog.csdn.net/qq_32599479/article/details/94439131

  4. MySQL通过POIN数据类型查询指定范围内数据

    情况一: 数据库:只有point类型的location字段 实体类:有经纬度字段(double).originLoction字段(存放string类型的数据库location字段:POINT(123. ...

  5. Bootstrap4总结

    一. bootstrap简介 Bootstrap,来自 Twitter(全国最大的微博),是目前最受欢迎的前端框架. bootstrap下载及演示 http://v3.bootcss.com 什么是b ...

  6. LG P2389 电脑班的裁员

    Description ZZY有独特的裁员技巧:每个同学都有一个考试得分$a_i(-1000 \leq a_i \leq 1000)$,在$n$个同学$(n \leq 500)$中选出不大于$k$段$ ...

  7. 简单地 Makefile 书写

    注意事项 每个标签分支前都不能用空格,必须用tab 标签外调用bash命令用 $(shell -),标签内可以正常使用 标签后可以指定其他标签,执行顺序是先执行其他标签,而后在执行自己 比如 all: ...

  8. 【译】Visual Studio 2019 的 Local Process with Kubernetes

    今天,我们自豪地宣布 Local Process with Kubernetes 的预览版已加入到 Visual Studio 2019 16.7 Preview 2 中.  Local Proces ...

  9. react学习 | 踩坑指南

    react样式模块化的"omit -loader"坑 众所周知 react样式的模块化(css modules) 是自己模块中写自己的css,与其他模块互补影响,解决了命名冲突和全 ...

  10. Linux实战(19):Shell交互式read 用法

    read 用法有好几种,我在实战过程中用到了 -p,记一笔以防不用忘记了. 实例 #!/bin/bash echo "检测IP是否被占用" while read -p " ...