C语言必会100题(7)。输入星期的第一个字母来判断一下是星期/Press any key to change color/学习gotoxy()与clrscr()函数/练习函数调用/文本颜色设置

特此鸣谢:鱼C_小甲鱼(B站up主)不二如是(鱼C论坛大佬)
题目来源:https://fishc.com.cn
注:这些题在网上都可以搜到,题下面的代码大多是流传下来的答案(我重新排了一下版,增加了可读性),部分是本人经过深思熟虑后编写的。

31,输入星期的第一个字母来判断一下是星期

1.题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母
2.程序源代码:

  1. 第一种解:
// 星期一:Monday
// 星期二:Tuesday
// 星期三:Wndnesday
// 星期四:Thursday
// 星期五:Friday
// 星期六:Saturday
// 星期日:Sunday

#include <stdio.h>

void secondLetter_T();
void secondLetter_S();

int main() {
    char first;

    printf("请输入第一个字母:");
    scanf("%s", &first);

    switch (first) {
        case 'M':
            printf("Monday!");
            break;
        case 'T':
            secondLetter_T();
            break;
        case 'W':
            printf("Wndnesday!");
            break;
        case 'F':
            printf("Friday!");
            break;
        case 'S':
            secondLetter_S();
            break;
        default:
            printf("输入错误!!!");
            break;
    }

    return 0;
}

void secondLetter_T() {
    char two;

    printf("请输入第二个字母:");
    scanf("%s", &two);

    switch (two) {
        case 'u':
            printf("Tuesday!");
            break;
        case 'h':
            printf("Thursday!");
            break;
        default:
            printf("输入错误!!!");
            break;
    }
}

void secondLetter_S() {
    char two;

    printf("请输入第二个字母:");
    scanf("%s", &two);

    switch (two) {
        case 'a':
            printf("Saturday!");
            break;
        case 'u':
            printf("Sunday!");
            break;
        default:
            printf("输入错误!!!");
            break;
    }
}

  1. 第二种解:
#include <stdio.h>

int main()
{
	char letter;
    printf("please input the first letter of someday\n");
    
    while((letter = getch()) != 'Y')//当所按字母为Y时才结束
    {
        switch (letter)
        {
            case 'S': printf("please input second letter\n");
                if((letter = getch()) == 'a')
                    printf("saturday\n");
                else if ((letter = getch()) == 'u')
                    printf("sunday\n");
                else printf("data error\n");
                break;
            case 'F': printf("friday\n");break;
            case 'M': printf("monday\n");break;
            case 'T': printf("please input second letter\n");
                if((letter = getch()) == 'u')
                    printf("tuesday\n");
                else if ((letter = getch()) == 'h')
                    printf("thursday\n");
                else printf("data error\n");
                break;
            case 'W':printf("wednesday\n");break;
            default: printf("data error\n");
        }
    }
    
    return 0;
}

32,Press any key to change color

1.题目:Press any key to change color, do you want to try it. Please hurry up!(按任意键更改颜色,你想要尝试一下吗?。那么就赶快开始吧!)
2.程序源代码:

// 我是在Codeblocks里面运行的,好像是conio文件里面东西不全,所以刚开始不能运行,报错。
// 网上搜发现codeblocks里面要自己写。所以在不同的编译器里面运行会发生不同的情况,不懂的自行百度。
// 后来从网上搜到了textbackground函数并添加上面才可以运行了。
// 里面很多东西我也不太了解,所以能写的注释也就那么多了,虽然都是从网上搜的。

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

int textbackground(short iColor)
{
    // 从一个特定的标准设备(标准输出)中取得一个句柄(用来标识不同设备的数值)
	HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);

    // 结构体,该结构体包含控制台屏幕缓冲区的信息。设置名称位csbInfo的结构体。
	CONSOLE_SCREEN_BUFFER_INFO csbInfo;

    // 函数,获取特定的控制台屏幕缓冲区信息。
	GetConsoleScreenBufferInfo(hd, &csbInfo);

    // SetConsoleTextAttribute()函数是一个API设置字体颜色和背景色的函数。
    // 有两个属性,第一个属性获得句柄,就是hd,也就是GetStdHandle(STD_OUTPUT_HANDLE),看这个函数的第一行;
    // 第一个属性和第二个属性中间用“,”隔开。
    // 第二个属性是设置颜色,中间有|
    // 结构体里面有wAttributes存储了绘制文本、背景的颜色等属性,在调用输出函数输出到控制台的时候就会作用。
    // 可以看一下这篇文章https://blog.csdn.net/weixin_30443731/article/details/99245800
    return SetConsoleTextAttribute(hd, (iColor<<4)|(csbInfo.wAttributes&~0xF0));
}

int main()
{
    int color;

    for(color = 0; color < 8; color++)
    {
        //设置文本的背景颜色
        textbackground(color);

        printf("this is color%d\n", color);
        printf("press any key to continue\n");

        // 设置用户输入的字符在控制台不显示
        getch();
    }
}

33,学习gotoxy()与clrscr()函数

1.题目:学习gutoxy()与clrscr()函数
2.程序源代码:

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

void gotoxy(int x, int y);
int textbackground(short iColor);

int main(void)
{
    // clrscr();清屏函数,但是在codeblocks里面无法调用,百度上说可以用下面的代替。
    system("cls");

    // 定位函数,定位光标到指定位置
    gotoxy(1, 5);
    // 设置输出字体的背景颜色
    textbackground(3);
    // 这里原来的是cprintf,很显然,在odeblocks里面无法运行,改变成了printf
    printf("Output at row 5 column 1\n");

    gotoxy(20, 10);
    textbackground(2);
    printf("Output at row 10 column 20\n");

    getch();
}

void gotoxy(int x, int y)
{
    COORD coord;

    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int textbackground(short iColor)
{
    // 从一个特定的标准设备(标准输出)中取得一个句柄(用来标识不同设备的数值)
    HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
    // 结构体,该结构体包含控制台屏幕缓冲区的信息。设置名称位csbInfo的结构体。
    CONSOLE_SCREEN_BUFFER_INFO csbInfo;
    // 函数,获取特定的控制台屏幕缓冲区信息。
    GetConsoleScreenBufferInfo(hd, &csbInfo);
    // SetConsoleTextAttribute()函数是一个API设置字体颜色和背景色的函数。
    // 有两个属性,第一个属性获得句柄,就是hd,也就是GetStdHandle(STD_OUTPUT_HANDLE),看这个函数的第一行;
    // 第一个属性和第二个属性中间用“,”隔开。
    // 第二个属性是设置颜色,中间有|
    // 结构体里面有wAttributes存储了绘制文本、背景的颜色等属性,在调用输出函数输出到控制台的时候就会作用。
    // 可以看一下这篇文章https://blog.csdn.net/weixin_30443731/article/details/99245800
    return SetConsoleTextAttribute(hd, (iColor<<4)|(csbInfo.wAttributes&~0xF0));
}

34,练习函数调用

1.题目:练习函数调用(发动你的小脑袋瓜,随便写一个函数然后调用就可以了~)
2.程序源代码:

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

void three_hellos(void);
void hello_world(void);

int main(void)
{
    three_hellos();//调用此函数
    getch();
}

void three_hellos(void)
{
    int counter;
    for (counter = 1; counter <= 3; counter++) {
        hello_world();//调用此函数
    }

}

void hello_world(void)
{
    printf("Hello, world!\n");
}

35,文本颜色设置

1.题目:文本颜色设置
2.程序源代码:

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

void textcolor(int color);

int main(void)
{
    int color;

    for (color = 1; color < 16; color++) {

        textcolor(color);// 设置文本颜色
        printf("This is color %d\r\n", color);

    }

    textcolor(128 + 15);
    printf("This is blinking\r\n");

    getch();
}

void textcolor(int color) // 更改字体颜色
{
    //在文本模式中选择新的字符颜色
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); // 得到标准输出的句柄
    SetConsoleTextAttribute(hOutput, color);//Attribute:属性
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值