枚举类型不怎么了解,不知道从什么地方入手
希望各位高手在下面能帮结构,关于枚举的函数原型,定义,声明写出来。
[此贴子已经被作者于2007-3-8 14:02:40编辑过]
[此贴子已经被作者于2007-3-8 14:02:40编辑过]
自己写了个简单的函数,想测试一下枚举的用法,半天还是没有任何的头绪,高手帮忙解答!!
#include<stdio.h>
enum WeekdayT Nextday( enum WeekdayT w);
int main()
{
enum WeekdayT{Sun,Mon , Thu ,Wed ,Thur ,Fri, Sat };
enum WeekdayT w;
w = Sun;
NextDay(w);
return 0;
}
enum WeekdayT NextDay(enum WeekdayT w)
{
if(w == Sat)
return Sun;
else return w+1;
}
================================
--------------------Configuration: 5 - Win32 Debug--------------------
Compiling...
data.c
D:\MyProjects\C\5\data.c(11) : warning C4013: 'NextDay' undefined; assuming extern returning int
D:\MyProjects\C\5\data.c(17) : error C2065: 'Sat' : undeclared identifier
D:\MyProjects\C\5\data.c(18) : error C2065: 'Sun' : undeclared identifier
Error executing cl.exe.
data.obj - 2 error(s), 1 warning(s)
[此贴子已经被作者于2007-3-8 12:48:24编辑过]
自己写了个简单的函数,想测试一下枚举的用法,半天还是没有任何的头绪,高手帮忙解答!!
#include<stdio.h>
enum WeekdayT{Sun,Mon , Thu ,Wed ,Thur ,Fri, Sat }; //全局变量
enum WeekdayT NextDay(enum WeekdayT w); // 这你的书写好象有问题;但
我没看出来,我重输了一遍就对了;
int main()
{
enum WeekdayT w;
w = Sun;
NextDay(w);
return 0;
}
enum WeekdayT NextDay(enum WeekdayT w)
{
if(w == Sat)
return Sun;
else return w+1;
}
================================
--------------------Configuration: 5 - Win32 Debug--------------------
Compiling...
data.c
D:\MyProjects\C\5\data.c(11) : warning C4013: 'NextDay' undefined; assuming extern returning int
D:\MyProjects\C\5\data.c(17) : error C2065: 'Sat' : undeclared identifier
D:\MyProjects\C\5\data.c(18) : error C2065: 'Sun' : undeclared identifier
Error executing cl.exe.
data.obj - 2 error(s), 1 warning(s)
#include<stdio.h>
enum WeekdayT {Sun,Mon,Thu,Wed,Thur,Fri,Sat};
enum WeekdayT GetDay();
enum WeekdayT NextDay(enum WeekdayT w);
enum WeekdayT PreviousDay(enum WeekdayT x);
enum WeekdayT IncrementDay(enum WeekdayT y ,int z);
void PrintDay(enum WeekdayT d);
int main()
{ enum WeekdayT w;
int n;
w=GetDay();
printf("输入你想跳跃的天数:");
scanf("%d",&n);
printf("这天的后一天是:");
PrintDay(NextDay(w));
printf("这天的前一天是:");
PrintDay(PreviousDay(w));
printf("跳跃后的那天是:");
PrintDay(IncrementDay(w,n));
return 0;
}
enum WeekdayT GetDay()
{
int a;
printf("输入你想查看的星期:\nSun:0,Mon:1,Thu:2,Wed:3,Thur:4,Fri:5,Sat:6\n");
scanf("%d",&a);
return (enum WeekdayT)a;
}
enum WeekdayT NextDay(enum WeekdayT w)
{
if(w == Sat)
return Sun;
else return w+1;
}
enum WeekdayT PreviousDay(enum WeekdayT x)
{
if(x == Sun)
return Sat;
return x-1;
}
enum WeekdayT IncrementDay(enum WeekdayT y ,int z)
{
if(z < 0)
return (7*100 + y + z)%7;
return (y+z)%7;
}
void PrintDay(enum WeekdayT d)
{
switch(d){
case Sun:
printf("Sun\n");
break;
case Mon:
printf("Mon\n");
break;
case Thu:
printf("Thu\n");
break;
case Wed:
printf("Wed\n");
break;
case Thur:
printf("Thur\n");
break;
case Fri:
printf("Fri\n");
break;
case Sat:
printf("Sat\n");
break;
default:
printf("Error\n");
break;
}
}