首页 > 编程语言 >C++ 基础语法2

C++ 基础语法2

时间:2023-01-20 18:32:21浏览次数:37  
标签:arr struct int 基础 C++ 语法 len student cout


#include<iostream>
using namespace std;

#include<string>
#include<time.h>

//结构体定义
//struct Student
//{
// string name;
// int age;
// int count;
//};
//在结构体尾处可以顺便创建结构体变量 struct{....}s3;
//int main()
//{
////结构体变量定义1//定义变量时struct可以省略不写
// struct Student s1;
////结构体变量的赋值
// s1.name = "张三";
// s1.age = 90;
// s1.count = 100;
////结构体访问
// cout << "姓名是:" << s1.name << "年龄是:" << s1.age << "成绩是:" << s1.count << endl;
//
////方法二:
// struct Student s2 = { "李四", 18, 90 };
// cout << "姓名是:" << s2.name << "年龄是:" << s2.age << "成绩是:" << s2.count << endl;
// return 0;
//}


//结构体数组
//struct student
//{
// string name;
// int age;
// int score;
//}s2;
//
//int main()
//{
// //创建结构体数组
// struct student stuarr[5] =
// {
// { "张三", 91, 100 },
// { "李四", 91, 100 },
// { "王五", 91, 100 },
// };
// //数组外部赋值
// stuarr[3].name = "赵柳";
// stuarr[3].age = 89;
// stuarr[3].score = 100;
// //数组遍历
// for (int i = 0; i < 5; i++)
// {
// cout << "姓名:" << stuarr[i].name << "年龄:" << stuarr[i].age << "分数:" << stuarr[i].score << endl;
// }
//
// //结构体指针
// student s2 = { "x", 100, 100 };
// student *p = &s2;
// cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
// return 0;
//}


//结构体嵌套
//struct student
//{
// string name;
// int age;
// int score;
//};
//struct teacher{
// int id;
// string name;
// int age;
// struct student stu;//辅导的学生 嵌套的结构体应该放在该嵌套体的前面
//};
//
//
//int main()
//{
// //创建嵌套结构体变量
// teacher t;
// t.id = 123456789;
// t.name = "xxxx";
// t.age = 50;
// t.stu.name = "小林";
// t.stu.age = 19;
// t.stu.score = 100;
// cout << "老师的id: " << t.id << " 老师的姓名: " << t.name << " 老师的年龄: " << t.age << endl;
// cout<< "老师的学生姓名: " << t.stu.name << " 学生年龄:" << t.stu.age << " 学生分数" << t.stu.score << endl;
// return 0;
//}


//结构体作参数
//struct student
//{
// string name;
// int age;
// int score;
//};
////1.值传递
//void Printf1(student s)
// {
// cout << " xm " << s.name << " 年龄 " << s.age << " 分数 " << s.score << endl;
//}
// //2.地址传递
//void Printf2(const student *p)//const 固定地址的值使得不能修改 (struct可以省略不写)!
//{
// cout << " xm " << p->name<< " 年龄 " << p->age << " 分数 " << p->score << endl;
//}
//int main()
//{
// student s = { "整数", 15, 100 };
// Printf1(s);
// Printf2(&s);
// return 0;
//}




//结构体案例1
//3个老师分别带着5个学生 打印其信息
//struct student
//{
// string sname;
// int score;
//};
//
//struct teacher
//{
// string tname;
// struct student sarr[5];
//};
//
////赋值函数
//void fuzhi(struct teacher tarr[],int len)//接收老师的数组 与数组长度
//{
// string nameSeed = "ABCDE";//命名字符串
// string nameseed2 = "HELLO";
// for (int i = 0; i < len; i++)
// {
// //老师姓名赋值
// tarr[i].tname = "teacher_";
// tarr[i].tname += nameSeed[i];//在teacher_后面 追加 nameseed字符串的第i位字符 +=是表示追加的意思!!!!!!
//
// //通过循环给每名学生赋值
// for (int j = 0; j < 5; j++)
// {
// tarr[i].sarr[j].sname = "student_";
// tarr[i].sarr[j].sname += nameseed2[j];
//
// //分数赋值
//
// int random = rand() % 61;//生成随机数0-60
// random = random + 40;//41-100
// tarr[i].sarr[j].score = random;
//
// }
//
// }
//}
//
//
//void printof(struct teacher tarr[], int len)
//{
// for (int i = 0; i < len; i++)
// {
// cout << " 老师的姓名是: " << tarr[i].tname << endl;
// for (int j = 0; j < 5; j++)
// {
// cout << "\t学生姓名:" << tarr[i].sarr[j].sname << " 其分数是: " << tarr[i].sarr[j].score << endl;//\t 表示缩进
// }
// }
//}
//int main()
//{
// //随机数种子 rand()根据时间产生随机数
// srand((unsigned int)time(NULL));
//
// //创建3名老师的数组,
// teacher tarr[3];
// //通过函数给3名老师的数组赋值并且个学生分别赋值
// int len = sizeof(tarr) / sizeof(tarr[0]);
// fuzhi(tarr , len);
// //打印信息
// printof(tarr, len);
// return 0;
//}




//案例2
//5个数组通过冒泡排序的方法按年龄排序
//{"张三", 52, "男"},{ "李四", 78, "男" },{ "王五", 62, "男" },{ "请六", 30, "男" },{ "技术", 99, "男" }

struct ryuang

{

string name;

int age;

string sex;

};void bubblesort(struct ryuang arr[], int len)//arr[]代表传入的是一个 结构体 数组!!!!! 记得表示是哪个结构体!!!!

{

for (int i = 0; i < len - 1; i++)

{

for (int j = 0; j < len - 1 - i; j++)

{

//如果j下标的数>j+1下标的数交换位置

if (arr[j].age > arr[j + 1].age)

{

struct ryuang temp = arr[j];//结构体冒泡排序 struct ___ ___=arr[]; 和普通是int 一样

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}

}

}

}void printfmy(ryuang arr[], int len)

{

for (int i = 0; i < len; i++)

{

cout << " 姓名 " << arr[i].name << " 年龄 " << arr[i].age << " 性别 " << arr[i].sex << endl;

}

}

int main()

{

//定义人员的结构体数组!!!!!!

ryuang arr[5] =

{

{ "张三", 52, "男" },

{ "李四", 78, "男" },

{ "王五", 62, "男" },

{ "请六", 30, "男" },

{ "技术", 99, "男" }

};

int len = sizeof(arr) / sizeof(arr[0]);
cout << "排序前的结果:" << endl;
for (int i = 0; i < len; i++) //测试
{
cout << arr[i].name << arr[i].age << arr[i].sex << endl;
}
//冒泡排序
bubblesort(arr,len);//传入数组与数组长度

cout << "排序后的结果:" << endl;
//打印
printfmy(arr,len);
return 0;
}


标签:arr,struct,int,基础,C++,语法,len,student,cout
From: https://blog.51cto.com/u_15826146/6020735

相关文章

  • C++基础语法 3(面向对象、C++在执行过程当中4个区域、引用)
    #include<iostream>usingnamespacestd;//标注空间#include<string>#include<time.h>#include<cstdlib>//面向对象/*C++在执行过程当中4个区域:代码区:存放二进制代码,由操作......
  • C++基础语法4()
    #include<iostream>usingnamespacestd;//标注空间#include<string>#include<time.h>#include<cstdlib>//函数的提高1;函数的默认参数函数的形参可以有默认值!返回类型......
  • C++ 基础语法5(封装、访问权限)
    #include<iostream>usingnamespacestd;//标注空间#include<string>#include<time.h>#include<cstdlib>//C++当中面向对象三大特性:封装、继承、多态//C++当中万事万物都能......
  • c++中内存分类
    局部变量,存储位置:栈,存储时间:变量所在程序运行时间全局变量,存储位置:静态存储区,存储时间:程序运行时间static静态变量,存储位置:静态数据区,存储时间:第一次初始化到程序运行结......
  • C++基础
    I/Ocout保留有效数字问题C++中cout默认保留六位有效数字,并且进行四舍五入修改保留数的方法cout.precision(2);//用这个来改变默认保留几位小数cout<<"保留两位有效:......
  • python基础
    字面量被写下来的固定的值,成为字面量常见的6种值数字intfloatcomplex(复数,以j结尾表示复数),bool控制精度m.n m控制宽度(小数点也计入),设置的宽度小于数字自身不生......
  • java基础
    p460-470List类子类的底层逻辑JAVA基础快捷键ctrl+alt+L整理代码shift+F10运行ctrl+B定位找到方法ctrl+H查看一个类的层级关系ctrl+d复制当前代码项向下shift......
  • DB SQL 转 ES DSL(支持多种数据库常用查询、统计、平均值、最大值、最小值、求和语法)
    1.简介  日常开发中需要查询Elasticsearch中的数据时,一般会采用RestHighLevelClient高级客户端封装的API。项目中一般采用一种或多种关系型数据库(如:Mysql、PostgreSQL......
  • Python基础之模块
    目录Python基础之模块一、关于模块的基础知识1.模块的本质2.模块的分类2.两种导入模块的语句及判断执行文件语句二、Python常见内置模块1.collections模块2.time时间模块3.......
  • python/c++ 混合编程
    官方简介pybind11isalightweightheader-onlylibrarythatexposesC++typesinPythonandviceversa,mainlytocreatePythonbindingsofexistingC++code.......