设为首页 加入收藏

TOP

C 开发学习 - 结构类型(枚举/结构/类型定义)(二)
2015-11-19 23:06:54 来源: 作者: 【 】 浏览:18
Tags:开发 学习 结构 类型 枚举 定义
nt 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. , 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 
  
   

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
, p->x, p->y);
    return p;
}

void output(struct point p){
    printf(%d, %d
,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 
  
   

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
, testTime[i].hour,testTime[i].minutes, testTime[i].seconds);
        
        testTime[i] = timeUpdate(testTime[i]);
        
        printf(... one second later it's %.2i:%.2i:%.2i
, 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不是指针




首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言强化(二)设计可以求最小元.. 下一篇linux用c语言获取系统启动时长

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: