/************************************************************************
***By : summon ***
***Data : 2010.01.09 ***
***Function : 求两个数的最大公约数和最小公倍数 ***
*** 应用欧几里得算法计算gcd,用n*m=lcm*gcd计算lcm ***
***Version : V1.0 ***
/************************************************************************/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int STATUS;
STATUS InputData(int *Datan, int *Datam);
STATUS GCDofTwoNum(int Datan, int Datam, int *gcd);
STATUS LCMofTwoNum(int Datan, int Datam, int gcd, int *lcm);
STATUS OutputData(int gcd, int lcm);
int main(int argc, char* argv[])
{
int gcd;
int lcm;
int Datan;
int Datam;
int ret;
ret = InputData(&Datan, &Datam);
ret = GCDofTwoNum(Datan, Datam, &gcd);
ret = LCMofTwoNum(Datan, Datam, gcd, &lcm);
ret = OutputData(gcd, lcm);
return 0;
}
STATUS InputData(int *Datan, int *Datam)
{
if (Datam == NULL || Datan == NULL)
{
return 0;
}
while (1)
{
printf("Please intput two positive int number:");
fflush(stdin);
scanf("%d %d", Datan, Datam);
printf("/n");
if (*Datan < 0 || *Datam < 0)
{
printf("You have inputted unlawful number, again!/n");
continue;
}else
{
break;
}
}
return 1;
}
STATUS GCDofTwoNum(int Datan, int Datam, int *gcd)
{
if (gcd == NULL)
{
return 0;
}
if (Datam > Datan)
{
int temp = Datam;
Datam = Datan;
Datan = temp;
}else if (Datan == Datam)
{
*gcd = Datam;
return 1;
}
while (1)//循环体中n>m
{
if (Datam == 0)
{
*gcd = Datan;
return 1;
}
int ret = Datan%Datam;
Datan = Datam;
Datam = ret;
}
return 1;
}
STATUS LCMofTwoNum(int Datan, int Datam, int gcd, int *lcm)
{
if (lcm == NULL)
{
return 0;
}
*lcm = (int)(Datan*Datam)/gcd;
return 1;
}
STATUS OutputData(int gcd, int lcm)
{
printf("最大公约数=%d/n", gcd);
printf("最小公倍数=%d/n", lcm);
return 1;
}