#import <Foundation/Foundation.h>
//定义结构体
struct student {
//结构体成员
char name[10]; //存储姓名
int age; //存储年龄
float score; //存储成绩
};//分号是结构体定义的结束标志,必不可少.
//给学生结构体类型重新起一个名字
typedef struct student Student;
//定义一个点结构体
struct point {
float x; //存储横坐标
float y; //存储纵坐标
};
typedef struct point point;
//定义一个矩形结构体
struct rect {
float length; //存储长
float wide; //存储宽
};
typedef struct rect rect;
//结构体的作用:
//1.结构体是一种自定义的数据类型,可以用来定义变量.
//2.结构体和数组一样都是大容器,可以存储多个变量,但是结构体比数组灵活,可以存储不同类型的数据.
struct date {
int year; //年
int month; //月
int day; //日
};
//1.typedef : 先定义结构体,再重新起名字
typedef struct date Date;
//2.tyoedef : 定义结构体的同时重新起名
typedef struct person{
char name[10]; //姓名
char gender; //性别
int age; //年龄
Date date; //存储出生日期
}Person;
//总结输出格式:
//%d --- int, short, BOOL
//%ld --- long
//%c --- char
//%s --- 字符串
//%lu --- unsigned long
//%f --- 浮点型
int main(int argc, const char * argv[])
{
//结构体嵌套: 某一个结构体的结构体成员又是另一个结构体的变量.
Person per = {"xiaoguang", 'm', 18, {2014, 12, 9}};
printf("year = %d\n", per.date.year);
printf("date = %d %d %d\nname = %s\nage = %d\ngender = %c\n", per.date.year, per.date.month, per.date.day, per.name, per.age, per.gender);
//结构体的内存对齐方式
//以结构体成员中所规定存储空间最大的数据类型为基本单位分配.
// printf("%lu", sizeof(Person));
//存储5个Person变量
//结构体数组
/*
Person stu[5] = {
{"yanhui", 'f', 38},
{"liutao", 'm', 9527},
{"axing", 'm', 18},
{"zhangjie", 'f', 20},
{"xiena", 'f', 213}
};
//将人按年龄升序排序
for (int i = 0; i < 5 - 1; i++) {
for (int j = 0; j < 5 - 1 - i; j++) {
if (stu[j].age < stu[j + 1].age) {
//两个结构体变量可以直接赋值
Person temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
for (int i = 0; i < 5; i++) {
printf("%s %c %d\n", stu[i].name, stu[i].gender, stu[i].age);
}
*/
/*
printf("%s\n", stu[2].name);
printf("%d\n", stu[1].age);
printf("%c\n", stu[2].gender);
printf("%c\n", stu[4].gender);
printf("%d\n", stu[0].age);
*/
/*
//通过结构体类型定义结构体变量
//结构体类型: struct + 结构体名字
laoWang stu = {"Duck", 38, 59.9};
struct student stu1 = {"Luck", 45, 65};
//访问结构体成员? 结构体变量,结构体成员
printf("%d\n", stu.age);
printf("%s\n", stu.name);
printf("%.2f\n", stu.score);
printf("%d\n%s\n%.2f\n", stu1.age, stu1.name, stu1.score);
stu.score = 100; //将stu里的成绩改成100.
strcpy(stu.name, "lool"); //给字符数组赋值字符串要使用strcpy
printf("%s\n", stu.name);
point poi = {25.5, 36.5};
struct point poi1 = {33.6, 55.4};
printf("%.2f\n", poi.x);
printf("%.2f\n", poi.y);
printf("%.2f\n%.2f\n", poi1.x, poi1.y);
rect rec = {24.5, 39.4};
struct rect rec1 = {36.8, 66.4};
printf("%.2f\n", rec.length);
printf("%.2f\n", rec.wide);
printf("%.2f\n%.2f\n", rec1.length, rec1.wide);
*/
//定义三个学生结构体变量,通过比较找出成绩最高者,以及年龄最小者.
Student stu1 = {"xiaoming", 20, 90};
Student stu2 = {"xiaohong", 18, 100};
Student stu3 = {"xiaoli", 25, 60};
/*
int temp = stu1.age;
if (temp > stu2.age) {
temp = stu2.age;
}
if (temp > stu3.age) {
temp = stu3.age;
}
switch (temp) {
case 20:
printf("年龄最小的是%s\n", stu1.name);
break;
case 18:
printf("年龄最小的是%s\n", stu2.name);
break;
case 25:
printf("年龄最小的是%sn", stu3.name);
break;
default:
break;
}
int temp1 = stu1.score;
if (temp1 < stu2.score) {
temp1 = stu2.score;
}
if (temp1 < stu3.score) {
temp1 = stu3.score;
}
*/
/*
Student maxScoreStu = {0}; //存储成绩最高者
//整体与部分
//stu1,stu2,stu3都是一个整体,而name,age,score只是整体的一部分,比较整体,保留部分
if (stu1.score > stu2.score) {
maxScoreStu = stu1;
} else {
maxScoreStu = stu2;
}
if (maxScoreStu.score < stu3.score) {
maxScoreStu = stu3;
}
printf("%s\n", maxScoreStu.name);
Student minAgeStu = {0};
if (stu1.age < stu2.age) {
minAgeStu = stu1;
} else {
minAgeStu = stu2;
}
if (minAgeStu.age > stu3.age) {
minAgeStu = stu3;
}
printf("%s\n", minAgeStu.name);
//查找姓名最大者
Student maxNameStu = {0};
if (strcmp(stu1.name, stu2.name) > 0) {
maxNameStu = stu1;
} else {
maxNameStu = stu2;
}
if (strcmp(maxNameStu.name, stu3.name) < 0) {
maxNameStu = stu3;
}
printf("%s\n", maxNameStu.name);
*/
return 0;
}
******************************************************************************************************************************************************************************************************
Homework
#import <Foundation/Foundation.h>
typedef struct student{
char name[10];
char gender;
int age;
float score;
}Student;
typedef struct date{
int year;
int month;
int day;
}Date;
int main(int argc, const char * argv[])
{
// 1.有5名学生保存在结构体数组中,编程查找年龄最小者,输出该学生全部信息,学生结构体数组再按姓名进行升序排序, 按年龄降序排序,按成绩升序排序
/*
Student stu[5] = {
{"sherlock", 'm', 43, 96.5},
{"jack", 'm', 25, 97},
{"holmes", 'w', 30, 89.5},
{"lucy", 'w', 21, 96},
{"jokeer", 'm', 60, 87.5}
};
Student minAgeStu = stu[0];
for (int i = 0; i < 5; i++) {
if (minAgeStu.age > stu[i].age) {
minAgeStu = stu[i];
}
}
printf("minAgeStu : %s %c %d %.2f\n", minAgeStu.name, minAgeStu.gender, minAgeStu.age, minAgeStu.score);
for (int i = 0; i < 5 - 1; i++) {
for (int j = 0; j < 5 - 1 - i; j++) {
if (strcmp(stu[j].name, stu[j + 1].name) > 0) {
Student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
for (int i = 0; i < 5; i++) {
printf("%s %c %d %.2f\n", stu[i].name, stu[i].gender, stu[i].age, stu[i].score);
}
printf("\n");
for (int i = 0; i < 5 - 1; i++) {
for (int j = 0; j < 5 - 1 - i; j++) {
if (stu[j].age < stu[j + 1].age) {
Student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
for (int i = 0; i < 5; i++) {
printf("%s %c %d %.2f\n", stu[i].name, stu[i].gender, stu[i].age, stu[i].score);
}
printf("\n");
for (int i = 0; i < 5 - 1; i++) {
for (int j = 0; j < 5 - 1 - i; j++) {
if (stu[j].score > stu[j + 1].score) {
Student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
for (int i = 0; i < 5; i++) {
printf("%s %c %d %.2f\n", stu[i].name, stu[i].gender, stu[i].age, stu[i].score);
}
*/
// 2、定义一个结构体变量(包含年月日),,计算该日期在本年中为第几天?
/*
Date date = {0};
int sum = 0;
printf("input a date\n");
scanf("%d%d%d", &date.year, &date.month, &date.day);
if (date.month < 8) {
if (date.month % 2 == 0 && date.month > 2) {
sum = 30 * (date.month - 1) + (date.month - 1) / 2 + 1 + date.day;
} else {
sum = 30 * (date.month - 1) + (date.month - 1) / 2 + date.day;
}
} else if (date.month % 2 == 0){
sum = 30 * (date.month - 1) + (date.month - 1) / 2 + 1 + date.day;
} else {
sum = 30 * (date.month - 1) + (date.month - 1) / 2 + 1 + date.day;
}
if (date.month > 2 &&((date.year % 4 == 0 && date.year % 100 != 0) || date.year % 400 == 0)) {
sum = sum - 1;
} else {
sum = sum - 2;
}
if (date.month <= 2) {
sum = (date.month - 1) * 31 + date.day;
}
printf("%d %d %d %d\n", date.year, date.month, date.day, sum);
*/
Date date = {0};
int sum = 0;
int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("input a date:\n");
scanf("%d%d%d", &date.year, &date.month, &date.day);
if ((date.year % 4 == 0 && date.year % 100 != 0) || date.year % 400 == 0) {
day[1] = 29;
}
for (int i = 0; i < date.month - 1; i++) {
sum += day[i];
}
sum += date.day;
if (date.month > 12 || date.day > day[date.month - 1]) {
printf("error\n");
} else {
printf("%d %d %d %d\n", date.year, date.month, date.day, sum);
}
return 0;
}