本文目录一览:
C语言枚举类型怎么使字符和相应的序号对应
关于“我想输入一个1-7的整数,程序对应输出相对的星期几简写。” 这个需求不需要用枚举,比如搞个字符串数组:
#include stdio.h
int main() {
const char *weekdayStrings[] = {"mon", "tue", "wed", "thu", "fri", "sat", "sun"};
int weekdayNum = 0;
printf("input weekday (1-7): ");
scanf("%d", &weekdayNum);
if (weekdayNum < 1 || weekdayNum > 7) {
printf("wrong input\n");
return -1;
}
printf("weekday: %s\n", weekdayStrings[weekdayNum-1]);
return 0;
}
如果你想通过枚举值得到枚举名字,这个C语言做不到,只能用switch-case或者if-else判断,或者像我这样用字符串数组。
什么是枚举数组啊
举个例子吧,比如说
enum color{black, white, red, blue, green};
- 计算机在处理的时候,实际上black=0, white=1,red=2, blue=3, green=4;
这些类似于
#define black 0
只是用black代表了0而已。 - 为什么说不可分呢,一般情况下3=1+2,但是blue不等于white+red;所以这些代表用的符号虽然实际上是整数,但是不能像一般整数那样拆分。
- 继续:
color color1;
color1 = white;
上面定义的color是一个类型名(像int、float那样是类型名),可以定义变量,color1是这种类型的一个变量,它等于white,实际上计算机处理的时候它就等于1,这是由enum color
的定义决定的。
c语言中数组下标可以是枚举类型吗
可以是枚举类型。 在C语言中,数组下标要求是整型数值。一般是非负整型数值,但实际上,负数下标在C语言中也是允许的。 而枚举类型,在C语言中有两种处理方式:
- 当枚举类型中不包含负值时,按照无符号整型处理;
- 当枚举类型中包含负值时,按照有符号整型处理; 无论是哪种形式,均符合C语言下标的要求。所以C语言数组下标可以是枚举类型。
c++数组枚举
int _tmain(int argc, _TCHAR* argv[])
{
int temp[5][5] = { { 1, 2, 3, 4, 5 }, { 1, 2, 3, 5, 5 }, { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 }, {1,2,3,4,5} };
for (int index = 0; index < 5; ++index)
{
int sum = temp[0][index] + temp[1][index] + temp[2][index] + temp[3][index] + temp[4][index];
if (sum == 5)
{
if (index != 0 && index != 4)
{
for (int Inner = 0; Inner < 5; ++Inner)
{
temp[Inner][index - 1] = 5;
temp[Inner][index + 1] = 5;
}
}
else if (index == 0)
{
for (int Inner = 0; Inner < 5; ++Inner)
{
temp[Inner][index + 1] = 5;
}
}
else
{
for (int Inner = 0; Inner < 5; ++Inner)
{
temp[Inner][index - 1] = 5;
}
}
}
}
int SumNumber = 0;
for (int index = 0; index < 5; ++index)
{
int sum1 = 0;
for (int Iner = 0; Iner < 5; ++Iner)
{
if (temp[index][Iner] == 5)
sum1++;
}
if (sum1 == 3)
SumNumber += 1;
if (sum1 == 4)
SumNumber += 3;
if (sum1 == 5)
SumNumber += 10;
}
printf("Sum==%d", SumNumber);
getchar();
return 0;
}
ios 中数组,字典 集合部类的几种常用枚举方法
NSMutableArray *array=[[NSMutableArray alloc]initWithObjects:@"apple",@"ab",@"aa",@"aac",@"appd", nil];
// 排序
[array sortUsingComparator:^NSComparisonResult(__strong id obj1,__strong id obj2){
NSString *str1=(NSString *)obj1;
NSString *str2=(NSString *)obj2;
return [str1 compare:str2];
}];
NSLog(@"array=%@",array);
// 枚举字典
NSNumber *age=[NSNumber numberWithInt:51];
NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"Anthony",@"FirstName",@"Robbins",@"LastName",age,@age, nil];
[dic enumerateKeysAndObjectsUsingBlock:^(__strong id key,__strong id value,BOOL *stop){
NSLog(@"Key=%@,Value For Key=%@",key,value);
}];
// 法二
NSEnumerator *keys=[dic keyEnumerator];
id keyInDic=nil;
while ((keyInDic =[keys nextObject])!=nil) {
id valueForKey=[dic objectForKey:keyInDic];
NSLog(@"Key=%@,ValueForKey=%@",keyInDic,valueForKey);
}
// NSSet
NSString *hisName=@"li";
NSString *hisLastname=@"san";
NSString *herName=@"zhang";
NSString *herLastname=@"san";
NSMutableSet *set=[[NSMutableSet alloc]initWithObjects:hisName,hisLastname,herName,herLastname, nil];
NSLog(@"%@",set);
// 删除对象
[set removeObject:herLastname];
NSLog(@"%@",set);
// 添加
[set addObject:hisLastname];
NSLog(@"%@",set);
[set addObjectsFromArray:array];
NSLog(@"%@",set);
// 遍历set
[set enumerateObjectsUsingBlock:^(__strong id objc1,BOOL *stop){
if ([objc1 isKindOfClass:[NSString class]]) {
NSString *str=(NSString*)objc1;
if ([str isEqualToString:@"san1"]) {
NSLog(@"find san in set");
*stop=YES;
}
}
}];
// set anyObject
enum 数组怎么使用
枚举。
一、Enum的定义
public enum UserRolesType
{
UnKnown=0,
BaseSimple=70,
BaseBasic=71,
BaseExtend=72,
BaseBasic2=88,
BaseSimple2=89,
BaseExtend2=90
}
方法一:
根据ID获取枚举对象
protected UserRolesType GetEnum(int t)
{
bool isInEnum = false;
UserRolesType c = UserRolesType.UnKnown;
if (t > 0)
{
foreach(int i in Enum.GetValues(typeof(UserRolesType)))
{
if (i == t)
{
c = (UserRolesType)Enum.Parse(typeof(UserRolesType), i.ToString());
isInEnum = true;
return c;
}
}
if (isInEnum == false)
{
return UserRolesType.UnKnown;
}
}
return c;
}
方法二:根据ID获取枚举名称
protected string GetEnumName(int s)
{
string str = Enum.GetName(typeof(UserRolesType), s);
if (str == null)
{
str = UserRolesType.UnKnown.ToString();
}
return str;
}