[ create a new paste ] login | about

Link: http://codepad.org/IlzQwpDD    [ raw code | output | fork ]

C, pasted on Jul 13:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>



struct date{     //日付の構造体
    int y;
    int m;
    int d;
};

struct profile{     //名簿データの構造体
    int id;
    char name[70];
    struct date birthday;
    char home[70];
    char *comment;
};

void subst( char *s, char f, char t)     //入力した文字列の改行をNULLに替える
{
    while(*s != '\0'){
        if(*s == f){
            *s = t;
        }
        s++;
    }
}

void split(char *s, char *s2[], char t, int n)     //アドレスをとる
{
    int i = 0;
    s2[i++] = s;
    while(*s != '\0' && i < n){
        if(*s == t){
            *s = '\0';
            s2[i++] = s+1;
        }
        s++;
    }
}

void add_profile(struct profile *p, char *ret[], int i){     //データを格納
  
  char *date[3];
  
  p[i].id = (int)atoi(ret[0]);
  sprintf(p[i].name, "%s", ret[1]);
  sprintf(p[i].home, "%s", ret[3]);
  p[i].comment = (char *)malloc(strlen(ret[4]) + 1);
  strcpy( p[i].comment, ret[4]);
  
  split( ret[2], date, '-', 3);
  
  p[i].birthday.y = (int)atoi(date[0]);
  p[i].birthday.m = (int)atoi(date[1]);
  p[i].birthday.d = (int)atoi(date[2]);
}


struct profile profile_data[10000];     //グローバル変数
int nprofiles = 0;
int writing = 0;


void read_profile(const char *s){       //%Rコマンド

  FILE *fp;
  char s2[2048];
  char *ret[5];

  if ((fp = fopen( s, "r")) == NULL){
    fprintf(stderr,"file open error\n");
    exit(1);
  }
  while(fgets( s2, sizeof(s2)-1, fp) != NULL){
    subst( s2, '\n', '\0');
    split(s2, ret, ',', 5);
    add_profile(profile_data , ret, nprofiles++);
  }
  
  fclose(fp);
}


void write_profile(const char *s){     //%Wコマンド

  FILE *fp;
  int i = 0;
  
  if((fp = fopen(s,"w")) == NULL){
    fprintf(stderr,"file open error\n");
    exit(1);
  }
  
  while(i < nprofiles){
    fprintf( fp, "%d,%s,%d-%d-%d,%s,%s\n",
	     profile_data[i].id, profile_data[i].name,
	     profile_data[i].birthday.y, profile_data[i].birthday.m, profile_data[i].birthday.d,
	     profile_data[i].home, profile_data[i].comment);
    i++;
  }
  fclose(fp);
}


void found_profile(struct profile *p, const char *word){

  int i;
  char str[2048];

  for(i = 0; i < nprofiles; i++, p = p + 1){
    sprintf(str, "%d,%s,%d-%d-%d,%s,%s", p->id, p->name, p->birthday.y,
	    p->birthday.m, p->birthday.d, p->home, p->comment);
    if( strstr(str, word) != NULL){
      printf("\nID\t: %d\n", p->id);
      printf("Name\t: %s\n", p->name);
      printf("Date\t: %d/%02d/%02d\n",
	     p->birthday.y, p->birthday.m, p->birthday.d);
      printf("Addr\t: %s\n", p->home);
      printf("Memo\t: %s\n", p->comment);
    }
  }
}

