Tex中的引号

【题目】

    在TeX中,左引号是“``”,右引号是“''"。输入一篇包含双引号的文章,你的任务是把它转换成TeX的格式。

    样例输入:

    "To be or not to be," quoth the Bard, "that

is the question".

    样例输出:

   ``To be or not to be,'' quoth the Bard,``that

is the question''.

【分析】

    1.得到要转换的字符串;

    2.判断字符串中的标点:左引号输出“``”,右引号输出“''”(因为TxT两种格式不一样,而英文的是需要判断的)。

    3.按规定要求输出字符串。

#include<stdio.h>
#include<string.h>
int main()
{
    char s[20];
    int q=1;
    scanf("%s",s);
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]=='"')
        {
            printf("%s",q?"``":"''");
            q=!q;
        }
        else
            putchar(s[i]);
    }
    return 0;
}

    运行结果,我们发现并不是题目要求的样子。

    因为,scanf("%s",s)语句不能读入 SPACE、TAB这样的字符,即获得的s中是没有"\t"、"\n"的。

    幸好还有其他解决方式。我们可以用getchar()依次获取字符,直到EOF。getchar函数获取输入内容后转为int型,为什么不是char呢?其实正是这个特点getchar才可以将"\t"等字符翻译准确,EOF不是一个字符,这样文件的末尾和转义字符就区分开了。

#include<stdio.h>
#include<string.h>
int main()
{
    int c,q=1;//将c定义成int型,q作为区分前引号和后引号的标志
    while((c=getchar())!=EOF)//读取的不是文件末尾,输出字符
    {

        if(c=='"')
        {
            printf("%s",q?"``":"''");
            q=!q;
        }
        else
            putchar(c);
    }
    return 0;
}

其他几种获取字符串的方式:

    fgetc(fin):从一个文件中依次获取单个字符,包括各种转义字符“\n”,“\t”等。将所要转换的语句放入"TxT.txt"文件中:

#include<stdio.h>
#include<string.h>
int main()
{
    int c,q=1;
    FILE *fin=fopen("TxT.txt","r");
    while((c=fgetc(fin))!=EOF)
    {

        if(c=='"')
        {
            printf("%s",q?"``":"''");
            q=!q;
        }
        else
            putchar(c);
    }
    return 0;
}

输出结果:

    ``To be or not to be'', he said, ``that

 is a question.''

    gets():用于获取一个字符串。参数为所要填充的字符串。这个函数同样也能获取空格,但它本身有一个问题:允许程序读写字符串的非法内存,这就造成了数据丢失的安全隐患,可能会出现你不想发生的事情。C11标准里,gets()函数已经被删除。

    <conio.h>:这个头文件中有许多非标准的读写函数,而且调用也有一些编译器需求,不常使用。


如有错误,恳请指出。谢谢^-^



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
TeX,引用公式可以使用`\ref`命令,但是这个命令只能引用带有标号的公式。如果你的公式没有标号,可以使用`\eqref`命令来引用,它会自动为公式加上括号和标号。以下是一个示例: ``` \documentclass{article} \begin{document} \section{Introduction} In this section we will introduce the Pythagorean theorem: \begin{equation} \label{eq:pythagorean} a^2 + b^2 = c^2 \end{equation} Equation \ref{eq:pythagorean} is known as the Pythagorean theorem. \end{document} ``` 在这个示例,我们使用了一个带有标号的方程,并使用`\label`命令为它设置了一个标签。然后,我们使用`\ref`命令来引用这个方程,并在引用添加了一个“Equation”的前缀。输出结果如下: ``` In this section we will introduce the Pythagorean theorem: a^2 + b^2 = c^2 (1) Equation 1 is known as the Pythagorean theorem. ``` 如果我们没有给方程设置标号,我们可以使用`\eqref`命令来引用它: ``` \documentclass{article} \usepackage{amsmath} \begin{document} \section{Introduction} In this section we will introduce an important equation: \begin{equation*} e^{i\pi} + 1 = 0 \end{equation*} This equation is known as Euler's identity (\eqref{eq:euler}). \begin{equation} \label{eq:euler} e^{i\pi} + 1 = 0 \end{equation} \end{document} ``` 在这个示例,我们没有给方程设置标号,但我们使用了`\eqref`命令来引用它,并在引用添加了一个“Equation”的前缀。输出结果如下: ``` In this section we will introduce an important equation: e^{i\pi} + 1 = 0 (1) This equation is known as Euler's identity (Equation 1). ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值