亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這是一個用單鏈表寫的大數階乘,求各位幫忙看一下這個代碼哪里不對,為什么輸不出結果

這是一個用單鏈表寫的大數階乘,求各位幫忙看一下這個代碼哪里不對,為什么輸不出結果

慕圣8344465 2018-09-27 22:34:20
#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;}
查看完整描述

2 回答

  • 2 回答
  • 1 關注
  • 1099 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號