已解决)关于c语言实现按下空格或回车或点击鼠标左键显....
Tofloor
poster avatar
kikyoulzg
deepin
2018-06-23 19:01
Author
本帖最后由 kik13143you 于 2018-6-23 12:12 编辑

下面是按空格显示文字的代码
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include "getputch.h"

  6. int main(void)
  7. {
  8.     init_getputch();
  9.    
  10.     printf("请点击空格显示文字\n");
  11.     while (getch() != ' ' ){
  12.         ;
  13.     }
  14.     printf("hi\n");
  15.     return 0;
  16.    
  17.     term_getputch();
  18.    
  19. }
Copy the Code
而下面是getputch.h的代码
  1. /* 用于getch/putch的通用头文件"getputch.h" */

  2. #ifndef __GETPUTCH

  3.   #define __GETPUTCH

  4.   #if defined(_MSC_VER) || (__TURBOC__) || (LSI_C)

  5.         /* MS-Windows/MS-DOS(Visual C++, Borland C++, LSI-C 86 etc ...)*/

  6.         #include

  7.         static void init_getputch(void) { /* 空 */ }

  8.         static void term_getputch(void) { /* 空 */ }


  9.   #else

  10.         /* 提供了Curses库的UNIX/Linux/OS X环境 */

  11.         #include

  12.         #undef putchar
  13.         #undef puts
  14.         #undef printf
  15.         static char __buf[4096];

  16.         /*--- _ _putchar:相当于putchar函数(用“换行符+回车符”代替换行符进行输出)---*/
  17.         static int __putchar(int ch)
  18.         {
  19.                 if (ch == '\n')
  20.                         putchar('\r');
  21.                 return putchar(ch);
  22.         }

  23.         /*--- putch:显示1个字符,清除缓冲区 ---*/
  24.         static int putch(int ch)
  25.         {
  26.                 int result = putchar(ch);

  27.                 fflush(stdout);
  28.                 return result;
  29.         }

  30.         /*--- _ _printf:相当于printf函数(用“换行符+回车符”代替换行符进行输出)---*/
  31.         static int __printf(const char *format, ...)
  32.         {
  33.                 va_list        ap;
  34.                 int         count;

  35.                 va_start(ap, format);
  36.                 vsprintf(__buf, format, ap);
  37.                 va_end(ap);

  38.                 for (count = 0; __buf[count]; count++) {
  39.                         putchar(__buf[count]);
  40.                         if (__buf[count] == '\n')
  41.                                 putchar('\r');
  42.                 }
  43.                 return count;
  44.         }

  45.         /*--- _ _puts:相当于puts函数(用“换行符+回车符”代替换行符进行输出)---*/
  46.         static int __puts(const char *s)
  47.         {
  48.                 int i, j;

  49.                 for (i = 0, j = 0; s[i]; i++) {
  50.                         __buf[j++] = s[i];
  51.                         if (s[i] == '\n')
  52.                                 __buf[j++] = '\r';
  53.                 }
  54.                 return puts(__buf);
  55.         }

  56.         /*--- 库初始处理 ---*/
  57.         static void init_getputch(void)
  58.         {
  59.                 initscr();
  60.                 cbreak();
  61.                 noecho();
  62.                 refresh();
  63.         }

  64.         /*--- 库终止处理 ---*/
  65.         static void term_getputch(void)
  66.         {
  67.                 endwin();
  68.         }

  69.         #define putchar        __putchar
  70.         #define        printf        __printf
  71.         #define puts        __puts

  72.   #endif

  73. #endif
Copy the Code
然而在终端运行以后,终端的显示就出问题了,这是为啥呢?




Reply Favorite View the author
All Replies
avatar
jianguo922
deepin
2018-06-23 19:36
#1
不懂,进来看看   
Reply View the author
avatar
老陌
deepin
2018-06-23 19:51
#2
来要金币的,不懂,哈哈。
Reply View the author
avatar
kikyoulzg
deepin
2018-06-23 20:09
#3
丢人了原来是return 0;的位置放到term_getputch();前面了
Reply View the author
avatar
zwq133
deepin
2018-06-23 20:34
#4
我来看看还有金币么
Reply View the author
avatar
linusboyle
deepin
2018-06-23 20:46
#5
2333333333
Reply View the author
avatar
zijinyise
deepin
2018-06-23 20:49
#6
还有金币?
Reply View the author
avatar
中国新蔡
deepin
2018-06-23 21:41
#7
我也是来领金币的,哈哈
Reply View the author
avatar
billy123456
deepin
2018-06-23 22:06
#8
你按玩空格之后回到bash之后输了什么?
Reply View the author
avatar
156******44
deepin
2018-06-23 22:49
#9
嗯,。。。。。
Reply View the author
avatar
kikyoulzg
deepin
2018-06-24 01:05
#10
第一次发奖励金币的帖子呢
Reply View the author
avatar
kikyoulzg
deepin
2018-06-24 01:06
#11
https://bbs.deepin.org/post/159226
你按玩空格之后回到bash之后输了什么?

ls,。。。。。。。。
Reply View the author
avatar
133******01
deepin
2018-06-24 01:18
#12
用的什么编的?  软件   学下C++
Reply View the author
avatar
kikyoulzg
deepin
2018-06-24 01:27
#13
https://bbs.deepin.org/post/159226
用的什么编的?  软件   学下C++

现在还是用c,c++的虽然很厉害但似乎槽点也很多?慢慢来好了
Reply View the author
avatar
133******01
deepin
2018-06-24 01:32
#14
https://bbs.deepin.org/post/159226
现在还是用c,c++的虽然很厉害但似乎槽点也很多?慢慢来好了

我是想知道。你用的什么编写C代码  和调试的
Reply View the author
avatar
douya250
deepin
2018-06-24 01:35
#15
我也来。。。
Reply View the author
avatar
kikyoulzg
deepin
2018-06-24 01:42
#16
https://bbs.deepin.org/post/159226
我是想知道。你用的什么编写C代码  和调试的

哦哦,编写是用emacs,调试的话。。。gdb?不过现在是看编译的错误信息找问题,确实调试这方面我还没好好学过
Reply View the author
avatar
133******01
deepin
2018-06-24 01:58
#17
https://bbs.deepin.org/post/159226
哦哦,编写是用emacs,调试的话。。。gdb?不过现在是看编译的错误信息找问题,确实调试这方面我还没好好 ...

哦。谢了  。我试试
Reply View the author