c - 我想用 printf 打印上标和下标,比如 x¹?
问题描述
我想在 c 中打印出一个多项式表达式,但我不知道 print 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: %ls\n", a);
number_to_superscript(wstr, a);
wprintf(L"Number as exponent: x%ls\n", 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)];
}
请记住,这不能对所有角色都进行。只有那些其对应物作为上标版本存在。
推荐阅读
- json - 嵌套的 JSON golang 映射
- ruby-on-rails - 带有 Rails 的 GraphQL gem,似乎找不到正确的类型定义?
- assembly - LDRB 和 STRB 重新排列字符并形成一个单词
- macros - Pulover Macro Creator:WinActivate 仅使用部分标题
- java - 带有包含嵌套对象的 json 主体的 POST 请求
- c# - 除非我的自定义白名单批准,否则如何阻止 Windows 10 上的所有 Web 请求?
- javascript - 如何使用 vuex 从状态访问对象
- google-tag-manager - 如何在跟踪事件时包含 cookie-id
- python - 在scrapy中获取重定向的url时出错:Python
- c# - Windows 窗体从数据库中获取数据并显示在标签中