20240301作业

本文介绍了如何使用C语言中的fwrite和fread函数操作BMP图片,将其背景色替换为德国国旗,并利用getch函数创建一个密码输入函数,实现输入时的星号掩码。
摘要由CSDN通过智能技术生成

 1.使用fwrite、fread将一张随意的bmp图片,修改成德国的国旗

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    FILE* fp = fopen("./gaoda.bmp","r");
 
    fseek(fp,2,SEEK_SET);
    int bmp_size = 0;
    fread(&bmp_size,4,1,fp);
    printf("文件大小为 %d 字节\n",bmp_size);
 
    int w = 0,h = 0;
    fseek(fp,18,SEEK_SET);
    fread(&w,4,1,fp);
    fread(&h,4,1,fp);
    printf("图像尺寸为:%d * %d\n",w,h);
 
    fclose(fp);
 
    fp = fopen("./gaoda.bmp","r+");
 
    fseek(fp,54,SEEK_SET);
    // bmp 图片默认像素格式是 bgr的
    unsigned char yel[3] = {0,255,255};
    unsigned char red[3] = {0,0,255};
    unsigned char bla[3] = {0,0,0};
    for(int i=0;i<w;i++)
    {
        for(int j=0;j<(h/3);j++)
        {
            fwrite(yel,3,1,fp);
        }
    }
    for(int i=0;i<w;i++)
    {
        for(int j=(h/3);j<(h/3*2);j++)
        {
            fwrite(red,3,1,fp);
        }
    }
    for(int i=0;i<w;i++)
    {
        for(int j=(h/3*2);j<h;j++)
        {
            fwrite(bla,3,1,fp);
        }
    }
    fclose(fp);
    return 0;
}

 

2.使用提供的getch函数,编写一个专门用来输入密码的函数,要求输入密码的时候,显示 * 号,输入回车的时候,密码输入结束

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <assert.h>
int getch() 
{
    int c = 0;
    struct termios org_opts, new_opts;
    int res = 0;
    res = tcgetattr(STDIN_FILENO, &org_opts);
    assert(res == 0);
 
    new_opts = org_opts;
 
    new_opts.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
 
    c = getchar();
 
    res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
    assert(res == 0);
    return c;
}
void password()
{
    char arr[6]={'1','2','3','4','5','6'};
    char pass[32]={0};
    int i=0;
    printf("请输入密码:");
    while(1)
    {
        char c=getch();
        if(c=='\n'){break;}
        printf("*");
        pass[i++]=c;
    }
    int a=1;
    for(i=0;i<6;i++)
    {
        int k=strlen(pass);
        if(k!=6)
        {
            a=0;
            break;
        }
        else if(pass[i]!=arr[i])
        {
            a=0;
            break;
        }
    }    
    if(a)
    {
        printf("\n密码正确\n");
    }
    else
    {    
        printf("\n密码错误,请重新输入:\n");
        password();
    }
    fflush(stdin);
}
int main(int argc, char *argv[])
{
    password(); 
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值