Reference:
《狂人C——程序员入门必备》
================================
1) 结构体struct跟数组、指针一样,此3哥们都是自定义数据类型。
2)自定义之后,享有同int、char等普通数据类型同等操作待遇。
================================
编程实例代码:
参考Reference中时间累积编程重写。
Platform environment:
CentOS 5.5 +GCC 4.1
================================
Code as follows:
#include <stdio.h>
#define secMax 60
#define minMax 60
#define hourMax 24
struct timeHMS
{
int second;
int minute;
int hour;
};
struct timeHMS timeAdd(struct timeHMS timeInit,struct timeHMS timeIncrease)
{
struct timeHMS timeFinal;
timeFinal.hour=timeInit.hour+timeIncrease.hour;
timeFinal.minute=timeInit.minute+timeIncrease.minute;
timeFinal.second=timeInit.second+timeIncrease.second;
timeFinal.minute+=timeFinal.second/secMax;
timeFinal.second%=minMax; // the final second value here
timeFinal.hour+=timeFinal.minute/minMax;
timeFinal.minute%=minMax; // the final minute value here
timeFinal.hour%=hourMax; // the final hour value here
return timeFinal;
};//timeAdd
int main()
{
struct timeHMS timeNow;
struct timeHMS timeInc;
struct timeHMS timeLater;
timeNow.hour=22;
timeNow.minute=59;
timeNow.second=58;
timeInc.hour=5;
timeInc.minute=48;
timeInc.second=39;
printf("time now is %d HH %d MM %d SS:/n",timeNow.hour,timeNow.minute,timeNow.second);
printf("time after:%d HH %d MM %d SS is:/n/n/t/t",timeInc.hour,timeInc.minute,timeInc.second);
timeLater= timeAdd(timeNow,timeInc);
printf("/t/t%d HH /t/t%d MM /t/t%d SS",timeLater.hour,timeLater.minute,timeLater.second);
return (0);
}//main
===========================================
上述程序经过5分钟排错,可以跑起来,有利于掌握C结构体学习。
运行结果如下:
//我暂时没权限上传图片,一周之后可以,^_^。