《Linux程式設計 第四版》之第五章的練習題

來源:互聯網
上載者:User

標籤:linux   c   

1、P168

一個完整的終端輸出選擇菜單的程式。

#include<stdio.h>#include<sys/time.h>#include<sys/types.h>#include<sys/resource.h>#include<math.h>#include<termios.h>    #include<term.h>  #include<curses.h>  #include<stdlib.h>  int getchoice(char* ,const char** ,FILE*,FILE*);static FILE* output_stream=(FILE*)0;char* opt[]={"a -- add a new record","d -- delete a new record","q -- quit",NULL};int char_to_terminal(int char_to_write){if (output_stream)putc(char_to_write,output_stream);return 0;}int getchoice(char* words,const char** opt,FILE* in,FILE* out){int choice;char** tmp_opt=opt;int screenrow=4;int screencol=10;char *cursor,*clear;output_stream=out;while(1){//將輸出修改成清屏,然後左上方輸出、screenrow=4;setupterm(NULL,fileno(out),(int *)0);cursor=tigetstr("cup");clear=tigetstr("clear");tputs(clear,1,(int *)char_to_terminal);tputs(tparm(cursor,screenrow,screencol),1,char_to_terminal);fprintf(out,"Choice:\n");screenrow+=2;while(*tmp_opt){tputs(tparm(cursor,screenrow,screencol),1,char_to_terminal);fprintf(out,"%s\n", *tmp_opt );tmp_opt++;screenrow++;}fprintf(out,"\n" );do{fflush(out);choice=fgetc(in);}while(choice=='\n' || choice=='\r');tmp_opt=opt;while(*tmp_opt){if (choice==*tmp_opt[0]){return choice;}else{tmp_opt++;}}tputs(tparm(cursor,screenrow,screencol),1,char_to_terminal);fprintf(stderr,"Incorrect choice,select again\n");}}int main(int argc,char** argv){int choice=0;FILE* in=fopen("/dev/tty","r");FILE* out=fopen("/dev/tty","w");if(!in||!out){fprintf(stderr, "%s\n", "Unable to open /dev/tty \n");exit(1);}struct termios tmp_tos,back_tos;tcgetattr(fileno(in),&tmp_tos);back_tos=tmp_tos;tmp_tos.c_lflag &= ~ICANON;tmp_tos.c_lflag &= ~ECHO;tmp_tos.c_lflag &= ~ISIG;tmp_tos.c_cc[VMIN] = 1;tmp_tos.c_cc[VTIME] = 0;if (tcsetattr(fileno(in),TCSANOW,&tmp_tos)!=0){fprintf(stderr, "%s\n", "Unable to set /dev/tty \n");}while(choice!='q'){choice=getchoice("Please select an action",opt,in,out);fprintf(out,"You have choose: %c \n", choice);sleep(1);}tcsetattr(fileno(in),TCSANOW,&back_tos);return 0;}

1、P171

一個檢測擊鍵事件的程式。

#include<stdio.h>#include<sys/time.h>#include<sys/types.h>#include<sys/resource.h>#include<math.h>#include<termios.h>    #include<term.h>  #include<curses.h>  #include<stdlib.h>  static struct  termios initsetting,newsetting;void close_keybord();void init_keybord();int readch();int kbhit();int main(int argc,char** argv){int choice=0;init_keybord();while(choice!='q'){printf("looping\n");sleep(1);if (choice=kbhit()){choice=readch();printf("You have key: %c \n", choice);}}close_keybord();return 0;}void close_keybord(){tcsetattr(0,TCSANOW,&initsetting);}void init_keybord(){tcgetattr(0,&initsetting);newsetting=initsetting;newsetting.c_lflag &= ~ICANON;newsetting.c_lflag &= ~ECHO;newsetting.c_lflag &= ~ISIG;newsetting.c_cc[VMIN] = 1;newsetting.c_cc[VTIME] = 0;tcsetattr(0,TCSANOW,&newsetting);}int readch(){}int kbhit(){char ch;int nread;newsetting.c_cc[VMIN] = 0;tcsetattr(0,TCSANOW,&newsetting);nread=read(0,&ch,1);newsetting.c_cc[VMIN] = 1;tcsetattr(0,TCSANOW,&newsetting);if (nread==1){return ch;}return 0;}


《Linux程式設計 第四版》之第五章的練習題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.