0

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 *.

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.

I used the following to get n:

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

Now, c is of type char. Is there a quick and easy way to get it into my enumerated aqn variable l?

Or, is there a quick and easy way to read the config string and assign the value directly to my variable l?

Could some preprocessor #define statements be used?

Or, and I going to need to do a tedious switch and case block?

Bascially, for the first line of my data, I want n=2 and l=p.

For the second line, I want n=2 and l=p.

...

...

For the 30th line, I want n=5 and l=d.

Mannix
  • 411
  • 10
  • 23
  • I don't recall any method to convert a string or char to something equivalent to a enum type in C. Enum is used to make the developer life easier, like when working with enumerable sets. If you want to take input from the user, or some file, I believe you'll have to create a correspondence table to do so, and them match – Inox Mar 31 '14 at 01:22
  • possible duplicate of [Get enum value by name](http://stackoverflow.com/questions/18070763/get-enum-value-by-name) – SilverCorvus Mar 31 '14 at 01:24
  • 1
    Aha! So you do have to make a map – Inox Mar 31 '14 at 01:37

1 Answers1

2
char *table = "spdfghiklmnoqrtuvwxyz";
char *p = strchr(table, c)
if(p!=NULL)
    l= p-table;
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70