标签:c
// // 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不是指针
标签:c
原文地址:http://blog.csdn.net/liuxinmingcode/article/details/45292769