2-Amaliy mashg‘ulotini bajarish bo‘yicha variantlar


Download 69.09 Kb.
Sana19.02.2023
Hajmi69.09 Kb.
#1214984
Bog'liq
vazifa


2-Amaliy mashg‘ulotini bajarish bo‘yicha variantlar
Topshiriqqa muvofiq funksiyaga doir bo‘lgan masalalarni yechish lozim.
n ta elementdan tashkil topgan massiv berilgan.Bu massivning tub
elementlari soni aniqlansin. ()

#include
using namespace std;

bool isPrime(int n)


{
if (n <= 1)
{
return false;
}
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}

int main()


{
int n;
cout << "Massiv uzunligini kiriting: ";
cin >> n;

int a[n];


for (int i = 0; i < n; i++)
{
a[i] = rand() % 100 + 1;
}

cout << "Massiv elementi: ";


for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
int count = 0;
for (int i = 0; i < n; i++)
{
if (isPrime(a[i]))
{
count++;
}
}

cout << "Massivda " << count << " ta tub son bor." << endl;




return 0;
}



2-jadval
Do’konda sotuvga tushgan avtomobillar ro’yxati mavjud. Bu ro’yxatning
har bir yozuvi quyidagicha: avtomobil markasi, narxi, 100 km uchun
yoqilg’i harajati. Xaridor tomonidan kiritilgan markali avtomobillar
ro’yxatini chiqarish.

#include
#include
#include
using namespace std;


struct Car
{
string make;
double price;
double fuelConsumption;
};

void printCars(vector &cars, string make)


{
cout << "Xaridor tomonidan kiritilgan " << make << " markali avtomobillar:" << endl;
for (int i = 0; i < cars.size(); i++)
{
if (cars[i].make == make)
{
cout << "Marka: " << cars[i].make << ", Narx: " << cars[i].price << ", 100 km uchun yoqilg'i harajati: " << cars[i].fuelConsumption << endl;
}
}
}

int main()


{
vector cars;

int n;
cout << "Avtomobillar sonini kiriting: ";


cin >> n;


for (int i = 0; i < n; i++)
{
Car car;
cout << "Avtomobil markasini kiriting: ";
cin >> car.make;
cout << "Avtomobil narxini kiriting: ";
cin >> car.price;
cout << "100 km uchun yoqilg'i harajatini kiriting: ";
cin >> car.fuelConsumption;

cars.push_back(car);


}

string make;


cout << "Markani kiriting: ";
cin >> make;


printCars(cars, make);


return 0;
}


Berilgan nomdagi matnli faylni yaratish va unga ixtiyoriy matn kiritish
dasturini tuzing.Undagi oxirgi so’z o’chirilsin .

#include
using namespace std;


// Faylni yaratish funksiyasi
void createFile(string fileName)
{
ofstream file(fileName);
if (!file)
{
cout << "Faylni yaratishda xatolik yuz berdi." << endl;
}
file.close();
}


// Faylga matn yozish funksiyasi
void writeToFile(string fileName, string text)
{
ofstream file(fileName, ios::app);
if (!file)
{
cout << "Faylda yozishda xatolik yuz berdi." << endl;
}
file << text << endl;
file.close();
}


// Fayldan matn o'qish funksiyasi
string readFromFile(string fileName)
{
ifstream file(fileName);
string text;
string line;
while (getline(file, line))
{
text += line;
}
file.close();
return text;
}


// Fayldagi oxirgi so'zni o'chirish funksiyasi
void deleteLastWord(string fileName)
{
fstream file(fileName);
string line, wordToDelete;
while (getline(file, line))
{
int pos = line.find_last_of(" ");
wordToDelete = line.substr(pos + 1);
line.erase(pos);
}
file.close();


// Faylni yangilash
ofstream newFile(fileName);
newFile << line << endl;
newFile.close();
}

int main()


{
string fileName = "text.txt";
createFile(fileName);

string text;


cout << "Matn kiriting: ";
getline(cin, text);


writeToFile(fileName, text);

cout << "Fayl ochilmoqda..." << endl;


string fileText = readFromFile(fileName);
cout << "Fayldan o'qilgan matn: " << fileText << endl;


deleteLastWord(fileName);
cout << "Fayldagi oxirgi so'z o'chirildi." << endl;


return 0;
}


Download 69.09 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling