حل معادلة من الدرجة الثانية
أكتب برنامجاً بلغة ج لحل معادلة من الدرجة الثانية بمجهول واحد (س)، واقتصر في الحل على مجال الأعداد الحقيقية. علماً أن للمعادلة الشكل العام :
آ س2 + ب س + ج = 0
الحل
سنكتب برنامجاً يقرأ أمثال س: آ و ب و ج ثم يكتب المعادلة كاملة في لوحة الكتابة. بعد ذلك يحسب البرنامج المميز (م) من العلاقة:
م = ب2 – 4آج
فإذا كان المميز موجباً، يحسب البرنامج جذري المعدلة س1 و س2 من العلاقتين:
س1 = (- ب + جذر (م) ) ÷ 2آ
س2 = (- ب - جذر (م) ) ÷ 2آ
ويكتب البرنامج حل المعادلة في لوحة الكتابة.
أما إذا كان المميز سالباً فيكتب البرنامج إشعاراً باستحالة حل المعادلة في مجال الأعداد الحقيقية. ويكرر البرنامج نفسه إلى أن يدخل المستخدم الصفر كقيمة لأمثال س2.
البرنامج
*(
------------------------------------------------------------------------------------------------
برنامج بلغة ج لحل معادلة بمجهول واحد من الدرجة الثانية في مجال الأعداد الحقيقية
جميع الحقوق محفوظة للمؤلف: د. محمد عمار السلكة ، 1420 هـ - 2000 م
------------------------------------------------------------------------------------------------
)*
!! الشكل العام للمعادلة آ س^2 + ب س + ج =0
المتحول آ، ب، ج : حقيقي
المتحول م، س1، س2 : حقيقي
أكرر طالما صواب
{
أكتب "أدخل آ ب ج (أدخل 0 قيمة لـ آ كي تنهي البرنامج) : "
أقرأ آ ، ب ، ج
إذا آ = 0 أنتهي
أكتب " المعادلة: " ، آ ، " س^2 "
إذا ب > 0 أكتب " + "
أكتب ب ،" س "
إذا ج > 0 أكتب " + "
أكتب ج ، " = 0" ، سطر
أجعل م = ب^2 - 4× آ × ج
أكتب " المميز = "، م ، سطر
إذا (م<0)
أكتب "المعادلة مستحيلةالحل !"، سطر
وإلا
{
أجعل س1 = (-ب + جذر(م)) ÷ (2×آ)
أجعل س2 = (-ب - جذر(م)) ÷ (2×آ)
أكتب " حل المعادلة: "،سطر
أكتب "س1 = "، س1 ، سطر
أكتب "س2 = " ، س2 ، سطر
}
}