C言語構造体とアルゴリズムの実践演習

課題4: 書籍販売データ処理

ソースコード

 1 #include <stdio.h>
 2 #define MAX_BOOKS 10
 3 
 4 typedef struct {
 5     char isbn[20];          // ISBN番号
 6     char title[80];         // 書籍タイトル
 7     char writer[80];        // 著者名
 8     double price;           // 価格
 9     int quantity;           // 販売冊数
10 } Publication;
11 
12 void display_items(Publication items[], int count);
13 void sort_by_quantity(Publication items[], int count);
14 double calculate_revenue(Publication items[], int count);
15 
16 int main() {
17     Publication catalog[MAX_BOOKS] = {
18         {"978-7-5327-6082-4", "ゴールキーパーの死", "ロナルド・レン", 42, 51},
19         {"978-7-308-17047-5", "自由と愛の地:イスラエル紀行", "雲也退", 49, 30},
20         {"978-7-5404-9344-8", "ロンドン人", "クレイグ・テイラー", 68, 27},
21         {"978-7-5447-5246-6", "ソフトウェア生命体のライフサイクル", "テッド・チャン", 35, 90},
22         {"978-7-5722-5475-8", "チップの歴史", "王波", 74.9, 49},
23         {"978-7-5133-5750-0", "コンソール戦争", "ブレイク・J・ハリス", 128, 42},
24         {"978-7-2011-4617-1", "世界の果てのカフェ", "ジョン・ストレイリー", 22.5, 44},
25         {"978-7-5133-5109-6", "こんにちは、宇宙人", "イギリス未来出版グループ", 118, 42},
26         {"978-7-1155-0509-5", "無限の始まり:世界進歩の源泉", "デイビッド・ドイッチ", 37.5, 55},
27         {"978-7-229-14156-1", "ザ・フォンテン", "A・ランド", 84, 59}
28     };
29     
30     printf("書籍販売ランキング(販売冊数順): \n");
31     sort_by_quantity(catalog, MAX_BOOKS);
32     display_items(catalog, MAX_BOOKS);
33 
34     printf("\n総売上高: %.2f\n", calculate_revenue(catalog, MAX_BOOKS));
35     
36     return 0;
37 }
38 
39 void display_items(Publication items[], int count){
40     int index;
41     for(index = 0; index < count; index++){
42         printf("%s,%s,%s,%.2lf,%d", 
43                items[index].isbn, 
44                items[index].title, 
45                items[index].writer, 
46                items[index].price, 
47                items[index].quantity);
48         printf("\n");
49     }
50 }
51     
52 void sort_by_quantity(Publication items[], int count){
53     int i, j;
54     Publication temp;
55     for(i = 0; i < count - 1; i++)
56         for(j = 0; j < count - 1 - i; j++)
57             if(items[j].quantity < items[j + 1].quantity){
58                 temp = items[j];
59                 items[j] = items[j + 1];
60                 items[j + 1] = temp;
61             }
62 }
63 
64 double calculate_revenue(Publication items[], int count){
65     int index;
66     double total = 0;
67     for(index = 0; index < count; index++)
68         total += items[index].price * items[index].quantity;
69 
70     return total;
71 }

実行結果画面

課題5: 日付処理プログラム

ソースコード

 1 #include <stdio.h>
 2 
 3 typedef struct {
 4     int year;
 5     int month;
 6     int day;
 7 } Date;
 8 
 9 // 関数宣言
10 void input_date(Date *pd);              // Date変数に日付を入力
11 int day_of_year(Date d);                // 指定日付がその年の何日目かを返す
12 int compare_dates(Date d1, Date d2);    // 2つの日付を比較:
13                                         // d1がd2より前なら-1を返す
14                                         // d1がd2より後なら1を返す
15                                         // d1とd2が同じなら0を返す
16 
17 void test1() {
18     Date d;
19     int i;
20 
21     printf("日付を入力してください(2025-06-01のような形式):\n");
22     for(i = 0; i < 3; ++i) {
23         input_date(&d);
24         printf("%d-%02d-%02dはこの年の%d日目です\n\n", 
25                d.year, d.month, d.day, day_of_year(d));
26     }
27 }
28 
29 void test2() {
30     Date alice_birth, bob_birth;
31     int i;
32     int result;
33 
34     printf("AliceとBobの誕生日を入力してください(2025-06-01のような形式):\n");
35     for(i = 0; i < 3; ++i) {
36         input_date(&alice_birth);
37         input_date(&bob_birth);
38         result = compare_dates(alice_birth, bob_birth);
39         
40         if(result == 0)
41             printf("AliceとBobは同い年です\n\n");
42         else if(result == -1)
43             printf("AliceはBobより年上です\n\n");
44         else
45             printf("AliceはBobより年下です\n\n");
46     }
47 }
48 
49 int main() {
50     printf("テスト1: 日付を入力し、その年の何日目かを表示\n");
51     test1();
52 
53     printf("\nテスト2: 2人の年齢の比較\n");
54     test2();
55 }
56 
57 // input_date関数の実装
58 // 機能: Date変数に日付を入力
59 void input_date(Date* pd) {
60     scanf("%d-%d-%d", &pd->year, &pd->month, &pd->day);
61 }
62   
63 // day_of_year関数の実装
64 // 機能:指定日付がその年の何日目かを返す
65 int day_of_year(Date d) {
66     int days_in_month[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
67     int is_leap = ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0));
68     if (is_leap) {
69         days_in_month[2] = 29;
70     }
71     int total_days = 0;
72     for (int i = 1; i < d.month; i++) {
73         total_days += days_in_month[i];
74     }
75     total_days += d.day;
76     return total_days;
77 }
78 
79 // compare_dates関数の実装
80 // 機能:2つの日付を比較
81 int compare_dates(Date d1, Date d2) {
82     if (d1.year > d2.year)
83         return 1;
84     else if (d1.year < d2.year)
85         return -1;
86     else {
87         if (day_of_year(d1) < day_of_year(d2))
88             return -1;
89         else if (day_of_year(d1) > day_of_year(d2))
90             return 1;
91         else
92             return 0;
93     }
94 }

