1、结构体
- 结构体声明
struct point
{
float x;
float y;
};
- 结构体定义变量
struct point p1={1,2};
- 通过定义结构体可以实现数组的直接赋值,例:
struct array5{
int a[5];
};
struct array5 array5={1,2,3,4,5};
struct array5 array6=array5;
- 结构体数组
与循环结构配合可简化多个结构体变量的操作
例:
struct point pArr[3]={{1,2},{2,3},{5,6}};
printf("%d",pArr[0].x); //打印第一个point中x的值,此处结果为1
- 结构体的空间大小
结构体的大小不是结构体元素单纯相加就行的,因为我们主流的计算机使用的都是32bit字长的CPU,对这类型的CPU取4个字节的数要比取一个字节要高效,也更方便。所以在结构体中每个成员的首地址都是4的整数倍的话,取数据元素时就会相对更高效,这就是内存对齐的由来。每个特定平台上的编译器都有自己的默认“对齐系数”(也叫对齐模数)。程序员可以通过预编译命令#pragma pack(n),n=1,2,4,8,16来改变这一系数,其中的n就是你要指定的“对齐系数”。
printf("%lu",sizeof(struct array5));//上例的结果为8
2、typedef
- 语法: typedef 原类型名 新类型名
- 如:
typedef int int1;
- 定义自定义数组类型:
int arr[3]={1,2,3};
typedef int array[3];//注意[3]的位置
array arr1={1,2,3};
3、typedef与结构体结合使用
前例可以改写为
typedef struct point
{
float x;
float y;
}point;
point p1={1,2};//可以直接用自定义类型名point定义变量
昨日的部分练习:
//
// main.m
// C07
//
// Created by 严诚 on 15/7/22.
// Copyright (c) 2015年 严诚. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef struct student{
int Num;
int age;
int score;
}student;
typedef struct ymd1{
int year;
int month;
int day;
}ymd1;
//定义⼀个结构体变量(包括年、⽉、日),计算该⽇在本年中为第⼏天?(注意考虑闰年问题),要求写⼀一个函数days,实现上⾯的计算。 由主函数将年⽉⽇传递给days函数,计算后将⽇子传递回主函数输出。
int day(ymd1 ymd){
int allday = 0;
int daysCount[2][13] =
{
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int leapYear = ((ymd.year % 4==0)&&(ymd.year % 100 != 0)) || (ymd.year %400 == 0);
for (int i = 1; i < ymd.month; i++)
{
allday += daysCount[leapYear][i];
}
allday += ymd.day;
return allday;
}
//仿写strcpy
void strcpy1(char *a, char *b){
for (int i=0; i<strlen(a); i++) {
a[i]='\0';
}
for (int i=0; i<strlen(b); i++) {
a[i]=b[i];
}
a[strlen(b)]='\0';
}
//仿写strlen
int strlen1(char *a){
int count=0;
for (int i=0; ; i++) {
if (a[i]!='\0') {
count++;
}else{
break;
}
}
return count;
}
void student1(){
int maxscore=0,minage=99;
student stu[3]={{001,19,97},{002,20,96},{003,21,99}};
for (int i=0; i<3; i++) {
if (maxscore<stu[i].score) {
maxscore=stu[i].score;
}
if (minage>stu[i].age) {
minage =stu[i].age;
}
}
printf("---minage=%d maxscore=%d\n",minage,maxscore);
for (int i=0; i<3-1; i++) {
for (int j=0; j<3-1-i; j++) {
student temp;
if (stu[j].score < stu[j+1].score ){
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
for (int i=0; i<3; i++) {
printf("%d %d %d\n",stu[i].Num,stu[i].age,stu[i].score);
}
}
void test1(){
int minage=99;
int maxacore=0;
NSLog(@"Hello, World!");
student stu1={001,19,97};
student stu2={001,20,98};
student stu3={001,21,99};
if (stu1.age>stu2.age) {
minage=stu2.age;
}else minage=stu1.age;
if (stu3.age<minage) {
minage=stu3.age;
}
if (stu1.score<stu2.score) {
maxacore=stu2.score;
}else maxacore=stu1.score;
if (stu3.age>minage) {
maxacore=stu3.score;
}
printf("%d %d\n",minage,maxacore);
char a[]="wo de mingzi shi yancheng";
char b[]="my name is yanccheng";
strcpy1(a,b);
puts(a);
int count = strlen1(a);
printf("%d\n",count);
}
//某班有5个学⽣生,三⻔门课。分别编写3个函数实现以下要求: (1) 求各⻔门课的平均分;(2) 找出有两⻔门以上不及格的学⽣生,并输出其学号和不及格课程的成 绩;(3) 找出三⻔门课平均成绩在85-90分的学⽣生,并输出其学号和姓名
typedef struct student_{
int Num;
int age;
int math_score;
int en_score;
int cs_score;
}student_;
void avg(student_ *a){
int score_math=0;
int score_en=0;
int score_cs=0;
for (int i=0; i<3; i++) {
score_math+=a[i].math_score;
score_en+=a[i].en_score;
score_cs+=a[i].cs_score;
}
printf("avg_math=%.2f avg_en=%.2f avg_cs=%.2f\n",score_math/3.0,score_en/3.0,score_cs/3.0);
}
void under60(student_ *a){
int count=0;
for (int i=0; i<3; i++) {
if (a[i].math_score<60) {
count++;
}
if (a[i].en_score<60) {
count++;
}
if (a[i].cs_score<60) {
count++;
}
if (count>=2) {
printf("学生%d有两门以上不及格\n",a[i].Num);
}
}
}
void avgstudent(student_ *a){
int avg=0,i;
for (i=0; i<3; i++) {
avg=(a[i].math_score + a[i].en_score + a[i].cs_score)/3;
if (avg>=85&&avg<=90) {
printf("学生%d的平均成绩为%d",a[i].Num,avg);
}
}
}
void studenttest(){
student_ stu[3]={{001,19,97,80,95},{002,20,90,80,92},{003,21,99,81,99}};
avg(stu);
under60(stu);
avgstudent(stu);
}
//模拟n个⼈人参加选举的过程,并输出选举结果:假设候选⼈人有四⼈人,分别⽤用A、B、C、D表⽰示,当选某候选⼈人时直接输入其编号(编号由计算机随机产⽣生),若输⼊入的不是A、B、C、D则视为⽆无效票,选举结束 后按得票数从⾼高到低输出候选⼈人编号和所得票数。
typedef struct voter{
char name;
int voteCount;
}voter;
void vote(){
int i=100;
voter voters[4]={{'A',0},{'B',0},{'C',0},{'D',0}};
while (i>0) {
i--;
char vot=arc4random()%5+65;
if (vot==voters[0].name) {
voters[0].voteCount++;
}else if (vot==voters[1].name) {
voters[1].voteCount++;
}else if (vot==voters[2].name) {
voters[2].voteCount++;
}else if (vot==voters[3].name) {
voters[3].voteCount++;
}else printf("无效票");
printf("当前选票为%c\n选手得票数分别为:",vot);
for (int k=0; k<4; k++) {
printf("%d ",voters[k].voteCount);
}
printf("\n");
}
}
void decimalToHEX(unsigned int value){
unsigned int temp;
temp = value/16;
if ( temp !=0 ) {
decimalToHEX(temp);
}
putchar( (value%16)["0123456789ABCDEF"] );
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
//test1();
//student1();
//return 0;
//studenttest();
//vote();
//printf("%lu",sizeof(struct voter));
decimalToHEX(50);
}
}