c++枚举类型在输出的时候是不是只能输出所代表的整数啊?
如果有一个描述一周日程的枚举类型(Sunday,Monday···Saturday),能不能在输入一个整数的时候,直接输出类似于Sunday这样的字符串啊?...
如果有一个描述一周日程的枚举类型(Sunday,Monday···Saturday),能不能在输入一个整数的时候,直接输出类似于Sunday这样的字符串啊?
展开
展开全部
我正在复习江苏省那本C++程序教材。你这个题目就是书上的吧?
书上有个例子132页,就是输入整数0~6,并转换为对应的枚举量sunday,monday……你所提的问题是可以通过case语句实现的。书上有句话:“枚举变量的输入输出很不方便。这是由于枚举类型变量的值在程序中是以枚举量表示的,而在执行程序时是以整数(枚举量的序号)表示的 ,致使枚举类型的变量的值不能直接输入输出。”
下面程序是我根据那个例子改写的,后面习题的一周日程,日程是我乱写的,哈哈!我编译过的,可以执行~
#include<iostream.h>
enum weekdays{Sunday,Monday,Tuesday,Wednesday,Thusday,Friday,Saturday};
void main()
{
int i;
weekdays day;
cout<<"0:Sunday,1:Monday,2:Tuesday,3:Wednesday,4:Thusday,5:Friday,6:Saturday"<<endl;
cin>>i;
if(i<0||i>6) cout<<"输入值不对!"<<endl;
else
{
switch(i)
{
case 0: day=Sunday; break;
case 1: day=Monday ; break;
case 2: day=Tuesday; break;
case 3: day=Wednesday; break;
case 4: day=Thusday; break;
case 5: day=Friday; break;
case 6: day=Saturday; break;
}
switch(day)
{
case Sunday: cout<<"今天飞上海"<<endl; break;
case Monday: cout<<"今天飞南京"<<endl; break;
case Tuesday: cout<<"今天飞广州"<<endl; break;
case Wednesday: cout<<"今天飞北京"<<endl; break;
case Thusday: cout<<"今天飞深圳"<<endl; break;
case Friday: cout<<"今天飞新加坡"<<endl; break;
case Saturday: cout<<"今天飞厦门"<<endl; break;
}
}
}
书上有个例子132页,就是输入整数0~6,并转换为对应的枚举量sunday,monday……你所提的问题是可以通过case语句实现的。书上有句话:“枚举变量的输入输出很不方便。这是由于枚举类型变量的值在程序中是以枚举量表示的,而在执行程序时是以整数(枚举量的序号)表示的 ,致使枚举类型的变量的值不能直接输入输出。”
下面程序是我根据那个例子改写的,后面习题的一周日程,日程是我乱写的,哈哈!我编译过的,可以执行~
#include<iostream.h>
enum weekdays{Sunday,Monday,Tuesday,Wednesday,Thusday,Friday,Saturday};
void main()
{
int i;
weekdays day;
cout<<"0:Sunday,1:Monday,2:Tuesday,3:Wednesday,4:Thusday,5:Friday,6:Saturday"<<endl;
cin>>i;
if(i<0||i>6) cout<<"输入值不对!"<<endl;
else
{
switch(i)
{
case 0: day=Sunday; break;
case 1: day=Monday ; break;
case 2: day=Tuesday; break;
case 3: day=Wednesday; break;
case 4: day=Thusday; break;
case 5: day=Friday; break;
case 6: day=Saturday; break;
}
switch(day)
{
case Sunday: cout<<"今天飞上海"<<endl; break;
case Monday: cout<<"今天飞南京"<<endl; break;
case Tuesday: cout<<"今天飞广州"<<endl; break;
case Wednesday: cout<<"今天飞北京"<<endl; break;
case Thusday: cout<<"今天飞深圳"<<endl; break;
case Friday: cout<<"今天飞新加坡"<<endl; break;
case Saturday: cout<<"今天飞厦门"<<endl; break;
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
char *week[]=
{
"Sunday",
"Monday",
"Tuesday",
"Wednesday"
"Thursday",
"Friday",
"Saturday"
};
enum
{
Sunday = 0,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
week[Monday]
....
week[Saturday]
{
"Sunday",
"Monday",
"Tuesday",
"Wednesday"
"Thursday",
"Friday",
"Saturday"
};
enum
{
Sunday = 0,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
week[Monday]
....
week[Saturday]
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个要自己输出,用switch case 判断输出.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询