実行結果画面

課題6: ユーザーアカウント管理システム

ソースコード

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 enum UserType {admin, student, teacher};
 5 
 6 typedef struct {
 7     char username[20];  // ユーザー名
 8     char password[20];  // パスワード
 9     enum UserType type; // アカウント種別
10 } UserAccount;
11 
12 
13 // 関数宣言
14 void display_accounts(UserAccount accounts[], int count); // アカウント情報を表示(パスワードは*で表示)
15 
16 int main() {
17     UserAccount accounts[] = {
18         {"U1001", "123456", student},
19         {"U1002", "abcdef123", student},
20         {"U1009", "xyz12121", student}, 
21         {"A1009", "9213071x", admin},
22         {"T11553", "129dfg32k", teacher},
23         {"S3005", "921kfmg917", student}
24     };
25     int count;
26     count = sizeof(accounts)/sizeof(UserAccount);
27     display_accounts(accounts, count);
28 
29     return 0;
30 }
31 
32 // display_accounts()関数の実装
33 // 機能:アカウント配列の情報を表示
34 //      パスワードは元のパスワードと同じ長さの*で表示
35 void display_accounts(UserAccount accounts[], int count) {
36     int i, length, j;
37     for(i = 0; i < count; i++){
38         length = strlen(accounts[i].password);
39         printf("%s   ", accounts[i].username);
40         for(j = 0; j < length; j++)
41             printf("*");
42     
43         switch(accounts[i].type){
44             case admin: printf("   管理者"); break;
45             case student: printf("   学生"); break;
46             case teacher: printf("   教師"); break;
47         }
48         printf("\n");
49     }
50 }

実行結果画面

課題7: 連絡先管理システム

ソースコード

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 typedef struct {
 5     char name[20];      // 氏名
 6     char phone[12];     // 電話番号
 7     int  priority;      // 緊急連絡先かどうか(1:はい、0:いいえ)
 8 } Person; 
 9 
10 
11 // 関数宣言
12 void set_priority_contact(Person contacts[], int count, char name[]); // 緊急連絡先を設定
13 void display_all(Person contacts[], int count);    // 全連絡先情報を表示
14 void sorted_display(Person contacts[], int count); // 氏名順に表示(緊急連絡先を先頭に)
15 
16 
17 #define MAX_CONTACTS 10
18 int main() {
19     Person directory[MAX_CONTACTS] = {
20         {"Liu Yi", "15510846604", 0},
21         {"Chen Er", "18038747351", 0},
22         {"Zhang San", "18853253914", 0},
23         {"Li Si", "13230584477", 0},
24         {"Wang Wu", "15547571923", 0},
25         {"Zhao Liu", "18856659351", 0},
26         {"Zhou Qi", "17705843215", 0},
27         {"Sun Ba", "15552933732", 0},
28         {"Wu Jiu", "18077702405", 0},
29         {"Zheng Shi", "18820725036", 0}
30     };
31     int priority_count, i;
32     char name[20];
33 
34     printf("元の連絡先リストを表示: \n"); 
35     display_all(directory, MAX_CONTACTS);
36 
37     printf("\n設定する緊急連絡先の数を入力: ");
38     scanf("%d", &priority_count);
39     
40     printf("%d人の緊急連絡先の氏名を入力:\n", priority_count);
41     for(i = 0; i < priority_count; ++i) {
42         scanf("%s", name);
43         set_priority_contact(directory, MAX_CONTACTS, name);
44     }
45 
46     printf("\n連絡先リストを表示(氏名順、緊急連絡先を先頭に):\n");
47     sorted_display(directory, MAX_CONTACTS);
48 
49     return 0;
50 }
51 
52 // set_priority_contact関数の実装
53 // 機能:指定した氏名の連絡先を緊急連絡先に設定
54 void set_priority_contact(Person contacts[], int count, char name[]) {
55     int index;
56     for(index = 0; index < count; index++)
57         if(strcmp(contacts[index].name, name) == 0)
58             contacts[index].priority = 1;
59 }
60 
61 // sorted_display関数の実装
62 // 機能: 連絡先を表示(氏名順、緊急連絡先を先頭に)
63 void sorted_display(Person contacts[], int count) {
64     int i, j;
65     Person temp;
66     for(i = 0; i < count - 1; i++)
67         for(j = 0; j < count - 1 - i; j++)
68             if ((contacts[j].priority < contacts[j + 1].priority) ||
69                 (contacts[j].priority == contacts[j + 1].priority && 
70                  strcmp(contacts[j].name, contacts[j + 1].name) > 0)) {
71                 temp = contacts[j];
72                 contacts[j] = contacts[j + 1];
73                 contacts[j + 1] = temp;
74             }
75     display_all(contacts, count);
76 }
77 
78 void display_all(Person contacts[], int count) {
79     int index;
80 
81     for(index = 0; index < count; ++index) {
82         printf("%-15s%-15s", contacts[index].name, contacts[index].phone);
83         if(contacts[index].priority)
84             printf("%8s", "*");
85         printf("\n");
86     }
87 }

実行結果画面

タグ: C言語 構造体 ソートアルゴリズム 日付処理 列挙型

7月27日 22:11 投稿