showcursor

本文详细介绍了Windows API中的ShowCursor函数,该函数通过调整内部计数器来实现光标的显示或隐藏。文章解释了函数参数bShow的作用及如何影响计数器,并说明了不同情况下光标显示的初始状态。
  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1193665761703&lmt=1193665780&format=336x280_as&output=html&correlator=1193665761687&url=http%3A%2F%2Fwww.codeguru.cn%2Fpublic%2Fiframe%2Fwinapiiframe.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=1285758818.1193665762&ga_sid=1193665762&ga_hid=111695597&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=8&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency">     函数功能:该函数显示或隐藏光标。

    函数原型:int ShowCursor(BOOL bShow);

    参数:

    bShow:确定内部的显示计数器是增加还是减少,如果bShow为TRUE,则显示计数器增加1,如果bShow为FALSE,则计数器减1。

    返回值:返回值规定新的显示计数器。

    备注:该函数设置了一个内部显示计数器以确定光标是否显示,仅当显示计数器的值大于或等于0时,光标才显示,如果安装了鼠标,则显示计数的初始值为0。如果没有安装鼠标,显示计数是C1。

    速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:不支持;头文件:winuser.h;库文件:user32.lib。

#include <graphics.h> #include <conio.h> #include <stdio.h> #include <string.h> #include <windows.h> #include <tchar.h> #include <time.h> #include <vector> #include <algorithm> using namespace std; // ================== 公共定义 ================== enum LoginState { LOGGING_IN, LOGIN_SUCCESS, LOGIN_FAILED }; enum UserType { ORDINARY_USER, ADMIN_USER }; // ================== 用户信息结构 ================== struct UserInfo { TCHAR username[50]; UserType type; }; // ================== 图书信息结构 ================== struct Book { int id; TCHAR title[100]; TCHAR author[50]; TCHAR publisher[50]; int year; int quantity; Book* next; }; // ================== 全局变量 ================== // 图书管理相关 vector<Book*> bookArray; Book* bookList = nullptr; const int PAGE_SIZE = 5; int currentPage = 0; int totalPages = 0; int selectedBookId = -1; bool isEditing = false; // ================== 函数声明 ================== // 登录模块 UserInfo loginModule(); void drawLoginInterface(LoginState state, const TCHAR* username, const TCHAR* password, bool showPassword, bool isAdminLogin, bool usernameSelected, bool passwordSelected, bool showCursor); void showMessage(const TCHAR* message, bool isSuccess = true); void registerUser(const TCHAR* username, const TCHAR* password); // 图书管理模块 void initializeBooks(); void addBook(Book* newBook); void deleteBook(int id); void drawBookManagement(); void drawBookEdit(bool isAdding); void bookManagementSystem(); // ================== 主函数 ================== int main() { // 初始化图形界面 initgraph(700, 500); BeginBatchDraw(); // 1. 运行登录模块 UserInfo currentUser = loginModule(); // 2. 检查是否管理员登录成功 if (currentUser.type == ADMIN_USER && _tcscmp(currentUser.username, _T("admin")) == 0) { // 3. 进入图书管理模块 bookManagementSystem(); } else { // 普通用户或其他情况处理 MessageBox(NULL, _T("欢迎使用图书借阅系统"), _T("系统提示"), MB_OK | MB_ICONINFORMATION); } // 清理资源 EndBatchDraw(); closegraph(); return 0; } // ================== 登录模块实现 ================== UserInfo loginModule() { UserInfo user = { _T(""), ORDINARY_USER }; LoginState loginState = LOGGING_IN; TCHAR username[50] = _T(""); TCHAR password[50] = _T(""); bool usernameSelected = true; bool passwordSelected = false; bool showPassword = false; bool isAdminLogin = false; clock_t cursorTimer = clock(); bool showCursor = true; while (true) { // 处理光标闪烁 if (clock() - cursorTimer > 500) { showCursor = !showCursor; cursorTimer = clock(); } // 绘制界面 drawLoginInterface(loginState, username, password, showPassword, isAdminLogin, usernameSelected, passwordSelected, showCursor); // 处理鼠标事件 MOUSEMSG m; while (MouseHit()) { m = GetMouseMsg(); if (m.uMsg == WM_LBUTTONDOWN) { // 点击用户名输入框 if (m.x > 270 && m.x < 510 && m.y > 175 && m.y < 215) { usernameSelected = true; passwordSelected = false; } // 点击密码输入框 else if (m.x > 270 && m.x < 540 && m.y > 235 && m.y < 275) { usernameSelected = false; passwordSelected = true; } // 点击显示/隐藏密码按钮 else if (m.x > 515 && m.x < 540 && m.y > 235 && m.y < 275) { showPassword = !showPassword; } // 点击管理员登录选项 else if (m.x > 270 && m.x < 290 && m.y > 285 && m.y < 305) { isAdminLogin = !isAdminLogin; } // 点击登录按钮 else if (m.x > 250 && m.x < 450 && m.y > 330 && m.y < 380) { if (_tcscmp(username, _T("")) == 0 || _tcscmp(password, _T("")) == 0) { showMessage(_T("用户名和密码不能为空"), false); } else if (isAdminLogin) { if (_tcscmp(username, _T("admin")) == 0 && _tcscmp(password, _T("admin123")) == 0) { loginState = LOGIN_SUCCESS; showMessage(_T("管理员登录成功!")); FlushBatchDraw(); Sleep(1000); // 返回管理员信息 _tcscpy_s(user.username, username); user.type = ADMIN_USER; return user; } else { showMessage(_T("管理员账号或密码错误"), false); } } else { if (_tcscmp(username, _T("user")) == 0 && _tcscmp(password, _T("user123")) == 0) { loginState = LOGIN_SUCCESS; showMessage(_T("登录成功!")); FlushBatchDraw(); Sleep(1000); // 返回普通用户信息 _tcscpy_s(user.username, username); user.type = ORDINARY_USER; return user; } else { showMessage(_T("用户名或密码错误"), false); } } _tcscpy_s(password, _T("")); } // 点击注册按钮 else if (m.x > 250 && m.x < 450 && m.y > 390 && m.y < 440) { if (_tcscmp(username, _T("")) == 0 || _tcscmp(password, _T("")) == 0) { showMessage(_T("用户名和密码不能为空"), false); } else { registerUser(username, password); _tcscpy_s(username, _T("")); _tcscpy_s(password, _T("")); } } } } // 处理键盘事件 while (_kbhit()) { int key = _getch(); if (key == 8) { // 退格键 if (usernameSelected && _tcslen(username) > 0) { username[_tcslen(username) - 1] = _T('\0'); } else if (passwordSelected && _tcslen(password) > 0) { password[_tcslen(password) - 1] = _T('\0'); } } else if (key == 9) { // Tab键 if (usernameSelected) { usernameSelected = false; passwordSelected = true; } else { usernameSelected = true; passwordSelected = false; } } else if (key == 13) { // 回车键 if (_tcscmp(username, _T("")) == 0 || _tcscmp(password, _T("")) == 0) { showMessage(_T("用户名和密码不能为空"), false); } else if (isAdminLogin) { if (_tcscmp(username, _T("admin")) == 0 && _tcscmp(password, _T("admin123")) == 0) { loginState = LOGIN_SUCCESS; showMessage(_T("管理员登录成功!")); FlushBatchDraw(); Sleep(1000); // 返回管理员信息 _tcscpy_s(user.username, username); user.type = ADMIN_USER; return user; } else { showMessage(_T("管理员账号或密码错误"), false); } } else { if (_tcscmp(username, _T("user")) == 0 && _tcscmp(password, _T("user123")) == 0) { loginState = LOGIN_SUCCESS; showMessage(_T("登录成功!")); FlushBatchDraw(); Sleep(1000); // 返回普通用户信息 _tcscpy_s(user.username, username); user.type = ORDINARY_USER; return user; } else { showMessage(_T("用户名或密码错误"), false); } } _tcscpy_s(password, _T("")); } else if (key >= 32 && key <= 126) { // 可打印字符 TCHAR c = (TCHAR)key; size_t len; if (usernameSelected) { len = _tcslen(username); if (len < 49) { username[len] = c; username[len + 1] = _T('\0'); } } else if (passwordSelected) { len = _tcslen(password); if (len < 49) { password[len] = c; password[len + 1] = _T('\0'); } } } } FlushBatchDraw(); Sleep(20); } return user; // 正常情况下不会执行到这里 } void drawLoginInterface(LoginState state, const TCHAR* username, const TCHAR* password, bool showPassword, bool isAdminLogin, bool usernameSelected, bool passwordSelected, bool showCursor) { // 设置背景 setbkcolor(RGB(240, 245, 255)); cleardevice(); // 绘制标题 settextcolor(RGB(25, 118, 210)); settextstyle(36, 0, _T("宋体")); outtextxy(220, 50, _T("图书借阅系统")); // 绘制系统信息 settextcolor(RGB(100, 100, 100)); settextstyle(14, 0, _T("宋体")); outtextxy(300, 100, _T("v1.2")); // 绘制登录框 setfillcolor(RGB(255, 255, 255)); setlinecolor(RGB(200, 200, 200)); fillrectangle(150, 150, 550, 450); // 绘制用户名标签 settextcolor(RGB(80, 80, 80)); settextstyle(20, 0, _T("宋体")); outtextxy(180, 180, _T("用户名:")); // 绘制用户名输入框 setfillcolor(usernameSelected ? RGB(235, 245, 255) : RGB(245, 245, 245)); fillrectangle(270, 175, 510, 215); setlinecolor(usernameSelected ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(270, 175, 510, 215); // 绘制用户名文本 settextcolor(RGB(50, 50, 50)); settextstyle(18, 0, _T("宋体")); outtextxy(275, 180, username); // 绘制光标 if (usernameSelected && showCursor) { int textWidth = textwidth(username); line(275 + textWidth, 180, 275 + textWidth, 205); } // 绘制密码标签 settextcolor(RGB(80, 80, 80)); settextstyle(20, 0, _T("宋体")); outtextxy(180, 240, _T("密 码:")); // 绘制密码输入框 setfillcolor(passwordSelected ? RGB(235, 245, 255) : RGB(245, 245, 245)); fillrectangle(270, 235, 510, 275); setlinecolor(passwordSelected ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(270, 235, 510, 275); // 创建掩码密码字符串 TCHAR maskedPassword[50] = _T(""); if (!showPassword) { size_t len = _tcslen(password); for (size_t i = 0; i < len; i++) { _tcscat_s(maskedPassword, 50, _T("?")); } } // 绘制密码文本 settextcolor(RGB(50, 50, 50)); settextstyle(18, 0, _T("宋体")); if (showPassword) { outtextxy(275, 240, password); } else { outtextxy(275, 240, maskedPassword); } // 绘制光标 if (passwordSelected && showCursor) { const TCHAR* displayText = showPassword ? password : maskedPassword; int textWidth = textwidth(displayText); line(275 + textWidth, 240, 275 + textWidth, 265); } // 显示/隐藏密码按钮 setfillcolor(showPassword ? RGB(220, 237, 255) : RGB(245, 245, 245)); fillrectangle(515, 235, 540, 275); setlinecolor(RGB(180, 180, 180)); rectangle(515, 235, 540, 275); settextcolor(RGB(100, 100, 100)); settextstyle(16, 0, _T("宋体")); outtextxy(520, 240, showPassword ? _T("??") : _T("??")); // 管理员登录选项 setfillcolor(RGB(245, 245, 245)); fillrectangle(270, 285, 290, 305); setlinecolor(RGB(180, 180, 180)); rectangle(270, 285, 290, 305); if (isAdminLogin) { setlinecolor(RGB(25, 118, 210)); setlinestyle(PS_SOLID, 2); line(272, 292, 282, 302); line(282, 302, 288, 286); setlinestyle(PS_SOLID, 1); } settextcolor(RGB(80, 80, 80)); settextstyle(16, 0, _T("宋体")); outtextxy(300, 285, _T("管理员登录")); // 绘制登录按钮 setfillcolor(RGB(245, 245, 245)); fillroundrect(250, 330, 450, 380, 10, 10); settextcolor(RGB(25, 118, 210)); settextstyle(24, 0, _T("宋体")); outtextxy(320, 340, _T("登 录")); // 绘制注册按钮 setfillcolor(RGB(245, 245, 245)); fillroundrect(250, 390, 450, 440, 10, 10); settextcolor(RGB(25, 118, 210)); settextstyle(24, 0, _T("宋体")); outtextxy(320, 400, _T("注 册")); // 绘制状态信息 if (state == LOGIN_FAILED) { settextcolor(RGB(211, 47, 47)); settextstyle(16, 0, _T("宋体")); outtextxy(250, 305, _T("用户名或密码错误,请重试")); } else if (state == LOGIN_SUCCESS) { settextcolor(RGB(56, 142, 60)); settextstyle(16, 0, _T("宋体")); outtextxy(300, 305, _T("登录成功!")); } // 绘制版权信息 settextcolor(RGB(150, 150, 150)); settextstyle(12, 0, _T("宋体")); outtextxy(240, 480, _T("? 2023 计算机科学与技术2304114班")); } void showMessage(const TCHAR* message, bool isSuccess) { settextcolor(isSuccess ? RGB(56, 142, 60) : RGB(211, 47, 47)); settextstyle(16, 0, _T("宋体")); outtextxy(250, 305, message); FlushBatchDraw(); Sleep(2000); } void registerUser(const TCHAR* username, const TCHAR* password) { // 在实际应用中,这里应该将用户信息保存到数据库或文件 showMessage(_T("注册成功! 请使用新账号登录"), true); } // ================== 图书管理模块实现 ================== void initializeBooks() { Book* books[] = { new Book{1, _T("C++程序设计"), _T("谭浩强"), _T("清华大学出版社"), 2019, 10, nullptr}, new Book{2, _T("数据结构"), _T("严蔚敏"), _T("清华大学出版社"), 2020, 8, nullptr}, new Book{3, _T("算法导论"), _T("Thomas Cormen"), _T("MIT Press"), 2009, 5, nullptr}, new Book{4, _T("深入理解计算机系统"), _T("Bryant"), _T("机械工业出版社"), 2016, 7, nullptr}, new Book{5, _T("设计模式"), _T("Erich Gamma"), _T("机械工业出版社"), 2020, 6, nullptr} }; // 构建链表 for (int i = 0; i < sizeof(books) / sizeof(books[0]); i++) { books[i]->next = bookList; bookList = books[i]; bookArray.push_back(books[i]); } // 按ID排序数组 sort(bookArray.begin(), bookArray.end(), [](Book* a, Book* b) { return a->id < b->id; }); totalPages = (bookArray.size() + PAGE_SIZE - 1) / PAGE_SIZE; } // 添加新图书 void addBook(Book* newBook) { // 添加到链表头部 newBook->next = bookList; bookList = newBook; // 添加到数组并排序 bookArray.push_back(newBook); sort(bookArray.begin(), bookArray.end(), [](Book* a, Book* b) { return a->id < b->id; }); totalPages = (bookArray.size() + PAGE_SIZE - 1) / PAGE_SIZE; } // 删除图书 void deleteBook(int id) { // 从链表中删除 Book* prev = nullptr; Book* current = bookList; while (current != nullptr) { if (current->id == id) { if (prev) { prev->next = current->next; } else { bookList = current->next; } break; } prev = current; current = current->next; } // 从数组中删除 for (auto it = bookArray.begin(); it != bookArray.end(); ) { if ((*it)->id == id) { delete* it; // 释放内存 it = bookArray.erase(it); } else { ++it; } } totalPages = (bookArray.size() + PAGE_SIZE - 1) / PAGE_SIZE; if (currentPage >= totalPages && totalPages > 0) { currentPage = totalPages - 1; } } // 绘制图书管理界面 void drawBookManagement() { // 设置背景 setbkcolor(RGB(240, 245, 255)); cleardevice(); // 绘制标题 settextcolor(RGB(25, 118, 210)); settextstyle(36, 0, _T("宋体")); outtextxy(250, 20, _T("图书管理系统")); // 绘制功能按钮 setfillcolor(RGB(25, 118, 210)); settextcolor(WHITE); settextstyle(20, 0, _T("宋体")); // 添加图书按钮 fillroundrect(50, 80, 180, 120, 10, 10); outtextxy(70, 85, _T("添加图书")); // 删除图书按钮 fillroundrect(210, 80, 340, 120, 10, 10); outtextxy(230, 85, _T("删除图书")); // 退出系统按钮 fillroundrect(530, 80, 660, 120, 10, 10); outtextxy(550, 85, _T("退出系统")); // 绘制图书列表标题 setfillcolor(RGB(200, 220, 255)); fillrectangle(50, 140, 650, 170); settextcolor(RGB(25, 118, 210)); settextstyle(18, 0, _T("宋体")); outtextxy(60, 145, _T("ID")); outtextxy(120, 145, _T("书名")); outtextxy(330, 145, _T("作者")); outtextxy(450, 145, _T("出版社")); outtextxy(560, 145, _T("年份")); outtextxy(620, 145, _T("库存")); // 绘制图书列表 int startIdx = currentPage * PAGE_SIZE; int endIdx = min(startIdx + PAGE_SIZE, (int)bookArray.size()); int y = 180; for (int i = startIdx; i < endIdx; i++) { Book* book = bookArray[i]; // 设置选中背景 if (book->id == selectedBookId) { setfillcolor(RGB(230, 240, 255)); solidrectangle(50, y, 650, y + 30); } else { setfillcolor(i % 2 == 0 ? RGB(245, 250, 255) : RGB(255, 255, 255)); solidrectangle(50, y, 650, y + 30); } setlinecolor(RGB(220, 220, 220)); rectangle(50, y, 650, y + 30); // 绘制图书信息 settextcolor(RGB(50, 50, 50)); TCHAR buffer[50]; _stprintf_s(buffer, _T("%d"), book->id); outtextxy(60, y + 5, buffer); outtextxy(120, y + 5, book->title); outtextxy(330, y + 5, book->author); outtextxy(450, y + 5, book->publisher); _stprintf_s(buffer, _T("%d"), book->year); outtextxy(560, y + 5, buffer); _stprintf_s(buffer, _T("%d"), book->quantity); outtextxy(620, y + 5, buffer); y += 32; } // 绘制分页控件 setfillcolor(RGB(245, 245, 245)); fillroundrect(300, 450, 380, 490, 10, 10); fillroundrect(400, 450, 480, 490, 10, 10); settextcolor(RGB(25, 118, 210)); TCHAR pageBuffer[20]; _stprintf_s(pageBuffer, _T("%d/%d"), currentPage + 1, totalPages); outtextxy(385, 455, pageBuffer); settextstyle(20, 0, _T("宋体")); outtextxy(320, 450, _T("<")); outtextxy(440, 450, _T(">")); // 绘制编辑按钮 if (selectedBookId != -1) { setfillcolor(RGB(25, 118, 210)); fillroundrect(500, 450, 620, 490, 10, 10); settextcolor(WHITE); outtextxy(520, 455, _T("编辑图书")); } } // 绘制添加/编辑图书界面 void drawBookEdit(bool isAdding) { static int lastEditedId = -1; static bool firstEntry = true; static TCHAR title[100] = _T(""); static TCHAR author[50] = _T(""); static TCHAR publisher[50] = _T(""); static TCHAR yearStr[10] = _T(""); static TCHAR quantityStr[10] = _T(""); static int fieldSelected = 0; // 0:书名, 1:作者, 2:出版社, 3:年份, 4:库存 static clock_t cursorTimer = clock(); static bool showCursor = true; // 检查是否需要重置编辑数据 if (firstEntry) { if (isAdding) { _tcscpy_s(title, _T("")); _tcscpy_s(author, _T("")); _tcscpy_s(publisher, _T("")); _tcscpy_s(yearStr, _T("")); _tcscpy_s(quantityStr, _T("")); } else { // 从当前选中书籍加载数据 for (Book* book : bookArray) { if (book->id == selectedBookId) { _tcscpy_s(title, book->title); _tcscpy_s(author, book->author); _tcscpy_s(publisher, book->publisher); _stprintf_s(yearStr, _T("%d"), book->year); _stprintf_s(quantityStr, _T("%d"), book->quantity); break; } } } firstEntry = false; lastEditedId = isAdding ? -1 : selectedBookId; } // 光标闪烁处理 if (clock() - cursorTimer > 500) { showCursor = !showCursor; cursorTimer = clock(); } // 设置背景 setbkcolor(RGB(240, 245, 255)); cleardevice(); // 绘制标题 settextcolor(RGB(25, 118, 210)); settextstyle(36, 0, _T("宋体")); outtextxy(250, 30, isAdding ? _T("添加图书") : _T("编辑图书")); // 绘制表单 int y = 100; const int fieldHeight = 40; const int buttonY = 360; // 固定按钮位置 // 书名 settextcolor(RGB(80, 80, 80)); settextstyle(20, 0, _T("宋体")); outtextxy(150, y + 10, _T("书名:")); setfillcolor(fieldSelected == 0 ? RGB(235, 245, 255) : RGB(245, 245, 245)); solidrectangle(250, y, 550, y + fieldHeight); setlinecolor(fieldSelected == 0 ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(250, y, 550, y + fieldHeight); settextcolor(RGB(50, 50, 50)); outtextxy(260, y + 10, title); if (fieldSelected == 0 && showCursor) { int textWidth = textwidth(title); line(260 + textWidth, y + 10, 260 + textWidth, y + fieldHeight - 10); } y += fieldHeight + 10; // 作者 settextcolor(RGB(80, 80, 80)); outtextxy(150, y + 10, _T("作者:")); setfillcolor(fieldSelected == 1 ? RGB(235, 245, 255) : RGB(245, 245, 245)); solidrectangle(250, y, 550, y + fieldHeight); setlinecolor(fieldSelected == 1 ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(250, y, 550, y + fieldHeight); settextcolor(RGB(50, 50, 50)); outtextxy(260, y + 10, author); if (fieldSelected == 1 && showCursor) { int textWidth = textwidth(author); line(260 + textWidth, y + 10, 260 + textWidth, y + fieldHeight - 10); } y += fieldHeight + 10; // 出版社 settextcolor(RGB(80, 80, 80)); outtextxy(150, y + 10, _T("出版社:")); setfillcolor(fieldSelected == 2 ? RGB(235, 245, 255) : RGB(245, 245, 245)); solidrectangle(250, y, 550, y + fieldHeight); setlinecolor(fieldSelected == 2 ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(250, y, 550, y + fieldHeight); settextcolor(RGB(50, 50, 50)); outtextxy(260, y + 10, publisher); if (fieldSelected == 2 && showCursor) { int textWidth = textwidth(publisher); line(260 + textWidth, y + 10, 260 + textWidth, y + fieldHeight - 10); } y += fieldHeight + 10; // 出版年份 settextcolor(RGB(80, 80, 80)); outtextxy(150, y + 10, _T("年份:")); setfillcolor(fieldSelected == 3 ? RGB(235, 245, 255) : RGB(245, 245, 245)); solidrectangle(250, y, 550, y + fieldHeight); setlinecolor(fieldSelected == 3 ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(250, y, 550, y + fieldHeight); settextcolor(RGB(50, 50, 50)); outtextxy(260, y + 10, yearStr); if (fieldSelected == 3 && showCursor) { int textWidth = textwidth(yearStr); line(260 + textWidth, y + 10, 260 + textWidth, y + fieldHeight - 10); } y += fieldHeight + 10; // 库存数量 settextcolor(RGB(80, 80, 80)); outtextxy(150, y + 10, _T("库存:")); setfillcolor(fieldSelected == 4 ? RGB(235, 245, 255) : RGB(245, 245, 245)); solidrectangle(250, y, 550, y + fieldHeight); setlinecolor(fieldSelected == 4 ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(250, y, 550, y + fieldHeight); settextcolor(RGB(50, 50, 50)); outtextxy(260, y + 10, quantityStr); if (fieldSelected == 4 && showCursor) { int textWidth = textwidth(quantityStr); line(260 + textWidth, y + 10, 260 + textWidth, y + fieldHeight - 10); } // 保存按钮 setfillcolor(RGB(25, 118, 210)); fillroundrect(250, buttonY, 380, buttonY + 50, 10, 10); settextcolor(WHITE); settextstyle(20, 0, _T("宋体")); outtextxy(280, buttonY + 15, _T("保存")); // 取消按钮 setfillcolor(RGB(200, 200, 200)); fillroundrect(420, buttonY, 550, buttonY + 50, 10, 10); settextcolor(WHITE); outtextxy(450, buttonY + 15, _T("取消")); // 处理鼠标事件 MOUSEMSG m; while (MouseHit()) { m = GetMouseMsg(); if (m.uMsg == WM_LBUTTONDOWN) { // 点击输入框 if (m.x > 250 && m.x < 550) { if (m.y > 100 && m.y < 140) fieldSelected = 0; else if (m.y > 150 && m.y < 190) fieldSelected = 1; else if (m.y > 200 && m.y < 240) fieldSelected = 2; else if (m.y > 250 && m.y < 290) fieldSelected = 3; else if (m.y > 300 && m.y < 340) fieldSelected = 4; } // 点击保存按钮 else if (m.x >= 250 && m.x <= 380 && m.y >= buttonY && m.y <= buttonY + 50) { setfillcolor(RGB(15, 98, 200)); fillroundrect(250, buttonY, 380, buttonY + 50, 10, 10); outtextxy(280, buttonY + 15, _T("保存")); FlushBatchDraw(); Sleep(100); int year = _ttoi(yearStr); int quantity = _ttoi(quantityStr); if (_tcslen(title) == 0) { MessageBox(NULL, _T("书名不能为空"), _T("错误"), MB_OK | MB_ICONERROR); } else if (year < 1900 || year > 2100) { MessageBox(NULL, _T("出版年份必须在1900-2100之间"), _T("错误"), MB_OK | MB_ICONERROR); } else if (quantity <= 0) { MessageBox(NULL, _T("库存数量必须大于0"), _T("错误"), MB_OK | MB_ICONERROR); } else { if (isAdding) { // 生成新ID int newId = 1; if (!bookArray.empty()) { newId = bookArray.back()->id + 1; } Book* newBook = new Book{ newId, _T(""), _T(""), _T(""), year, quantity, nullptr }; _tcscpy_s(newBook->title, title); _tcscpy_s(newBook->author, author); _tcscpy_s(newBook->publisher, publisher); addBook(newBook); MessageBox(NULL, _T("图书添加成功"), _T("成功"), MB_OK | MB_ICONINFORMATION); } else { // 更新现有图书 for (Book* book : bookArray) { if (book->id == selectedBookId) { _tcscpy_s(book->title, title); _tcscpy_s(book->author, author); _tcscpy_s(book->publisher, publisher); book->year = year; book->quantity = quantity; break; } } MessageBox(NULL, _T("图书信息更新成功"), _T("成功"), MB_OK | MB_ICONINFORMATION); } isEditing = false; firstEntry = true; // 重置标志 return; } } // 点击取消按钮 else if (m.x >= 420 && m.x <= 550 && m.y >= buttonY && m.y <= buttonY + 50) { // 添加点击反馈 setfillcolor(RGB(180, 180, 180)); fillroundrect(420, buttonY, 550, buttonY + 50, 10, 10); outtextxy(450, buttonY + 15, _T("取消")); FlushBatchDraw(); Sleep(100); isEditing = false; firstEntry = true; return; } } } // 处理键盘输入 while (_kbhit()) { wint_t key = _getwch(); if (key == 8) { // 退格键 if (fieldSelected == 0 && _tcslen(title) > 0) { title[_tcslen(title) - 1] = _T('\0'); } else if (fieldSelected == 1 && _tcslen(author) > 0) { author[_tcslen(author) - 1] = _T('\0'); } else if (fieldSelected == 2 && _tcslen(publisher) > 0) { publisher[_tcslen(publisher) - 1] = _T('\0'); } else if (fieldSelected == 3 && _tcslen(yearStr) > 0) { yearStr[_tcslen(yearStr) - 1] = _T('\0'); } else if (fieldSelected == 4 && _tcslen(quantityStr) > 0) { quantityStr[_tcslen(quantityStr) - 1] = _T('\0'); } } else if (key == 9 || key == 13) { // Tab键或回车键切换字段 fieldSelected = (fieldSelected + 1) % 5; } else if (key >= 32) { // 可打印字符 TCHAR c = (TCHAR)key; if (fieldSelected == 0 && _tcslen(title) < 99) { size_t len = _tcslen(title); title[len] = c; title[len + 1] = _T('\0'); } else if (fieldSelected == 1 && _tcslen(author) < 49) { size_t len = _tcslen(author); author[len] = c; author[len + 1] = _T('\0'); } else if (fieldSelected == 2 && _tcslen(publisher) < 49) { size_t len = _tcslen(publisher); publisher[len] = c; publisher[len + 1] = _T('\0'); } else if (fieldSelected == 3 && _tcslen(yearStr) < 4) { if (c >= '0' && c <= '9') { size_t len = _tcslen(yearStr); yearStr[len] = c; yearStr[len + 1] = _T('\0'); } } else if (fieldSelected == 4 && _tcslen(quantityStr) < 5) { if (c >= '0' && c <= '9') { size_t len = _tcslen(quantityStr); quantityStr[len] = c; quantityStr[len + 1] = _T('\0'); } } } } FlushBatchDraw(); Sleep(20); } // 图书管理主函数 void bookManagementSystem() { initializeBooks(); while (true) { if (isEditing) { drawBookEdit(false); } else { drawBookManagement(); // 只在非编辑状态下处理主界面事件 if (MouseHit()) { MOUSEMSG m = GetMouseMsg(); if (m.uMsg == WM_LBUTTONDOWN) { // 点击添加图书按钮 if (m.x > 50 && m.x < 180 && m.y > 80 && m.y < 120) { isEditing = true; // 清空鼠标消息队列 while (MouseHit()) GetMouseMsg(); } // 点击删除图书按钮 else if (m.x > 210 && m.x < 340 && m.y > 80 && m.y < 120 && selectedBookId != -1) { if (MessageBox(NULL, _T("确定要删除这本图书吗?"), _T("确认删除"), MB_YESNO | MB_ICONQUESTION) == IDYES) { deleteBook(selectedBookId); selectedBookId = -1; } } // 点击退出系统按钮 else if (m.x > 530 && m.x < 660 && m.y > 80 && m.y < 120) { if (MessageBox(NULL, _T("确定要退出系统吗?"), _T("确认退出"), MB_YESNO | MB_ICONQUESTION) == IDYES) { break; } } // 点击上一页按钮 else if (m.x > 300 && m.x < 380 && m.y > 450 && m.y < 490 && currentPage > 0) { currentPage--; } // 点击下一页按钮 else if (m.x > 400 && m.x < 480 && m.y > 450 && m.y < 490 && currentPage < totalPages - 1) { currentPage++; } // 点击编辑图书按钮 else if (m.x > 500 && m.x < 620 && m.y > 450 && m.y < 490 && selectedBookId != -1) { isEditing = true; // 清空鼠标消息队列 while (MouseHit()) GetMouseMsg(); } // 点击图书列表 else if (m.x > 50 && m.x < 650 && m.y > 180 && m.y < 180 + PAGE_SIZE * 32) { int idx = (m.y - 180) / 32; int bookIdx = currentPage * PAGE_SIZE + idx; if (bookIdx < bookArray.size()) { selectedBookId = bookArray[bookIdx]->id; } } } } } FlushBatchDraw(); Sleep(20); } 添加图书和编辑图书功能有bug,无法保存和取消(鼠标点击按钮没有反应),请你检查鼠标处理相关的代码发现问题所在并尝试解决。
07-02
#include<thread> #include<iostream> #include<string.h> #include<windows.h> #include<conio.h> #include<mmsystem.h> #include<stdlib.h> using namespace std; #pragma comment(lib,"winmm.lib") //[1] #define MAXLEN 127 #define XSTART 16 #define YSTART 5 #define LENOFPAGE 146 #define DEPTHOFPAGE 20 short piano_type = 1; //2表示亮音钢琴,1是原声钢琴 typedef union { int i; char c[10]; }CwithI; typedef struct { char name[MAXLEN]; CwithI qusu; char ver[20]; }HEAD; void play_sound(char keyboard_key);//播放音频 void play_song(FILE* puzi, short* ystart, HEAD mus_info, short* flag);//播放音乐 void decoding_func(char keyboard_key, char* sound_name, short piano_type);//解码 void gotoxy(int x, int y);//移动光标至指定位置 void print_kuang();//打印框框 void print_pkeys();//打印手动模式下的静态钢琴键盘 void cls_kuang(short, short);//清除框框内指定行的信息 HEAD readhead(FILE*);//读取谱子文件的信息头 void HideCursor();//隐藏光标 void ShowCursor();//显示光标 char begin_page();//初始界面 char exit_page();//退出界面 char manual_page();//手动挡 char auto_page();//自动挡 int main() { system("mode con cols=180 lines=38"); //[8] short mode = 0; while (1) { //所有页面的中转站 if (mode == 0) { mode = begin_page(); } if (mode == 1) { mode = manual_page(); } if (mode == 2) { mode = auto_page(); } if (mode == 3) { exit_page(); break; } fflush(stdin); } return 0; } char exit_page() { HideCursor(); char sentences[][MAXLEN] = { "PROGRAMME DESIGN",\ "名字 from 院系 in 学校",\ "MUSIC RESOURCE",\ "autopiano.cn",\ "SPECIAL THANKS TO",\ "Professor 老师",\ "T.A. 助教1",\ "T.A. 助教2",\ "室友",\ "github.com/WarpPrism/AutoPiano",\ "runoob.com/cprogramming/c-tutorial.html",\ " ",\ "Thank You for Playing My Game!"\ }; print_kuang(); short ystart = YSTART + DEPTHOFPAGE / 2 - 11, line = 0, sigofstr = 0; int speed = 500; for (line = YSTART + DEPTHOFPAGE - 2; line + (sizeof(sentences) / MAXLEN - 2) * 2 > YSTART; line--) { for (sigofstr = 0; sigofstr < sizeof(sentences) / MAXLEN - 1; sigofstr++) { if (kbhit()) { speed = 0; //快速跳过制作人员名单 } if ((line + sigofstr * 2) <= YSTART + DEPTHOFPAGE - 3 && (line + sigofstr * 2) >= YSTART + 2) { cls_kuang(line + sigofstr * 2, -1); cls_kuang(line + sigofstr * 2, 1); //上下两行都清除一下 gotoxy(XSTART + LENOFPAGE / 2 - strlen(sentences[sigofstr]) / 2, line + sigofstr * 2); cout << sentences[sigofstr]; } } Sleep(speed); //滚动字幕的效果 } for (line = YSTART + DEPTHOFPAGE - 3; line >= YSTART + DEPTHOFPAGE / 2 - 1; line--) { cls_kuang(line, 1); gotoxy(XSTART + LENOFPAGE / 2 - strlen(sentences[sizeof(sentences) / MAXLEN - 1]) / 2, line); cout << sentences[sizeof(sentences) / MAXLEN - 1]; Sleep(speed); //同上一条注释 } gotoxy(XSTART + LENOFPAGE / 2 - strlen("请按任意键继续. . .") / 2, YSTART + DEPTHOFPAGE - 2); system("PAUSE"); return 0; } char begin_page() { char line_one[] = "Welcome to elec-piano!"; char line_two[] = "choose your mode:1.manual 2.auto 3.exit"; print_kuang(); gotoxy(XSTART + LENOFPAGE / 2 - strlen(line_one) / 2, YSTART + DEPTHOFPAGE / 2 - 3); //将光标移到方框的正中央并打印文字 cout << line_one; gotoxy(XSTART + LENOFPAGE / 2 - strlen(line_two) / 2, YSTART + DEPTHOFPAGE / 2 - 1); cout << line_two; gotoxy(XSTART + LENOFPAGE / 2, YSTART + DEPTHOFPAGE / 2 + 1); //将光标移至中央 char mode[127] = { 0 }; while (1) { cin >> mode; //输入检测 while (getchar() != '\n') { continue; } if (mode[0] >= '1' && mode[0] <= '3' && strlen(mode) == 1) { break; } else { cls_kuang(YSTART + DEPTHOFPAGE / 2 + 3, 0); gotoxy(XSTART + LENOFPAGE / 2 - strlen("Input error!please try again:") / 2, YSTART + DEPTHOFPAGE / 2 + 1); cout << "Input error!please try again:" << endl; gotoxy(XSTART + LENOFPAGE / 2, YSTART + DEPTHOFPAGE / 2 + 3); } } system("CLS"); return (mode[0] - '0'); } char auto_page() { FILE* dir, * puzi, * puzi_hx; char sys_dir_msg[MAXLEN]; char mus_namelist[50][MAXLEN] = { 0 }; short numofsong = 0, ystart = YSTART + 1, xstart = XSTART + 1, lenofsongname = 0; HEAD mus_info, hx_info; print_kuang(); gotoxy(XSTART + 1, ystart); cout << "Choose your music:"; gotoxy(XSTART + 1, ++ystart); dir = _popen("dir .\\songs", "r"); while (!feof(dir)) { fscanf(dir, "%s", sys_dir_msg); if (strstr(sys_dir_msg, ".dat") != NULL) {//列出songs文件夹中的dat文件 strcpy(mus_namelist[numofsong], sys_dir_msg); cout << ++numofsong << "." << sys_dir_msg; if (strlen(sys_dir_msg) > lenofsongname) { lenofsongname = strlen(sys_dir_msg); } gotoxy(xstart, ++ystart); if (ystart == DEPTHOFPAGE + YSTART - 1) { //文件过多时换个行继续 xstart += lenofsongname + 5; ystart = YSTART + 2; lenofsongname = 0; gotoxy(xstart, ystart); } } } fclose(dir); CwithI tempci; while (1) { scanf("%9s", tempci.c);//输入检测 while (getchar() != '\n') { continue; } tempci.c[9] = '\0'; tempci.i = atoi(tempci.c); if (tempci.i == 0 || tempci.i > 50 || tempci.i < 0) { cls_kuang(ystart, 1); cout << "Input Error!please try again:"; } else { break; } } thread t(play_sound, '+'); //播放一段无声的MP3以加载播放器相关dll t.detach(); gotoxy(XSTART + 1, ++ystart); char path_mode[50] = "songs\\%s"; char path[50] = { 0 }; char path_hx[50] = { 0 }; sprintf(path, path_mode, mus_namelist[tempci.i - 1]); strcpy(path_hx, path); path_hx[strlen(path) - 1] = 'x'; path_hx[strlen(path) - 2] = 'h'; puzi = fopen(path, "r"); if (puzi == NULL) { cout << "File does not exist! Check your input"; Sleep(2500); system("cls"); return 0; } short hx_flag = 1; puzi_hx = fopen(path_hx, "r"); if (puzi_hx == NULL) { hx_flag = 0; } else { hx_info = readhead(puzi_hx); } mus_info = readhead(puzi); system("cls"); print_kuang(); ystart = YSTART + 1; gotoxy(XSTART + 1, ystart); cout << "now play: " << mus_info.name; gotoxy(XSTART + 1, ++ystart); short flag = 0; if (hx_flag == 1) {//有和弦时进入此分支 while (1) { thread t1(play_song, puzi, &ystart, mus_info, &flag); thread t2(play_song, puzi_hx, &ystart, hx_info, &hx_flag); t2.detach(); t1.join(); if (feof(puzi) || feof(puzi_hx) || flag == 2 || hx_flag == 2) {//其中一个谱子放完了自动跳出整个循环 break; } } } else { while (1) { thread t1(play_song, puzi, &ystart, mus_info, &flag); t1.join(); if (feof(puzi) || flag == 2) { break; } } } fclose(puzi); if (hx_flag != 0) { fclose(puzi_hx); } gotoxy(XSTART + 1, ++ystart); cls_kuang(ystart, 1); cout << "end" << endl; cls_kuang(++ystart, 1); system("PAUSE"); system("CLS"); return 0; } char manual_page() { HideCursor(); print_kuang(); print_pkeys(); gotoxy(XSTART + 1, YSTART + DEPTHOFPAGE - 2); cout << "press Enter to quit the manual mode!"; thread t(play_sound, '+'); //播放一段无声的MP3以加载播放器相关dll t.detach(); int i = 0; while (1) { char keyboard_key; keyboard_key = getch(); //[2] if (keyboard_key == '\r') { break; //按回车退出 } thread t(play_sound, keyboard_key); //[3] t.detach(); FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)); //[4] } gotoxy(XSTART + LENOFPAGE / 2 - strlen("exit") / 2, YSTART + DEPTHOFPAGE / 2); cout << "exit!"; ShowCursor(); system("CLS"); return 0; } void decoding_func(char keyboard_key, char* sound_name, short piano_type) { //在plays函数中被引用,可有效降低创建线程时传参的复杂程度 char jian[] = { '1','!','2','@','3','4','$','5','%','6','^','7','8','*','9','(','0','q','Q',\ 'w','W','e','E','r','t','T','y','Y','u','i','I','o','O','p','P','a','s','S','d','D',\ 'f','g','G','h','H','j','J','k','l','L','z','Z','x','c','C','v','V','b','B','n','m' }; //所有音名对应的按键 char zhi_zimu[][3] = { "C","Cs","D","Ds","E","F","Fs","G","Gs","A","As","B" }; //音名字母部分 char zhi_shuzi[][2] = { "0","1","2","3","4","5","6","7" }; //音名数字部分 for (short i = 0; i < sizeof(jian); i++) { if (keyboard_key == jian[i]) { short zimu_bianhao = i % 12; //字母部分12个一轮回 short shuzi_bianhao = i / 12; //数字部分每十二个音+1 char ss_zimu[5], ss_shuzi[2]; strcpy(ss_zimu, zhi_zimu[zimu_bianhao]); strcpy(ss_shuzi, zhi_shuzi[shuzi_bianhao + piano_type]); //带一个piano_type补正可以拓展音域 strcat(ss_zimu, ss_shuzi); strcpy(sound_name, ss_zimu); } else { continue; } } } void play_sound(char keyboard_key) { char sound_name[5] = { 0 }; //音名 char temp_command[MAXLEN] = { 0 }; //mciSendString的命令 if (keyboard_key == '+') { strcpy(sound_name, "No_sound"); //播放一段无声的声音,用于加载与播放器有关的DLL文件 } else { decoding_func(keyboard_key, sound_name, piano_type); //将键盘上的键对应地解码成音名 } sprintf(temp_command, "open piano\\%s.mp3 alias %s", sound_name, sound_name); mciSendStringA(temp_command, 0, 0, 0); //[5] //打开音名.mp3 sprintf(temp_command, "play %s", sound_name); mciSendStringA(temp_command, 0, 0, 0); //播放 Sleep(10000); sprintf(temp_command, "close %s", sound_name); //关闭 mciSendStringA(temp_command, 0, 0, 0); return; } void play_song(FILE* puzi, short* ystart, HEAD mus_info, short* flag) { char keyboard_key; keyboard_key = fgetc(puzi); if (*flag == 0) { cout << keyboard_key; } if (keyboard_key == '|' || keyboard_key == '\n') {//遇到|和换行符时,由于不对乐曲本身发挥任何作用,因此需要特殊处理 if (keyboard_key == '\n' && (*flag) == 0) { if ((*ystart) + 1 == DEPTHOFPAGE + YSTART - 1) { *ystart = YSTART + 2; cls_kuang(*ystart, 1); } else { if ((*ystart) + 2 == DEPTHOFPAGE + YSTART - 1) { (*ystart)++; gotoxy(XSTART + 1, *ystart); } else { (*ystart)++; cls_kuang(*ystart, 1); } } } return; } else { Sleep(mus_info.qusu.i);//此处控制曲速,注意:与一般音乐软件使用的曲速单位bpm不同,这里只是单纯的停顿一定毫秒 } if (kbhit()) { char kbout = '\0'; kbout = _getch(); if (kbout == '\r') { //播放中途按回车键退出 *flag = 2; return; } } thread t(play_sound, keyboard_key); //单独分出线程来播放声音 t.detach(); return; } void gotoxy(int x, int y) { //[6] _COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); //设置鼠标位置 } void print_kuang() { short start_x = XSTART, start_y = YSTART; short page_x = LENOFPAGE, page_y = DEPTHOFPAGE; gotoxy(start_x, start_y); //将鼠标移动到起始点 for (short y = 0; y < page_y; y++) { if (y == 0 || y == page_y - 1) { gotoxy(start_x, start_y + y); for (short x = 0; x <= page_x; x++) { cout << "="; //若为第一行或最后一行,则打印page_x个= } } else { gotoxy(start_x, start_y + y); cout << '|'; gotoxy(start_x + page_x, start_y + y); cout << '|'; //每行开头和末尾打印| } } } void print_pkeys() { short ystart = YSTART + 1; gotoxy(XSTART + 1, ystart);//普通的打印(很蠢) cout << "_________________________________________________________________________________________________________________________________________________"; for (short i = 0; i < 6; i++) { gotoxy(XSTART + 1, ++ystart); cout << "| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |"; } gotoxy(XSTART + 1, ++ystart); cout << "| |!| |@| | |$| |%| |^| | |*| |(| | |Q| |W| |E| | |T| |Y| | |I| |O| |P| | |S| |D| | |G| |H| |J| | |L| |Z| | |C| |V| |B| | |"; for (short i = 0; i < 2; i++) { gotoxy(XSTART + 1, ++ystart); cout << "| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |"; } gotoxy(XSTART + 1, ++ystart); cout << "| |_| |_| | |_| |_| |_| | |_| |_| | |_| |_| |_| | |_| |_| | |_| |_| |_| | |_| |_| | |_| |_| |_| | |_| |_| | |_| |_| |_| | |"; for (short i = 0; i < 2; i++) { gotoxy(XSTART + 1, ++ystart); cout << "| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |"; } gotoxy(XSTART + 1, ++ystart); cout << "| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | q | w | e | r | t | y | u | i | o | p | a | s | d | f | g | h | j | k | l | z | x | c | v | b | n | m |"; gotoxy(XSTART + 1, ++ystart); cout << "| C2| D2| E2| F2| G2| A2| B2| C3| D3| E3| F3| G3| A3| B3| C4| D4| E4| F4| G4| A4| B4| C5| D5| E5| F5| G5| A5| B5| C6| D6| E6| F6| G6| A6| B6| C7|"; gotoxy(XSTART + 1, ++ystart); cout << "|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|"; } void cls_kuang(short line, short mode) { gotoxy(XSTART + 1, line);//清除框框内的文字,比system(“cls”)更快 for (short c = 0; c < LENOFPAGE - 1; c++) { printf(" "); } gotoxy(XSTART + 1, line + mode);//清除当前行数之外的另一行 for (short c = 0; c < LENOFPAGE - 1; c++) { printf(" "); } gotoxy(XSTART + 1, line);//光标回到初始位置 } void HideCursor() { //[7] CONSOLE_CURSOR_INFO cursor; //隐藏光标 cursor.bVisible = FALSE; cursor.dwSize = sizeof(cursor); HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorInfo(handle, &cursor); } void ShowCursor() { CONSOLE_CURSOR_INFO cursor; //显示光标 cursor.bVisible = TRUE; cursor.dwSize = sizeof(cursor); HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorInfo(handle, &cursor); } HEAD readhead(FILE* puzi) { //读取曲谱文件的标头,获取曲速,曲名等信息 char flag = 0, keyboard_key, count = 0; char tempstr[MAXLEN] = { 0 }; HEAD temphead; while (!feof(puzi)) { keyboard_key = fgetc(puzi); if (keyboard_key == '<') { flag++; continue; } if (keyboard_key == '>') { count = 0; flag++; //使得变量flag兼具计数和条件判断的功能 if (flag == 2) { strcpy(temphead.name, tempstr); } if (flag == 4) { strcpy(temphead.qusu.c, tempstr); temphead.qusu.i = atoi(temphead.qusu.c); } if (flag == 6) { strcpy(temphead.ver, tempstr); keyboard_key = fgetc(puzi); //将信息头最后一个换行符给吃掉 break; } memset(tempstr, '\0', sizeof(tempstr)); } if (flag % 2 == 1) { tempstr[count++] = keyboard_key; } } return temphead; } 我用visual studio运行报错,我一会给你发送错误,请你修改
12-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值