Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

‫‪1‬‬ ‫الجلسة الثالثة‪-‬عملي‬

‫جامعة حلب في املناطق املحررة‬

‫كلية الهندسة املعلوماتية‬

‫السنة األولى‬

‫مقرر البرمجة ‪ 2‬عملي‬

‫الجلسة الثالثة‬
‫تمارين‬

‫‪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;

if (num < 1 || num > 100) {


cout << "Invalid input! Please enter a number between 1 and 100." << endl;
}
} while (num < 1 || num > 100);
}

int main() {
int userNumber;
getNumber(userNumber);
cout << "You entered: " << userNumber << endl;
}

2024/2023
3 ‫عملي‬-‫الجلسة الثالثة‬

. ‫ برنامج يقوم بحساب سعر البيع للسلعة استنادا إىل تكلفة الجملة ونسبة الرب ح المطلوبة‬:‫الثالث‬ ‫المثال‬
ً

.‫المتغب يحمل قيمة تكلفة السلعة بالجملة‬


‫ر‬ ‫ هذا‬:)‫(تكلفة الجملة‬wholesaleCost

.‫المتغب يحمل قيمة نسبة الرب ح المطلوبة‬


‫ر‬ ‫ هذا‬:)‫(نسبة الرب ح‬markupPercentage
ً ‫المتغب يحسب مبلغ الرب ح‬
.‫بناء عىل نسبة الرب ح وتكلفة الجملة‬ ‫ر‬ ‫ هذا‬:)‫(مبلغ الرب ح‬markupAmount
ً ‫المتغب يحسب سعر البيع للسلعة‬
‫بناء عىل تكلفة الجملة‬ ‫ر‬ ‫ هذا‬:)‫(سعر البيع للتجزئة‬retailPrice
ً ‫ تقوم هذه الدالة بحساب سعر البيع للسلعة‬calculateRetail:
‫و تقوم بالتحقق من أن تكلفة الجملة ونسبة الرب ح‬.‫بناء عىل تكلفة الجملة ونسبة الرب ح‬
.‫ يتم حساب سعر البيع وإرجاعه‬،‫ إذا كانت القيم صالحة‬.‫غب سالبة‬
‫ر‬

#include <iostream>
using namespace std;

// Function to calculate retail price


double calculateRetail(double wholesaleCost, double markupPercentage) {
if (wholesaleCost < 0 || markupPercentage < 0) {
cout<<"Invalid input! Wholesale cost and markup percentage must be non-negative."<< endl;
return -1; // Return an error value
}

double markupAmount = wholesaleCost * (markupPercentage / 100);


double retailPrice = wholesaleCost + markupAmount;
return retailPrice;
}

int main() {
double wholesaleCost, markupPercentage;

cout << "Enter the wholesale cost: $";


cin >> wholesaleCost;

cout << "Enter the markup percentage: ";


cin >> markupPercentage;

double retailPrice = calculateRetail(wholesaleCost, markupPercentage);


if (retailPrice >= 0) {
cout << "Retail price: $" << retailPrice << endl;
}

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 ‫عملي‬-‫الجلسة الثالثة‬

‫ إذا كانت‬.‫الت عملها العامل‬ ‫رن‬


‫ يعتمد عىل قيمة األجرة للساعة الواحدة وعدد الساعات ي‬.‫للعاملي‬ ‫اإلجماىل‬
‫ي‬ ‫ برنامج يحسب األجر‬:‫المثال السادس‬
‫االساس‬ ‫األجر‬ ‫بجمع‬ ‫اإلجماىل‬ ‫األجر‬ ‫حساب‬ ‫يتم‬ . ‫االساس‬ ‫واالجر‬ ‫ن‬
‫اإلضاف‬ ‫األجر‬ ‫حساب‬ ‫يتم‬ ، ‫االساسية‬ ‫الساعات‬ ‫أكب من‬
‫ي‬ ‫ي‬ ‫ي‬ ‫ي‬ ‫الت عملها العامل ر‬ ‫الساعات ي‬
‫ن‬
.‫ثم يتم عرض األجور‬. ‫اإلضاف‬ ‫واألجر‬
‫ي‬
#include <iostream>
using namespace std;
const double PAY_RATE = 22.55; // Hourly pay rate
const double BASE_HOURS = 40.0; // Max non-overtime hours
const double OT_MULTIPLIER = 1.5; // Overtime multiplier
// Function prototypes
double getBasePay(double hoursWorked);
double getOvertimePay(double hoursWorked);
int main()
{
double hours, // Hours worked
basePay, // Base pay
overtime = 0.0, // Overtime pay
totalPay; // Total pay
cout << "How many hours did you work? ";
cin >> hours;

basePay = getBasePay(hours);

if (hours > BASE_HOURS)


overtime = getOvertimePay(hours);

totalPay = basePay + overtime;


cout << "Base pay: $" << basePay << endl
<< "Overtime pay: $" << overtime << endl
<< "Total pay: $" << totalPay << endl;
return 0;
}

double getBasePay(double hoursWorked)


{
double basePay; // To hold base pay
if (hoursWorked > BASE_HOURS)
basePay = BASE_HOURS * PAY_RATE;
else
basePay = hoursWorked * PAY_RATE;
return basePay;
}

double getOvertimePay(double hoursWorked)


{
double overtimePay; // To hold overtime pay

// Determine overtime pay.


if (hoursWorked > BASE_HOURS)
{
overtimePay = (hoursWorked - BASE_HOURS) * PAY_RATE * OT_MULTIPLIER;
}
else
{
overtimePay = 0.0;
}

return overtimePay;
}

2024/2023

You might also like