简体   繁体   English

在C中读取和转换枚举类型

[英]Reading and converting an enum type in C

I have defined the following data type: 我已经定义了以下数据类型:

typedef enum
{
    s=0,
    p=1,
    d=2,
    f=3,
    g=4,
    h=5,
    i=6,
    k=7,
    l=8,
    m=9,
    n=10,
    o=11,
    q=12,
    r=13,
    t=14,
    u=15,
    v=16,
    w=17,
    x=18,
    y=19,
    z=20
} aqn; /* azimuthal quantum number */

I have declared these variables: 我已经声明了这些变量:

aqn l;
int n;
char c;

I have the following data in a file: 我在文件中有以下数据:

2p  ²P°  2.5    2201.333333
2p  ²D   4.5    232287.200000
2p  ²S   0.5    298282.000000
2p  ²P°  2.5    524778.000000
3s  ²S   0.5    1210690.000000
3d  ²D   4.5    1335962.000000
3s  ²P°  2.5    1382990.000000
3p  ²D   4.5    1441942.000000
3p  ²S   0.5    1460910.000000
3s  ²P°  2.5    1486970.000000
3d  ²F°  6.5    1506161.428571
3d  ²P°  2.5    1513486.666667
3p  ²D   2.5    1548850.000000
3p  ²S   0.5    1556590.000000
3d  ²F°  6.5    1597480.000000
3d  ²P°  2.5    1610670.000000
3s  ²D   4.5    1638790.000000
4s  ²S   0.5    1647880.000000
3p  ²F°  6.5    1690802.857143
4d  ²D   4.5    1693830.000000
3d  ²D   2.5    1703280.000000
3d  ²D   2.5    1733900.000000
4p  ²D   4.5    1824376.000000
4d  ²F°  6.5    1847218.571429
5d  ²D   4.5    1858380.000000
6d  ²D   4.5    1946060.000000
4d  ²F°  6.5    1964300.000000
5d  ²F°  6.5    2006054.285714
6d  ²F°  3.5    2092940.000000
5d  ²F°  6.5    2130100.000000

The data is read and parsed. 读取和解析数据。 The first term on each line is stored in a variable called config , which is type char * . 每行的第一项存储在名为config的变量中,该变量类型为char *

I need to get the numeral into the variable n , which I declared as an int , and the alphabetic character into the variable l , which I declared as an enum type. 我需要将数字输入变量n ,我将其声明为int ,将字母字符输入变量l ,我将其声明为enum类型。

I used the following to get n : 我使用以下来获得n

sscanf(config,"%d%c",&n,&c);

Now, c is of type char . 现在, c的类型为char Is there a quick and easy way to get it into my enumerated aqn variable l ? 是否有一种快速简便的方法将其纳入我的枚举aqn变量l

Or, is there a quick and easy way to read the config string and assign the value directly to my variable l ? 或者,是否有一种快速简便的方法来读取config字符串并将值直接赋值给我的变量l

Could some preprocessor #define statements be used? 可以使用一些预处理器#define语句吗?

Or, and I going to need to do a tedious switch and case block? 或者,我需要做一个繁琐的switchcase块?

Bascially, for the first line of my data, I want n=2 and l=p . 基本上,对于我的数据的第一行,我想要n=2l=p

For the second line, I want n=2 and l=p . 对于第二行,我想要n=2l=p

... ...

... ...

For the 30th line, I want n=5 and l=d . 对于第30行,我想要n=5l=d

char *table = "spdfghiklmnoqrtuvwxyz";
char *p = strchr(table, c)
if(p!=NULL)
    l= p-table;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM