如何创建在C词典吗? [英] How to create a dictionary in C?

查看:151
本文介绍了如何创建在C词典吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C编程的单片机和作为其一部分要显示一个7段显示器上的某些字母。每个字母都有相应的编号,使7段显示器显示的字母。有没有真正的模式给它造成数只是那些需要显示的函,这将会是非常好的,如果我能建立某种形式的词典要做到这一点的7段显示器上增加了位的。

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.

如果我使用C#或东西,我只是让想字典,然后添加字母键和数字作为值,但据我知道,我不能在C这样做是否有另一种方式来做到这还是我应该只写像 INT displayletter(字符字母)的函数,它使用了一堆的if语句返回正确的数字

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 displayletter(char letter) that uses a bunch of if statements to return the right numbers?

很抱歉,如果这是一个愚蠢的问题,我很新的C

Sorry if it's a stupid question, I'm quite new to C.

推荐答案

您可以创建一个数组

 int values[26];

和与每个字母的值来填充它,但是他们正在计算

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

然后创建一个函数,一个字符,返回一个int

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

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

这是简单的,因为它假定你将只在ASCII字符集使用大写字母,但你应该明白了吧。

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

这篇关于如何创建在C词典吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