Cプログラミング
 Computer >> コンピューター >  >> プログラミング >> Cプログラミング

C言語のswitch文で図書館管理システムを作成する方法【初心者向け解説】

C言語を学ぶ上で、構造体(struct)とswitch文を組み合わせた実践的なプログラムは非常に良い練習になります。本記事では、図書館の書籍情報を管理できるシンプルなシステムをC言語で実装する方法を、アルゴリズム・サンプルコード・実行結果とともに詳しく解説します。

課題(Problem)

Cプログラミングを使用して、図書館の書籍に関する情報(書名、著者名、ページ数、価格など)をどのように保存・管理するかを実装します。

アルゴリズム

プログラムの全体の流れは以下の手順に従います。

ステップ1:書籍データを保持する構造体を宣言する
ステップ2:ループ処理で使用する変数を宣言する
ステップ3:switch文を使って各モジュール(機能)ごとに処理を分岐させる
ステップ4:各ケースの処理内容は以下の通り
    case 1 … 書籍情報の登録
    case 2 … 書籍情報の表示
    case 3 … 図書館内の蔵書数を表示
    case 4 … プログラムの終了

サンプルプログラム

以下が完全なソースコードです。構造体libraryに書名・著者名・ページ数・価格を格納し、whileループとswitch文によってメニュー形式の操作を実現しています。

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct library{
    char bookname[50];
    char author[50];
    int noofpages;
    float price;
};
int main(){
    struct library lib[100];
    char bookname[30];
    int i,j, keepcount;
    i=j=keepcount = 0;
    while(j!=6){
        printf("\n1. Add book information\n");
        printf("2.Display book information\n");
        printf("3. no of books in the library\n");
        printf("4. Exit");
        printf ("\n\nEnter one of the above : ");
        scanf("%d",&j);
        switch (j){
            /* 書籍情報を追加 */
            case 1:
                printf ("Enter book name = ");
                scanf ("%s",lib[i].bookname);
                printf ("Enter author name = ");
                scanf ("%s",lib[i].author);
                printf ("Enter pages = ");
                scanf ("%d",&lib[i].noofpages);
                printf ("Enter price = ");
                scanf ("%f",&lib[i].price);
                keepcount++;
                i++;
                break;
            case 2:
                printf("you have entered the following information\n");
                for(i=0; i<keepcount; i++){
                    printf ("book name = %s\n",lib[i].bookname);
                    printf ("\t author name = %s\n",lib[i].author);
                    printf ("\t pages = %d\n",lib[i].noofpages);
                    printf ("\t price = %f\n",lib[i].price);
                }
                break;
            case 3:
                printf("\n No of books in library : %d", keepcount);
                break;
            case 4:
                exit (0);
        }
    }
    return 0;
}

コードのポイント解説

  • 構造体の定義:struct libraryは、書名(bookname)・著者名(author)・ページ数(noofpages)・価格(price)の4つのメンバーを持っています。
  • 配列による複数冊の管理:lib[100]として最大100冊分のデータを保存できます。
  • カウンタ変数:keepcountが登録された書籍の総数を追跡し、表示や集計の際に利用されます。
  • メニュー式の操作性:whileループの中で毎回メニューを表示するため、ユーザーは連続して操作を行えます。「4」を選択するとexit(0)によりプログラムが終了します。

実行結果(出力例)

実際にコンパイルして実行すると、以下のような動作になります。まず「1」で書籍情報を登録し、「2」で一覧表示、「3」で蔵書数を確認します。

1. Add book information
2.Display book information
3. no of books in the library
4. Exit

Enter one of the above : 1
Enter book name = HarryPotter
Enter author name = hp
Enter pages = 250
Enter price = 350.6

1. Add book information
2.Display book information
3. no of books in the library
4. Exit

Enter one of the above : 2
you have entered the following information
book name = HarryPotter
         author name = hp
         pages = 250
         price = 350.600006

1. Add book information
2.Display book information
3. no of books in the library
4. Exit

Enter one of the above : 3

No of books in library : 1
1. Add book information
2.Display book information
3. no of books in the library
4. Exit

Enter one of the above : 4

まとめ

このプログラムでは、構造体配列とswitch文を組み合わせることで、書籍情報の「登録」「表示」「件数確認」といった基本機能を備えた図書館管理システムを実現しました。float型の価格を出力すると「350.600006」のように誤差が生じる点は浮動小数点数特有の挙動であり、実務では%fの代わりに%.2fなどの書式指定を使うと見やすくなります。さらに発展させたい場合は、書籍の検索機能や削除機能、ファイルへのデータ保存などを追加してみると良いでしょう。

  1. C言語のtime.hライブラリ関数を使って現在日時をISO形式で表示する方法

    課題C言語を使用して、現在の日付と時刻をISO標準形式で表示するにはどうすればよいのでしょうか。解決策システムから取得した現在の日付と時刻を、ISO形式で出力する方法を解説します。たとえば、出力結果は「2020年12月15日(月)22:50」のような形式になります。このプログラムで使用する主な標準ライブラリ関数は以下の2つです。使用する関数time()関数現在の暦時刻(カレンダー時間)を取得して返します。引数にNULLを指定すると、現在時刻がtime_t型の値として返されます。strftime()関数時刻情報を指定した書式の文字列に変換します。この関数はtime.hヘッダーファイルに含まれてお

  2. 【C言語】strncmpライブラリ関数を使って2つの文字列を比較するプログラムの作り方

    strncmpは、C言語のヘッダーファイル「string.h」に定義されている標準ライブラリ関数の一つです。この関数を使用すると、2つの文字列を比較し、どちらの文字列が大きい(辞書順で後ろにある)のかを判定することができます。 strcmp関数(文字列比較) strcmp関数は、2つの文字列を比較するための関数です。戻り値として、両方の文字列の中で最初に一致しなかった文字同士のASCIIコードの差を返します。 構文 int strcmp(string1, string2); 差が0の場合:string1 = string2(2つの文字列は等しい) 差が正の値の場合:string1 &