C++:最小公倍数&最大公约数&润年判别

来源:互联网 发布:安卓系统源码下载 编辑:程序博客网 时间:2024/05/16 14:28

/**

*    头文件

**/

/*
* 润年判别
*/
#ifndef __MYYEAR__
#define __MYYEAR__

class myYear{
 public:
  myYear(const int *i = NULL, const int *j = NULL);
  ~myYear(void);
  
  void init(int *i, int *j);
  void *isYear(int *i);
  void getResult(int *i, int *j);
  void resetData(int *i, int *j);
 
 private:
  int *m;
  int *n;
};

#endif

 

/**

*   源文件

**/

 

#include "iostream"
#include "conio.h"
#include "c++2.h"

using namespace std;

myYear::myYear(const int *i, const int *j) {
 if (NULL == i || NULL == j) {
  m = new int[1];
  n = new int[1];
  if (NULL == m || NULL == n) {
   cout << "Memorry fails!" << endl;
   exit(1);
  }
  
  *m = '\0';
  *n = '\0';
 }
 else {
  m = new int[sizeof(int) + 1];
  n = new int[sizeof(int) + 1];
  if (NULL == m || NULL == n) {
   cout << "Memorry fails!" << endl;
   exit(1);
  }
  
  *m = *i;
  *n = *j;
 }
}

myYear::~myYear(void) {
 delete []m;
 delete []n;
}

void *myYear::isYear(int *i) {
 if (NULL == i) {
  return NULL;
 }
 
 if (((*i%4 == 0) && (*i%100 != 0)) || (*i%400 == 0)) {
  cout << "LOG:myYear::isYear: " << *i << " years is runnian!" << endl;
 }
 else {
  cout << "LOG:myYear::isYear: " << *i << " years is't runnian!" << endl;
 }
 
 return m;
}

void myYear::getResult(int *i, int *j) {
 if((0 == *n) || NULL == i || NULL == j){
  cout << "Bei chu shu can't zero!" << endl;
  return ;
 }
 int k = 0;
 while (*m%*n != 0){
  k = *m%*n;
  *m = *n;
  *n = k;
 }

 int minNumber = (*i * *j)/(*n);

 cout << "The Max gong yue shu: " << *n << endl;
 cout << "The Min gong bei shu: " << minNumber << endl;


}

void myYear::init(int *i, int *j) {
 if (NULL == i || NULL == j) {
  return;
 }

 m = new int[sizeof(int) + 1];
 n = new int[sizeof(int) + 1];
 if (NULL == m || NULL == n) {
  cout << "Memorry fails!" << endl;
  exit(1);
 }

 *m = *i;
 *n = *j;
}

void myYear::resetData(int *i, int *j) {
 *m = *i;
 *n = *j;
}

int main(int argc, char *argv[]){
 int n;
 int m;
 char ch;
 bool flag = false;
 
//  cout << "Please input one year:";
//  cin >> n;
//  cout << "You enter number is: " << n << endl;
 
 myYear *pYear = new myYear();
 if (NULL == pYear) {
  cout << "Class object fails!" << endl;
  return 0;
 }

 do{
  cout << "Please input two numbers:" << endl;
  cin >> n;
  cin >> m;
  cout << "You input two numbers are: " << n << "," << m << endl;

  if(!flag){
   pYear->init(&n, &m);
   flag = true;
  }else {
   pYear->resetData(&n, &m);
  }
  pYear->getResult(&n, &m);

  fflush(stdin);
  cout << "Enter [q] will exit! Other char will go on!" << "qiut or continu?" ;
 }while((ch = getchar()) != 'q');
 
//  int *num = (int *)pYear->isYear(&n);
//  cout << "LOG:main::main: " << *num << " years is runnian!" << endl;
//  cout << "LOG:main::main: " << *num << " years is't runnian!" << endl;

 delete pYear;
 pYear = NULL;
 
/* getch();*/
 return 1;
}

原创粉丝点击