繁体   English   中英

有条件的退格键。 我想在printf中使用条件退格键

[英]conditional backspace. I want to use conditional backspace inside printf

在程序中,如果条件成立,我想删除一个单词。 有没有办法做到这一点。 我正在考虑使用转义序列。

/* program to print number into words.  */ 

# include <stdio.h>
void main()
{
    char *single_numbers[10]={" ","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
    char *tens_numbers[9]={"Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
    char *teens[9]={"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
    int number,thousands,hundreds,tens,units,temp;
    printf("enter the number");
    scanf("%d",&number);
    if(number>9999)
    {
        printf("please enter a number between 0-9999");
    }else
    {
        thousands=number/1000;
        hundreds=(number%1000)/100;
        temp=(number%100);
        tens=temp/10;
        units=number%10;
        if(temp>=20)
            printf("%s thousand %s hundred %s %s only",
                    single_numbers[thousands],
                    single_numbers[hundreds],
                    tens_numbers[tens-2],
                    single_numbers[units]);
        else
            printf("%s thousand %s hundred %s only ",
                    single_numbers[thousands],
                    single_numbers[hundreds],
                    teens[temp-11]);
    }
}

是的,可以使用退格键( '\\b' )覆盖已经打印到屏幕上的内容,但这不是您通常使用的技术。

不必使用退格字符来覆盖已打印的内容,而是修改代码以仅打印所需的部分。 这是一个很好的起点,但是您需要对其进行修改以涵盖1000、0和1001之类的情况。

    if (thousands > 0)
        printf("%s thousand ", single_numbers[thousands]);
    if (hundreds > 0)
        printf("%s hundred ", single_numbers[hundred]);
    if (temp >= 20)
        printf("%s %s only\n", tens_numbers[tens-2], single_numbers[units]);
    else
        printf("%s only\n", teens[temp-11]);

完成目标的另一种方法是使用sprintf()打印到缓冲区中,然后有选择地截断缓冲区以删除书写的字符。 但是请考虑这是多么低效的-为什么仅完全跳过打印步骤就先打印然后擦除?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM