做题记录

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

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. HDU-多校2-Everything Is Generated In Equal Probability(公式+逆元)

    Problem Description One day, Y_UME got an integer N and an interesting program which is shown below: ...

  2. 【python】迭代器与生成器到底是什么?看完你就知道

    迭代器跟生成器,与上篇文章讲的装饰器一样,都是属于我的一个老大难问题. 通常就是遇到的时候就去搜一下,结果在一大坨各种介绍博客中看了看,回头又忘记了. 你是不是也是这样呢? 俗话说:好记性不如烂笔头, ...

  3. 20190928-02使用Redis客户端Jedis连接Redis,以及用Java代码操作Redis 000 030

    启动redis package com.yujie.jedis; import java.util.HashMap; import java.util.Map; import java.util.Se ...

  4. LongAccumulator类的BUG——reset方法并不能保证初始值正确赋值

    LongAccumulator.reset方法并不能重置重置LongAccumulator的identity:初始值正确,使其恢复原来的初始值.当初始值为0是不会发生这个问题,而当我们设置初始值如1时 ...

  5. python基础三(集合、文件)

    1.集合定义 集合天生能去重,且与字典一样,无序.集合用大括号括起来,里面的元素之间用逗号分隔,要跟字典区分开. 集合定义方法:s=set() #定义一个空集合 s={'1','a','b','c', ...

  6. 利用预编译解决C/C++重复定义的错误 -2020.09.13

    利用预编译解决C/C++重复定义的错误 -2020.09.13 我们现在有main.c和function.h两个文件 main.c #include <stdio.h> #include ...

  7. pytest(3):pytest运行参数介绍

    前言 pytest 带有很多参数,可以使用 pytest --help  来查看帮助文档,下面介绍几种常用的参数: 无参数 读取路径下所有符合规则的文件,类,方法,函数全部执行.使用方法如下:  py ...

  8. swift基本数据类型使用-数组使用

    目录 数组的使用 1.数组的定义 2.对可变数组的基本操作 3.数组的遍历 4.数组的合并 5. 示例 数组的使用 1.数组的定义 1> 定义不可变数组 2> 定义可变数组 2.对可变数组 ...

  9. 透过 Cucumber 学习 BDD

    在需求的开发过程中,最令人困惑的地方就在于需求模糊.需求是解决业务的问题,那么验收的方式应该是由业务方提出,但是往往业务方(可能是产品经理,也可能是直接是客户)只能给出比较模糊的一个验收标准,而程序却 ...

  10. 1.2Hadoop概述