demo7:函数份文件编写

swap.h

#include <iostream>
using namespace std;//函数的声明
void swap(int a, int b);

swap.cpp

#include "swap.h"//函数的定义
void swap(int a, int b)
{int temp = a;a = b;b = temp;cout << "a = " << a << endl;cout << "b = " << b << endl;
}

main函数

#include<iostream>
using namespace std;
#include "swap.h"//函数的分文件编写
//实现两个数字进行交换的函数函数的声明
//void swap(int a, int b);
函数的定义
//void swap(int a, int b)
//{
//  int temp = a;
//  a = b;
//  b = temp;
//
//  cout << "a = " << a << endl;
//  cout << "b = " << b << endl;
//}//1、创建.h后缀名的头文件
//2、创建.cpp后缀名的源文件
//3、在头文件中写函数的声明
//4、在源文件中写函数的定义int main() {int a = 10;int b = 20;swap(a, b);system("pause");return 0;
}

demo6:形参不改变 ,当我们做值传递的时候,函数的形参发生改变,并不会影响实参

#include<iostream>
using namespace std;//值传递
//定义函数,实现两个数字进行交换函数//如果函数不需要返回值,声明的时候可以写void
void swap3(int num1, int num2)
{cout << "交换前: " << endl;cout << "num1 = " << num1 << endl;cout << "num2 = " << num2 << endl;int temp = num1;num1 = num2;num2 = temp;cout << "交换后: " << endl;cout << "num1 = " << num1 << endl;cout << "num2 = " << num2 << endl;//return; 返回值不需要的时候,可以不写return
}int main() {int a = 10;int b = 20;cout << "a = " << a << endl;cout << "b = " << b << endl;//当我们做值传递的时候,函数的形参发生改变,并不会影响实参swap3(a, b);cout << "a = " << a << endl;cout << "b = " << b << endl;system("pause");return 0;
}

demo5:结构体中const: 将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来

#include<iostream>
using namespace std;
#include <string>//const的使用场景struct student
{//姓名string name;//年龄int age;//分数int score;
};//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
void printStudents(const student *s)
{//s->age = 150; //加入const之后,一旦有修改的操作就会报错,可以防止我们的误操作cout << "姓名: " << s->name << " 年龄: " << s->age << " 得分: " << s->score << endl;
}int main() {//创建结构体变量struct student s = { "张三" , 15 , 70 };//通过函数打印结构体变量信息printStudents(&s);cout << "main中张三年龄为: " << s.age << endl;system("pause");return 0;
}

demo4:结构体做参数(值传递和地址传递)

#include<iostream>
using namespace std;
#include <string>//定义学生结构体
struct student
{//姓名string name;//年龄int age;//分数int score;
};//打印学生信息函数
//1、值传递
void printStudent1(struct student s)
{s.age = 100;cout << "子函数中 姓名: " << s.name << " 年龄: " << s.age << " 分数: " << s.score << endl;
}//2、地址传递
void printStudent2(struct student * p)
{p->age = 200;cout << "子函数2中 姓名:" << p->name << " 年龄: " << p->age << " 分数:" << p->score << endl;}int main() {//结构体做函数参数//将学生传入到一个参数中,打印学生身上的所有信息//创建结构体变量struct student s;s.name = "张三";s.age = 20;s.score = 85;//printStudent1(s);printStudent2(&s);cout << "main函数中打印 姓名: " << s.name << " 年龄: " << s.age << " 分数:" << s.score << endl;system("pause");return 0;
}

demo3:结构体指针;使用一个指针接受结构体对象,类似与数组的声明    //通过结构体指针 访问结构体中的属性,需要利用 ' -> '

#include<iostream>
using namespace std;
#include <string>//结构体指针//定义学生结构体
struct stu
{string name;  //姓名int age;   //年龄int score; //分数
};int main() {//1、创建学生结构体变量stu s = { "张三" , 18 , 100 };//2、通过指针指向结构体变量stu * p = &s;//3、通过指针访问结构体变量中的数据//通过结构体指针 访问结构体中的属性,需要利用 ' -> 'cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;system("pause");return 0;
}

demo2:结构体数组复制,替换stuArray[2]覆盖原始值,重新计算得到新值赋值

