一、枚舉
//
// main.c
// enum
//
// Created by liuxinming on 15/4/26.
// Copyright (c) 2015年 liuxinming. All rights reserved.
//
#include <stdio.h>
enum COLOR {RED, YELLOW, GREEN, NumCOLORS};
int main(int argc, const char * argv[]) {
int color = -1;
char *ColorNames[NumCOLORS] = {
"red", "yellow", "green"
};
char *colorName = NULL;
printf("輸入你喜歡的顏色代碼:");
scanf("%d", &color);
if(color >= 0 && color < NumCOLORS){
colorName = ColorNames[color];
}else{
colorName = "unknown";
}
printf("你喜歡的顏色是%s\n", colorName);
return 0;
}
案例二:枚舉量
二、結構
結構中可以使用不同的數據類型。
申明結構的形式
struct point{
int x;
int y;
}
struct point p1, p2; #p1和p2都是point,裏面有x和y的值
struct {
int x;
int y;
}p1, p2;
#p1和p2都是一種無名結構,裏面有x和y的值
結構變量
struct point p; #p就是一個結構變量
p.x = 12;
p.y = 20;
案例一:使用結構體
//
// main.c
// structure
//
// Created by liuxinming on 15/4/12.
// Copyright (c) 2015年 liuxinming. All rights reserved.
//
#include <stdio.h>
//聲明結構類型
struct date{
int month;
int day;
int year;
};
int main(int argc, const char * argv[]) {
//結構變量&使用
struct date today;
today.month = 04;
today.day = 12;
today.year = 2015;
printf("Today is date is %i-%i-%i.\n",today.year, today.month, today.day);
return 0;
}
案列二:結構的初始化
//
// main.c
// structure
//
// Created by liuxinming on 15/4/12.
// Copyright (c) 2015年 liuxinming. All rights reserved.
//
#include <stdio.h>
//聲明結構類型
struct date{
int month;
int day;
int year;
};
int main(int argc, const char * argv[]) {
struct date today = {04, 26, 2015};
struct date thismonty = {.month = 4, .year = 2015};
printf("Today is date is %i-%i-%i.\n",today.year, today.month, today.day);
printf("This month is %i-%i-%i.\n", thismonty.year, thismonty.month, thismonty.day);
return 0;
}
輸出:
Today is date is 2015-4-26.
This month is 2015-4-0.
結構成員
結構運算
結構指針
//
// main.c
// structure
//
// Created by liuxinming on 15/4/12.
// Copyright (c) 2015年 liuxinming. All rights reserved.
//
#include <stdio.h>
//聲明結構類型
struct date{
int month;
int day;
int year;
};
int main(int argc, const char * argv[]) {
struct date today;
today = (struct date){04, 26, 2015};
struct date day;
struct date *pDate = &today;
printf("Today's date is %i-%i-%i.\n", today.year,today.month,today.day);
printf("The day's date is %i-%i-%i.\n", day.year, day.month, day.day);
printf("address of today is %p\n", pDate);
return 0;
}
輸出:Today's date is 2015-4-26.
The day's date is 0-1606416456-32767.
address of today is 0x7fff5fbff7f0
Program ended with exit code: 0
結構作爲函數參數
//
// main.c
// structure
//
// Created by liuxinming on 15/4/12.
// Copyright (c) 2015年 liuxinming. All rights reserved.
//
#include <stdio.h>
#include <stdbool.h>
//聲明結構類型
struct date{
int month;
int day;
int year;
};
bool isLeap(struct date d);//判斷是否爲閏年
int numberOfDays(struct date d);
int main(int argc, const char * argv[]) {
struct date today, tomorrow;
printf("Enter today's date(mm dd yyyy):");
scanf("%i %i %i", &today.month, &today.day, &today.year);
if(today.day != numberOfDays(today)){
tomorrow.day = today.day + 1;
tomorrow.month = today.month;
tomorrow.year = today.year;
} else if (today.month == 12){
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = today.year + 1;
} else{
tomorrow.day = 1;
tomorrow.month = today.month + 1;
tomorrow.year = today.year;
}
printf("Tomorrow's date is %i-%i-%i.\n", tomorrow.year, tomorrow.month, tomorrow.day);
return 0;
}
int numberOfDays(struct date d){
int days;
const int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
if(d.month == 2 && isLeap(d)){
days = 29;//閏年
} else{
days = daysPerMonth[d.month - 1];
}
return days;
}
bool isLeap(struct date d){
bool leap = false;
if ( (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0){
leap = true;
}
return leap;
}
輸出:
Enter today's date(mm dd yyyy):12 31 2014
Tomorrow's date is 2015-1-1.
Program ended with exit code: 0
指向結構的指針
struct date {
int month;
int day;
int year;
} myday;
struct date *p = &myday;
(*p).month = 12;
p->month = 12;
* 用->表示指針所指向的結構變量中的成員
//
// main.c
// structure
//
// Created by liuxinming on 15/4/12.
// Copyright (c) 2015年 liuxinming. All rights reserved.
//
#include <stdio.h>
struct point {
int x;
int y;
};
struct point* getStruct(struct point*);
void output(struct point);
void print(const struct point *p);
int main(int argc, const char * argv[]) {
struct point y = {0, 0};
getStruct(&y);
output(y);
output(*getStruct(&y));
print(getStruct(&y));
return 0;
}
struct point* getStruct(struct point *p){
scanf("%d", &p->x);
scanf("%d", &p->y);
printf("%d, %d\n", p->x, p->y);
return p;
}
void output(struct point p){
printf("%d, %d\n",p.x, p.y);
}
void print(const struct point *p){
printf("%d, %d",p->x, p->y);
}
輸出:
10
50
10, 50
10, 50
結構數組
struct date dates[100];
struct date dates[] = {
{4,5,2005},
{2,4,2005}
};
案例:
//
// main.c
// structure
//
// Created by liuxinming on 15/4/12.
// Copyright (c) 2015年 liuxinming. All rights reserved.
//
#include <stdio.h>
struct time{
int hour;
int minutes;
int seconds;
};
struct time timeUpdate(struct time now);
int main(void) {
struct time testTime[5] = {
{11,59,59},
{12,0,0},
{1,29,29},
{23,59,59},
{19,12,27}
};
int i;
for (i = 0; i < 5; i++) {
printf("Time is %.2i:%.2i:%.2i\n", testTime[i].hour,testTime[i].minutes, testTime[i].seconds);
testTime[i] = timeUpdate(testTime[i]);
printf("... one second later it's %.2i:%.2i:%.2i\n", testTime[i].hour,testTime[i].minutes, testTime[i].seconds);
}
return 0;
}
struct time timeUpdate(struct time now){
++ now.seconds;
if( now.seconds == 60){
now.seconds = 0;
++ now.minutes;
if (now.minutes == 60){
now.minutes = 0;
++ now.hour;
if(now.hour == 24){
now.hour = 0;
}
}
}
return now;
}
輸出:
Time is 11:59:59
... one second later it's 12:00:00
Time is 12:00:00
... one second later it's 12:00:01
Time is 01:29:29
... one second later it's 01:29:30
Time is 23:59:59
... one second later it's 00:00:00
Time is 19:12:27
... one second later it's 19:12:28
Program ended with exit code: 0
結構中的結構
struct dateAndTime{
struct date sdate;
struct time stime;
}
嵌套的結構
struct rectangle{
struct point pt1;
struct point pt2;
};
//如果有變量
struct rectangle r;
//就可以有:
r.pt1.x = 1;
r.pt1.y = 2;
r.pt2.x = 11;
r.pt2.y = 22;
//如果有變量定義
struct rectangle *rp;
rp = &r;
//那麼這四種形式是等價的
//r.pt1.x , rp->pt1.x, (r.pt1).x, (rp->pt1).x
//但是沒有rp->pt1->x 因爲pt1不是指針