落書きノート

ふと自分が気になった事を書いてます

C言語で問題を解く

#include <stdio.h>

#define VMAX 21

typedef struct {
  char name[20];
  int height;
  double vision;
} PhysCheck;

double ave_height(const PhysCheck dat[], int n) {
  int i;
  double sum = 0;
  for(i = 0; i < n; i++)
    sum += dat[i].height;

  return sum / n;
}

void dist_vision(const PhysCheck dat[], int n, int dist[]) {
  int i;
  for(i = 0; i < VMAX; i++)
    dist[i] = 0;
  for(i = 0; i < n; i++)
    if(dat[i].vision >= 0.0 && dat[i].vision <= VMAX / 10.0)
      dist[(int)(dat[i].vision * 10)]++;
}

int main(void) {
  int i,j;
  PhysCheck x[] = {
    {"AKASAKA Tadao", 162, 0.3},
    {"KATOH Tomiaki", 173, 0.7},
    {"SAITOH Syouji", 175, 2.0},
    {"TAKEDA Shinya", 171, 1.5},
    {"NAGAHAMA Masaki", 168, 0.4},
    {"HAMADA Tetsuaki", 174, 1.2},
    {"MATSUTOMI Akio", 169, 0.8},
  };
  int nx = sizeof(x) / sizeof(x[0]);
  int vdist[VMAX];

  puts("■□■ 身体検査一覧表 ■□■");
  puts(" 氏名             身長  視力 ");
  puts("---------------------------");
  for(i = 0; i < nx; i++)
    printf("%-18.18s%4d%5.1f\n", x[i].name, x[i].height, x[i].vision);
  printf("\n平均身長 : %5.1fcm\n", ave_height(x, nx));
  dist_vision(x, nx, vdist);
  printf("\n視力の分布\n");
  for(i = 0; i < VMAX; i++) {
    printf("%3.1f~ : ", i / 10.0);
    for(j = 0; j < vdist[i]; j++)
      printf("*");
    puts("");
  }
  return 0;
}




#include <stdio.h>

typedef struct {
  int y;
  int m;
  int d;
} Date;

Date Before(Date x, int n);
Date After(Date x, int n);

static int mdays[2][13] = {
  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};

int isleap(int year) {
  return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

Date DateOf(int y, int m, int d) {
  Date temp = {y, m, d};
  return temp;
}

Date After(Date x, int n) {
  if (n < 0)
    return Before(x, -n);

  x.d += n;

  while(x.d > mdays[isleap(x.y)][x.m - 1]) {
    x.d -= mdays[isleap(x.y)][x.m - 1];
    if(++x.m > 12) {
      x.y++;
      x.m = 1;
    }
  }
  return x;
}

Date Before(Date x, int n) {
  if(n < 0)
    return After(x, -n);

  x.d -= n;
  
  while(x.d < 1) {
    if(--x.m < 1) {
      x.y--;
      x.m = 12;
    }
    x.d += mdays[isleap(x.y)][x.m - 1];
  }

  return x;
}

void Print(Date x) {
  int y = x.y;
  int m = x.m;
  int d = x.d;

  char *ws[] = {"日", "月", "火", "水", "木", "金", "土"};
  if(m == 1 || m == 2) {
    y--;
    m += 12;
  }
  printf("%04d%02d%02d日(%s)", x.y, x.m, x.d, ws[(y + y / 4 - y / 100 + y / 400 + (13 * m + 8) / 5 + d) % 7]);
}

int main(void) {
  int n;
  int y, m, d;
  Date x;
  char buf[100];
  
  printf("日付を入力してください。\n");
  fgets(buf, sizeof(buf), stdin);
  sscanf(buf, "%d%d%d", &y, &m, &d);

  x = DateOf(y, m, d);

  printf("何日前後の日付を求めますか : ");
  scanf("%d", &n);

  printf("%d日後の日付 : ", n); Print(After(x, n)); putchar('\n');
  printf("%d日前の日付 : ", n); Print(Before(x, n)); putchar('\n');

  return 0;
}





#include <stdio.h>
#include <stdlib.h>

int search(int a[], int n, int key) {
  int i;
  a[n] = key;
  for(i = 0;;i++) {
    if(a[i] == key)
      break;
  }

  return i == n ? -1 : i;
}

int main(void) {
  int i, nx, ky, idx;
  int *x;

  puts("線形探索(番兵法)");
  printf("要素数 : ");
  scanf("%d", &nx);
  x = calloc(nx + 1, sizeof(int));
  for(i = 0; i < nx; i++) {
    printf("x[%d] : ", i);
    scanf("%d", &x[i]);
  }
  printf("探す値 : ");
  scanf("%d", &ky);
  idx = search(x, nx, ky);
  if(idx == -1)
    puts("探索に失敗しました。");
  else
    printf("%dはx[%d]にあります。\n", ky, idx);

  free(x);
  
  return 0;
}