14. Ho'l mevalar haqida (nomi, mazasi, rangi, turi va narxi) to'plam berilgan. Ho'l mevalarni nomi, mazasi, rangi, turi va narxi bo'yicha qidiruvchi hamda topilgan natijani faylga saqlashni taklif qiladigan dastur tuzing


Download 12.24 Kb.
Sana16.06.2023
Hajmi12.24 Kb.
#1489390
Bog'liq
5-amaliyot Quralova Mashhura


5-amaliyot
14. Ho'l mevalar haqida (nomi, mazasi, rangi, turi va narxi) to'plam berilgan. Ho'l mevalarni nomi, mazasi, rangi, turi va narxi bo'yicha qidiruvchi hamda topilgan natijani faylga saqlashni taklif qiladigan dastur tuzing.

#include


#include
#include
using namespace std;

class Fruit {


public:
string name;
float weight;
string color;
string type;
int price;

Fruit(string n, float w, string c, string t, int p) {


name = n;
weight = w;
color = c;
type = t;
price = p;
}
};

void saveToFile(Fruit f) {


ofstream file;
file.open("fruits.txt", ios::app);
file << f.name << " " << f.weight << " " << f.color << " " << f.type << " " << f.price << endl;
file.close();
}

Fruit searchByName(string name) {


ifstream file;
file.open("fruits.txt");
string n, c, t;
float w;
int p;
while (file >> n >> w >> c >> t >> p) {
if (n == name) {
file.close();
return Fruit(n, w, c, t, p);
}
}
file.close();
return Fruit("", 0, "", "", 0);
}

Fruit searchByColor(string color) {


ifstream file;
file.open("fruits.txt");
string n, c, t;
float w;
int p;
while (file >> n >> w >> c >> t >> p) {
if (c == color) {
file.close();
return Fruit(n, w, c, t, p);
}
}
file.close();
return Fruit("", 0, "", "", 0);
}

Fruit searchByType(string type) {


ifstream file;
file.open("fruits.txt");
string n, c, t;
float w;
int p;
while (file >> n >> w >> c >> t >> p) {
if (t == type) {
file.close();
return Fruit(n, w, c, t, p);
}
}
file.close();
return Fruit("", 0, "", "", 0);
}

int main() {


int n;
cout << "Qancha meva kiritmoqchisiz? ";
cin >> n;

for (int i = 0; i < n; i++) {


string name, color, type;
float weight;
int price;

cout << i+1 << "-chi mevani nomini kiriting: ";


cin >> name;
cout << i+1 << "-chi mevani og'irligini kiriting: ";
cin >> weight;
cout << i+1 << "-chi mevani rangini kiriting: ";
cin >> color;
cout << i+1 << "-chi mevani turini kiriting: ";
cin >> type;
cout << i+1 << "-chi mevani narxini kiriting: ";
cin >> price;

Fruit f(name, weight, color, type, price);


saveToFile(f);
}

string searchType;


cout << "Qaysi turdagi mevalarni qidirmoqchisiz? ";
cin >> searchType;
Fruit result = searchByType(searchType);
if (result.name != "") {
cout << "Natija: " << result.name << " " << result.weight << " " << result.color << " " << result.type << " " << result.price << endl;
} else {
cout << "Bunday meva topilmadi" << endl;
}

return 0;


}

Download 12.24 Kb.

Do'stlaringiz bilan baham:




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