|
@@ -0,0 +1,93 @@
|
|
|
+#include <iostream>
|
|
|
+#include <ctime>
|
|
|
+
|
|
|
+#if(0)
|
|
|
+
|
|
|
+//结构体案例1
|
|
|
+//案例描述:
|
|
|
+//学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:
|
|
|
+//设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员
|
|
|
+//学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值
|
|
|
+//最终打印出老师数据以及老师所带的学生数据
|
|
|
+
|
|
|
+
|
|
|
+//学生的结构体
|
|
|
+struct Student
|
|
|
+{
|
|
|
+ std::string SName; //学生姓名
|
|
|
+
|
|
|
+ int score; //学生分数
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+//老师的结构体定义
|
|
|
+struct Teacher
|
|
|
+{
|
|
|
+ std::string TName; //姓名
|
|
|
+
|
|
|
+ struct Student SArray[5];
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+//给老师和学生赋值的函数
|
|
|
+void allocateSpace(struct Teacher TArray[] , int len)
|
|
|
+{
|
|
|
+ std::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 ~ 100
|
|
|
+
|
|
|
+ TArray[i].SArray[j].score = random;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//打印所有信息的函数
|
|
|
+void printInfo(struct Teacher TArray[], int len)
|
|
|
+{
|
|
|
+ for (int i = 0; i < len; i++)
|
|
|
+ {
|
|
|
+ std::cout << "老师姓名:" << TArray[i].TName << std::endl;
|
|
|
+
|
|
|
+ for (int j = 0; j < 5; j++)
|
|
|
+ {
|
|
|
+ std::cout << "\t学生姓名:" << TArray[i].SArray[j].SName << "考试分数:" << TArray[i].SArray[j].score << std::endl;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+int main()
|
|
|
+{
|
|
|
+ //创建一个随机种子
|
|
|
+ srand((unsigned int)time(NULL));
|
|
|
+
|
|
|
+ //1、创建三名老师的数组
|
|
|
+ struct Teacher TArray[3];
|
|
|
+
|
|
|
+ //2、通过函数给3名老师的信息赋值 并给老师带的学生信息赋值
|
|
|
+ int len = sizeof(TArray) / sizeof(TArray[0]);
|
|
|
+ allocateSpace(TArray, len);
|
|
|
+
|
|
|
+ //3、打印所有老师及所带的学生信
|
|
|
+ printInfo(TArray, len);
|
|
|
+
|
|
|
+
|
|
|
+ system("pause");
|
|
|
+
|
|
|
+ return 0;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+#endif
|