/*広告*/

/*[C言語]サンプルプログラム集 線形探索法ソース3*/

/*目次へ戻る*/

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> struct STUDENT{ char name[128]; int score; }; struct STUDENT set_data(const char* , int); int my_linear2(const char* ,struct STUDENT* ,int); int main(){ int result = 0; const char* target = "saitou"; int j = 0; struct STUDENT student[5]; student[0] = set_data("tanaka",100); student[1] = set_data("suzuki",69); student[2] = set_data("satou",80); student[3] = set_data("saitou",92); student[4] = set_data("watanabe",35); result = my_linear2(target,student,sizeof(student) / sizeof(struct STUDENT)); printf("配列の内容\n"); for(j=0;j<5;j++){ printf("名前 %s:得点 %d\n",student[j].name,student[j].score); } printf("\n探す対象 = %s\n\n",target); if(result == -1){ printf("見つかりませんでした!\n"); } else{ printf("添え字番号%d番目で見つかりました!\n",result); printf("名前 %s:得点 %d\n",student[result].name,student[result].score); } return 0; } struct STUDENT set_data(const char* name, int score){ struct STUDENT tmp; strcpy(tmp.name,name); tmp.score = score; return tmp; } int my_linear2(const char* target,struct STUDENT* ary,int ary_size){ int i = 0; while(1){ if(i == ary_size){ break; } else if(strcmp((ary + i)->name,target)==0){ break; } i++; } if(i == ary_size){ return -1; } else{ return i; } }

/*ページの先頭へ*/

/*目次へ戻る*/

/*HOME*/

/*Copyright 2016 K.N/petitetech.com*/

/*広告*/