📄 nnn.cpp
字号:
//计算任意大小阶乘
//于鑫 2004.7 Visual C++ 6.0 & Windows XP环境调试通过//
//-------------------------------------------------------------
#include "iostream.h"
#include "fstream.h"
#include "stdlib.h"
//-------------------------------------------------------------
//定义一个链表
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){};
//获得下一个节点
Node* GetNextNode(){return next;};
//获得上一个节点
Node* GetPrevNode(){return prev;};
//插入
void InsertAfterMe(Node* p);
//删除所有指定
void DeleteMe(void);
int GetData(void){return data;};
void SetData(int item){data = item;};
//重置
Node* GoBacktoHead();
//跳到后面
Node* GoForwardtoRear();
//全不清除
void ClearAll(void);
//获得链的计数
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;
}
};
void Node::DeleteMe(void)
{
if(prev == NULL) // 如果是第一个节点
{
next->prev = NULL;
head = next; // 那么下一个节点变为第一个
delete this; //删除此节点
return;
}
if(next == NULL)//如果是最后一个节点
{
prev->next = NULL;
rear = prev; // 那么上一个节点变为最后一个
return;
}
prev->next = next;
delete this;
};
Node* Node::GoBacktoHead()
{
if(head == this)//如果是第一个节点
{
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(void)
{
Node* p = GoBacktoHead();
Node* p2;
while(p->GetNextNode() != NULL)
{
p2 = p;
p = p->GetNextNode();
delete p2;
}
delete p;
};
//--------------------------------------------------
//主程序开始
int main(void)
{
Codebegin:
int remain;//余数
int carry;//进位
int result;//结果
int N;//输入的待机算数
char filename[20];//保存结果的文件名
Node* p = new Node(1);
cout<<"请输入一个任意大小的非负整数:";
cin>>N;
cout<<"请输入存储计算结果的文件名:";
cin>>filename;
cout<<"正在计算"<<N<<"的阶乘,请稍等...";
ofstream fout (filename);
for(int n=1;n<=N;n++)
{
cout<<".";
remain = carry = 0;
p = p->GoBacktoHead();
//一个一个处理元素,直到链尾
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);
}
p = p->GetNextNode();
carry = result/10;
}
result = p->GetData()*n+carry;
//如果产生进位,处理进位并存入新分配的空间
while(result >= 10)
{
Node * newNode = new Node(0);
p->SetData(result%10);//余数
result = result/10;
p->InsertAfterMe(newNode);
p = p->GetNextNode();
}
p->SetData(result);
}
p = p->GoForwardtoRear();
fout<<"本软件由于鑫于2004年7月制作,用于计算任意大小阶乘.\n\n";
fout<<"--------------------------------------------------------------------\n";
fout<<"\n您要计算"<<N<<"的阶乘(注意'==>'符号后的部分为计算结果的科学技术形式).\n\n结果如下:";
fout<<"\n\n"<<N<<"!=";
cout<<"\n\n"<<N<<"!=";
while(p->GetPrevNode()!=NULL)
{
fout<<p->GetData();
cout<<p->GetData();
p=p->GetPrevNode();
}
fout<<p->GetData();//输出结果写入文件
cout<<p->GetData();//输出结果
int num;
num=p->GetElementNum();
if(num>=5)
{
p = p->GoForwardtoRear();
fout<<"==>"<<p->GetData()<<".";
cout<<"==>"<<p->GetData()<<".";
p = p->GetPrevNode();
for(int i=1;i<5;i++)
{
fout<<p->GetData();
cout<<p->GetData();
p = p->GetPrevNode();
}
fout<<"E"<<num-1<<"\n";//科学技术形式写入文件
cout<<"E"<<num-1<<"\n";//输出结果的科学技术形式
}
fout.close();//关闭文件
//释放内存
p->ClearAll();
char ch;
cout<<"是否继续?(Y=是/N=否):";
cin>>ch;
if (ch=='Y'||ch=='y') goto Codebegin;
return 0;
}
//程序结束
//------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -