我想用printf打印上标和下标,比如x¹?
我想在 c 中打印出一个多项式表达式,但我不知道将 x 打印为一个数字的幂printf
回答
不幸的是,这远非微不足道。您无法通过printf. 你需要wprintf. 此外,在正常和上标之间进行转换并非易事。你想要这样的功能:
wchar_t digit_to_superscript(int d) {
wchar_t table[] = { // Unicode values
0x2070,
0x00B9, // Note that 1, 2 and 3 does not follow the pattern
0x00B2, // That's because those three were common in various
0x00B3, // extended ascii tables. The rest did not exist
0x2074, // before unicode
0x2075,
0x2076,
0x2077,
0x2078,
0x2079,
};
return table[d];
}
这个函数当然也可以被改变来处理其他字符,只要它们被支持。你也可以编写更完整的函数来操作完整的字符串。
但正如我所说,这不是微不足道的,它不能用简单的格式字符串来完成printf,甚至不能wprintf。
这是一个有点工作的例子。它可用,但它很短,因为我省略了所有错误检查等。能够使用负浮点数作为指数的最短时间。
#include <wchar.h>
#include <locale.h>
wchar_t char_to_superscript(wchar_t c) {
wchar_t digit_table[] = {
0x2070, 0x00B9, 0x00B2, 0x00B3, 0x2074,
0x2075, 0x2076, 0x2077, 0x2078, 0x2079,
};
if(c >= '0' && c <= '9') return digit_table[c - '0'];
switch(c) {
case '.': return 0x22C5;
case '-': return 0x207B;
}
}
void number_to_superscript(wchar_t *dest, wchar_t *src) {
while(*src){
*dest = char_to_superscript(*src);
src++;
dest++;
}
dest++;
*dest = 0;
}
和一个主要的功能来演示:
int main(void) {
setlocale(LC_CTYPE, "");
double x = -3.5;
wchar_t wstr[100], a[100];
swprintf(a, 100, L"%f", x);
wprintf(L"Number as a string: %lsn", a);
number_to_superscript(wstr, a);
wprintf(L"Number as exponent: x%lsn", wstr);
}
输出:
Number as a string: -3.500000
Number as exponent: x?³???????
为了制作一个完整的翻译器,你需要这样的东西:
size_t superscript_index(wchar_t c) {
// Code
}
wchar_t to_superscript(wchar_t c) {
static wchar_t huge_table[] {
// Long list of values
};
return huge_table[superscript_index(c)];
}
请记住,这无法对所有角色进行。只有那些对应物作为上标版本存在的那些。