首页 > 其他分享 >每日打卡-21.4

每日打卡-21.4

时间:2023-05-11 23:12:35浏览次数:35  
标签:numberdayalive 21.4 weight int 每日 Dog filename 打卡 dog1

一.问题描述

定义一个dog类,包含体重和年龄两个成员变量及相应的成员函数,声明一个实例dog1,体重为5,年龄为10,使用I/O流把dog1的状态写入磁盘文件,再声明另一个实例dog2,通过读文件把dog1的状态赋给dog2。分别使用文本方式和二进制方式操作文件,看看结果有何不同;再看看磁盘文件的ASCII码有何不同。

三.流程图

四.伪代码 

1

五.代码实现 

1#include<iostream>
#include<fstream>
using namespace std;
class Dog {
private:
	int weight;
	long numberdayalive;

public:
	Dog(int weight, long days) :weight(weight), numberdayalive(days) {}
	~Dog() {}
	int getweight() const {
		return weight;
	}
	void setweight() {
		cin >> weight;
	}
	int getnumberdayalive() const {
		return numberdayalive;
	}
	void setnumberdayalive() {
		cin >> numberdayalive;
	}
};
int main() {
	char filename[80];
	cout << "Please enter file name:";
	cin >> filename;
	ofstream fout(filename);
	if (!fout) {
		cout << "unable to open " << filename << " for writing." << endl;
		return { 1 };
	}
	Dog dog1(5, 10);
	fout.write((char*)&dog1, sizeof dog1);

	fout.close();

	ifstream fin(filename);
	if (!fin) {
		cout << "unable to open " << filename << " for reading." << endl;
		return { 1 };
	}
	Dog dog2(2, 10);
	cout << "dog2 weight:" << dog2.getweight() << endl;
	cout << "dog2 days:" << dog2.getnumberdayalive() << endl;

	fin.read((char*)&dog2, sizeof dog2);
	cout << "dog2 weight:" << dog2.getweight() << endl;
	cout << "dog2 days:" << dog2.getnumberdayalive() << endl;
	fin.close();
	return 0;
}

 

标签:numberdayalive,21.4,weight,int,每日,Dog,filename,打卡,dog1
From: https://www.cnblogs.com/leapssisbird/p/17392527.html

相关文章

  • 5.11打卡
     二、思路设计 三、代码实现#include<bits/stdc++.h>usingnamespacestd;#defineTAXBASE3500;typedefstruct{longstart;longend;doubletaxrate;}TAXTABLE;TAXTABLETaxTable[]={{0,1500,0.03},{1500,4500,0.10},{4500,9000,0.20}......
  • 5.11每日总结
    今天学习了nextInt、nextFloat、nextDoublenext():用于读取String字符串数组,以空格划分(只读取输入直到空格),在读取后将光标指向本行nextLine():用于读取String字符串数组,读取包括单词之间的空格和除回车以外的所有符号,在读取后将光标指向下一行publicstaticvoidmain(String[]arg......
  • 第十四天打卡
    以点类Point及平面图形类Plane为基类公有派生圆类Circle,再以圆类Circle及立体图形类Solid为基类公有派生球类Sphere,main(void)函数完成对球类Sphere的测试。#include<iostream>usingnamespacestd;//点类PointclassPoint{private:doublex;doubley;public:......
  • 5.11每日总结
    hasNextXxx():判断下一个输入是否是某种类型的元素如:hasNextInt(),hasNextFloat()、hasNextDouble()等hasNest():判断下一个输入是否是字符串nextXxx():用于获取下一个输入项如:nextInt、nextFloat、nextDouble等next():用于读取String字符串数组,以空格划分(只读取输入直到空格),在读取后将光......
  • 2023.5.11每日总结
    packageget;importorg.apache.commons.fileupload.FileItem;importorg.apache.commons.fileupload.FileUploadException;importorg.apache.commons.fileupload.disk.DiskFileItemFactory;importorg.apache.commons.fileupload.servlet.ServletFileUpload;importj......
  • 每日总结-23.5.11
    <%@pageimport="wangzhan.Thesql"%><%@pageimport="wangzhan.Pd_P_assignment"%><%@pageimport="wangzhan.Pd_S_assignment"%><%@pageimport="wangzhan.Pd_lesson"%><%@pagelanguage=&......
  • 每日总结
    遍历文件importjava.io.File;publicclassFileTraversal{publicstaticvoidmain(String[]args){//要遍历的文件路径StringfilePath="C:\\Users\\username\\Documents";//创建File对象,代表要遍历的目录或文件Filefile=newFile(filePath);/......
  • 每日打卡一道用vector写的题
    编程团体赛的规则为:每个参赛队由若干队员组成;所有队员独立比赛;参赛队的成绩为所有队员的成绩和;成绩最高的队获胜。现给定所有队员的比赛成绩,请你编写程序找出冠军队。输入格式:输入第一行给出一个正整数 N(≤104),即所有参赛队员总数。随后 N 行,每行给出一位队员的成绩,格式为......
  • c++打卡练习(25)
    回文数流程图:伪代码:源代码:#include<stdio.h>intmain(){ intm[16],n,i,t,count=0; longunsigneda,k; printf("No.numberit'ssquare(palindrome)\n"); for(n=1;n<256;n++) { k=0;t=1;a=n*n; for(i=0;a!=0;i++) { m[i]=a%10; a/=10; ......
  • 每日打卡
    水仙花数:问题描述;一个数等于其各个位上的数字的立方和的数被称为水仙花数,求100-1000内的水仙花数问题分析:可以将一个三位数的各个数位上的数拆开求立方和代码:#include<stdio.h> intmain() {            intg,s,b,n;            printf("结......