#include"matrix.h"#include"vector.h"intmain(){Vectorx=(Vector){.v={1.0,2.0}};Vectory=(Vector){.v={3.0,4.0}};MatrixA=(Matrix){.v={1.0,-1.0,-1.0,1.0}};// c = 2.0 x + yVectorc=axpy(2.0,x,y);// d = 2.0 Ax + yVectord=gemv(2.0,A,x,1.0,y);print_vector(c);print_vector(d);return0;}
#include<stdio.h>#include<stdlib.h>#include<errno.h>#include<string.h>#include"intlib.h" // これはこのファイルのコンパイル自体には不要ですが、ライブラリとペアということで設置intcomp_int(constvoid*x0,constvoid*x1){constint*p0=x0;constinty0=*p0;constint*p1=x1;constinty1=*p1;if(y0>y1)return1;if(y0<y1)return-1;return0;}intload_int(constchar*str){errno=0;char*e;longn=strtol(str,&e,10);if(errno==ERANGE){fprintf(stderr,"%s: %s\n",str,strerror(errno));exit(1);}if(*e!='\0'){fprintf(stderr,"%s: an irregular character '%c' is detected.\n",str,*e);exit(EXIT_FAILURE);}return(int)n;}
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>#include<errno.h> // for error catch// Structure for canvastypedefstruct{intwidth;intheight;char**canvas;charpen;}Canvas;// Command 構造体と History構造体// [*]typedefstructcommandCommand;structcommand{char*str;size_tbufsize;Command*next;};typedefstruct{Command*begin;size_tbufsize;}History;// functions for Canvas typeCanvas*init_canvas(intwidth,intheight,charpen);voidreset_canvas(Canvas*c);voidprint_canvas(Canvas*c);voidfree_canvas(Canvas*c);// display functionsvoidrewind_screen(unsignedintline);voidclear_command(void);voidclear_screen(void);// enum for interpret_command results// interpret_command の結果をより詳細に分割typedefenumres{EXIT,LINE,UNDO,SAVE,UNKNOWN,ERRNONINT,ERRLACKARGS,NOCOMMAND}Result;// Result 型に応じて出力するメッセージを返すchar*strresult(Resultres);intmax(constinta,constintb);voiddraw_line(Canvas*c,constintx0,constinty0,constintx1,constinty1);Resultinterpret_command(constchar*command,History*his,Canvas*c);voidsave_history(constchar*filename,History*his);// [*] list.c のpush_backと同じCommand*push_command(History*his,constchar*str);intmain(intargc,char**argv){//for history recordingconstintbufsize=1000;// [*]Historyhis=(History){.begin=NULL,.bufsize=bufsize};intwidth;intheight;if(argc!=3){fprintf(stderr,"usage: %s <width> <height>\n",argv[0]);returnEXIT_FAILURE;}else{char*e;longw=strtol(argv[1],&e,10);if(*e!='\0'){fprintf(stderr,"%s: irregular character found %s\n",argv[1],e);returnEXIT_FAILURE;}longh=strtol(argv[2],&e,10);if(*e!='\0'){fprintf(stderr,"%s: irregular character found %s\n",argv[2],e);returnEXIT_FAILURE;}width=(int)w;height=(int)h;}charpen='*';charbuf[bufsize];Canvas*c=init_canvas(width,height,pen);printf("\n");// required especially for windows envwhile(1){// [*]// hsize はひとまずなし// 作る場合はリスト長を調べる関数を作っておくprint_canvas(c);printf("* > ");if(fgets(buf,bufsize,stdin)==NULL)break;constResultr=interpret_command(buf,&his,c);if(r==EXIT)break;// 返ってきた結果に応じてコマンド結果を表示clear_command();printf("%s\n",strresult(r));// LINEの場合はHistory構造体に入れるif(r==LINE){// [*]push_command(&his,buf);}rewind_screen(2);// command resultsclear_command();// command itselfrewind_screen(height+2);// rewind the screen to command input}clear_screen();free_canvas(c);return0;}Canvas*init_canvas(intwidth,intheight,charpen){Canvas*new=(Canvas*)malloc(sizeof(Canvas));new->width=width;new->height=height;new->canvas=(char**)malloc(width*sizeof(char*));char*tmp=(char*)malloc(width*height*sizeof(char));memset(tmp,' ',width*height*sizeof(char));for(inti=0;i<width;i++){new->canvas[i]=tmp+i*height;}new->pen=pen;returnnew;}voidreset_canvas(Canvas*c){constintwidth=c->width;constintheight=c->height;memset(c->canvas[0],' ',width*height*sizeof(char));}voidprint_canvas(Canvas*c){constintheight=c->height;constintwidth=c->width;char**canvas=c->canvas;// 上の壁printf("+");for(intx=0;x<width;x++)printf("-");printf("+\n");// 外壁と内側for(inty=0;y<height;y++){printf("|");for(intx=0;x<width;x++){constcharc=canvas[x][y];putchar(c);}printf("|\n");}// 下の壁printf("+");for(intx=0;x<width;x++)printf("-");printf("+\n");fflush(stdout);}voidfree_canvas(Canvas*c){free(c->canvas[0]);// for 2-D array freefree(c->canvas);free(c);}voidrewind_screen(unsignedintline){printf("\e[%dA",line);}voidclear_command(void){printf("\e[2K");}voidclear_screen(void){printf("\e[2J");}intmax(constinta,constintb){return(a>b)?a:b;}voiddraw_line(Canvas*c,constintx0,constinty0,constintx1,constinty1){constintwidth=c->width;constintheight=c->height;charpen=c->pen;constintn=max(abs(x1-x0),abs(y1-y0));if((x0>=0)&&(x0<width)&&(y0>=0)&&(y0<height))c->canvas[x0][y0]=pen;for(inti=1;i<=n;i++){constintx=x0+i*(x1-x0)/n;constinty=y0+i*(y1-y0)/n;if((x>=0)&&(x<width)&&(y>=0)&&(y<height))c->canvas[x][y]=pen;}}voidsave_history(constchar*filename,History*his){constchar*default_history_file="history.txt";if(filename==NULL)filename=default_history_file;FILE*fp;if((fp=fopen(filename,"w"))==NULL){fprintf(stderr,"error: cannot open %s.\n",filename);return;}// [*] 線形リスト版for(Command*p=his->begin;p!=NULL;p=p->next){fprintf(fp,"%s",p->str);}fclose(fp);}Resultinterpret_command(constchar*command,History*his,Canvas*c){charbuf[his->bufsize];strcpy(buf,command);buf[strlen(buf)-1]=0;// remove the newline character at the endconstchar*s=strtok(buf," ");if(s==NULL){// 改行だけ入力された場合returnUNKNOWN;}// The first token corresponds to commandif(strcmp(s,"line")==0){intp[4]={0};// p[0]: x0, p[1]: y0, p[2]: x1, p[3]: x1 char*b[4];for(inti=0;i<4;i++){b[i]=strtok(NULL," ");if(b[i]==NULL){returnERRLACKARGS;}}for(inti=0;i<4;i++){char*e;longv=strtol(b[i],&e,10);if(*e!='\0'){returnERRNONINT;}p[i]=(int)v;}draw_line(c,p[0],p[1],p[2],p[3]);returnLINE;}if(strcmp(s,"save")==0){s=strtok(NULL," ");save_history(s,his);returnSAVE;}if(strcmp(s,"undo")==0){reset_canvas(c);//[*] 線形リストの先頭からスキャンして逐次実行// pop_back のスキャン中にinterpret_command を絡めた感じCommand*p=his->begin;if(p==NULL){returnNOCOMMAND;}else{Command*q=NULL;// 新たな終端を決める時に使うwhile(p->next!=NULL){// 終端でないコマンドは実行して良いinterpret_command(p->str,his,c);q=p;p=p->next;}// 1つしかないコマンドのundoではリストの先頭を変更するif(q==NULL){his->begin=NULL;}else{q->next=NULL;}free(p->str);free(p);returnUNDO;}}if(strcmp(s,"quit")==0){returnEXIT;}returnUNKNOWN;}// [*] 線形リストの末尾にpush するCommand*push_command(History*his,constchar*str){Command*c=(Command*)malloc(sizeof(Command));char*s=(char*)malloc(his->bufsize);strcpy(s,str);*c=(Command){.str=s,.bufsize=his->bufsize,.next=NULL};Command*p=his->begin;if(p==NULL){his->begin=c;}else{while(p->next!=NULL){p=p->next;}p->next=c;}returnc;}char*strresult(Resultres){switch(res){caseEXIT:break;caseSAVE:return"history saved";caseLINE:return"1 line drawn";caseUNDO:return"undo!";caseUNKNOWN:return"error: unknown command";caseERRNONINT:return"Non-int value is included";caseERRLACKARGS:return"Too few arguments";caseNOCOMMAND:return"No command in history";default:returnNULL;}returnNULL;}
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>#include<errno.h>#include"canvas.h"#include"history.h"#include"display.h"#include"command.h"intmain(intargc,char**argv){// for history recordingconstintbufsize=1000;Historyhis=(History){.begin=NULL,.bufsize=bufsize};intwidth;intheight;if(argc!=3){fprintf(stderr,"usage: %s <width> <height>\n",argv[0]);returnEXIT_FAILURE;}else{char*e;longw=strtol(argv[1],&e,10);if(*e!='\0'){fprintf(stderr,"%s: irregular character found %s\n",argv[1],e);returnEXIT_FAILURE;}longh=strtol(argv[2],&e,10);if(*e!='\0'){fprintf(stderr,"%s: irregular character found %s\n",argv[2],e);returnEXIT_FAILURE;}width=(int)w;height=(int)h;}charpen='*';charbuf[bufsize];Canvas*c=init_canvas(width,height,pen);printf("\n");// required especially for windows envwhile(1){print_canvas(c);printf("* > ");if(fgets(buf,bufsize,stdin)==NULL)break;constResultr=interpret_command(buf,&his,c);if(r==EXIT)break;// 返ってきた結果に応じてコマンド結果を表示clear_command();printf("%s\n",strresult(r));// LINEの場合はHistory構造体に入れるif(r==LINE){push_command(&his,buf);}rewind_screen(2);// command resultsclear_command();// command itselfrewind_screen(height+2);// rewind the screen to command input}clear_screen();free_canvas(c);return0;}
#include<stdio.h>#include<stdlib.h>#include<string.h>#include"command.h"#include"canvas.h"#include"history.h"Resultinterpret_command(constchar*command,History*his,Canvas*c){charbuf[his->bufsize];strcpy(buf,command);buf[strlen(buf)-1]=0;// remove the newline character at the endconstchar*s=strtok(buf," ");if(s==NULL){// 改行だけ入力された場合returnUNKNOWN;}// The first token corresponds to commandif(strcmp(s,"line")==0){intp[4]={0};// p[0]: x0, p[1]: y0, p[2]: x1, p[3]: x1char*b[4];for(inti=0;i<4;i++){b[i]=strtok(NULL," ");if(b[i]==NULL){returnERRLACKARGS;}}for(inti=0;i<4;i++){char*e;longv=strtol(b[i],&e,10);if(*e!='\0'){returnERRNONINT;}p[i]=(int)v;}draw_line(c,p[0],p[1],p[2],p[3]);returnLINE;}if(strcmp(s,"save")==0){s=strtok(NULL," ");save_history(s,his);returnSAVE;}if(strcmp(s,"undo")==0){reset_canvas(c);// 線形リストの先頭からスキャンして逐次実行Command*p=his->begin;if(p==NULL){returnNOCOMMAND;}else{Command*q=NULL;// 新たな終端を決める時に使うwhile(p->next!=NULL){// 終端でないコマンドは実行して良いinterpret_command(p->str,his,c);q=p;p=p->next;}// 1つしかないコマンドのundoではリストの先頭を変更するif(q==NULL){his->begin=NULL;}else{q->next=NULL;}free(p->str);free(p);returnUNDO;}}if(strcmp(s,"quit")==0){returnEXIT;}returnUNKNOWN;}char*strresult(Resultres){switch(res){caseEXIT:break;caseSAVE:return"history saved";caseLINE:return"1 line drawn";caseUNDO:return"undo!";caseUNKNOWN:return"error: unknown command";caseERRNONINT:return"Non-int value is included";caseERRLACKARGS:return"Too few arguments";caseNOCOMMAND:return"No command in history";default:returnNULL;}returnNULL;}
#include<stdio.h>#include<stdlib.h>#include<string.h>#include"history.h"Command*push_command(History*his,constchar*str){Command*c=(Command*)malloc(sizeof(Command));char*s=(char*)malloc(his->bufsize);strcpy(s,str);*c=(Command){.str=s,.bufsize=his->bufsize,.next=NULL};Command*p=his->begin;if(p==NULL){his->begin=c;}else{while(p->next!=NULL){p=p->next;}p->next=c;}returnc;}voidsave_history(constchar*filename,History*his){constchar*default_history_file="history.txt";if(filename==NULL)filename=default_history_file;FILE*fp;if((fp=fopen(filename,"w"))==NULL){fprintf(stderr,"error: cannot open %s.\n",filename);return;}// 線形リスト版for(Command*p=his->begin;p!=NULL;p=p->next){fprintf(fp,"%s",p->str);}fclose(fp);}
#pragma once// Structure for canvastypedefstruct{intwidth;intheight;char**canvas;charpen;}Canvas;Canvas*init_canvas(intwidth,intheight,charpen);voidreset_canvas(Canvas*c);voidprint_canvas(Canvas*c);voidfree_canvas(Canvas*c);intmax(constinta,constintb);voiddraw_line(Canvas*c,constintx0,constinty0,constintx1,constinty1);
#include<stdio.h>#include<stdlib.h>#include<string.h>typedefstructstudent{intid;charname[100];}Student;intcomp_name(constvoid*x0,constvoid*x1){constStudent*p0=(Student*)x0;constStudent*p1=(Student*)x1;return0;// いまはダミーの値を返している }intmain(intargc,char**argv){Studentv[]={(Student){.id=1,.name="situ"},(Student){.id=2,.name="hrs"},(Student){.id=3,.name="ire"},(Student){.id=4,.name="hsgw"},(Student){.id=5,.name="aizw"},(Student){.id=6,.name="mti"}};constintn=sizeof(v)/sizeof(v[0]);// before sortprintf("== before ==\n");for(inti=0;i<n;i++){printf("%d: v[%d] = %s\n",v[i].id,i,v[i].name);}qsort(v,n,sizeof(v[0]),comp_name);// after sortprintf("== after ==\n");for(inti=0;i<n;i++){printf("%d: v[%d] = %s\n",v[i].id,i,v[i].name);}returnEXIT_SUCCESS;}