#include<iostream>
using namespace std;
#include <string>//结构体数组
//1、定义结构体
struct Student
{//姓名string name;//年龄int age;//分数int score;
};int main() {//2、创建结构体数组struct Student stuArray[3] ={{ "张三", 18 , 100 },{ "李四",28 , 99 },{ "王五",38 , 66 }};//3、给结构体数组中的元素赋值stuArray[2].name = "赵六";stuArray[2].age = 80;stuArray[2].score = 60;//4、遍历结构体数组for (int i = 0; i < 3; i++){cout << " 姓名: " << stuArray[i].name<< " 年龄: " << stuArray[i].age<< " 分数: " << stuArray[i].score << endl;}system("pause");return 0;
}

demo1(结构体嵌套,结构体数组做参数):教师结构体:包含教师的学生,姓名;学生结构体包含:学生姓名分数。注:结构体有交叉。

#include<iostream>
using namespace std;
#include <string>
#include <ctime>//学生的结构体
struct Student
{//姓名string sName;//分数int score;
};//老师的结构体定义
struct Teacher
{//姓名string tName;//学生数组struct Student sArray[5];
};//给老师和学生赋值的函数
void allocateSpace(struct Teacher tArray[], int len)
{string nameSeed = "ABCDE";//给老师开始赋值for (int i = 0; i < len; i++){tArray[i].tName = "Teacher_";tArray[i].tName += nameSeed[i];//通过循环给每名老师所带的学生赋值for (int j = 0; j < 5; j++){tArray[i].sArray[j].sName = "Student_";tArray[i].sArray[j].sName += nameSeed[j];int random = rand() % 61 + 40; // 40 ~ 100tArray[i].sArray[j].score = random;}}
}//打印所有信息
void printInfo(struct Teacher tArray[], int len)
{for (int i = 0; i < len; i++){cout << "老师姓名: " << tArray[i].tName << endl;for (int j = 0; j < 5; j++){cout << "\t学生姓名: " << tArray[i].sArray[j].sName <<" 考试分数: " << tArray[i].sArray[j].score << endl;}}
}int main() {//随机数种子srand((unsigned int)time(NULL));//1、创建3名老师的数组struct Teacher tArray[3];//2、通过函数给3名老师的信息赋值,并给老师带的学生信息赋值int len = sizeof(tArray) / sizeof(tArray[0]);allocateSpace(tArray, len);//3、打印所有老师及所带的学生信息printInfo(tArray, len);system("pause");return 0;
}

demo0: 游戏英雄(结构体)编程练习。使用冒泡排序,年龄升序排列。

#include<iostream>
using namespace std;
#include <string>//1、设计英雄结构体
//英雄结构体
struct Hero
{//姓名string name;//年龄int age;//性别string sex;
};//冒泡排序 实现年龄升序排列
void bubbleSort(struct Hero heroArray[], int len)
{for (int i = 0; i < len - 1; i++){for (int j = 0; j < len - i - 1; j++){//如果j 下标的元素年龄 大于 j+1下标的元素的年龄 ,交换两个元素if (heroArray[j].age > heroArray[j + 1].age){struct Hero temp = heroArray[j];heroArray[j] = heroArray[j + 1];heroArray[j + 1] = temp;}}}
}//打印排序后数组中的信息
void printHero(struct Hero heroArray[], int len)
{for (int i = 0; i < len; i++){cout << "姓名: " << heroArray[i].name << " 年龄: " << heroArray[i].age<< " 性别: " << heroArray[i].sex << endl;}}int main() {//2、创建数组存放5名英雄struct Hero heroArray[5] ={{ "刘备",23,"男" },{ "关羽",22,"男" },{ "张飞",20,"男" },{ "赵云",21,"男" },{ "貂蝉",19,"女" },};int len = sizeof(heroArray) / sizeof(heroArray[0]);cout << "排序前打印: " << endl;for (int i = 0; i < len; i++){cout << "姓名: " << heroArray[i].name << " 年龄: " << heroArray[i].age<< " 性别: " << heroArray[i].sex << endl;}//3、对数组进行排序,按照年龄进行升序排序bubbleSort(heroArray, len);cout << "排序后打印: " << endl;//4、将排序后结果打印输出printHero(heroArray, len);system("pause");return 0;
}

【C++】【一】结构体数组相关推荐

  1. 【HDU】1305 Immediate Decodability(字典树:结构体数组,二维数组,链表/指针)

    一.用的二维数组 #include <iostream> #include <cstring> #include <algorithm> using namespa ...

  2. 【HDU】1251统计难题 (字典树:二维数组,结构体数组,链表,map)

    使用二维数组或者结构体数组都可以,但是在计数的时候有一点点小区别 一.结构体数组 #include <cstdio> #include <cstring> #include & ...

  3. 使用结构体数组统计男、女人数,计算全体学生的平均年龄、平均成绩,并将高于平均成绩的学生信息输出

    <程序设计基础-c语言>杨莉 刘鸿翔 ISBN-978-7-03-032903-5 p165 习题6 3.输入10个学生的信息(包括学号.姓名.性别.年龄.成绩)组成结构体数组,分别统计男 ...

  4. matlab多维数组、结构体数组

    1.多维数组 第三维称为页,需要注意的是每一页存放的二维数组维度要一致,也就是行列数要一致... 1 2 3 4 5 6 7 a=[1,2;     3,4]; b=[2,2;     5,6]; A ...

  5. c拆分字符串,并按照指定格式存入结构体数组

    c将字符串拆分,并按照指定格式存入结构体数组 函数功能 代码实现 结果显示 函数功能 字符串格式:username1,password1;username2,password2; 结构体格式: typ ...

  6. 【C 语言】结构体 ( 结构体 数组 作为函数参数 | 数组 在 堆内存创建 )

    文章目录 一.结构体 数组 作为函数参数 ( 数组 在 堆内存创建 ) 二.完整代码示例 一.结构体 数组 作为函数参数 ( 数组 在 堆内存创建 ) 在上一篇博客 [C 语言]结构体 ( 结构体 数 ...

  7. 【C 语言】文件操作 ( 读取文件中的结构体数组 | feof 函数使用注意事项 )

    文章目录 一.读取文件中的结构体数组 | feof 函数使用注意事项 二.代码示例 一.读取文件中的结构体数组 | feof 函数使用注意事项 读取文件结构体时 , 可以循环读取文件中的数据 , 只使 ...

  8. golang结构体数组

    转自: https://www.liaotaoo.cn/200.html package mainimport "fmt"type student struct{id intnam ...

  9. C语言结构体和结构体数组示例 - Win32窗口程序演示

    C语言结构体和结构体数组的使用: /* C结构体和结构体数组示例,by bobo */#include <windows.h>LRESULT CALLBACK WndProc (HWND, ...

最新文章

  1. 四. 常见H.264视频编解码器(X264和JM)及参考软件JM的下载与编解码
  2. 023_Jedis的发布和订阅
  3. 数博会重磅活动:第二届大数据科学与工程国际会议日程
  4. CentOS7.3上部署安装Oracle12c
  5. How to make an app
  6. android打印处理服务已停止,Print Spooler服务停止 打印机服务无法启动的完美解决方案共享...
  7. Win10怎么打开管理员命令提示符窗口
  8. 【CDN学习笔记6】CDN回源到阿里云主机被拒绝的案例
  9. 男cd是啥意思_CD伪娘是什么意思?
  10. 关于自然语言理解的一些理解
  11. 火车采集器V2010免费版下载
  12. Miles per gallon to kilometers per liter
  13. [深度应用]·DC竞赛轴承故障检测开源Baseline(基于Keras1D卷积 val_acc:0.99780)
  14. threejs examples 学习
  15. win10+ ubuntu16.04 双系统及无线、输入法、deepin-wineQQ微信等配置(亲测)
  16. 【转载】知识普及:天煞的HTML5到底是个什么东西
  17. ubuntu 下 电驴下载及配置
  18. matlab中不能找到ccs,搭建matlab连接ccs生成28335代码的环境(路径不在C盘)
  19. 找不到dns linux,linux – 服务器找不到XXX.in-addr.arpa:NXDOMAIN
  20. Web经典BS快速开发框架,强大后台+简洁UI一体化开发工具

热门文章

  1. python字符串操作
  2. 易语言win10写文件到c盘,Win10电脑怎么转移c盘文件?
  3. python networkx绘制图
  4. 使用Python,OpenCV从图像中删除轮廓
  5. 机器学习中的数学基础(4.1):支持向量机Support Vector Machine(SVM)
  6. yolov3网络结构笔记
  7. 二、如何保存MNIST数据集中train和test的图片?
  8. js去el的map_转:el表达式获取map对象的内容 js中使用el表达式 js 中使用jstl 实现 session.removeattribute...
  9. Unity从头到尾无代码游戏制作学习教程
  10. Substance Painter实时角色制作视频教程