📄 table.c
字号:
#include <stdio.h>#include <vga.h>#include <vgagl.h>#include <vgakeyboard.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <string.h>#define startX 95#define startY 155#define LEN sizeof(struct stock_data)GraphicsContext *physicalscreen;struct stock_data { char name[20]; char code[7]; char dealing_price[10]; char dealing_amount[10]; char opening_price[10]; char closing_price[10];};void draw_table_head();void draw_table();void get_data(struct stock_data *,FILE *);void rolling_data(struct stock_data *data);void draw_table_head_line();void draw_table_line();void draw_text(int x,int y,unsigned char *ch);void convert_int_to_char(int);int leftx,lefty;int flg=1;int data_starting_x=95;int data_starting_y=125;int index_no=1;char getInt[5]=" ";int main(void){ struct stock_data *data; data=(struct stock_data*)malloc(LEN); FILE *fp; if((fp=fopen("stock_data","r"))==NULL) { printf("file open failure\n"); exit(1); } double j=0; leftx = startX; lefty = startY; vga_init(); vga_setmode(G800x600x256); gl_setcontextvga(G800x600x256); physicalscreen = gl_allocatecontext(); gl_setfont(8,8,gl_font8x8); gl_setwritemode(FONT_COMPRESSED + WRITEMODE_OVERWRITE); gl_setfontcolors(0,vga_white()); draw_table_head(); draw_table_line(); get_data(data,fp); if(keyboard_init()) { printf("keyboard initilize failure\n"); exit(1); } keyboard_translatekeys(TRANSLATE_CURSORKEYS | TRANSLATE_KEYPADENTER | TRANSLATE_DIAGONAL); for (;;) { gl_putbox(0, 0, 128, 1, keyboard_getstate()); keyboard_update(); draw_table_head_line(); //draw_table_head_line(); //terminate the loop and exit with button"Q" or "ESC" if (keyboard_keypressed(SCANCODE_Q) || keyboard_keypressed(SCANCODE_ESCAPE)) break; j+=0.01; if(j>200) { gl_fillbox(90,123,585,124,0); draw_table_line(); get_data(data,fp); j=0; } } keyboard_close(); vga_setmode(TEXT); vga_getch(); fclose(fp); return 0;}/* draw the head of the table this is called only once */void draw_table_head(){ vga_setrgbcolor(251,251,0); draw_text(95,100,(unsigned char *)"序号"); draw_text(150,100,(unsigned char *)"股票名称"); draw_text(250,100,(unsigned char *)"代码"); draw_text(330,100,(unsigned char *)"成交价"); draw_text(420,100,(unsigned char *)"现手"); draw_text(500,100,(unsigned char *)"今开盘"); draw_text(590,100,(unsigned char *)"昨收盘");}/* draw the line of the table's head */void draw_table_head_line(){ if(flg==1) { vga_setcolor(4); flg=0; }else { vga_setcolor(5); flg=1; } vga_drawline(90,97,675,97); vga_drawline(90,122,675,122); vga_drawline(90,97,90,122); vga_drawline(675,97,675,122); vga_drawline(90,97,90,122); vga_drawline(145,97,145,122); vga_drawline(245,97,245,122); vga_drawline(325,97,325,122); vga_drawline(415,97,415,122); vga_drawline(495,97,495,122); vga_drawline(585,97,585,122); }/* draw the line of every blank this is called while refreshing the data */void draw_table_line(){ int i; vga_setcolor(7); for(i=0;i<8;i++) { vga_drawline(90,147,675,147); vga_drawline(90,172,675,172); vga_drawline(90,197,675,197); vga_drawline(90,222,675,222); vga_drawline(90,247,675,247); } vga_drawline(90,123,90,247); vga_drawline(145,123,145,247); vga_drawline(245,123,245,247); vga_drawline(325,123,325,247); vga_drawline(415,123,415,247); vga_drawline(495,123,495,247); vga_drawline(585,123,585,247); vga_drawline(90,123,90,247); vga_drawline(90,123,90,247); vga_drawline(90,123,90,247); vga_drawline(90,123,90,247); vga_drawline(90,123,90,247); vga_drawline(675,123,675,247);}/* get the data form the file get four pieces of data each time but remember: get the rest if the remain is less than 4 and rewind the cursor to the top */void get_data(struct stock_data *data,FILE *fp){ int i; data_starting_y=125; for(i=0;i<5;i++) { fread(data,LEN,1,fp); if (!feof(fp)) { rolling_data(data); index_no+=1; }else { rewind(fp); index_no=1; data_starting_y=125; break; } }}/* print the data you get from the file on the screen one by one */void rolling_data(struct stock_data *data){ convert_int_to_char(index_no); gl_printf(data_starting_x,data_starting_y+5,getInt); draw_text(data_starting_x+60,data_starting_y,(unsigned char *)data->name); gl_printf(data_starting_x+160,data_starting_y+5,data->code); gl_printf(data_starting_x+240,data_starting_y+5,data->dealing_price); gl_printf(data_starting_x+330,data_starting_y+5,data->dealing_amount); gl_printf(data_starting_x+410,data_starting_y+5,data->opening_price); gl_printf(data_starting_x+500,data_starting_y+5,data->closing_price); data_starting_y+=25;}/* convert index from integer to char */void convert_int_to_char(int index_no){ char tmp[4]=" "; int i=0,j=0; while(index_no>0) { tmp[i]=index_no%10+48; index_no/=10; i++; } for(;i>0;i--,j++) { getInt[j]=tmp[i-1]; } getInt[j]='\0';}void draw_text(int x,int y,unsigned char *ch){ int i,j,k,c,l; unsigned char mat[16][2]; unsigned char mat1[16]; char *p; FILE *hzk,*ywk; vga_setcolor(1); hzk=fopen("hzk16","rb"); ywk=fopen("asc16","rb"); if (hzk==NULL) { perror("Open hzk16 file error"); exit(1); } if(ywk==NULL) { perror("Open asc16 file error"); exit(1); } l=strlen(ch); vga_setrgbcolor(251,251,0); for(c=0;c<l;c++) { if(ch[c]>127) { i=ch[c]-0xa0;j=ch[c+1]-0xa0; fseek(hzk,(94*(i-1)+(j-1))*32,SEEK_SET); fread(mat,32,1,hzk); for(i=0;i<16;i++) for(j=0;j<2;j++) for(k=0;k<8;k++) if(mat[i][j]&(0x80>>k)) vga_drawpixel(x+c*8+8*j+k,y+i); c++; } else { fseek(ywk,(ch[c]+48)*16,SEEK_SET); fread(mat1,16,1,ywk); for(i=0;i<16;i++) for(k=0;k<8;k++) if(mat1[i]&(0x80>>k)) vga_drawpixel(x+c*8+k,y+i); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -