分数の計算をおこなうプログラム、分数の演算を行えるRatioクラスを次の手順で作成する。
入力例:
Input two numbers : 2 3
x = 2/3
Input two numbers : 2 0
Invalid denominator!
#include <iostream> using namespace std: class Ratio{ // クラスの宣言 public: Ratio(int n,int d); // メンバ関数の宣言 void displa(); private: int num, den; // メンバ変数の宣言 }; Ratio::Ratio(int n, ind d){ // Ratioの定義(処理内容) num = n; den = d; } void Ratio::display(){ // displayの定義(処理内容) cout << num << "/" << den << "\n"; } int main(){ int n,d; cout << "Input two numbers : "; cin >> n >> d; if(d==0){ cout << "Invalid denominator! \n"; // エラーメッセージ return 0; } Ratio x(n,d); cout << "x = "; x.display(); return 0; }
入力例:
Input two numbers : 2 3
x = 2/3
Input two numbers : 2 0
Invalid denominator!
このページへのコメント
BmKYIr A round of applause for your blog.Really looking forward to read more. Much obliged.