首页 > 其他分享 >PTA练习题

PTA练习题

时间:2023-05-15 20:56:07浏览次数:31  
标签:练习题 file1 weight int ages Dog PTA dog1

定义一个Dog类,包括体重和年龄两个数据成员及其成员函数,声明一个实例dog1,体重5,年龄10,使用I/O流把dog1的状态写入磁盘文件。再声明一个实例dog2,通过读取文件dog1的状态赋给dog2。分别用文本方式和二进制方式操作文件。

 1 #include <iostream>
 2 #include <fstream>
 3 using namespace std;
 4 class  Dog{
 5     public:
 6         int ages;
 7         int weight;
 8         Dog(int w,int a)
 9         {
10             weight=w;
11             ages=a;
12         }
13         Dog(){
14         }
15 };
16 int main()
17 {
18     Dog dog1(5,10);
19     fstream file1;
20     file1.open("text.txt",ios::out);
21     file1<<dog1.weight<<endl;
22     file1<<dog1.ages<<endl;
23     file1.close();
24     ifstream file2;
25     file2.open("text.txt",ios::in);
26     if(!file2.is_open())
27     {
28         cout<<"文件打开失败"<<endl;
29         return 0;
30     }
31     Dog dog2(1,1);
32     file2>>dog2.weight>>dog2.ages;
33     cout<<dog2.weight<<endl;
34     cout<<dog2.ages<<endl;
35     file2.close();    
36 }
 1 #include <iostream>
 2 #include <fstream>
 3 using namespace std;
 4 class  Dog{
 5     public:
 6         int ages;
 7         int weight;
 8         Dog(int w,int a)
 9         {
10             weight=w;
11             ages=a;
12         }
13         Dog(){
14         }
15 };
16 int main()
17 {
18     Dog dog1(5,10);
19     fstream file1("text.txt",ios::out|ios::binary);
20     file1.write((const char*)&dog1,sizeof(dog1));
21     file1.close();    
22     ifstream file2;
23     file2.open("text.txt",ios::in|ios::binary);
24     if(!file2.is_open())
25     {
26         cout<<"文件打开失败"<<endl;
27     }
28     Dog dog2;
29     file2.read((char*)&dog2,sizeof(dog1));
30     cout<<dog2.weight<<endl;
31     cout<<dog2.ages<<endl;
32     file2.close();
33 }

 

标签:练习题,file1,weight,int,ages,Dog,PTA,dog1
From: https://www.cnblogs.com/Lyh3012648079/p/17403083.html

相关文章

  • Linux下基于iptables的hashlimit模块限速
    记录一下:【对目标IP限制下载速率】出站包,目标IP为x.x.x.x,限速200KBytes/s:iptables-AOUTPUT-dx.x.x.x-mhashlimit--hashlimit-above200kb/s--hashlimit-modedstip--hashlimit-nameout-jDROP 【对自身tcp/12345提供的服务限制下载速率】出站包,源端口是tcp/12......
  • pta python实验3-6
    python实验4循环结构 1importmath23defestimate_pi(n):4total=05ret=06foriinrange(n+1):7ifi%2==0:8sign=19else:10sign=-111term=sign/((2*i+1)*mat......
  • 第二次博客:PTA题目集4-5总结
    第二次博客:PTA题目集4-5总结 前言:有了前三次的菜单系列的洗礼,我对这两次的点菜还是有一定的信心的。老师也说期中考试会于点菜系列有一定的联系。但是事实告诉你永远不要试图揣测蔡老师的心思。先说期中考试:题目概述:总共4题,(和点菜没有半毛钱关系)不过总体来说......
  • [信友队图灵杯中级组-D]基础循环结构练习题
    2023-5-13题目题目传送门难度&重要性(1~10):6.5题目来源信友队图灵杯题目算法构造解题思路我们可以知道,在一开始我们得到的\(a\)数组是\(1,2,3,\dotsn\)。所以我们可以看做是:题目让我们构造出一个\(b\)数组。因为数据是\(1\len\le10^3,1\leb_i\le10^5\)......
  • pta_【CPP0029】以圆类Circle及立体图形类Solid为基础设计圆锥类Cone
    #include<iostream>#include<cmath>usingnamespacestd;//点类PointclassPoint{private:doublex;doubley;public:Point(doublexv=0,doubleyv=0);/*构造函数*/Point(constPoint&p);/*拷贝构造*/~Point();/*......
  • Aptana3 SVN Client安装
    Aptana3SVNClient安装安装SVN Client1.开启Aptana,打开help菜单,点击“InstallNewSoftware”2.点击Add按钮,在AddSite窗口,name输入:SVN ,location输入:http://subclipse.tigris.org/update_1.6.x,点击ok按钮3.全选除了“SubclipseIntegrationforMylyn3.x(Optional)3.0.0“ ......
  • 2023 PTA天梯赛补题(L1 & L2)
    2023天梯赛L1&L2补题L1L1-089最好的文档输入输出题#include<bits/stdc++.h>usingnamespacestd;intmain(){cout<<"Goodcodeisitsownbestdocumentation.";return0;}L1-090什么是机器学习输入输出题#include<bits/stdc++.h>us......
  • iptables之forward转发
    1、网络防火墙2、iptables之FORWARD转发实例3、iptables之FORWARD过滤实例1、网络防火墙网络防火墙处于网络入口的边缘,针对网络入口进行防护,针对整个网络入口后面的局域网。作用:当外部网络主机与内部网络主机互相进行通讯时,都要经过iptables所在的主机,由iptables所在的主机进行“......
  • pta_【CPP0027】以圆类Circle及立体图形类Solid为基础设计球类Sphere
    #include<iostream>usingnamespacestd;//点类PointclassPoint{private:doublex;doubley;public:Point(doublexv=0,doubleyv=0);/*构造函数*/Point(constPoint&p);/*拷贝构造*/~Point();/*析构函数*/voidsetX(d......
  • C/C++数据结构练习题[2023-05-08]
    C/C++数据结构练习题[2023-05-08]基本习题部分1字符串距离目的:字符串是一种基础且广泛使用的数据结构,与字符串相关的题目既可以考察基本程序设计能力和技巧,也可以考查算法设计能力。题目:求字符串之间距离要求:设有字符串X,称在X的头尾及中间插入任意多个空格后构成的新字......