PDA

View Full Version : محاتسبه فاکتوریل



fatimah
شنبه 28 آبان 1384, 07:38 صبح
با سلام . مدتی است که به دنبال نوشتن این برنامه هستم و متاسفانه هنوز نتوانستم بنویسم . خواهشمندم اگر کسی می تونه کمکم کنه .
برنامه ای که با گرفتن X ( بزرگتر از 500) مقدار !X را محاسبه کنه . خواهشن اگه کسی می تونه کمک کنه چون بد جوری کارم گیره . مرسی بای

اَرژنگ
شنبه 28 آبان 1384, 08:14 صبح
استاد، شما شوخی مکنید؟
فاکتوریل اعداد از ۵۰۰ بزرگتر حداقل ۱۰ به توانه ۸۹۰ تا رقم داره!
شما فاکتوریل دقیق را میخواهید یا یک جوابه تقریبی؟
اگر دقیقاَ را میخواهید لطف کنید بگید برایه چه کاری میخواهید استفاده کنید شاید روشی دیگه‌ای را بتونیم پیشنهاد کنیم.
با احترام

scpanc
شنبه 28 آبان 1384, 09:29 صبح
سلام
وقتی از حلقه ها استفاده می کنین زمان اجرا به حد مرگ زیاد میشه. خصوصا در مورد مسایلی مثل فاکتوریل. راه حل این مسائل استفاده از روال بازگشتی است اگر با ان مفهوم آشنایی نداری به یکی از مراجع زیر رجوع کن:
1- ساختمان داده ها لیپس شوتز
2- برنامه نویسی C++ - دیتل و دیتل
3-Thinking in C++ Bruce eckle
موفق باشی

اَرژنگ
شنبه 28 آبان 1384, 09:35 صبح
#include "stdafx.h"

#include "iostream.h"
#include "stdlib.h"


//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(void);

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

//reset
Node* GoBacktoHead();

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

//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;}
};


void Node::DeleteMe(void)
{
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
return;
}

prev->next = next;
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(void)
{
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);

cout<<"Pls input the number:";
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);}

p = p->GetNextNode();
carry = result/10;
}

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

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

while(result >= 10){
Node * newNode = new Node(0);
p->SetData(result%10);//remainder
result = result/10;
p->InsertAfterMe(newNode);
p = p->GetNextNode();
}

p->SetData(result);

}//end of if

p = p->GoForwardtoRear();

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

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

cout<<endl<<"Or"<<endl<<endl;

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

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

cout<<"E"<<num-1<<endl;
}

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

return 0;
}

fatimah
دوشنبه 30 آبان 1384, 07:42 صبح
آقای ارژنگ سلام . از اینکه جواب منو دادین خیلی خیلی ممنون هستم ، ولی من می خوام این برنامه رو با استفاده از آرایه دو بعدی بنویسم . اگه فقط بگین که آرایه هاش رو چه طوری بنویسم ممنون می شم . موفق باشید .

taimaz
سه شنبه 08 آذر 1384, 19:53 عصر
سلام عزیز جان
اگه بخواید برام ایمیل بزنید حتماً براتون میفرستم.

e_Mail : taimazus@yahoo.com