Professional Documents
Culture Documents
برمجة2 عملي ج3
برمجة2 عملي ج3
السنة األولى
الجلسة الثالثة
تمارين
2024/2023
2 عملي-الجلسة الثالثة
. البرنامج التالي يوضح كيف يمكن ان نعرف متغيرين ثابتين بنفس االسم االول عالمي والثاني :المثال االول
#include <iostream>
using namespace std;
const int BIRDS = 500;
void california();
int main(){
cout << "In main there are " << BIRDS<< " birds.\n";
california();
return 0;
}
void california(){
const int BIRDS = 10000;
cout << "In california there are " << BIRDS< " birds.\n";
cout << "In main there are " << ::BIRDS << " birds.\n";
}
؟79 ما هو خرج رالبنامج إذا أدخلنا الرقم،مثال عن تمرير البارامترات عن طريق العنوان:المثال الثاني
#include <iostream>
using namespace std;
// Function to get a valid number in the range [1, 100]
void getNumber(int& num) {
do {
cout << "Enter a number (1-100): ";
cin >> num;
int main() {
int userNumber;
getNumber(userNumber);
cout << "You entered: " << userNumber << endl;
}
2024/2023
3 عملي-الجلسة الثالثة
. برنامج يقوم بحساب سعر البيع للسلعة استنادا إىل تكلفة الجملة ونسبة الرب ح المطلوبة:الثالث المثال
ً
#include <iostream>
using namespace std;
int main() {
double wholesaleCost, markupPercentage;
return 0;
}
2024/2023
4 عملي-الجلسة الثالثة
الفكرة من البرنامج هي تجزئته الى دوال بسيطة كل دالة تقوم. برنامج حساب مساحة مستطيل باستخدام الدوال: المثال الرابع
.بمهمة ما
#include <iostream>
using namespace std;
// Function to get rectangle length from user
double getLength() {
double length;
cout << "Enter the length of the rectangle: ";
cin >> length;
return length;
}
// Function to get rectangle width from user
double getWidth() {
double width;
cout << "Enter the width of the rectangle: ";
cin >> width;
return width;
}
// Function to calculate rectangle area
double getArea(double length, double width) {
return length * width;
}
// Function to display rectangle data
void displayData(double length, double width, double area) {
cout << "Rectangle length: " << length << endl;
cout << "Rectangle width: " << width << endl;
cout << "Rectangle area: " << area << endl;
}
int main() {
double length = getLength();
double width = getWidth();
double area = getArea(length, width);
displayData(length, width, area);
return 0;
}
2024/2023
5 عملي-الجلسة الثالثة
.برنامج يهدف إلى تحليل مبيعات األقسام المختلفة وتحديد القسم الذي يحقق أعلى مبيعات :المثال الخامس
:المتغيرات
.الشرقي الشمالي القسم مبيعات على يحتوي المتغير هذا northeastSales
.الشرقي الجنوبي القسم مبيعات على يحتوي المتغير هذا southeastSales
.الغربي الشمالي القسم مبيعات على يحتوي المتغير هذا northwestSales
.الغربي الجنوبي القسم مبيعات على يحتوي المتغير هذا southwestSales
:الدوال
. تقوم بالتحقق من أن المبيعات غير سالبة وتعيد القيمة المدخلة. هذه الدالة تطلب من المستخدم إدخال مبيعات الربع السنوية لقسم معينgetSales
. هذه الدالة تحسب أعلى مبيعات وتطبع اسم القسم الذي يمتلكهاfindHighest
:التنفيذ الرئيسي
. للحصول على مبيعات كل قسمgetSales يتم استدعاء الدالة
. للعثور على أعلى مبيعات وطباعة اسم القسم الذي يمتلكهاfindHighest يتم استدعاء الدالة
#include <iostream>
#include <string>
using namespace std;
// Function to get quarterly sales figure for a division
double getSales(const string& divisionName) {
double sales;
cout << "Enter the quarterly sales for " << divisionName << ": $";
cin >> sales;
// Validate input (non-negative sales)
while (sales < 0.0) {
cout << "Invalid input! Sales must be non-negative. Enter again: $";
cin >> sales;
}
return sales;
}
// Function to find the highest sales and print the division name
void findHighest(double northeast, double southeast, double northwest, double southwest) {
double maxSales = max({ northeast, southeast, northwest, southwest });
string msg = " division has the highest sales: $";
if (maxSales == northeast) {cout << "Northeast"+msg << maxSales << endl;}
else if (maxSales == southeast) {cout << "Southeast"+msg << maxSales << endl;}
else if (maxSales == northwest) {cout << "Northwest"+msg << maxSales << endl;}
else {cout << "Southwest"+msg << maxSales << endl;}
}
int main() {
double northeastSales = getSales("Northeast");
double southeastSales = getSales("Southeast");
double northwestSales = getSales("Northwest");
double southwestSales = getSales("Southwest");
findHighest(northeastSales, southeastSales, northwestSales, southwestSales);
return 0;
}
2024/2023
6 عملي-الجلسة الثالثة
basePay = getBasePay(hours);
return overtimePay;
}
2024/2023