演習 13-13

#include <stdio.h>

int main(){

    int ch;
    FILE *sfp;
    FILE *dfp;
    char sname[FILENAME_MAX];
    char dname[FILENAME_MAX];

    printf("コピー元ファイル名 : "); scanf("%s", sname);
    printf("コピー先ファイル名 : "); scanf("%s", dname);

    if((sfp = fopen(sname, "rb")) == NULL){
        printf("コピー元ファイルをオープンできません\n");
    }else{
        if((dfp = fopen(dname, "wb")) == NULL){
            printf("コピー先ファイルをオープンできません\n");           
        }else{
            while((fread(&ch, sizeof(int), 1, sfp)) > 0){
                fwrite(&ch, sizeof(int), 1, dfp);
                putchar(ch);
            }
            fclose(dfp);
        }
        fclose(sfp);
    }
    return 0;
}

演習 13-12

#include <time.h>
#include <stdio.h>

char data_file[] = "datetime.bin";

void get_data(){
    FILE *fp;

    if((fp = fopen(data_file, "rb")) == NULL){
        printf("本プラグラムを実行するのは初めてですね\n");
    }else{
        int year, month, day, h, m, s;

        fscanf(fp, "%d%d%d%d%d%d", &year, &month, &day, &h, &m, &s);
        printf("前回は%d年%d月%d日%d時%d分%d秒でした\n", year, month, day, h, m, s);

        fclose(fp);
    }
}

void put_data(){
    FILE *fp;
    time_t current = time(NULL);
    struct tm *timer = localtime(&current);

    if((fp = fopen(data_file, "wb")) == NULL){
        printf("ファイルをオープンできません\n");
    }else{
        fprintf(fp, "%d %d %d %d %d %d\n", timer->tm_year + 1900, timer->tm_mon + 1, timer->tm_mday, timer->tm_hour, timer->tm_min, timer->tm_sec);
        fclose(fp);
    }
}

int main(){

    get_data();

    put_data();

    return 0;
}

演習 13-11

#include <stdio.h>

int main(){

    int i;
    FILE *fp;
    double data[10];

    for(i = 0; i < 10; i++){
        data[i] = 3.14159265358979323846;
    }

    if((fp = fopen("data.bin", "wb")) == NULL){
        printf("ファイルをオープンできません\n");
    }else{
        fwrite(data, sizeof(double), 10, fp);
        fclose(fp);
    }

    if((fp = fopen("data.bin", "rb")) == NULL){
        printf("ファイルをオープンできません\n");
    }else{
        fread(data, sizeof(double), 10, fp);
        for(i = 0; i < 10; i++){
            printf("data[%d] = %23.21lf\n", i, data[i]);
        }
        fclose(fp);
    }

    return 0;
}

演習 13-10

#include <stdio.h>

int main(){

    int ch;
    FILE *sfp;
    FILE *dfp;
    char sname[FILENAME_MAX];
    char dname[FILENAME_MAX];

    printf("コピー元ファイル名 : "); scanf("%s", sname);
    printf("コピー先ファイル名 : "); scanf("%s", dname);

    if((sfp = fopen(sname, "r")) == NULL){
        printf("コピー元ファイルをオープンできません\n");
    }else{
        if((dfp = fopen(dname, "w")) == NULL){
            printf("コピー先ファイルをオープンできません\n");           
        }else{
            while((ch = fgetc(sfp)) != EOF){
                if('A' <= ch && ch <= 'Z'){
                    ch -= 'A' - 'a';
                }
                fputc(ch, dfp);
                putchar(ch);
            }
            fclose(dfp);
        }
        fclose(sfp);
    }
    return 0;
}

演習 13-9

#include <stdio.h>

int main(){

    int ch;
    FILE *sfp;
    FILE *dfp;
    char sname[FILENAME_MAX];
    char dname[FILENAME_MAX];

    printf("コピー元ファイル名 : "); scanf("%s", sname);
    printf("コピー先ファイル名 : "); scanf("%s", dname);

    if((sfp = fopen(sname, "r")) == NULL){
        printf("コピー元ファイルをオープンできません\n");
    }else{
        if((dfp = fopen(dname, "w")) == NULL){
            printf("コピー先ファイルをオープンできません\n");           
        }else{
            while((ch = fgetc(sfp)) != EOF){
                if('a' <= ch && ch <= 'z'){
                    ch += 'A' - 'a';
                }
                fputc(ch, dfp);
            }
            fclose(dfp);
        }
        fclose(sfp);
    }
    return 0;
}

演習 13-8

#include <stdio.h>

int main(){

    int ch;
    FILE *sfp;
    FILE *dfp;
    char sname[FILENAME_MAX];
    char dname[FILENAME_MAX];

    printf("コピー元ファイル名 : "); scanf("%s", sname);
    printf("コピー先ファイル名 : "); scanf("%s", dname);

    if((sfp = fopen(sname, "r")) == NULL){
        printf("コピー元ファイルをオープンできません\n");
    }else{
        if((dfp = fopen(dname, "w")) == NULL){
            printf("コピー先ファイルをオープンできません\n");           
        }else{
            while((ch = fgetc(sfp)) != EOF){
                fputc(ch, dfp);
                putchar(ch);
            }
            fclose(dfp);
        }
        fclose(sfp);
    }
    return 0;
}

演習 13-7

#include <stdio.h>

int main(){

    int ch, count = 0;
    FILE *fp;
    char file_name[256];

    printf("ファイル名 : "); scanf("%s", file_name);

    if((fp = fopen(file_name, "r")) == NULL){
        printf("cannot open a file.\n");
        return (-1);
    }

    while((ch = fgetc(fp)) != EOF){
        if('0' <= ch && ch <= '9') count++;
    }

    printf("数字は%d回文字ありました。\n", count);

    return 0;
}

演習 13-6

#include <stdio.h>

int main(){

    int ch, count = 0;
    FILE *fp;
    char file_name[256];

    printf("ファイル名 : "); scanf("%s", file_name);

    if((fp = fopen(file_name, "r")) == NULL){
        printf("cannot open a file.\n");
        return (-1);
    }

    while((ch = fgetc(fp)) != EOF){
        if(ch == '\n') count++;
    }

    printf("改行は%d回行われていました。\n", count);

    return 0;
}

演習 13-5

#include <time.h>
#include <stdio.h>

char data_file[] = "datetime.dat";

void get_data(){
    FILE *fp;

    if((fp = fopen(data_file, "r")) == NULL){
        printf("本プラグラムを実行するのは初めてですね\n");
    }else{
        int year, month, day, h, m, s;
        char feel[256];
        fscanf(fp, "%d%d%d%d%d%d%s", &year, &month, &day, &h, &m, &s, feel);
        printf("前回は%d年%d月%d日%d時%d分%d秒で、気分は%sでした\n", year, month, day, h, m, s, feel);

        fclose(fp);
    }
}

void put_data(){
    FILE *fp;
    time_t current = time(NULL);
    struct tm *timer = localtime(&current);
    char feel[256];

    if((fp = fopen(data_file, "w")) == NULL){
        printf("ファイルをオープンできません\n");
    }else{
        printf("今の気分は?:"); scanf("%s", feel);
        fprintf(fp, "%d %d %d %d %d %d %s\n", timer->tm_year + 1900, timer->tm_mon + 1, timer->tm_mday, timer->tm_hour, timer->tm_min, timer->tm_sec, feel);
        fclose(fp);
    }
}

int main(){

    get_data();

    put_data();

    return 0;
}

演習 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;
}

演習 12-2

#include <stdio.h>

#define NAME_LEN 64

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


void hiroko(Student *std){
    if(std->height < 180) std->height = 180;
    if(std->weight > 80) std->weight = 80;
}

int main(){

    Student sanaka;

    printf("氏名 : "); scanf("%s", sanaka.name);
    printf("身長 : "); scanf("%d", &sanaka.height);
    printf("体重 : "); scanf("%f", &sanaka.weight);
    printf("奨学金 : "); scanf("%ld", &sanaka.schols);

    hiroko(&sanaka);

    printf("氏名 = %s\n", sanaka.name);
    printf("身長 = %d\n", sanaka.height);
    printf("体重 = %.1f\n", sanaka.weight);
    printf("奨学金 = %ld\n", sanaka.schols);

    return 0;
}

演習 12-1

#include <stdio.h>

#define NAME_LEN 64

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

int main(){

    struct student takao = {"Takao", 173, 86.2};

    printf("氏名 = %s, adress = %p\n", takao.name, &takao.name);
    printf("身長 = %d, adress = %p\n", takao.height, &takao.height);
    printf("体重 = %.1f, adress = %p\n", takao.weight, &takao.weight);
    printf("奨学金 = %ld, adress = %p\n", takao.schols, &takao.schols);

    return 0;
}

演習 11-10

#include <stdio.h>

int strtoi(const char *nptr){

    int num = 0, sign = 1;

    if(*nptr == '+'){
        sign = 1;
        *nptr++;
    }else if(*nptr == '-'){
        sign = -1;
        *nptr++;
    }

    while(true){
        num += *nptr - '0';
        *nptr++;
        if(*nptr == '\0') break;
        num *= 10;
    }

    return num * sign;
}

long strtol(const char *nptr){
   
    int sign = 1;
    long num = 0;

    if(*nptr == '+'){
        sign = 1;
        *nptr++;
    }else if(*nptr == '-'){
        sign = -1;
        *nptr++;
    }

    while(true){
        num += *nptr - '0';
        *nptr++;
        if(*nptr == '\0') break;
        num *= 10;
    }

    return num * sign;
}

double strtof(const char *nptr){
   
    int sign = 1;
    double num = 0;

    // 符号の処理
    if(*nptr == '+'){
        sign = 1;
        *nptr++;
    }else if(*nptr == '-'){
        sign = -1;
        *nptr++;
    }

    // 整数部の処理
    while(true){
        num += *nptr - '0';
        *nptr++;
        if(*nptr == '\0' || *nptr == '.') break;
        num *= 10;
    }

    // 小数部の処理
    if(*nptr == '.'){
        *nptr++;
        double dec = 1;
        while(true){
            num += (*nptr - '0') / (dec *= 10);
            *nptr++;
            if(*nptr == '\0') break;
        }
    }

    return num * sign;
}

int main(){

    char str1[256] = "12345";
    char str2[256] = "123.45";

    printf("str1:%s -> atoi(str1):%d\n", str1, strtoi(str1));
    printf("str1:%s -> atol(str1):%ld\n", str1, strtol(str1));
    printf("str2:%s -> atof(str2):%lf\n", str2, strtof(str2));

    return 0;
}

演習 11-9

#include <stdio.h>

int strlen(const char *s){
   
    int len = 0;

    while(*s++) len++;

    return len;
}

void strcpy(char *d, const char *s){

    while(*d++ = *s++);
}

void strncpy(char *s1, const char *s2, size_t n){
   
    char *tmp = s1;
    int len = strlen(s1);

    while(n){
        *s1++ = *s2++;
        n--;
    }

    *s1++ = '\0';

}

void strcat(char *s1, const char *s2){

    while(*s1) *s1++;
    while(*s1++ = * s2++);
}

void strncat(char *s1, const char *s2, size_t n){

    while(*s1) *s1++;
    while(n--) *s1++ = * s2++;
    *s1++ = '\0';
}

int strcmp(const char *s1, const char *s2){

    while(*s1 == *s2){
        if(*s1 == '\0') return 0;
        s1++;
        s2++;
    }
    return (unsigned char)*s1 - (unsigned char)*s2;
}

int strncmp(const char *s1, const char *s2, size_t n){
   
    while(n && *s1 && *s2){
        if(*s1 != *s2) return (unsigned char)*s1 - (unsigned char)*s2;
        s1++;
        s2++;
        n--;
    }
    if(!n) return 0;
    if(*s1) return 1;

    return -1;
}

int main(){

    char str1[256] = "abcde";
    char str2[256] = "12345";
    char str3[256] = "AaBbCcDdEe";
    int n = 3;

    printf("str1:%s\n", str1);
    printf("str2:%s\n", str2);

    printf("str1の長さは%dです\n", strlen(str1));

    printf("str2をstr1にコピーします\n");
    strcpy(str1, str2);
    printf("str1:%s\n", str1);

    printf("str2の先頭%d文字をstr1にコピーします\n", n);
    strncpy(str1, str2, n);
    printf("str1:%s\n", str1);

    printf("str2をstr1と連結します\n");
    strcat(str1, str2);
    printf("str1:%s\n", str1);

    printf("str2の先頭%d文字をstr1に連結します\n", n);
    strncat(str1, str2, n);
    printf("str1:%s\n", str1);

    printf("str2とstr1を比較します\n");
    printf("str1 : str2 ? %d\n", strcmp(str1, str2));

    printf("str2とstr1の先頭%d文字を比較します\n", n);
    printf("str1 : str2 ? %d\n", strncmp(str1, str2, n));

    return 0;
}