本文目錄一覽:
- 1、C語言枚舉類型怎麼使字符和相應的序號對應
- 2、什麼是枚舉數組啊
- 3、c語言中數組下標可以是枚舉類型嗎
- 4、c++數組枚舉
- 5、ios 中數組,字典 集合部類的幾種常用枚舉方法
- 6、enum 數組怎麼使用
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};
1.計算機在處理的時候,實際上black=0,white=1,red=2,blue=3,green=4;
這些類似於#define black 0
只是用black代表了0而已。
2.為什麼說不可分呢,一般情況下3=1+2,但是blue不等於white+red;所以這些代表用的符號雖然實際上是整數,但是不能像一般整數那樣拆分。
3.繼續:
color color1;
color1=white;
上面定義的color是一個類型名(像int、float那樣是類型名),可以定義變量,color1是這種類型的一個變量,它等於white,實際上計算機處理的時候它就等於1,這是由enum color的定義決定的。
c語言中數組下標可以是枚舉類型嗎
可以是枚舉類型。
在C語言中,數組下標要求是整型數值。一般是非負整型數值,但實際上,負數下標在C語言中也是允許的。
而枚舉類型,在C語言中有兩種處理方式:
1 當枚舉類型中不包含負值時,按照無符號整型處理;
2 當枚舉類型中包含負值時,按照有符號整型處理;
無論是哪種形式,均符合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);}//NSSetNSString *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)
{
//this.Debug(t.ToString(), “”);
c =(UserRolesType)Enum.Parse(typeof(UserRolesType),i.ToString());
isInEnum = true;
return c;
//(Colors)Enum.Parse(typeof(Colors), “Red, Yellow”);
}
}
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;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/255091.html