#54351: 詳解(排版精美的c++程式碼)


bluesquid29 (bluesquid29)


#include <iostream> // cin, cout
#include <string> // string, getline

using std::cin;
using std::cout;

int main()
{
std::string line;
bool first = true;
while (std::getline(cin, line))
{
// Print a newline before every case except the first one
if (!first)
{
cout << '\n';
}
first = false;
// count the ASCII frequency
int cnt[129] = {0};
for (char c : line)
{
cnt[c]++;
}
// Output
for (int freq = 1; freq <= 1000; freq++)
{
for (int ascii = 128; ascii > 31; ascii--)
{
if (cnt[ascii] == freq)
{
cout << ascii << ' ' << freq << '\n';
}
}
}
}
return 0;
}
#54352: Re: 詳解(排版精美的c++程式碼)


bluesquid29 (bluesquid29)


#include // cin, cout
#include // string, getline

using std::cin;
using std::cout;

int main()
{
std::string line;
bool first = true;
while (std::getline(cin, line))
{
// Print a newline before every case except the first one
if (!first)
{
cout << '\n';
}
first = false;
// count the ASCII frequency
int cnt[129] = {0};
for (char c : line)
{
cnt[c]++;
}
// Output
for (int freq = 1; freq <= 1000; freq++)
{
for (int ascii = 128; ascii > 31; ascii--)
{
if (cnt[ascii] == freq)
{
cout << ascii << ' ' << freq << '\n';
}
}
}
}
return 0;
}

我沒想到「送出」後格式跑掉了,不知道為什麼會這樣😢

#54353: Re: 詳解(排版精美的c++程式碼)


pofly (不挖鼻孔有害身心健康)


我貼程式碼都是用 <pre> 這個標籤,可以在下圖的位置找到

 

其他標籤好像容易把空格(縮排)吃掉(?)

 

<pre> 的好處是不吃縮排,而且會用等寬字體顯示,很適合貼程式碼

如果是需要嚴格保證格式的東西,用 <pre> 就對了

我習慣上會搭配表格的功能,開一個 1x1 的表格,把程式碼框起來,裡面用 <pre> 這個格式貼程式碼

例如這樣

#include <iostream>

int main() {
std::cout << "Hello World";
}

 

至於高亮程式碼的功能 ZJ 貌似沒有,想要的話只能徒手上色

我自己是有點懶了,現在只給關鍵位置上色,或是直接貼外部連結,像 Gist 之類的

 

也有人是直接放棄,改貼截圖,但怎樣讓圖片大小適中,怎麼處理比較長的程式碼,就又是另一回事了。

 

應該還有其他作法,但我沒深入研究

 

 

題外話,還有一個和程式碼有關的標籤是 <code> 

效果如你所見,會給對應的文字添加底色

但它在 ZJ 上渲染多行程式碼的效果很差

比較適合單行的、簡短的程式碼

 

至於為什麼我說多行的效果很差,我想用看的應該會比較明白 ¯\_(ツ)_/¯

#include <iostream>

int main() {
    std::cout << "Hello World";
}