创建表

创建表

我有一个 C++ 运算符优先级表,它导致了错误。

据我所知,第 9 行有问题,但我不明白为什么 [] 不能正确地转义 []

我确信存在很多错误,因此,任何修复都将不胜感激。

\input{preamble.tex}
\begin{document}
\mainmatter  
\begin{tabular}{l|l|l|l}
Precedence  & Operator & Description & Associativity            \\
1   &   ::  &   Scope resolution    &   Left-to-right       \\
2   &   ++   --                 \\
\(\) Function call                  \\
\[\]    Array subscripting              \\
.   Element selection by reference      \\
\-> Element selection through pointer   \\
    &   postincrement and decrement &   type\(\) type\{\}   Function-style type cast    \\
3   &   ++   --         \\
+   -   Unary plus and minus        \\
!   ~   Logical NOT and bitwise NOT \\
(type)  C-style type cast           \\
*   Indirection (dereference)       \\
\&  Address-of              \\
sizeof                      \\
new                         \\
delete  
&   Preincrement and decrement  &   Right-to-left           \\
4   &   .*   ->*    &   Pointer to member   &   Left-to-right   \\
5   &   *   /   \%  &   Multiplication, division, and remainder \\
6   &   +   -       &   Addition and subtraction            \\
7   &   <<   >> &   Bitwise left shift and right shift      \\
8   &   <   <=  
        >   >=
&   Relational operators            \\
    Relational operators >  ≥ respectively  \\
9   &   ==   != &   For relational = and ≠ respectively \\
10  &   \&      &   Bitwise AND \\
11  &   ^       &   Bitwise XOR     \\
12  &   |       &   Bitwise OR  \\
13  &   \&\&        &   Logical AND \\
14  &   ||      &   Logical OR  \\
15  &   ?:
        throw
        =   Direct assignment (provided by default for C++ classes)
        +=   -= Assignment by sum and difference
        *=   /=   \%=   Assignment by product, quotient, and remainder
        <<=   >>=   Assignment by bitwise left shift and right shift
        \&=   ^=   |=   Assignment by bitwise AND, XOR, and OR
        &   Ternary &   Right-to-left   \\
16  &   ,   &   Comma   &   Left-to-right
\end{tabular}
\end{document}                          

当我去掉括号上的反斜杠时,第 9 行仍然出现错误:

! 缺少数字,视为零。} l.9 [] 数组下标 \ ? ! 非法计量单位(插入 pt)。

新的(精简的)代码是:

\input{preamble.tex}
\begin{document}
\mainmatter  
\begin{tabular}{l|l|l|l}
Precedence  &    Operator &  Description    &   Associativity           \\
1   &   ::  &   Scope resolution    &   Left-to-right       \\
2   &   ++   --                 \\
() Function call                    \\
[] Array subscripting               \\
. Element selection by reference        \\
-> Element selection through pointer    \\
    &   postincrement and decrement &   Function-style type cast    \\

好的,给出了答案,我对表格进行了大量清理,包括使其独立,以便任何人都可以复制它来学习。

\documentclass{standalone}
\begin{document}
%\tiny
%\begin{tabular}{l|l|l|l}
\begin{tabular}{llll}
{\huge Precedence}  &    {\huge Operator}   &   {\huge Description} &   {\huge Associativity}           \\ \hline
1   &   \verb$::$   &    Scope resolution    &   Left-to-right              \\
2   &   \verb$++$   \verb$--$   &   pre-increment and decrement &       \\ \hline
    &   \verb$()$   &   Function call       &                       \\
    &   \verb$[]$   &   Array subscripting  &                       \\
    &   \verb$.$    &   Object member   &                       \\
    &   \verb$->$   &   Element selection through pointer               \\
    &   \verb$()$   &   Function-style type cast                        \\ \hline
3   &   \verb$++$   \verb$--$   &   post-increment and decrement & Right-to-left    \\
    &   \verb$+$   \verb$-$ &   Unary plus and minus                \\
    &   \verb$!$   \verb$~$ &   Logical NOT and bitwise NOT         \\
    &   (type)          &   C-style type cast                           \\
    &   \verb$*$    &   Pointer dereference                     \\
    &   \verb$&$    &   Address-of                                          \\
    &   sizeof      &   The number of atomic units of memory used           \\
    &   new         &   dynamic memory allocation                   \\
    &   delete      &   dynamic memory deallocation                 \\
4   &   \verb$.*$   \verb$->*$  &   Pointer to member   &   Left-to-right       \\ \hline
5   &   \verb$*$   \verb$/$   \verb+\%+  &  Multiplication, division, and remainder \\ \hline
6   &   \verb$+$   \verb$-$       & Addition and subtraction                            \\ \hline
7   &   \verb$<<$   \verb$>>$ &   Bitwise left shift and right shift                \\ \hline
8   &   \verb$<$   \verb$<=$    &   Relational operators \verb$>$  \verb$≥$ \\
    &   \verb$>$  \verb$>=$ &   Relational operators \verb$\greaterequal$  \verb$≥$ \\ \hline
9   &   \verb$==$   \verb$!=$ &   Relational equality and inequality \verb$≠$       \\ \hline
10  &   \verb$&$        & Bitwise AND                           \\ \hline
11  &   \verb$^$        &   Bitwise XOR                         \\ \hline
12  &   \verb$|$        &   Bitwise OR                          \\ \hline
13  &   \verb$&&$       &   Logical AND                         \\ \hline
14  &   \verb$||$       &   Logical OR                              \\ \hline
15  &   \verb$?:$       & ternary        &   Right-to-left              \\
    &   throw           & throw an exception                        \\
    &   \verb$=$        & assignment                            \\
    &   \verb$+=$   \verb$-=$   &   additive assignment                 \\
    &   \verb$*=$   \verb$/=$   \verb$\%=$  & Multiplicative assignment         \\
    &   \verb$<<=$   \verb$>>=$   &  Bitwise left shift and right shift assignment  \\
    &   \verb$\&=$   \verb$^=$   \verb$|=$  & Bitwise AND, XOR, and OR assignment   \\ \hline
16  &   \verb$,$         &   Comma   &   Left-to-right                  \\
\end{tabular}
\end{document} 

答案1

我建议你使用 inline verbatim 命令来显示所有特殊运算符。代码如下,当你编译它时你会发现它非常丑陋。而且你有很多内容错误需要修复,但这不是 C++ 编程论坛。

\documentclass{standalone}
\begin{document}
\tiny
\begin{tabular}{l|l|l|l}
Precedence  & Operator & Description & Associativity            \\
1   &   \verb+::+  &   Scope resolution    &   Left-to-right       \\
2   &   \verb-++-   \verb+--+ &&                \\
\verb+()+ Function call  &&&                \\
\verb+[]+    Array subscripting  &&&            \\
\verb+.+   Element selection by reference &&&     \\
\verb+->+ Element selection through pointer &&&  \\
    &   postincrement and decrement &   type\(\) type\{\}   Function-style type cast    \\
3   &   \verb-++-   \verb+--+         \\
\verb-+-   \verb+-+   Unary plus and minus        \\
\verb+!+   \verb+~+   Logical NOT and bitwise NOT \\
(type)  C-style type cast           \\
\verb+*+   Indirection (dereference)       \\
\verb+&+  Address-of              \\
sizeof                      \\
new                         \\
delete  
&   Preincrement and decrement  &   Right-to-left           \\
4   &   \verb+.*   \verb+->*    &   Pointer to member   &   Left-to-right   \\
5   &   \verb+*+   \verb+/+   \verb+\%+  &   Multiplication, division, and remainder \\
6   &   \verb-+-   \verb+-+       &   Addition and subtraction            \\
7   &   \verb+<<+   \verb+>>+ &   Bitwise left shift and right shift      \\
8   &   \verb+<+   \verb+<=+  
        \verb+>+   \verb+>=+
&   Relational operators            \\
    Relational operators \verb+>  \verb+≥+ respectively  \\
9   &   \verb+==+   \verb+!=+ &   For relational \verb+=+ and \verb+≠+ respectively \\
10  &   \verb+\+ &      &   Bitwise AND \\
11  &   \verb+^+       &   Bitwise XOR     \\
12  &   \verb+|+       &   Bitwise OR  \\
13  &   \verb+&&+        &   Logical AND \\
14  &   \verb+||+      &   Logical OR  \\
15  &   \verb+?:+
        throw
        \verb+=+   &Direct assignment (provided by default for C++ classes)\\
        \verb-+=-   \verb+-=+& Assignment by sum and difference\\
        \verb+*=+   \verb+/=+   \verb+\%=+  & Assignment by product, quotient, and remainder\\
        \verb+<<=+   \verb+>>=+   &Assignment by bitwise left shift and right shift\\
        \verb+\&=+   \verb+^=+   \verb+|=+  & Assignment by bitwise AND, XOR, and OR\\
        &   Ternary &   Right-to-left   \\
16  &   \verb+,+   &   Comma   &   Left-to-right\\
\end{tabular}
\end{document} 

答案2

你有很多错误在你的代码中。

首先,某些字符具有特殊含义。您使用过的字符包括^&~\。对于这些字符,您需要使用特殊控制字符和宏。

\^{}       for `^`
\&         for `&`
\backslash for `\`
$\sim$     for `~`

该字符^在数学模式下用于创建上标。

该字符&有多种用途。表中的此字符标记了列之间的边界。

字符\后跟空格会形成空格。

字符~与 类似\,但是它创建的空白不会跨行或跨页边界。

如果您要排版计算机语言,您可能需要查看该listings包。

相关内容