演習 13-4

#include <stdio.h>

int main(){

    FILE *fp;

    int select;
    char name[100];
    double height, weight;

    if(((fp = fopen("hw.dat", "w")) == NULL)){
        printf("can't open file\n");
        return -1;
    }

    while(true){

        printf("名前 身長 体重 : ");
        scanf("%s %lf %lf", name, &height, &weight);

        fprintf(fp, "%s %.lf %.1lf\n", name, height, weight);

        printf("入力を続けますか yes:1/no:0\n");    scanf("%d", &select);
        if(select == 0) break;
    }

    printf("ファイルに書き込みました\n");

    return 0;
}

演習 13-3

#include <stdio.h>
#include <string.h>

typedef struct student{
    char name[100];
    double height;
    double weight;
} Student;

void swap_Student(Student *x, Student *y){
    Student tmp = *x;
    *x = *y;
    *y = tmp;
}

void sort_by_name(Student a[], int n){
   
    int i, j;

    for(i = 0; i < n; i++){
        for(j = n - 1; j > i; j--){
            if(strcmp(a[j - 1].name, a[j].name) > 0) swap_Student(&a[j - 1], &a[j]);
        }
    }
}

int main(){

    FILE *fp;
    Student std[256];
    int i, ninzu = 0;
    double hsum = 0.0;
    double wsum = 0.0;

    if((fp = fopen("hw.dat", "r")) == NULL){
        printf("ファイルをオープンできません\n");
        return (-1);
    }

    while(fscanf(fp, "%s%lf%lf", std[ninzu].name, &std[ninzu].height, &std[ninzu].weight) == 3){
        hsum += std[ninzu].height;
        wsum += std[ninzu].weight;
        ninzu++;
    }
    sort_by_name(std, ninzu);
    for(i = 0; i < ninzu; i++){
        printf("%-10s %5.1f %5.1f\n", std[i].name, std[i].height, std[i].weight);
    }
    printf("----------------------\n");   
    printf("平均       %5.1f %5.1f\n", hsum / ninzu, wsum / ninzu);
   
    fclose(fp);

    return 0;
}

演習 13-2

#include <stdio.h>

int main(){
   
    FILE *fp;
    char filename[256];

    printf("ファイル名を入れてください : ");  scanf("%s", filename);

    fp = fopen(filename, "w");

    printf("ファイルの中身を全て消去しました.");

    fclose(fp);

    return 0;
}

演習 13-1

#include <stdio.h>

int main(){
   
    FILE *fp;
    char filename[256];

    printf("ファイル名を入れてください : ");  scanf("%s", filename);

    fp = fopen(filename, "r");

    if(fp == NULL){
        printf("そのファイルは存在しません\n");
    }else{
        printf("そのファイルは存在します\n");
        fclose(fp);
    }
    return 0;
}

演習 12-5

#include <math.h>
#include <stdio.h>

#define sqr(n) ((n) * (n))

typedef struct{
    double x;
    double y;
} Point;

typedef struct{
    Point pt;
    double fuel;
} Car;

double distance_of(Point pa, Point pb){
    return sqrt(sqr(pa.x - pb.x) + sqr(pa.y - pb.y));
}

void put_info(Car c){
    printf("現在位置 : (%.2f, %.2f)\n", c.pt.x, c.pt.y);
    printf("残り燃料 : %.2fリットル\n", c.fuel);
}

int move(Car *c, Point dest){
    double d = distance_of(c->pt, dest);
    if(d > c->fuel) return 0;
    c->pt = dest;
    c->fuel -= d;
    return 1;
}

int main(){

    Car mycar = {{0.0, 0.0}, 90.0};

    while(true){
        int select;
        Point dest;
        put_info(mycar);
        printf("移動しますか? yes:1/no:0 -> ");   scanf("%d", &select);
        if(select != 1) break;
        printf("目的地を入力:1 / 移動距離を入力:2 -> "); scanf("%d", &select);
        if(select == 1){
            printf("目的地のx座標 : ");   scanf("%lf", &dest.x);
            printf("目的地のy座標 : ");   scanf("%lf", &dest.y);
            if(!move(&mycar, dest)) printf("移動できません\n");
        }else if(select == 2){
            printf("x方向の移動距離 : ");   scanf("%lf", &dest.x);
            printf("y方向の移動距離 : ");   scanf("%lf", &dest.y);
            dest.x += mycar.pt.x;
            dest.y += mycar.pt.y;
            if(!move(&mycar, dest)) printf("移動できません\n");                       
        }
    }

    return 0;
}

演習 12-4

#include <stdio.h>
#include <string.h>

#define NAME_LEN 64

typedef struct{
    char name[NAME_LEN];
    int height;
    float weight;
    long schols;
} Student;

void swap_Student(Student *x, Student *y){
    Student tmp = *x;
    *x = *y;
    *y = tmp;
}

void sort_by_height(Student a[], int n){

    int i, j;

    for(i = 0; i < n; i++){
        for(j = n - 1; j > i; j--){
            if(a[j - 1].height > a[j].height) swap_Student(&a[j - 1], &a[j]);
        }
    }
}

void sort_by_name(Student a[], int n){
   
    int i, j;

    for(i = 0; i < n; i++){
        for(j = n - 1; j > i; j--){
            if(strcmp(a[j - 1].name, a[j].name) > 0) swap_Student(&a[j - 1], &a[j]);
        }
    }
}

int main(){

    int i, n;

    Student std[256];

    printf("何人の情報を入力しますか?");    scanf("%d", &n);

    for(i = 0; i < n; i++){
        printf("氏名 : "); scanf("%s", std[i].name);
        printf("身長 : "); scanf("%d", &std[i].height);
        printf("体重 : "); scanf("%f", &std[i].weight);
        printf("奨学金 : "); scanf("%ld", &std[i].schols);
    }

    printf("身長を昇順にソート:1, 名前を昇順にソート:2 -> "); scanf("%d", &i);

    if(i == 1){
        sort_by_height(std, n);
        printf("身長順にソートしました\n");
    }
    else if(i == 2){
        sort_by_name(std, n);
        printf("名前順にソートしました\n");
    }

    for(i = 0; i < n; i++){
        printf("%-8s %6d%6.1f%7ld\n", std[i].name, std[i].height, std[i].weight, std[i].schols);
    }

    return 0;
}

演習 12-3

#include <stdio.h>

typedef struct xyz{
    int x;
    long y;
    double z;
}xyz;

xyz scan_xyz(){

    xyz tmp;

    printf("x : "); scanf("%d", &tmp.x);
    printf("y : "); scanf("%ld", &tmp.y);
    printf("z : "); scanf("%lf", &tmp.z);

    return tmp;
}

int main(){

    xyz s;

    s = scan_xyz();

    printf("xyz.x : %d\n", s.x);
    printf("xyz.y : %ld\n", s.y);
    printf("xyz.z : %lf\n", s.z);

    return 0;
}