这是一个用单链表写的大数阶乘,求各位帮忙看一下这个代码哪里不对,为什么输不出结果

#include "stdafx.h"#include<iostream>
using namespace std;
template<class T>class Link;//链表类 
template<class T>class LinkNode
{	
friend Link<T>;
private:	
T data;	
LinkNode<T>*link;};
template<class T>class Link
{public:
Link()	
{		
first = 0;	
};
~Link();	  
bool IsEmpty()const	  
{      
return first = 0;	   
}	
int Length()const;	
bool Find(int k, T&x);	
Link<T>&Insert(int k, const T&x);	
Link<T>&Change(int k, T x);	
Link<T>&Delete(int k, T &x);	
Link<T>&Search(const T&x)const;	
int OutPut();private:	
LinkNode<T>*first;};//析构函数(删除链表的所有节点)
template<class T>Link<T>::~Link()
{	
LinkNode<T>*next;	
while (first)	
{		
next = first->link;		
delete first;		
first = next;	
}}//确定链表的长度
template<class T>int Link<T>::Length()const
{	
LinkNode<T>*current = first;	
int len = 0;	
while (current)	
{		
len++;		
current = current->link;	
}	
return len;}//在链表中查找第K个元素
template<class T>bool Link<T>::Find(int k, T &x)
{	
LinkNode<T>*current = first;	
int index = 0;	
while (index < k&&current)	
{		
current = current->link;		
index++;	
}	
if (current)	
{	
int x = current->data;	return true;    
}	
return false;}//向链表中插入元素
template<class T>Link<T>&Link<T>::Insert(int k, const T&x)
{	
LinkNode<T>*p = first;	
for (int index = 1; index < k&&p; index++)		
p = p->link;	
LinkNode<T>*y = new LinkNode<T>;	
y->data = x;	
if (k)	
{		
y->link = p->link;		
p->link = y;	}	
else 	
{		
y->link = first;		
first = y;	}	
return *this;}	//改变链表第k个元素的值    
template<class T>	
Link<T>&Link<T>::Change(int k, T x)	
{		
LinkNode<T>*p = first;		
for (int index = 0; p&&index < k; index++) 		
{			
p = p->link;		
}		
if (p)			
p->data = x;		
return *this;	}	
//删除链表第k个元素	
template<class T>	
Link<T>&Link<T>::Delete(int k, T&x)	
{		
if (k = 0)		
{			
first = first->link;		
}		
else			
LinkNode<T>*p= first;		
LinkNode<T>*q = first;		
for (int index = 1; index < k - 1 && q; index++)		
{			
q = q->link;			
p = q->link;			
q->link = p->link;			
x = p->data;			
delete p;			
return *this;		
}	}	
//搜索第k个元素	
template<class T>	
Link<T>&Link<T>::Search(const T&x)const
{		
LinkNode<T>*current = first;		
int index = 1;		
while (current&&current->data != x)		
{			
current = current->link;			
index++;		}		
if (current)			
return index;					
return 0;    }	
//倒序输出链表	
template<class T>	
int Link<T>::OutPut()	
{		
LinkNode<T>*current = first;		
int index = 0;		
int len = 0;		
while (current)		
{			
len++;			
current = current->link;		
}	
int *arry = new int[len];	
current = first;	
while (current)	
{		
arry[index] = current->data;		
current = current->link;		
index++;}	
index = index - 1;	
cout << arry[index];	
index = index - 1;	
for (index; index >= 0; index--)	
{		
cout.fill('0');		
cout.width(3);		
cout << arry[index];	}	
cout << endl;	return 0;}
int main(){	
Link<int>A;		
int n, i, j, k;	int l = 0;	
A.Insert(0, 1);	
cout << "输入n=:" << endl;	
cin >> n;		
for (i = 1; i <= n; i++)		
{			
int m = A.Length();			
for (j = 0; j <m;j++)			
{				
A.Find(j, k);				
k = i * k;				
A.Change(j, k);			
}			
for (j = 0; j < m; j++)			
{				
A.Find(j, k);				
if (k >= 1000)				
{					
if (j < m - 1)						
A.Find(j + 1, l);					
else					
{						
A.Insert(j + 1, 0);						
l = 0;					
}					
l += k / 1000;					
A.Change(j + 1, l);					
k = k % 1000;					
A.Change(j, k);				
}			
}		
}	
cout << "Length=" << A.Length() << endl;	
cout << "阶乘为:";	
A.OutPut();   
 return 0;}


慕圣8344465
浏览 1041回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP