研究研究!!
回复 17楼 lccwyj
编译没错,链接出错、
# include <stdio.h> # include <conio.h> # define Key_Esc 0x1b int main(void) { double count(double a, char b, double c);// 声明一个计算函数 double a=0; char b; double c; printf("This is a calculator:\n"); printf("格式如: 4*5\n"); while (1) //循环多次计算 { a = getch(); if(a==Key_Esc) //判断按键是否为ESC { printf("你输入了ESC,程序结束\n"); break; } else printf("%lf", (double)a); b = getch(); if(b==Key_Esc) //判断按键是否为ESC { printf("你输入了ESC,程序结束\n"); break; } else printf("%c", b); c = getch(); if(c==Key_Esc) //判断按键是否为ESC { printf("你输入了ESC,程序结束\n"); break; } else printf("%lf", (double)c); printf("%lf\n", count(a,b,c)); } return 0; } double count(double a, char b, double c) //定义一个计算函数 { switch (b) //判断b是什么符合 { case 42 : return a*c; break; //42对应的符合是 * case 43 : return a+c; break; // + case 45 : return a-c; break; // - case 47 : return a/c; break; // / default : printf("错误:只能计算加(+)、减(-)、乘(*)、除(/).\n"); } }
#include <stdio.h> #include <conio.h> #include "get.h" void Print(const wchar_t cBuffer[]); void main(void) { GET Get; Print(L"请输入50个字符以内的字符串(Enter确认, Esc中断): "); if (Get.Read_String(50) >= 0) { Print(L"\n\n您输入的字符串是: "); Print(Get.Buffer); printf_s("\nLength = %d", Get.Length); } else { Print(L"\n\n== 输入中断 =="); } _getwch(); } // 输出字符串 void Print(const wchar_t cBuffer[]) { while (*cBuffer) { _putwch(*cBuffer); cBuffer++; } }
#include <stdio.h> // 键码常数 #ifndef _KEYCODE #define _KEYCODE const wctype_t K_NUL = 0x0000; // 空字符 const wctype_t K_SOH = 0x0001; // 标题开始 const wctype_t K_STX = 0x0002; // 正文开始 const wctype_t K_ETX = 0x0003; // 正文结束 const wctype_t K_EOT = 0x0004; // 传输结束 const wctype_t K_ENQ = 0x0005; // 请求 const wctype_t K_ACK = 0x0006; // 收到通知 const wctype_t K_BEL = 0x0007; // 响铃 const wctype_t K_BACKSPACE = 0x0008; // 退格 const wctype_t K_TAB = 0x0009; // 水平制表符 const wctype_t K_LF = 0x000A; // 换行 const wctype_t K_VT = 0x000B; // 垂直制表符 const wctype_t K_FF = 0x000C; // 换页符 const wctype_t K_ENTER = 0x000D; // 回车 const wctype_t K_SO = 0x000E; // 不用切换 const wctype_t K_SI = 0x000F; // 启用切换 const wctype_t K_DLE = 0x0010; // 数据链路转义 const wctype_t K_DC1 = 0x0011; // 设备控制1 const wctype_t K_DC2 = 0x0012; // 设备控制2 const wctype_t K_DC3 = 0x0013; // 设备控制3 const wctype_t K_DC4 = 0x0014; // 设备控制4 const wctype_t K_NAK = 0x0015; // 拒绝接收 const wctype_t K_SYN = 0x0016; // 同步空闲 const wctype_t K_ETB = 0x0017; // 传输块结束 const wctype_t K_CAN = 0x0018; // 取消/作废 const wctype_t K_EM = 0x0019; // 介质中断 const wctype_t K_SUB = 0x001A; // 替补 const wctype_t K_ESC = 0x001B; // 溢出 const wctype_t K_FS = 0x001C; // 文件分割符 const wctype_t K_GS = 0x001D; // 分组符 const wctype_t K_RS = 0x001E; // 记录分离符 const wctype_t K_US = 0x001F; // 单元分隔符 const wctype_t K_SPACE = 0x0020; const wctype_t K_CDEL = 0x007F; // ASCII DEL const wctype_t K_UP = 0xE048; const wctype_t K_DOWN = 0xE050; const wctype_t K_LEFT = 0xE04B; const wctype_t K_RIGHT = 0xE04D; const wctype_t K_HOME = 0xE047; const wctype_t K_END = 0xE04F; const wctype_t K_PGUP = 0xE049; const wctype_t K_PGDN = 0xE051; const wctype_t K_INS = 0xE052; const wctype_t K_DEL = 0xE053; #endif class GET { private: bool IsAscii(const wchar_t Character); // 检测是否ASCII字符 bool IsHz(const wchar_t Character); // 检测是否汉字 wctype_t Read_Key(void); // 从键盘读入一键 public: wchar_t Buffer[256]; // 没人会从键盘敲入超过255(Unicode)字符 size_t Length; // 实际字符串的字符数 int Read_String(const size_t nSize); // 从键盘读入字符串 };
#include <conio.h> #include <new.h> #include "get.h" // 检测是否ASCII字符 bool inline GET::IsAscii(const wchar_t Character) { return ((Character >= 0x0020) && (Character <= 0x007E)) || ((Character >= 0xF900) && (Character <= 0xFA2D)); } // 检测是否汉字 bool inline GET::IsHz(const wchar_t Character) { return (Character >= 0x4E00) && (Character <= 0x9FA5); } // 从键盘读入一个按键, 返回其键码 wctype_t inline GET::Read_Key(void) { wint_t KeyCode = _getwch(); if (KeyCode == 0xE0) { KeyCode = (KeyCode << 8) + _getwch(); } return KeyCode; } // 读入字符串 int GET::Read_String(const size_t nSize) { wctype_t KeyCode; Buffer[0] = '\0'; Length = 0; do { KeyCode = Read_Key(); switch (KeyCode) { case K_ENTER: return Length; case K_ESC: Buffer[0] = '\0'; return -1; case K_BACKSPACE: if (Length > 0) { Length--; if (IsAscii(Buffer[Length])) { Buffer[Length] = K_SPACE; _putwch(K_BACKSPACE); _putwch(Buffer[Length]); _putwch(K_BACKSPACE); } if (IsHz(Buffer[Length])) { Buffer[Length] = K_SPACE; _putwch(K_BACKSPACE); _putwch(K_BACKSPACE); _putwch(Buffer[Length]); _putwch(K_BACKSPACE); } } else _putwch(K_BEL); break; default: Buffer[Length] = KeyCode; _putwch(Buffer[Length]); Length++; Buffer[Length] = '\0'; } } while (Length < nSize); return Length; }