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);
}
}