免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1703 | 回复: 3

[C] 求助这个程序的问题? [复制链接]

论坛徽章:
0
发表于 2008-03-18 13:56 |显示全部楼层
5可用积分
  1. #include <stdio.h>    /* for getchar(), printf() */
  2. #include <termios.h>  /* for tcxxxattr, ECHO, etc */
  3. #include <unistd.h>   /* for STDIN_FILENO */

  4. int Getch(void);

  5. int main(void)
  6. {
  7.         char Control;
  8.         char Cursor;
  9.         char Str;

  10.         int State;
  11.         int Page;
  12.         int InKey;
  13.         int Len;
  14.         int LenCur;
  15.        
  16. //        Str         = '' ;
  17.         State         = 0 ;
  18.         Page        = 0 ;
  19.         Control = 'N' ;
  20.         Cursor  = 'N' ;
  21.         Len                = 9;       
  22.         LenCur        = 9;


  23.         printf("<LanGate>");
  24.         while(1){

  25.                 InKey = Getch();       

  26.                 if (InKey==27) {
  27.                         Control = 'Y';
  28.                 }

  29.                 if ((Control=='Y') && (Cursor == 'Y') && Page > 0 ) {
  30.                         State = InKey ;
  31.                 }

  32.                 if ((Control=='Y') && (Cursor == 'Y') && (Page == 0)) {
  33.                         Page = InKey;       
  34.                 }

  35.                 if ((Control=='Y') && InKey==91) {
  36.                         Cursor = 'Y';
  37.                 }

  38.                 if (Control=='N') {
  39.                         if (InKey==10) {
  40.                                 putchar(InKey);
  41.                                 printf("Command="+Str);
  42.                                 putchar(10);
  43.                                 printf("<LanGate>");
  44.                                 Len                = 9;       
  45.                                 LenCur        = 9;
  46.                         }
  47.                         if (InKey==127) {
  48.                                 if (Len > 9 ) {
  49. //                                        printf("back");
  50.                                         putchar(InKey);
  51.                                         Len = Len - 1 ;
  52.                                         LenCur = LenCur - 1;
  53.                                 }
  54.                         }
  55.                         if (InKey==9) {
  56.                                 printf("tab");
  57.                         }

  58.                         if (InKey > 31 && InKey < 127) {
  59.                                 putchar(InKey);

  60.                                 Len        = Len + 1;       
  61.                                 LenCur = LenCur + 1;
  62.                         }
  63.                 }

  64.                 if ( State == 126 ) {
  65.                         if ( Page == 53 ) {
  66.                                 printf("pgup");
  67.                         }
  68.                         if ( Page == 54 ) {
  69.                                 printf("pgdown");
  70.                         }
  71.                         if ( Page == 49 ) {
  72.                                 printf("home");
  73.                         }
  74.                         if ( Page == 52 ) {
  75.                                 printf("end");
  76.                         }
  77.                         State         = 0 ;
  78.                         Page        = 0 ;
  79.                         Control = 'N' ;
  80.                         Cursor  = 'N' ;
  81.                 } else {

  82.                         if ( Page == 65 ) {
  83. //                                printf("up");
  84.                                 Page        = 0 ;
  85.                                 Control = 'N' ;
  86.                                 Cursor  = 'N' ;
  87.                         }
  88.                         if ( Page == 66 ) {
  89. //                                printf("down");
  90.                                 Page        = 0 ;
  91.                                 Control = 'N' ;
  92.                                 Cursor  = 'N' ;
  93.                         }
  94.                         if ( Page == 67 ) {
  95.                                 if (LenCur < Len ) {
  96.                                         LenCur = LenCur + 1;
  97. //                                        printf("right");
  98.                                         putchar(27);
  99.                                         putchar(91);
  100.                                         putchar(67);
  101.                                 }       
  102.                                 Page        = 0 ;
  103.                                 Control = 'N' ;
  104.                                 Cursor  = 'N' ;
  105.                         }
  106.                         if ( Page == 68 ) {
  107.                                 if (LenCur > 9 ) {
  108.                                         LenCur = LenCur - 1;
  109. //                                        printf("left");
  110.                                         putchar(27);
  111.                                         putchar(91);
  112.                                         putchar(68);
  113.                                 }
  114.                                 Page        = 0 ;
  115.                                 Control = 'N' ;
  116.                                 Cursor  = 'N' ;
  117.                         }

  118. //                        if ( Page < 65 || Page > 68) {
  119. //                                Page        = 0 ;
  120. //                                Control = 'N' ;
  121. //                                Cursor  = 'N' ;
  122. //                        }

  123.                 }

  124.         }
  125.           return 0;
  126. }

  127. int Getch (void)
  128. {
  129.     int ch;
  130.     struct termios oldt, newt;
  131.   
  132.     tcgetattr(STDIN_FILENO, &oldt);
  133.     newt = oldt;
  134.     newt.c_lflag &= ~(ECHO|ICANON);
  135.     tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  136.     ch = getchar();
  137.     tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  138.   
  139.     return ch;
  140. }
复制代码

这是一个终端输入程序,我用的ASCII值全部是用常数代替,所以导致只对我的终端有效,到别的终端就没用了,听说可以用变量代替,不知道怎么弄?
另外还有个问题,就是我在终端输入字符,一直输入到第二行,然而退格键只能删除第二行的数据,不能删除第一行的数据,不知道如何解决??
请高手指点啊

[ 本帖最后由 songpure520 于 2008-3-19 11:20 编辑 ]

论坛徽章:
38
2017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之深圳
日期:2023-02-16 14:39:0220周年集字徽章-年
日期:2022-08-31 14:25:28黑曼巴
日期:2022-08-17 18:57:0919周年集字徽章-年
日期:2022-04-25 13:02:5920周年集字徽章-20	
日期:2022-03-29 11:10:4620周年集字徽章-年
日期:2022-03-14 22:35:1820周年集字徽章-周	
日期:2022-03-09 12:51:3220周年集字徽章-年
日期:2022-02-10 13:13:4420周年集字徽章-周	
日期:2022-02-03 12:09:4420周年集字徽章-20	
日期:2022-01-25 20:14:2720周年集字徽章-周	
日期:2022-01-13 15:12:33
发表于 2008-03-18 20:01 |显示全部楼层
tc写的代码

论坛徽章:
0
发表于 2008-03-19 10:00 |显示全部楼层
不是,用vi写的!!!

论坛徽章:
0
发表于 2008-03-19 10:39 |显示全部楼层
没人能够解答吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP