您的位置:首页 > 编程语言 > C语言/C++

C语言第七天课堂笔记<详细+注释>

2015-11-10 21:12 405 查看
//声明一个匿名结构体 没有名字的结构体

struct {

char name[20]; // 注意: 中间用分号隔开

char sex;

int age;

}stu1 = {“wanglong”, ‘m’, 18},

stu2 = {“lutaotao”, ‘m’, 22};

// 起别名 给类型其别名

// 使用关键字 typedef 老名字 新名字;

typedef int Zhengxing;

// 给float 起个别名

typedef float Fudianxing;

// 给int array[5]起个别名 是给数组起个别名

typedef int MyArray[5];

// 声明结构体和其别名 分开写

// 描述一个男的

struct Man{

char name[20];

float height;

};

typedef struct Man Superman;

// 声明结构体和其别名 连一起写

typedef struct Woman{

char name[20];
char sex;


}SuperWoman;

int main(int argc, const char * argv[]) {

//

// int array[5] = {1, 2, 3, 4, 5};

// MyArray haha = {1, 2, 3, 4, 5};

//

// // 定义一个结构体变量

// // struct 结构体名 变量名 = {初值};

// // 初值的顺序 需要跟声明时相同

//

//

// // 初值为自己和同学的相关信息

// 因为有别名 所以 struct Student 可以用student代替


// struct Student zhangpeng = {“zhang”, ‘x’, 77, 99.9, 22};

// struct Student mazegong = {“马泽功”, ‘x’, 88, 99.99, 28};

// struct Student qiaodi = {“乔砥”, ‘x’, 88, 100.0, 23};

// struct Student lutaotao = { “陆逃逃”, ‘x’, 66, 88.8, 23};

////

//

//

// // 取出结构体中成员变量的值 结构体变量.成员变量

// printf(“%s\n”,lutaotao.name);

// printf(“%d\n”,lutaotao.age);

//

// // 修改结构体变量的成员变量的值

// lutaotao.age = 25;

// printf(“%d\n”,lutaotao.age);

//

// // 修改名字 字符串拷贝

// strcpy(lutaotao.name, “taotaolu”);

// printf(“%s\n”,lutaotao.name);

//

// // 编写一个 打印结构体的所有信息

// // 声明

// // 变量的数据类型: 变量名以前 全是类型

// printStudent(lutaotao);

//  结构体变量可以直接复制 复制过程 是一个拷贝的过程
//  数组 不能直接复制 数组名字是首元素的地址 是常量 是程序运行期间不能更改的


// lutaotao = mazegong;

// printStudent(lutaotao);

//

//

// // 有三个学生,变成找出分数最高者以及年龄最小者

// // 声明三个学生

// student stu1 = {“lutaotao”, ‘m’, 987068393, 80, 22};

// student stu2 = {“qiaodi”, ‘m’, 546512135, 90, 20};

// student stu3 = {“mazegong”, ‘m’, 987068393, 100, 28};

//

// // 结构体数组

// student studentArray[3] = {stu1, stu2, stu3};

//

//

// // 找最高分 再根据最高分找人

// // 找最高分

// float maxScore = 0;

// maxScore = stu1.score > stu2.score ? stu1.score :stu2.score;

// maxScore = maxScore > stu3.score ? maxScore : stu3.score;

// // 根据分找人

// if (maxScore == stu1.score) {

// printf(“%s\n”,stu1.name);

// printStudent(stu1);

// }else if (maxScore == stu2.score)

// {

// printf(“%s\n”,stu2.name);

// printStudent(stu2);

// }else{

// printf(“%s\n”,stu3.name);

// printStudent(stu3);

// }

// printf(“\n”);

//

// // 找出最小年龄

// int minAge = 0;

// minAge = stu1.age < stu2.age ? stu1.age :stu2.age;

// minAge = minAge < stu3.age ? minAge : stu3.age;

// // 根据最小年龄找人

// if (minAge == stu1.age) {

// printf(“%s”,stu1.name);

// }else if (minAge == stu2.age)

// {

// printf(“%s”,stu2.name);

// }else{

// printf(“%s”,stu3.name);

// }

//

//

// printf(“\n%d”,(int)sizeof(student));

//  需求: 描述一个人名字与出生年月日以及家庭成员
//  声明一个Person的结构体变量
//  声明一个结构体变量


// date d1 = {1997, 8, 24};

// families f1 = {“mom”, “dad”, “sisiter”, “brother”};

// person p1 = {“wanglong”,d1,f1};

// // 访问名字

// printf(“%s “,p1.name);

// printf(“%s”,p1.families.father);

//

student stu1 = {"wanglong", 'm', 100, 88.8, 18};
student stu2 = {"lutaotao", 'm', 101, 85.9, 19};
student stu3 = {"mazegong", 'm', 102, 89.0, 20};
student stu4 = {"liuhang ", 'm', 103, 90.8, 21};
student stu5 = {"qiaodi  ", 'm', 104, 95.8, 22};

//  把这五个学生信息放入一个数组中

student studengtArray[5] = {stu1, stu2, stu3, stu4, stu5};


// student tempStu3 = studengtArray[2];

// printf(“%s”,tempStu3.name);

// 或者直接访问

// printf(“%s”,studengtArray[2].name);

// 有5名学⽣保存在结构体数组中,编程查找

// 成绩最⾼者,输出该学⽣全部信息

// 对上述5名学⽣数组,按成绩从⾼到低排序,

// 并输出

// 调用排序

sortScore(studengtArray, 5);

//  答应最高分的那个人信息
//  是打印排序后的数组的第一个元素
printStudent(studengtArray[0]);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: