الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

جنون ال e

مغلق
بدأه hanymedhat2005 في 20 أغسطس 2005 · 3 رد · 574 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم.

كنت باحل الاسئلة اللى فى كتاب ال C++ how to program.

كان فى سؤال فى باب ال Control structure عن حساب قيمة الثابت الرياضى e من المتسلسلة الشهيرة :

e = 1 + (1/1! ) + (2/2!) + (3/3!) + ............................... etc

عن طريق ال while loop

هذا هوالكود اللى كتبته

 

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int Fac(int);
void e();

int main()
{
	e();
	return 0;

}

int Fac(int x)
{
	int y = 1;
	while(x>1)
	{
  y *= x;
  x--;
	}
	return y;
}

void e()
{
	int counter = 1;
	double e = 1.0;

	while( counter > 0 )
	{
  int y;
  y = Fac(counter);
  e +=  static_cast<double>(1) / y;
  
  counter++;
  cout << e<< endl; 
	}
}

وهذه هى المخرجات

ايه الحاجات العجيبة دى!!!!!!!!!

post-44491-1124543250_thumb.jpg

لو كانت الرجولة بالشوارب لكان الصرصار أكثرنا رجولة.

#2

INF يعني انك تعمل قسمة على صفر

كما ان الـ cout << e<< endl; موضوعة داخل الـ loop لذلك ستخرج باستمرار

#3

أعتقد هوة دة اللى انت عاوزة دة كود كنت كاتبة بالجافا ولكن عملت تعديلات بسيطة علشان تقدر تفهمة

import javax.swing.JOptionPane;


	void main()
	{
  double re=0;
  double s;
  for(int i=1;i<=3;i++)
  {
        s=vac(i);	
    re+=1/s;
        }
        //System.out.println("the result is:"+re);
         cout<<"the result is: "<<re;
    }
    double vac(double i)
    {
   	 double sum=1;
   	 for(double j=i;j>=1;j--)
   	 {
   	 sum*=j;
   	 //--i;
         }
         return sum;
    
}

أو

//import javax.swing.JOptionPane;
#include <iostream>
#include <conio>
#include <math>
double vac(double i);
void main()
{

	int x;
	cout<<"enter the val:\t";
  cin>>x;
   double re=0;
   double s;
   for(int i=1;i<=3;i++)
   {
     s=vac(i);
       re+=pow(x,i)/s;
   }
    cout<<"the result is: "<<re;
   getch();
}
double vac(double i)
{
  double sum=1;
  for(double j=i;j>=1;j--)
    {
   	 sum*=j;

    }
    return sum;
    }
//

Muhammad Allam

Computer Science

@Resource(MappedURL="My Blog" )

#4

السلام عليكم

الحمد لله لقيت الغلط

 

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

double Fac(int);
void e();

int main()
{
e();
return 0;

}

double Fac(int x)
{
double y = 1;
while(x>1)
{
 y *= x;
 x--;
}
return y;
}

void e()
{
int counter = 1;
double e = 1.0;

while( counter > 0 )
{
 double y;
 y = Fac(counter);
 e +=  1/ y;

 counter++;
 cout << e<< endl;
}
}

الدالة Fac كانت من نوع int غيرتها لDouble و لغيت ال Static_cast و الكود السابق نجح.

------------------------------------------------

------------------------------------------------

كود الباشمهندس علام أدى العمل على أكمل وجه.

شكراً جزيلاً.

لو كانت الرجولة بالشوارب لكان الصرصار أكثرنا رجولة.

هذا الموضوع مغلق.

مواضيع مشابهة