Cprogramming 简明教程
Lookup Tables in C
C 语言中的查找表(简称 LUT )是用特定预计算值填充的数组。查找表有助于避免在程序中执行大量计算。人们可以使用查找表来提高 C 程序的效率,而不用使用冗长的嵌套 if-else 语句或 switch 语句。
Lookup tables (popularly known by the abbreviation LUT) in C are arrays populated with certain pre-computed values. Lookup tables help avoid performing a lot of calculations in a program. Instead of lengthy nested if-else statements or switch statements, one can use Lookup tables to enhance the efficiency of a C program.
Example 1
我们来看一个查找表的简单应用。在下面的代码中,我们计算给定整数的平方。
Let us have a look at a simple application of a lookup table. In the following code, we compute the square of a given integer.
#include <stdio.h>
int square(int x){
return x*x;
}
int main(){
int num[5] = {1, 2, 3, 4, 5};
for (int i = 0; i <= 4; i++){
printf("No: %d \tSquare(%d): %d\n", i+1, i+1, square(i+1));
}
return 0;
}
Example 2
尽管上述程序运行良好,但对于数组索引的每个值,它都涉及对 square() 函数的频繁调用。
While the above program works satisfactorily, it involves frequent calls to the square() function, for each value of the array index.
相反,我们可以声明一个数组来存储数字的平方,并直接从索引访问计算得到的平方。
Instead, we can declare an array to store the squares of numbers and access the computed square directly from the index.
#include <stdio.h>
int main(){
int squares[5] = {1, 4, 9, 16, 25};
for (int i = 0; i <= 4; i++){
printf("No: %d \tSquare(%d): %d\n", i+1, i+1, squares[i]);
}
return 0;
}
Example 3
在下面的示例中,我们提取与其原子序数对应的元素的名称。
In the example below, we fetch the name of the element corresponding to its atomic number.
# include <stdio.h>
int main(){
int num = 3;
switch (num){
case 1: puts("Hydrogen"); break;
case 2: puts("Helium"); break;
case 3: puts("Lithium"); break;
case 4: puts("Beryllium"); break;
case 5: puts("Boron"); break;
default: puts("error: unknown element!");
}
return 0;
}
Example 4
我们使用查找表(填充所有元素名称的数组)来简化程序,而不是为每个元素使用冗长的 switch 语句 −
Instead of a lengthy switch statements with a case for each element, we use a lookup table (an array populated with the names of all the elements) to simplify the program −
#include <stdio.h>
static const char *table[] = {
"Hydrogen", "Helium", "Lithium", "Beryllium", "Boron"
};
int main(){
int num = 3;
if (num >= 1 && num <= 5){
printf("Name of the element with atomic number %d is %s", num, table[num-1]);
} else {
puts("error: Atomic number not in the lookup table!");
}
return 0;
}
Lookup Tables in 7-Segment LED Display
在嵌入式系统的设计中,查找表被广泛使用,因为它可以提高应用程序的性能。
Lookup tables are extensively used in the design of embedded systems, as they enhance the performance of the application.
在很多设备中,七段式 LED 显示器用于显示视觉输出。该设备的八个段根据二进制数字序列照明。我们使用查找表将数字 0 至 9 转换为七段信号以驱动显示器。
In many devices, the seven-segment LED display is used to show visual output. Eight segments of the unit are illuminated based on a sequence of binary digits. We use a lookup table to convert numbers ranging from 0 to 9 to seven-segment signals to drive a display.
Example
数字的 7 段二进制代码存储为数组元素。然后将十六进制代码转换为用于驱动 7 段显示器的二进制代码。
The 7-segment binary code for the number is stored as an array element. The hexadecimal code is then converted to binary code that will drive the 7-segment display.
#include <stdio.h>
int main(){
// Array to represent numbers 0-9 in 7-segment display binary encoding
int const nums[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
static int bin[8];
int i = 0, num = 7, rem;
printf("The binary equivalent of 7 is: ");
for (i = 7; i >= 0; i--){
rem = num % 2;
bin[i] = rem;
num /= 2;
}
for (i = 0; i <= 7; i++){
printf("%d", bin[i]);
if (i == 3) printf(" ");
}
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
The binary equivalent of 7 is: 0000 0111
最低有效位表示段“a”、“b”、“c”和“d”。最高有效位为“e”、“f”、“g”和“h”。段“a”、“b”和“c”点亮以显示 7,让其他段关闭。
The least significant bits represent segments "a", "b", "c" and "d". The most significant bits are "e", "f", "g" and "h". The segments "a", "b" and "c" illuminate to display 7, leaving the other segments off.