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

مساعده في حل برنامج بلغة ال C

بدأه haia في 1 أبريل 2009 · 6 رد · 1,254 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكــم ورحمـة الله وبركاتــه ،،

اخواني .. عندي برنامجين .. بلغه الـ C ..

حاولت حلهم بنفسي .. ولكن لعدم توفر البرنامج لدي .. احببت ان تساعدوني في حلها .. الله يوفقكم ..

عندي امتحان فيه .. وهذي نماذج من أسئلة ..

Question 1: )

The equation of a line is given by y=mx+c where m is the slope and c is the intercept.

If given 2 points of the line, then m is given by

m=(y2-y1)/(x2-x1)

The intercept c represents the intersection of the line with y axis i.e. whose x=0.

The particular cases are:

x=x1 (or x2) in case x1=x2

y=y1 (or y2) in case y1=y2

in case the line passes by the origin it should be displayed as y=mx.

In case y1=y2 and x1=x2, the points are identical and they cannot form a line.

You program will prompt the user for the coordinates of 2 points P1(x1,y1) and P2(x2,y2) and displays the equation of the line according to the cases cited above.

Write a function to display 2 or 3 lines at the top (once your program is executed) indicating what your program is doing.

The following sample runs highlights the different cases. I am not displaying what the program is doing in my sample runs but you should do that in your homework.

You should separate between c>0 case and c<0 case i.e. the equation should not appear as for example y=0.55x+-6.78. It should appear in this case as y=0.55x-6.78

picture103a34e.png

****

Write a program that reads the coefficients a, b, and c of a quadratic equation and displays the roots of this equation.

The discriminant of the equation is given by

The equation will have:

2 distinct roots if ∆>0

1 repeated root if ∆=0

2 distinct complex roots if ∆<0

picture1185998.png

تم تعديل هذه المشاركة بواسطة haia في 1 أبريل 2009 في 22:01

#2

السلام عليكــم ورحمـة الله وبركاتــه ،،

يمكن حل السؤال الأول كالآتى


#include <conio.h>
int main()
{
float x1,y1,x2,y2;
float m,c;
printf("Enter x,y coordinates of point 1> ");
scanf("%f %f",&x1,&y1);
printf("Enter x,y coordinates of point 2> ");
scanf("%f %f",&x2,&y2);
if(x1==x2 && y1==y2)
printf("The 2 points are identical \n they cannot form line \n");
else if(x1==x2)
printf("The equation is X=%.2f",x1);
else if(y1==y2)
printf("The equation is Y=%.2f",y1);

else
{
m=(y2-y1)/(x2-x1);
c=y1-m* x1;
printf("the equation of two points is Y= %.2f X%c%.2f",m,(c>0?'+':' '),c);
}
getch();
return 0;
}
#include <stdio.h>

والثانى نريد ان نرى محاولاتك

؟

اللهم صل على سيدنا محمد وعلى آله وصحبه وسلم

#3

السلام عليكم . . هذي محاولتي لـ حل البرنامج الثاني . .

اعرف ان هناك بعض الاخطاء . . لكني لم استطع تفاديها ! . . مثل (( عدم جمع الطرف الايمن مع الطرف الايسر من معادلة الجذر التخيلي )) . .

كذلك وجود خطأ بـ البرنامج عند كل كلمة (( else )) . . ولم اعرف كيف حلها ! . .

هذا هو برنامجي . .

#include <stdio.h>
#include <math.h>

int main (void)
{
  double a,b,c,x,x1,x2,x3,x4,x5;
  printf ("Enter a,b,c of the quadratic eq. >");
  scanf("%lf%lf%lf",&a,&b,&c);
  x = b*b-4*a*c;
	   if (x>0)
	   x1 = (-b+sqrt(x))/(2*a);
	   x2 = (-b-sqrt(x))/(2*a);
	   printf("the quadratic eq. has 2 distinct roots\n root1=%f\troot=%f",x1,x2); 
			else if (x==0)
			x3 = -b/(2*a);
		printf("the quadratic eq. has double roots\nroot1=root2=%f",x3); 
			   else if (x<0)
			   x4 = (-b/(2*a)) + sqrt(-x)/(2*a);
			   x5 = (-b/(2*a)) - sqrt(-x)/(2*a);
		   printf("the quadratic eq. has 2 imaginary roots\n root1=%fi\troot=%fi",x1,x2);
				 else printf("");	  

return 0;
}

تم تعديل هذه المشاركة بواسطة Xacker في 3 أبريل 2009 في 18:22

#4
#include <stdio.h>
#include <math.h>

int main (void)
{
  double a,b,c,x,x1,x2,x3,x4,x5;
  printf ("Enter a,b,c of the quadratic eq. >");
  scanf_s("%lf%lf%lf",&a,&b,&c);
  x = b*b-4*a*c;
  if (x>0) {
	x1 = (-b+sqrt(x))/(2*a);
	x2 = (-b-sqrt(x))/(2*a);
	printf("the quadratic eq. has 2 distinct roots\n root1=%f\troot=%f",x1,x2);
  } else if (x==0) {
		x3 = -b/(2*a);
		printf("the quadratic eq. has double roots\nroot1=root2=%f",x3);
  } else if (x<0) {
		x4 = (-b/(2*a)) + sqrt(-x)/(2*a);
		x5 = (-b/(2*a)) - sqrt(-x)/(2*a);
		printf("the quadratic eq. has 2 imaginary roots\n root1=%fi\troot=%fi",x4,x5);
  } else
	  printf("");
return 0;
}

استبدلت scanf بـ scanf_s وهو ليس خطأ بكل الأحوال لكن الأفضل الاعتياد على الثانية لأنها أءمن.

بخصوص else if عليك باستخدام الأقواس { } عندما تكون هناك كتلة شرطية تريدين تنفيذها مع IF، عند عدم استخدام الأقواس الكومبايلر يعتبر أن أول سطر يلي جملة IF هي الكتلة كاملة وكل ما يليها هو كود خارجها، بالتالي عندما يصل إلى ELSE IF لا يجد IF مناسبة تتبع لها ويظهر خطأ.

الشرح سئ؟ نعم، هذا مثال

if (condition1)
	// one line statement;
else if (condition2)
	// one line statement;
else
	// one line statement;

if (condition1) {						  /* هنا أستخدم الأقواس لأن الجملة الشرطية تقتضي تنفيذ ثلاث أسطر لو تحققت */
	// first statement line;
	// second statement line;
	// third statement line;
} else if (condition2)				/* هنا لا أحتاج إلى أقواس لوجود سطر واحد فقط للتنفيذ */
	// single statement line;
else {										  /* أستخدم الأقواس مرة أخرى لوجود أكثر من سطر */
	// a statement line;
	// another statement line;
}

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#5

وعليكــم السـلام ورحمة الله وبركاتـه . . يعطيك العافية ياخوي على هذا الحل الرائع . .

بس حبيت استفسر عن المكتبة ][ كونيو ][ . . لماذا استخدمتها في البرنامج !؟ . .

لأننا حتى الان لم ندرس هذه المكتبه . . واخاف الدكتور يقول لي كيف وصلت لهذي المكتبة ! . . او يرفض اجابتي على الواجب ! . .

فـ ياليت لو توضح لي ماهي المكتبة . . وما الاوامر اللي فيها . .

وكذلك . . ياليت توضح لي ما معنى هذا الامر ][ c>0?'+':' ' ][ !؟ . .

وجزاك الله الف خير . .

#6
Xacker كتب:
#include <stdio.h>
#include <math.h>

int main (void)
{
  double a,b,c,x,x1,x2,x3,x4,x5;
  printf ("Enter a,b,c of the quadratic eq. >");
  scanf_s("%lf%lf%lf",&a,&b,&c);
  x = b*b-4*a*c;
  if (x>0) {
	x1 = (-b+sqrt(x))/(2*a);
	x2 = (-b-sqrt(x))/(2*a);
	printf("the quadratic eq. has 2 distinct roots\n root1=%f\troot=%f",x1,x2);
  } else if (x==0) {
		x3 = -b/(2*a);
		printf("the quadratic eq. has double roots\nroot1=root2=%f",x3);
  } else if (x<0) {
		x4 = (-b/(2*a)) + sqrt(-x)/(2*a);
		x5 = (-b/(2*a)) - sqrt(-x)/(2*a);
		printf("the quadratic eq. has 2 imaginary roots\n root1=%fi\troot=%fi",x4,x5);
  } else
	  printf("");
return 0;
}

جزاك الله الف خير . . بـ صراحة قدّمت اضافة كبيرة لي . . وشرح مميز . .

وفي المرات الجاية بـ اكون مركّزه على الاقواس اكثر . .

هذي مشكلة وحليناها . . باقي المشكلة الثانية . . اللي هي في السطر المبين بـ الاحمر . .

فـ السؤال طلب مننا ان نضع الجذر التخيلي للعدد . . وهو يكون بـ هذه الطريقة ][ a + bi ][ . .

اما في البرنامج اللي وضعته فـ هو يجمع ][ a + b ][ بعدين يضع جنبها i . .

وآسفه اذا عطلتكم معي . .

#7

السلام عليكــم ورحمـة الله وبركاتــه ،،

بالنسبه للمكتبه conio.h فهى موجوده لأنى استخدمت

الداله getch()

وفايدتها تثبيت شاشة الكونسول

ويمكن الغائها

بالنسبه للمعامل

؟ :

فهو يشبه if else واستخدمته للتحكم فى خرج الإشاره

وبالنسبه لمشكلة الأعداد المركبه فى برنامجك فهذا هو الحل

بعد اذن أخونا الكريمXacker


#include <math.h>

int main (void)
{
double a,b,c,x,x1,x2,x3,x4r,x5r,x4i,x5i;
printf ("Enter a,b,c of the quadratic eq. >");
scanf("%lf%lf%lf",&a,&b,&c);
x = b*b-4*a*c;
if (x>0) {
x1 = (-b+sqrt(x))/(2*a);
x2 = (-b-sqrt(x))/(2*a);
printf("the quadratic eq. has 2 distinct roots\n root1=%f\troot=%f",x1,x2);
} else if (x==0) {
x3 = -b/(2*a);
printf("the quadratic eq. has double roots\nroot1=root2=%f",x3);
} else if (x<0) {
x4r = (-b/(2*a));
x4i= sqrt(-x)/(2*a);
x5r = (-b/(2*a));
x5i= -sqrt(-x)/(2*a);
printf("the quadratic eq. has 2 complex roots\n root1=%.2f%c%.2fi ",x4r,(x4i>0?'+':' '),x4i);
printf("\nroot2=%.2f%c%.2fi\n",x5r,(x5i>0?'+':' '),x5i);
} else
printf("");
return 0;
}
#include <stdio.h>

اللهم صل على سيدنا محمد وعلى آله وصحبه وسلم

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