void print_profile(const char *s){     //%Pによってメモリ中にあるデータを出力
  
  long n =  atol(s), i;
  struct profile *p;
  int d;
  
  if (n > 0) {
    d = 1;
    p = profile_data;
  } else if (n < 0){
    n = -n;
    d = 1;
    p = profile_data + nprofiles - n;
  } else if (n == 0){
    d = 1;
    p = profile_data;
    n = nprofiles;
  }
  if ( n > nprofiles)
    n = nprofiles;
  for (i = 0; i < n; i++, p = p + d) {
    printf("\nID\t: %d\n", p->id);
    printf("Name\t: %s\n", p->name);
    printf("Date\t: %d/%02d/%02d\n",
           p->birthday.y, p->birthday.m, p->birthday.d);
    printf("Addr\t: %s\n", p->home);
    printf("Memo\t: %s\n", p->comment);
  }
}
/*
int compare_id(struct profile *a, struct profile *b)
{
  return ( *(int*)a->id - *(int*)b->id );
}
int compare_name(const struct profile* a, const struct profile* b)
{
  return strcmp( *(char**)a->name, *(char**)b->name );
}
int compare_date(const struct date* a, const struct date* b)
{
  return ( *(int*)a->y - *(int*)b->y );
}
int compare_addr(const struct profile* a, const struct profile* b)
{
  return strcmp( *(char**)a->home, *(char**)b->home );
}

void command_sort(int n){
  
  if(n == 0){
    qsort(profile_data, sizeof(profile_data) / sizeof(profile_data[0]), sizeof(int), compare_id);
  }
  else if(n == 1){
    qsort(profile_data, sizeof(profile_data) / sizeof(profile_data[0]), sizeof(char*), compare_name);
  }
  else if(n == 2){

    qsort(profile_data, sizeof(profile_data) / sizeof(profile_data[0]), sizeof(int), compare_date);
  }
  else if(n == 3){
    qsort(profile_data, sizeof(profile_data) / sizeof(profile_data[0]), sizeof(char*), compare_addr);
  }
}
*/
/*
  struct profile kari;
  int i, j;
  int(*comp[5])(struct profile *a, struct profile *b);
  comp[0] = compare_id;
*/
/* 
   comp[1] = compare_name;
   comp[2] = compare_date;
   comp[3] = compare_home;
   comp[4] = compare_comment;
*/

/*
  for( i = 0; i < nprofiles - 1; i++){
  for( j = nprofiles - 1; j > i; j--){
  if(n = 0 && comp[0]<0){
  kari = struct profile i; struct profile i = struct profile j; struct profile j = kari;
  }
  }
  }
  }
*/
int compare_id(struct profile *a, struct profile *b){
  return ( *(int*)a->id - *(int*)b->id );
}
int compare_name(struct profile *a, struct profile *b){
  return strcmp( *(char**)a->name, *(char**)b->name );
}
int compare_date(const struct date *a, const struct date *b){
  return ( *(int*)a->y - *(int*)b->y );
}
int compare_home(const struct profile* a, const struct profile* b){
  return strcmp( *(char**)a->home, *(char**)b->home );
}

void command_sort(int n){
  
  struct profile kari;
  int (*comp)(struct profile *a,struct profile *b);
  int i, j;
  
  switch(n){
  case 1:
    comp = compare_id;
    break;
  case 2:
    comp = compare_name;
    break;
  case 3:
    comp = compare_date;
    break;
  case 4:
    comp = compare_home;
    break;
  }
  for(i = 0; i < nprofiles; i++){
    for(j = nprofiles - 1; j > i; j--){
      if((*comp)( &profile_data[i], &profile_data[j]>0)){
	  kari = profile_data[i];
	  profile_data[i] = profile_data[j];
	  profile_data[j] = kari;
	}
    }
  }
}
	
void delete_profile(int id){
  exit(0);
}



int parse_input(FILE *fp){     //文字列を入力、%の分岐、データを分解・格納
  
  char line[2048] = {0};
  char *ret[5];
  int i = 1;
  
  printf("? ");
  if (fgets(line, sizeof(line)-1, fp) == NULL)
    return 0;
  
  subst(line, '\n', '\0');
  
  if(line[0] == '%'){
    switch( line[1] ){
    case 'Q':
      if(writing == 0){
	printf("");
      }
      exit(0);
      break;
    case 'C':
      printf(" %d profile(s).\n", nprofiles);
      break;
    case 'P':
      print_profile(line + 2);
      break;
    case 'R':
      read_profile(line + 3);
      break;
    case 'W':
      write_profile(line + 3);
      writing++;
      break;
    case 'F':
      found_profile(profile_data, line + 3);
      break;
    case 'S':
      command_sort((int)line + 2);
      break;
    case 'D':
      delete_profile((int)line + 3);
      break;
    }
  } 
  else  {
    split(line, ret, ',', 5);
    add_profile(profile_data , ret, nprofiles++);
  }
  
  return i;
}

int main(){
  
  printf("ID,学校名,設立年月日(年-月-日),所在地,備考データ\n");
  while( parse_input(stdin) );     //無限ループ
  return 0;
}


Output:
1
2
ID,学校名,設立年月日(年-月-日),所在地,備考データ
? 


Create a new paste based on this one


Comments: