⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 factorial.cpp

📁 计算任意整数的节乘的精确数值,计算速度比较快
💻 CPP
字号:
// factorial.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream.h"
#include <fstream.h>
//#include "stdlib.h"
class Node {   //here is a dual link list
private:
 int data;
 Node *next, *prev;
public:
 Node(const int& item) :data(item),prev(NULL),next(NULL){ };
 Node* GetNextNode( )  {return next;};  //get next node
 Node* GetPrevNode( )  {return prev;};
 void InsertAfterMe(Node* p);   //insert after
  int GetData(void)   {return data;};
 void SetData(int item)   {data = item;};
 Node* GoBacktoHead( );      //reset
 Node* GoForwardtoRear( );   //go to the rear
 void ClearAll(void);     //clear the whole
 int GetElementNum( );   //get the counts of the link
  int numzero( );
  void save();
};
int Node::GetElementNum( ){          //------------------
 int count = 0;
 Node* p =GoBacktoHead( );
 while(p->GetNextNode( )!=NULL){   count++;  p = p->GetNextNode( ); }
 count++;   return count;
}
int Node::numzero( )  {
int count = 0;
Node* p =GoBacktoHead( );
 while(p->GetNextNode( )!=NULL){   
	 if(p->GetData()==0) {count++;  p = p->GetNextNode( ); }
	else return count;}
return  count;
}

void Node ::save()
{
	ofstream ofile("d:\\fractorial.txt");
	if(!ofile)
	{
		cout<<"打开文件失败!!!"<<endl;
		return ;}
  int i=0;

 Node* p =GoForwardtoRear( );
 while(p->GetPrevNode( )!=NULL){ 
	 ofile<<p->GetData();  p=p->GetPrevNode(); 
  i++;
  if(i==100){i=0;ofile<<endl;}
 }
 ofile<<p->GetData( )<<endl; 
   
 ofile<<endl;
 ofile.close( );
}

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

Node* Node::GoBacktoHead( )  {             //-------------------------
if(this->prev == NULL){   return this; }  //this is the first node
 Node *p = this;
 while(p->prev != NULL){  p = p->prev; }
 return p;
}
Node* Node::GoForwardtoRear( ) {          //--------------------------
 if(this->next == NULL){  return this;  }
 Node *p = this;
 while(p->next != NULL){  p = p->next; }
 return p;
}
void Node::ClearAll(void)  {            //----------------------------
 Node* p = GoBacktoHead( );
 Node* p2;
 while(p->GetNextNode( ) != NULL){  p2 = p;  p = p->GetNextNode( ); delete p2; }
 delete p;
}
void factorial(int N)
{
 int temp=0;
 const int B=10000;
 int remain, carry, result;
 Node* p = new Node(1);
  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>=B)
   {  remain = result%B;    carry = result/B;    p->SetData(remain);  }
   else   { p->SetData(result); }
  p = p->GetNextNode( );  carry = result/B;  }
  result = p->GetData( )*n+carry;
  //if carry occurs,process the carry and store into the newly allocated space.
  while(result >= B)
  {
   Node * newNode = new Node(0);   p->SetData(result%B);//remainder
   result = result/B;   p->InsertAfterMe(newNode);
   p = p->GetNextNode( );
  }
  p->SetData(result);
 }//end of if
 p = p->GoForwardtoRear( );
 while(p->GetPrevNode( )!=NULL){  
	 
	 temp=p->GetData(); 
	      if(temp<10) cout<<"000";
	 else if(temp<100) cout<<"00";
	 else if(temp<1000) cout<<"0";
	 cout<<temp<<" "; 
	 p=p->GetPrevNode(); }
 // cout<<p->GetData( )<<endl; 
       temp=p->GetData(); 
	      if(temp<10) cout<<"000";
	 else if(temp<100) cout<<"00";
	 else if(temp<1000) cout<<"0";
	 cout<<temp<<endl; 

 int numzero=p->numzero();
 int num = p->GetElementNum();cout<<num<<"     "<<"末尾有"<<numzero<<"个零"<<endl;
 p->save();
  if(num >=12){
  p = p->GoForwardtoRear();
  cout<<p->GetData( )<<".";  p = p->GetPrevNode( );
  for(int i=1;i<10;i++){   cout<<p->GetData( );   p = p->GetPrevNode( );  }
  cout<<"E"<<num-1<<endl; 
 }
 
  p->ClearAll();    //clear the memory
}
void main( )  {   //----------------------------
int N;	
cout<<"Please input the number:";  
cin>>N;
factorial(N);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -