鱼C论坛

 找回密码
 立即注册
查看: 2630|回复: 1

[技术交流] C++(10th)

[复制链接]
发表于 2021-2-6 22:35:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 一叶枫残 于 2021-2-8 18:04 编辑

对象的基础——结构
结构(Structure)是一种由程序员定义的、由其他变量类型组合而成的数据类型
创建

定义结构的基本语法是:
  1. struct name
  2. {
  3.         type varName1;
  4.         type varName2;
  5.         ......
  6. };        //注意不要忘记这个分号
复制代码

对于本人而言,结构简单来说就是一个可以放不同数据类型的数组,类似于python中的列表;

赋值方式

  1. struct Imformation
  2. {
  3.         string name;
  4.         char sex;
  5.         int idnum;
  6. } ;
复制代码

(1)我们可以使用 . (点操作符)来进行赋值
  1. struct Imformation Friend;   //在C++里面,我们可以去掉前面的struct
  2. Friend.name = "Ming";
  3. Friend.sex = 'F';
  4. Friend.idnum = 125;
复制代码

(2)声明新变量时,也可以像定义数组一样的方式进行赋值
  1. struct Imformation Friend = { "Ming" , 'F' , 125 };
复制代码

(3)使用指针来赋值:
<1>解引用方式
  1. Imformation Friend;
  2. Imformation *p;
  3. p = &Friend;

  4. (*p).name = "Ming";        //请注意,括号必不可少
  5. (*p).sex = 'F';
  6. (*p).idnum = 125;
复制代码


<2>使用" ->"
  1. p->name = "Ming";
  2. p->sex = 'F';
  3. p->idnum = 125;
复制代码


课后作业:
定义一个结构,至少储存:姓名,身份证,性别
实现文件储存
可以打印到屏幕
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <fstream>

  5. using namespace std;

  6. struct DataStorage
  7. {
  8.         string name;
  9.         string idn;
  10.         char sex;
  11. };

  12. typedef DataStorage DS;

  13. bool Datainput();
  14. bool Datapring();
  15. bool DataStore( DS*);

  16. int main()
  17. {
  18.         int get;
  19.         while( 1 )
  20.         {
  21.                 std::cout << "请输入您需要的功能: " << endl;
  22.                 std::cout << "1. 打印数据到屏幕" << endl;
  23.                 std::cout << "2. 录入数据" << endl;
  24.                 std::cout << "3. 退出程序" << endl;
  25.                 cin >> get;
  26.                 if( get == 1)
  27.                 {
  28.                         cout << "姓名        身份证        性别" << endl;
  29.                         Datapring();
  30.                 }
  31.                 else if( get == 2)
  32.                 {
  33.                         Datainput();
  34.                 }
  35.                 else if( get == 3 )
  36.                 {
  37.                         return 0;
  38.                 }
  39.        
  40.         }
  41.        
  42.        
  43.         return 0;
  44. }


  45. bool Datainput()
  46. {
  47.         DS one;
  48.         DS* p;
  49.         p = &one;
  50.        
  51.         cout << "请输入姓名:" ;
  52.         cin >> one.name;
  53.         cout << "请输入身份证:";
  54.         cin >> one.idn;
  55.         cout << "请输入性别(F/M):";
  56.         cin.ignore( 100 , '\n' );
  57.         one.sex = cin.get();
  58.         cin.clear();
  59.         cin.ignore( 100 , '\n');
  60.         char sign;
  61.         cout << "是否保存数据(Y/N):";
  62.         sign = cin.get();
  63.        
  64.         if( sign == 'Y')
  65.         {
  66.                 DataStore( p );
  67.         }
  68.         else if( sign == 'N')
  69.         {
  70.                 ;
  71.         }
  72.         else
  73.         {
  74.                 cout << "请重新输入(N/Y):" << endl;
  75.         }
  76.        
  77. }

  78. bool Datapring()
  79. {
  80.         ifstream in( "Data.txt" );
  81.         if( !in )
  82.         {
  83.                 cerr << "打开文件失败" << endl;
  84.                 return 0;
  85.         }
  86.        
  87.         char c;
  88.         while( in.read( &c , 1))
  89.         {
  90.                 cout << c;       
  91.         }
  92.        
  93.         in.close();
  94. }

  95. bool DataStore( DS* p )
  96. {
  97.         ofstream out( "Data.txt" , ios::app);
  98.        
  99.         if( !out )
  100.         {
  101.                 cerr << "打开文件失败" << endl;
  102.                 return 0;
  103.         }

  104.        
  105.         out << p->name;
  106.         out << "     ";
  107.         out << p->idn;
  108.         out << "     ";
  109.         out << p->sex;
  110.         out << "\n";
  111.                
  112.         out.close();
  113. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-7 09:10:46 | 显示全部楼层
不错,111
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-1 23:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表