开发者

How to create a dictionary in C?

I'm programming a microcontroller in C and as part of it want to display certain letters on a 7 segment display. Each letter has a corresponding number that makes the 7 segment display show the letter. There's no real pattern to it cause the number is just made by adding up the bits on the 7 segment display that are needed to show the letter so it'd be really nice if I could create some sort of dictionary to do this.

If I was using C# or something I'd just make a dictionary and then add the letters as keys and the numbers as values but as far as I know I can't do this in C. Is there another way to do it or do should I just write a function like int d开发者_Python百科isplayletter(char letter) that uses a bunch of if statements to return the right numbers?


You could create an array

 int values[26];

and populate it with the values for each letter, however they're calculated

Then create a function that takes a character and returns an int

int GetValueFromChar(char c)
{
    return values[c - 'A'];
}

This is simplistic, as it assumes you'll only use upper case letters in an ASCII character set, but you should get the idea.


Easiest would be to just use a function and a switch statement... I believe for this purpose the switch statement would be very efficient. I also second the Judy library as a good one to use for efficient mapping, but it's kind of overkill for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