Created
November 17, 2017 03:30
-
-
Save komasaru/519a7f327b2be59b73eaceda9a10ff31 to your computer and use it in GitHub Desktop.
C++ source code to compute G.C.D. with Euclid's algorithm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************* | |
* ユークリッドの互除法(再帰的に求める方法) | |
*********************************************/ | |
#include <iostream> | |
#include <stdio.h> | |
using namespace std; | |
/* | |
* 計算クラス | |
*/ | |
class Calc | |
{ | |
public: | |
// 最大公約数計算 | |
int calcGCD(int a, int b); | |
}; | |
/* | |
* 最大公約数計算 | |
*/ | |
int Calc::calcGCD(int a, int b) | |
{ | |
if (b == 0) { | |
return a; | |
} else { | |
return calcGCD(b, a % b); | |
} | |
} | |
/* | |
* メイン処理 | |
*/ | |
int main() | |
{ | |
int a, b; | |
try { | |
// データ入力 | |
cout << "1つ目の自然数:"; | |
scanf("%d", &a); | |
cout << "2つ目の自然数:"; | |
scanf("%d", &b); | |
cout << "a = " << a << ", " | |
<< "b = " << b << endl; | |
// 計算 | |
Calc objCalc; | |
cout << "最大公約数 = " << objCalc.calcGCD(a, b) << endl; | |
} | |
catch (...) { | |
cout << "例外発生!" << endl; | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment