菜单

几乎任意大小的阶乘算法

2019年8月2日 - 4166am金沙下载
//
//
// Nnn.cpp : Defines the entry point for the console application.
// author  : Hai Yi
// review : V. Cortes / Feb,23,2012
// date    : Sept,11,2000
//
//

#include <iostream>
#include <stdlib.h>
using namespace std;

//here is a dual link list
class Node{

private:
 int data;
 Node *next;
 Node *prev;
 Node *head;
 Node *rear;

public:
 Node(const int& item)
     :data(item),prev(NULL),next(NULL),head(NULL),rear(NULL){}

 //get next node
 Node* GetNextNode(){return next;}
 Node* GetPrevNode(){return prev;}

 //insert after
 void InsertAfterMe(Node* p);

 //Delete the appointed
 void DeleteMe();

 int GetData(){return data;}
 void SetData(int item){data = item;}

 //reset
 Node* GoBacktoHead();

 //go to the rear
 Node* GoForwardtoRear();
 //clear the whole
 void ClearAll();

 //get the counts of the link
 int GetElementNum();
};

int Node::GetElementNum()
{
 int count = 0;
 Node* p =GoBacktoHead();

 while(p->GetNextNode()!=NULL){
  count++;
  p = p->GetNextNode();
 }

 count++;
 return count;
}

void Node::InsertAfterMe(Node* p)
{
 // Node* p;
 if(prev == NULL) { head = this;}
 p->next = next;
 p->prev = this;

 next = p;
 if(p->next == NULL) rear = p;
 else p->next->prev = p;
}

void Node::DeleteMe()
{
 if(prev == NULL) { // if this node is the first one
  next->prev = NULL;
  head = next;  // then the next one becomes the first one
  delete this;  //delete this node
  return;
 }

 if(next == NULL){  //if this node is the last one
  prev->next = NULL;
  rear = prev; // then the previous one becomes the last one
  delete this;
  return;
 }

 prev->next = next;
 next->prev = prev;
 delete this;
}

Node* Node::GoBacktoHead()
{
 if(head == this){ //this is the first node
  return this;
 }

 Node *p = this;
 while(p->prev != NULL){
  p = p->prev;
 }

 return p;
}

Node* Node::GoForwardtoRear()
{
 if(rear == this){
  return this;
 }

 Node *p = this;
 while(p->next != NULL){
  p = p->next;
 }

 return p;
}

void Node::ClearAll()
{
 Node* p = GoBacktoHead();
 Node* p2;
 while(p->GetNextNode() != NULL){
  p2 = p;
  p = p->GetNextNode();
  delete p2;
 }

 delete p;
}

int main(int argc, char* argv[])
{
 int remain;
 int carry;
 int result;
 int N;
 Node* p = new Node(1);

 std::cout<<"Input the number: ";
 std::cin>>N;
 for(int n=1;n<=N;n++)
 {
  remain = carry = 0;
  p = p->GoBacktoHead();

  //while not the end of the list,process the element one by one
  while(p->GetNextNode() != NULL){
   result = p->GetData()*n+carry;
   if(result>=10){
    remain = result%10;
    carry = result/10;
    p->SetData(remain);
   }
   else{
       p->SetData(result);
       carry = result/10;
   }

  p = p->GetNextNode();
  }

  result = p->GetData()*n+carry;

  //if carry occurs,process the carry and
  //**store into the newly allocated space.**

  while(result >= 10){
   p->SetData(result%10);//remainder
   Node * newNode = new Node(0); //create new node with data 0
   result = result/10; //cut last digit
   p->InsertAfterMe(newNode); //put new node in the rear
   p = p->GetNextNode(); //advance to this new node
  }

  p->SetData(result); //when result < 10, put result in this new node

 }//end of for

 p = p->GoForwardtoRear();

 while(p->GetPrevNode()!=NULL){
  std::cout<<p->GetData();
  p=p->GetPrevNode();
 }

 std::cout<<p->GetData()<<std::endl;
 int num = p->GetElementNum();
 if(num >=5){
  p = p->GoForwardtoRear();

  std::cout<<std::endl<<"Or"<<std::endl<<std::endl;

  std::cout<<p->GetData()<<".";
  p = p->GetPrevNode();

  for(int i=1;i<10;i++){
   std::cout<<p->GetData();
   p = p->GetPrevNode();
  }

  std::cout<<"e"<<num-1<<std::endl;
 }

 //clear the memory
 p->ClearAll();

 return 0;
}

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图