c/c++代码 No.15 用一个字节控制8个家电的开和关

本程序使用C++实现了一个控制台应用,能够控制显示八种不同物品的状态(开启或关闭),并利用位操作来高效更新和展示这些状态。用户可以通过输入数字1到8来切换对应物品的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <iomanip>
#include <windows.h>
#include <conio.h>

using namespace std;

const char items[8][20] = {"冰箱", "电视", "电脑", "空调", "暖壶", "台灯", "闹钟", "门帘"};

void gotoXY(int x, int y) {
   COORD coord = {x, y};
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void hideTheCursor() {
   CONSOLE_CURSOR_INFO cciCursor;
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

   if(GetConsoleCursorInfo(hStdOut, &cciCursor))
   {
      cciCursor.bVisible = FALSE;
      SetConsoleCursorInfo(hStdOut, &cciCursor);
   }
}
char* getBit(char n, char *str) {
    int size = 8 * sizeof(char);

    for (int i = size - 1; i >= 0; n >>= 1) {
        str[i--] = (n & 1) + '0';
    }
    str[size] = '\0';
    return str;
}

int toBit(char *str) {
    int n = 0;
    for (int i = 0; str[i] != '\0'; i++) {
        n <<= 1;
        n += str[i] - '0';
    }
    return n;
}

bool getBitW(char n, int w) {
    int bitval = 1;
    while (w--) {
        bitval <<= 1;
    }
    return n & bitval;
}

int setBitW(char n, int w) {
    int bitval = 1;
    while (w--) {
        bitval <<= 1;
    }
    return n ^ bitval;
}

int main(void) {
    char status = 0;
    char *str = new char[101];
    int m[8] = {0};
    hideTheCursor();
    system("cls");
    str = getBit(status, str);
    cout << setfill('0') << setw(8 * sizeof(char)) << str << endl;
    for (int i = 0; i < 8; i++) {
        cout << items[i] << ':';
        if (getBitW(status, i)) {
            cout << "开启";
            for (int j = 0; j < m[i]; j++) {
                cout << '.';
            }
            m[i]++;
            m[i] &= 7;
        } else {
            cout << "关闭";
        }
        cout << endl;
    }
    while (true) {
        if (kbhit()) {
            int a = getch() - '0';
            if (a >= 1 && a <= 8) {
                status = setBitW(status, a - 1);
            }
        }
        gotoXY(0,0);
        str = getBit(status, str);
        cout << setfill('0') << setw(8 * sizeof(char)) << str << endl;
        for (int i = 0; i < 8; i++) {
            if (getBitW(status, i)) {
                gotoXY(6,i + 1);
                cout << "开启";
                for (int j = 0; j < 8; j++) {
                    if (m[i] > j) {
                        cout << '.';
                    } else {
                        cout << ' ';
                    }
                }
                m[i]++;
                m[i] &= 7;
            } else {
                gotoXY(6,i + 1);
                cout << "关闭";
                for (int j = 0; j < 8; j++) {
                    cout << ' ';
                }
            }
        }
        Sleep(50);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值