经典编程900例(c语言)(第二十五篇)

例301:cscanf()和cprintf()

#include <conio.h>

int main(int argc, char const *argv[])
{
    int a, b, c;

    cprintf("Type 3 integer values and press Enter\r\n");
    // 建议运行查看同scanf()的区别
    cscanf("%d %d %d", &a, &b, &c);
    cprintf("The values entered were %d %d %d\r\n", a, b, c);
    
    return 0;
}

例302:利用gotoxy()控制输出文本

#include <conio.h>

int main(int argc, char const *argv[])
{
    int line;

    // 清屏
    clrscr();

    for (line = 1; line < 25; line++)
        cprintf("This is line %d\r\n", line);

    cprintf("Press a key to Continue: ");
    getch();

	// 光标移到第12行第1个字符(0开始)
    gotoxy(1, 12);

    for (line = 12; line < 15; line++)
        // 删除行
        delline();

    gotoxy(1, 25);
    return 0;
}

例303:不使用库函数读取字符串

#include <stdio.h>

int main(int argc, char const *argv[])
{
    char string[128];

    int index = 0;
    int letter;

    printf("Type in a string and press Enter\n");
    while ((letter = getchar()) != '\n')
        string[index++] = letter;

    string[index] = NULL;

    printf("The string was: %s\n", string);
    
    return 0;
}

例304:将输入的所有字母转为大写输出

#include <stdio.h>
#include <conio.h>
#include <ctype.h>

int main(int argc, char const *argv[])
{
    int letter;

    printf("Type in a string of characters and press Enter\n");

    do { 
        letter = getch();
        letter = toupper(letter);
        putch(letter);
    } while (letter != '\r');
    
    return 0;
}

例305:在遇到特定字符前可一直输入

#include <stdio.h>
#include <ctype.h>

int main(int argc, char const *argv[])
{
    int letter;

    printf("Type Y or N to continue and press Enter\n");

    do { 
        letter = toupper(getchar());
    } while ((letter != 'Y') && (letter != 'N'));

    printf("You typed %c\n", ((letter == 'Y') ? 'Y': 'N'));
    
    return 0;
}

例306:输入指定字符停止继续输入

#include <stdio.h>
#include <ctype.h>
#include <conio.h>

int main(int argc, char const *argv[])
{
    int letter;

    printf("Do you want to continue? (Y/N): ");
    do 
    {
        letter = getche();
        letter = toupper(letter);
    }
    while ((letter != 'Y') && (letter != 'N'));

    if (letter == 'Y')
        printf("\nYour response was Yes\n");
    else 
        printf("\nWhy not?\n");
    
    return 0;
}

例307:字符串读取函数gets()

#include <stdio.h>

int main(int argc, char const *argv[])
{
    char string[256];

    printf("Type in a string of characters and press Enter\n");
    gets(string);

    printf("The string was %s\n", string);
    
    return 0;
}

例308:光标位置函数gotoxy()

#include <conio.h>

int main(int argc, char const *argv[])
{
    clrscr();

    gotoxy(1, 5);
    cprintf("Output at row 5 column 1\n");

    gotoxy(20, 10);
    cprintf("Output at row 10 column 20\n");
    return 0;
}

例309:插入空行函数insline()

#include <conio.h>

int main(int argc, char const *argv[])
{
    int line;

    clrscr();

    for (line = 1; line < 25; line++)
        cprintf("This is line %d\r\n", line);

    cprintf("Press a key to Continue: ");
    getch();

    gotoxy(1, 12);

	// 插入空行
    insline();
    cprintf("This is new text!!!");
    gotoxy(1, 25);
    return 0;
}

例310:区域文本复制函数movetext()(类似于cmd的快速编辑)

#include <conio.h>

int main(int argc, char const *argv[])
{
    int i;

    clrscr();
    for (i = 1; i <= 5; i++)
        cprintf("This is line %d\r\n", i);

    cprintf("Press any key\n\r");
    getch();   

    // int movetext(int left, int top, int right, int bottom,int newleft, int newtop)
    movetext(1, 1, 30, 6, 45, 18);
    gotoxy(1, 24);

    return 0;
}

例311:高亮字符选择函数

#include <conio.h>

int main(int argc, char const *argv[])
{
    clrscr();

	// 选择高亮度文本字符
    highvideo();
    cprintf("This text is high video\r\n");

	// 选择低亮度字符
    lowvideo();
    cprintf("This text is low video\r\n");

	// 选择正常亮度字符
    normvideo();
    cprintf("This text is normal video\r\n");
    
    return 0;
}

例312:putchar()和putch()的效率对比

#include <stdio.h>
#include <conio.h>
#include <time.h>

int main(int argc, char const *argv[])
{
    int letter;
    int count;
    time_t start_time, stop_time;

    // 使用putchar()打印1000次的时间
    time(&start_time);
    for (count = 0; count < 1000; count++)
        for (letter = 'A'; letter <= 'Z'; letter++)
            putchar(letter);
    time(&stop_time);

    printf("\n\nTime required for putchar %d seconds\n", stop_time-start_time);
    printf("Press any key...\n");
    getch();

    // 使用putch()打印1000次的时间
    time(&start_time);
    for (count = 0; count < 1000; count++)
        for (letter = 'A'; letter <= 'Z'; letter++)
            putch(letter);
    time(&stop_time);

    printf("\n\nTime required for putch %d seconds\n", stop_time-start_time);

    return 0;
}

例313:字符打印函数putchar()

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int letter;

    for (letter = 'A'; letter <= 'Z'; letter++)
        putchar(letter);
    
    return 0;
}

例314:printf()和puts()效率对比

#include <stdio.h>
#include <conio.h>
#include <time.h>

int main(int argc, char const *argv[])
{
    int count;
    time_t start_time, stop_time;

    // printf()打印1000次的时间
    time(&start_time);
    for (count = 0; count < 1001; count++)
        printf("Jamsa's 1001 C/C++ Tips\n");
    time(&stop_time);

    printf("\n\nTime required for printf %d seconds\n", stop_time-start_time);
    printf("Press any key...\n");
    getch();

    // puts()打印1000次的时间
    time(&start_time);
    for (count = 0; count < 1001; count++)
        puts("Jamsa's 1001 C/C++ Tips");
    time(&stop_time);

    printf("\n\nTime required for puts %d seconds\n", stop_time-start_time);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值