Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created November 17, 2017 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/519a7f327b2be59b73eaceda9a10ff31 to your computer and use it in GitHub Desktop.
Save komasaru/519a7f327b2be59b73eaceda9a10ff31 to your computer and use it in GitHub Desktop.
C++ source code to compute G.C.D. with Euclid's algorithm.
/*********************************************
* ユークリッドの互除法(再帰的に求める方法)
*********************************************/
#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