lineبرمجة

أمثلة محلولة C++

نعم، يمكن استخدام C++ في العديد من التطبيقات المتقدمة، بما في ذلك:

  • تطبيقات الويب: يمكن استخدام C++ في تطوير تطبيقات الويب باستخدام إطار العمل CPPCMS، والذي يستخدم C++ كلغة لبرمجة الخادم. كما يمكن استخدام مكتبة Wt الرائدة لتطوير تطبيقات الويب باستخدام C++.

  • تطبيقات سطح المكتب: يمكن استخدام C++ في تطوير تطبيقات سطح المكتب باستخدام مكتبات مثل Qt وwxWidgets وFLTK.

  • الألعاب: تستخدم C++ على نطاق واسع في صناعة الألعاب، حيث يستخدم محرك Unreal Engine C++ كلغة رئيسية لبرمجة اللعبة.

  • الذكاء الاصطناعي وتعلم الآلة: يمكن استخدام C++ في تطوير تطبيقات الذكاء الاصطناعي وتعلم الآلة باستخدام مكتبات مثل OpenCV وTensorFlow وCaffe.

  • التطبيقات العلمية والهندسية: يمكن استخدام C++ في تطوير التطبيقات العلمية والهندسية، مثل برامج معالجة الصور والصوت والفيديو، وبرامج النمذجة والمحاكاة والمحسابات الرقمية.

بشكل عام، يمكن استخدام C++ في أي تطبيق يتطلب أداءً عاليًا وتحكمًا دقيقًا في الموارد المنخفضة المستوى، مثل الذاكرة والمعالجة.

هنا بعض الأمثلة على برامج بلغة C++:

1- برنامج لحساب مجموع قائمة من الأرقام:

 

#include <iostream>
using namespace std;int main() {
int n, sum = 0;
cout << “Enter the number of integers: “;
cin >> n;
int arr[n];
cout << “Enter ” << n << ” integers: “;
for(int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
cout << “The sum of the entered integers is ” << sum << endl;
return 0;
}

2- برنامج لتحويل درجات مئوية إلى درجات فهرنهايت:

 

#include <iostream>
using namespace std;int main() {
float celsius, fahrenheit;
cout << “Enter the temperature in Celsius: “;
cin >> celsius;
fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
cout << “The temperature in Fahrenheit is ” << fahrenheit << ” degrees.” << endl;
return 0;
}

3- برنامج لحساب مساحة الدائرة باستخدام قيمة ثابتة للـπ:

 

#include <iostream>
using namespace std;int main() {
float radius, area;
const float pi = 3.14159;
cout << “Enter the radius of the circle: “;
cin >> radius;
area = pi * radius * radius;
cout << “The area of the circle is ” << area << endl;
return 0;
}

هذه الأمثلة تظهر استخدام بعض المفاهيم الأساسية في برمجة C++، مثل الأوامر المشروطة والحلقات والمتغيرات والثوابت. وتوضح كيفية استخدام هذه المفاهيم في كتابة برامج بسيطة وفعالة.

4- برنامج لحساب عمر الشخص بناء على تاريخ ميلاده:

 

#include <iostream>
using namespace std;int main() {
int birthYear, currentYear, age;
cout << “Enter your birth year: “;
cin >> birthYear;
cout << “Enter the current year: “;
cin >> currentYear;
age = currentYear – birthYear;
cout << “Your age is ” << age << ” years.” << endl;
return 0;
}

5- برنامج لعرض جدول الضرب من 1 إلى 10:

 

#include <iostream>
using namespace std;int main() {
int i, j;
for(i = 1; i <= 10; i++) {
for(j = 1; j <= 10; j++) {
cout << i * j << “\t”;
}
cout << endl;
}
return 0;
}

6- برنامج لإيجاد العناصر المتكررة في مصفوفة:

 

#include <iostream>
using namespace std;int main() {
int n, i, j, count;
cout << “Enter the size of the array: “;
cin >> n;
int arr[n];
cout << “Enter ” << n << ” integers: “;
for(i = 0; i < n; i++) {
cin >> arr[i];
}
cout << “The repeated elements are: “;
for(i = 0; i < n; i++) {
count = 1;
for(j = i+1; j < n; j++) {
if(arr[i] == arr[j]) {
count++;
arr[j] = -1;
}
}
if(count > 1 && arr[i] != -1) {
cout << arr[i] << ” “;
}
}
return 0;
}

هذه الأمثلة تظهر بعض البرامج الأكثر تقدمًا في C++، وتوضح كيفية استخدام المفاهيم المتقدمة مثل الدوال والمصفوفات والحلقات الداخلية. وتوضح أيضًا كيفية استخدام بعض الأدوات المتقدمة لحل مشكلات محددة، مثل إيجاد العناصر المتكررة في مصفوفة.

7- برنامج لإيجاد العدد الأكبر في مصفوفة:

 

#include <iostream>
using namespace std;int main() {
int n, i, max;
cout << “Enter the size of the array: “;
cin >> n;
int arr[n];
cout << “Enter ” << n << ” integers: “;
for(i = 0; i < n; i++) {
cin >> arr[i];
}
max = arr[0];
for(i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
cout << “The largest element in the array is ” << max << endl;
return 0;
}

8- برنامج لتحويل رقم إلى نظام عشري آخر:

 

#include <iostream>
using namespace std;int main() {
int num, base, rem, i = 0;
char hexNum[20];
cout << “Enter the number to convert: “;
cin >> num;
cout << “Enter the base to convert to: “;
cin >> base;
while(num > 0) {
rem = num % base;
if(rem < 10) {
hexNum[i] = rem + ‘0’;
}
else {
hexNum[i] = rem + ‘A’ – 10;
}
i++;
num /= base;
}
cout << “The converted number is: “;
for(int j = i-1; j >= 0; j–) {
cout << hexNum[j];
}
cout << endl;
return 0;
}

9- برنامج لحساب الأجر الإجمالي للعاملين في شركة:

 

#include <iostream>
using namespace std;int main() {
int n, i;
float wage, hours, totalWage = 0;
cout << “Enter the number of employees: “;
cin >> n;
for(i = 1; i <= n; i++) {
cout << “Enter the hourly wage and hours worked for employee ” << i << “: “;
cin >> wage >> hours;
if(hours <= 40) {
totalWage += wage * hours;
}
else {
totalWage += (40 * wage) + ((hours – 40) * wage * 1.5);
}
}
cout << “The total wage paid to the employees is $” << totalWage << endl;
return 0;
}

هذه الأمثلة توضح استخدام بعض المفاهيم المتقدمة في C++ مثل الشروط والحلقات والعمليات الحسابية المعقدة، وكيفية استخدامها في برامج فعالة وقوية.

 

 

10- برنامج لتحويل النص إلى أحرف كبيرة:

 

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

int main() {
char str[100];
int i;
cout << “Enter a string: “;
cin.getline(str, 100);
for(i = 0; i < strlen(str); i++) {
str[i] = toupper(str[i]);
}
cout << “The converted string is: ” << str << endl;
return 0;
}

11- برنامج لإيجاد المصفوفة المنظمة بشكل تصاعدي:

 

#include <iostream>
using namespace std;

int main() {
int n, i, j, temp;
cout << “Enter the size of the array: “;
cin >> n;
int arr[n];
cout << “Enter ” << n << ” integers: “;
for(i = 0; i < n; i++) {
cin >> arr[i];
}
for(i = 0; i < n-1; i++) {
for(j = i+1; j < n; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
cout << “The sorted array is: “;
for(i = 0; i < n; i++) {
cout << arr[i] << ” “;
}
cout << endl;
return 0;
}

12- برنامج لتحويل النص إلى رمز مورس:

 

#include <iostream>
#include <cstring>
using namespace std;int main() {
string str;
cout << “Enter a string: “;
getline(cin, str);
string morse[] = {“.-“, “-…”, “-.-.”, “-..”, “.”, “..-.”, “–.”, “….”, “..”, “.—“, “-.-“, “.-..”, “–“, “-.”, “—“, “.–.”, “–.-“, “.-.”, “…”, “-“, “..-“, “…-“, “.–“, “-..-“, “-.–“, “–..”, “/”, ” “};
string alpha[] = {“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”, ” “, ” “};
string result = “”;
for(int i = 0; i < str.length(); i++) {
char c = toupper(str[i]);
for(int j = 0; j < 27; j++) {
if(c == alpha[j][0]) {
result += morse[j];
result += ” “;
break;
}
}
}
cout << “The converted string to Morse code is: ” << result << endl;
return 0;
}

هذه الأمثلة تظهر كيفية استخدام بعض المكتبات القياسية في C++ مثل cstring وcctype، وكيفية استخدامها في برامج فعالة ومفيدة.

أيضًا بعض البرامج الذكية التي تستخدم بعض التقنيات الحديثة:

13- برنامج لتحويل العملات:

 

#include <iostream>
#include <curl/curl.h>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
using namespace std;size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
((string*)stream)->append((char*)ptr, size * nmemb);
return size * nmemb;
}int main() {
string api_key = “INSERT_YOUR_API_KEY_HERE”;
string from_currency, to_currency;
float amount;
cout << “Enter the amount to convert: “;
cin >> amount;
cout << “Enter the currency code to convert from: “;
cin >> from_currency;
cout << “Enter the currency code to convert to: “;
cin >> to_currency;
string url = “https://v6.exchangerate-api.com/v6/” + api_key + “/pair/” + from_currency + “/” + to_currency + “/” + to_string(amount);
CURL *curl;
CURLcode res;
string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
rapidjson::Document document;
document.Parse(readBuffer.c_str());
if(document.HasParseError()) {
cout << “Error parsing JSON data.” << endl;
return 1;
}
if(document.HasMember(“result”)) {
float result = document[“result”].GetFloat();
cout << amount << ” ” << from_currency << ” is equivalent to ” << result << ” ” << to_currency << endl;
}
else {
cout << “Unable to retrieve conversion rate data.” << endl;
}
return 0;
}

14- برنامج لتعيين موقع المستخدم:

 

#include <iostream>
#include <curl/curl.h>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
using namespace std;size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
((string*)stream)->append((char*)ptr, size * nmemb);
return size * nmemb;
}int main() {
CURL *curl;
CURLcode res;
string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, “http://ip-api.com/json/”);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
rapidjson::Document document;
document.Parse(readBuffer.c_str());
if(document.HasParseError()) {
cout << “Error parsing JSON data.” << endl;
return 1;
}
if(document.HasMember(“city”)) {
string city = document[“city”].GetString();
cout << “You are currently in ” << city << endl;
}
else {
cout << “Unable to retrieve location data.” << endl;
}

 

 

15- برنامج لتحويل الصور إلى نمط الرسم البياني:

 

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;int main() {
string image_path = “image.jpg”;
Mat image = imread(image_path, IMREAD_COLOR);
if(image.empty()) {
cout << “Could not read the image file.” << endl;
return 1;
}
Mat gray, edges;
cvtColor(image, gray, COLOR_BGR2GRAY);
blur(gray, edges, Size(3, 3));
Canny(edges, edges, 50, 150);
vector<Vec4i> lines;
HoughLinesP(edges, lines, 1, CV_PI/180, 50, 50, 10);
Mat output = Mat::zeros(image.size(), CV_8UC3);
for(size_t i = 0; i < lines.size(); i++) {
Vec4i l = lines[i];
line(output, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255, 255, 255), 1, LINE_AA);
}
imshow(“Original Image”, image);
imshow(“Detected Lines”, output);
waitKey();
return 0;
}

هذه الأمثلة تستخدم بعض التقنيات الحديثة والمكتبات الشائعة في C++ مثل curl وrapidjson وOpenCV، وتوضح كيفية استخدامها في برامج فعالة ومفيدة. وهذه الأمثلة تعرض بعض الاستخدامات الحقيقية لـ C++ في برمجة التطبيقات المتقدمة.

 

 

 

 

مقالات ذات صلة

زر الذهاب إلى الأعلى
إغلاق

أنت تستخدم إضافة Adblock

يرجى تعطيل مانع الإعلانات حيث أن موقعنا غير مزعج ولا بأس من عرض الأعلانات لك فهي تعتبر كمصدر دخل لنا و دعم مقدم منك لنا لنستمر في تقديم المحتوى المناسب و المفيد لك فلا تبخل بدعمنا عزيزي الزائر