C语言的两个小问题,请指教,谢谢

来源:百度知道 编辑:UC知道 时间:2024/07/06 19:55:57
case -32:
ch=getch();
if(ch==77) //Right
{
if(month==12)
year++;
month=month%12+1;
}

else if(ch==75) //Left
{
if(month==1)
year--;
month=(month-2+12)%12+1;
}
else if(ch==72) //Up
{
year--;
}
else if(ch==80) //Down
year++;
}
上断程序的CASE:-32 是什么作用,上下左右的键值为什么不能直接用,要用这种方式才行呢?

void time()
{
time_t now = time(NULL);
tm *t = localtime(&now);
printf("\n现在时间:\n %d年%d月%d日 %d:%d\n\n",
t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min);
}

这个时间的函数,能不能详细的解释一下,我没找到具体的参考书,谢谢啊!
第一个程序,我是想知道,使用上下左右键来操作,为什么先要
-32,这个的用处是什么?用键盘上的字母之类的就没这个问题啊

按上下左右键,getch()会返回两个键值的,一个是-32(其实是224,估计楼主的程序里,赋值给char类型的了),后面还有一个是区分上下左右的。
而普通的键,getch()只有一个键值的,你可以写个小程序测试一下,就清楚了

#include<stdio.h>
#include<conio.h>

int main()
{
int c;

while((c=getch())!=27)//27表示ESC
{
printf("%d ",c);
}
return 0;
}

按A,B,C输出为:
97 98 99
按上下左右按键,输出为:
224 72 224 80 224 75 224 77

第一个问题
switch(*)
*为int类型的变量
case 语句后加-32 表示当变量值为-32的时候运行下面的语句

第二个问题
函数time能够获取系统时间。(返回的是自1970.1.1以来走过的
秒数)
原型: time_t time(time_t *timer);
函数localtime
能够把精确到秒的时间值转换为tm型结构体,并
校正环境时区。
原型: tm* localtime(const time_t *timer);
另外tm类型结构体定义如下:
struct tm
{
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon