C语言 怎么设置键盘输入然后关闭程序 c语言里面,如何设置一个按键使程序停止

C\u8bed\u8a00\u5982\u4f55\u4f7f\u7528\u952e\u76d8\u6309\u952e\u4e2d\u6b62\u7a0b\u5e8f

\u5475\u5475\uff0c\u8fd9\u5012\u662f\u6709\u4e2a\u51fd\u6570\u7684\uff01\uff01\uff01
kbhit()\uff0c\u8fd9\u4e2a\u51fd\u6570\u662f\u4f60\u53ef\u4ee5\u968f\u4fbf\u6309\u4e00\u4e2a\u6309\u952e\u5c31\u662f1\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u6309\u4e0b\u4efb\u4f55\u7684\u6309\u952e\uff0c\u90a3\u5c31\u662f0\uff1b\u6240\u4ee5\u5c31\u53ef\u4ee5\u628a\u8fd9\u4e2a\u51fd\u6570\u7528\u5728\u67d0\u4e2a\u5faa\u73af\u4e0a\u9762\uff0c\u6bd4\u5982\uff1a
while(!kbhit())
{......
.....
...
}
\u5f53\u4f60\u4e0d\u6309\u6309\u952e\u7684\u65f6\u5019\uff0ckbhit()\u51fd\u6570\u5c31\u662f0\uff0c\uff01kbhit()\u5c31\u662f1\uff1b\u6240\u4ee5\u5c31\u4e00\u76f4\u5faa\u73af\u4e0b\u53bb\uff0c\u76f4\u5230\u4f60\u6309\u4e0b\u6309\u952e\u7684\u65f6\u5019kbhit()\u5c31\u4e3a1\u4e86\uff0c\uff01kbhit()\u5c31\u4e3a0\u4e86\uff1b\u5faa\u73af\u5c31\u7ed3\u675f\u4e86\uff0c\u4f46\u8fd9\u5fc5\u987b\u662f\u4f60\u6309\u952e\u7684\u65f6\u5019\u4e0d\u662f\u7ed9\u67d0\u4e2a\u8f93\u5165\u51fd\u6570\u8f93\u5165\uff01\uff01\uff01
\u8fd8\u662f\u7ed9\u4f60\u4e2a\u4f8b\u5b50\u5427\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u5728TC\u4e2d\u83b7\u53d6\u7cfb\u7edf\u65f6\u95f4\u7684\u7a0b\u5e8f\uff0c\u76f4\u5230\u4f60\u6309\u4e0b\u6309\u952e\u5c31\u4e0d\u518d\u66f4\u65b0\u65f6\u95f4\u4e86\uff01\uff01\uff01
#include
#include
#include
#include
int main()
{struct tm *aa;

int year,month,days,sec,min,hour,tian,xingqi;

long bios_time,t1;

while(!kbhit())
{
bios_time = biostime(0, 0L);

sec=(long)(bios_time / CLK_TCK)%60;

min=(long)(bios_time / CLK_TCK / 60)%60;

hour=(long)(bios_time/ CLK_TCK / 3600);

time(&t1);

aa=gmtime(&t1);

year=aa->tm_year+1900;

month=aa->tm_mon+1;

days=aa->tm_mday;

tian=aa->tm_yday+1;

xingqi=(aa->tm_wday)%7;

gotoxy(5,6);

printf("%d/%.2d/%.2d %.2d:%.2d:%.2d\t xing qi %d\t\t ben nian di %d tian\n",year,month,days,hour,min,sec,xingqi,tian);
}
getch();
}

main()
{
....;
/* \u9700\u8981\u6309\u952e\u6682\u505c\u6216\u505c\u6b62\u90e8\u5206\uff0c\u901a\u5e38\u662f\u5faa\u73af\u4f53*/
#define STOP_KEY 'm'
while(\u4f60\u7684\u5faa\u73af\u6761\u4ef6) {
if(kbhit()) {
char ch=getchar();
if(ch == STOP_KEY) break; /*\u4e2d\u6b62\u5faa\u73af*/
}
}
....;

}

你的意思应该是程序是一个循环程序,当输入某个字符时就跳出循环,结束程序,那只要
#include<stdio.h>
int main()
{
char a;
while(1)
{
scanf("%c",&a);

if(a=='$')

{
break;

}

}
}

int getchar(void);
该函数定义在stdio.h中

程序最后用循环判断即可
while ('a' != getchar());

假如输入Q后关闭程序。 if ( input_char == 'Q' ) return 0;

扩展阅读:oppo选择语言退不出 ... c语言键盘录入 ... c语言键盘输入字母 ... c语言每输出5个然后换行 ... 二级c语言打开sln然后 ... c语言输入若干个数以0结束 ... c语言如何控制键盘 ... 手机一直卡在选择语言 ... c语言如何输入0然后结束 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网