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

【C++】構造体を使って2つの距離(フィート・インチ)を加算するプログラムの作成方法

構造体(struct)とは

構造体とは、異なるデータ型の項目をひとつにまとめて扱えるデータ構造です。複数のデータ型を持つレコードを組み合わせた複雑なデータ構造を作成する際に非常に役立ちます。構造体は struct キーワードを使用して定義します。

構造体の定義例は以下のとおりです。

struct DistanceFI {
    int feet;
    int inch;
};

この構造体は、フィート(feet)とインチ(inch)の2つのメンバ変数で距離を表現しています。

2つの距離を加算するC++プログラム

以下は、C++で構造体を使用して2つの距離(フィート・インチ)を加算するプログラムの例です。

サンプルコード

#include <iostream>

using namespace std;
struct DistanceFI {
    int feet;
    int inch;
};
int main() {
    struct DistanceFI distance1, distance2, distance3;
    cout << "Enter feet of Distance 1: "<<endl;
    cin >> distance1.feet;
    cout << "Enter inches of Distance 1: "<<endl;
    cin >> distance1.inch;

    cout << "Enter feet of Distance 2: "<<endl;
    cin >> distance2.feet;
    cout << "Enter inches of Distance 2: "<<endl;
    cin >> distance2.inch;

    distance3.feet = distance1.feet + distance2.feet;
    distance3.inch = distance1.inch + distance2.inch;

    if(distance3.inch > 12) {
        distance3.feet++;
        distance3.inch = distance3.inch - 12;
    }
    cout << endl << "Sum of both distances is " << distance3.feet << " feet and " << distance3.inch << " inches";
    return 0;
}

実行結果

上記プログラムの実行結果は以下のとおりです。

Enter feet of Distance 1: 5
Enter inches of Distance 1: 9
Enter feet of Distance 2: 2
Enter inches of Distance 2: 6
Sum of both distances is 8 feet and 3 inches

プログラムの解説

1. 構造体の定義

上記プログラムでは、フィートとインチで距離を表す構造体 DistanceFI を定義しています。該当するコードは以下のとおりです。

struct DistanceFI{
    int feet;
    int inch;
};

2. ユーザーからの入力

加算する2つの距離の値は、ユーザーからの入力として受け取ります。該当するコードは以下のとおりです。

cout << "Enter feet of Distance 1: "<<endl;
cin >> distance1.feet;
cout << "Enter inches of Distance 1: "<<endl;
cin >> distance1.inch;

cout << "Enter feet of Distance 2: "<<endl;
cin >> distance2.feet;
cout << "Enter inches of Distance 2: "<<endl;
cin >> distance2.inch;

3. 距離の加算と繰り上げ処理

2つの距離のフィートとインチをそれぞれ加算します。インチの合計が12を超えた場合は、フィートに1を加え、インチから12を減算します。これは「1フィート=12インチ」という単位換算のためです。該当するコードは以下のとおりです。

distance3.feet = distance1.feet + distance2.feet;
distance3.inch = distance1.inch + distance2.inch;
if(distance3.inch > 12) {
    distance3.feet++;
    distance3.inch = distance3.inch - 12;
}

4. 結果の表示

最後に、加算結果のフィートとインチの値を画面に表示します。該当するコードは以下のとおりです。

cout << endl << "Sum of both distances is " << distance3.feet << " feet and " << distance3.inch << " inches";

このように構造体を使うことで、フィートとインチという関連する2つの値をひとつのまとまりとして扱え、距離の加算処理をわかりやすく実装できます。

  1. C++で2つの2進数文字列を加算するプログラムの書き方

    2つの2進数を表す文字列が与えられたとき、それらを加算した結果を求め、その結果を2進数の文字列として返すことを考えます。2進数とは、0か1のいずれかで表現される数値のことです。2進数同士を足し合わせる際には、以下のような2進数特有の加算ルールに従う必要があります。0+0 → 0 0+1 → 1 1+0 → 1 1+1 → 0(繰り上がり1)入力例str1 = {11}, str2 = {1}出力例100入力例str1 = {110}, str2 = {1}出力例111問題を解くためのアプローチ両方の文字列を末尾(最下位桁)から走査する対応する桁の2進数同士を加算する1と1を足した場合は、その桁

  2. C++プログラムから外部アプリケーション(メモ帳など)を起動する方法

    この記事では、C++プログラムを使ってメモ帳(Notepad)などのサードパーティ製アプリケーションを起動する方法を解説します。実装は非常にシンプルで、コマンドプロンプトで使うコマンドをそのままC++から呼び出すだけで実現できます。ポイントとなるのは、標準ライブラリの system() 関数です。この関数の引数にアプリケーション名(コマンド)を文字列として渡すと、OSがそのコマンドを実行し、対応するアプリケーションが起動します。サンプルコード#include <iostream> using namespace std; int main() { cout <<