Apelsin so’zidan “qirqib olish” va “yopishtirish” lar orqali


Matnli fayl berilgan. Unda nechta belgi borligini aniqlang


Download 59.58 Kb.
bet2/13
Sana25.04.2023
Hajmi59.58 Kb.
#1399450
1   2   3   4   5   6   7   8   9   ...   13
Bog'liq
Algoritmik tillar va dasturlash

Matnli fayl berilgan. Unda nechta belgi borligini aniqlang.

#include


#include
using namespace std;

int main() {


string fileName = "example.txt";
ifstream file(fileName);
int numChars = 0;
char c;
while (file.get(c)) {
numChars++;
}
file.close();
cout << "fayl " << fileName << " belgilarga " << numChars << " ega." << endl;
return 0;
}



  1. Matnli fayl berilgan. Undagi uchinchi qatorni o’chiring. Natijani boshqa faylga yozing.

#include
#include
using namespace std;

int main() {


string fileName = "example.txt";
string outputFileName = "output.txt";
ifstream file(fileName);
ofstream outputFile(outputFileName);
int lineNum = 1;
string line;
while (getline(file, line)) {
if (lineNum != 3) {
outputFile << line << endl;
}
lineNum++;
}
file.close();
outputFile.close();
cout << "Uchinchi qator " << fileName << " o'chirildi. Natija saqlanadi " << outputFileName << endl;
return 0;
}

  1. Matnli fayl berilgan. Uning oxirgi qatorni o’chiring. Natijani boshqa faylga yozing.

#include


#include
#include
using namespace std;

int main() {


string fileName = "example.txt";
string outputFileName = "output.txt";
ifstream file(fileName);
ofstream outputFile(outputFileName);
vector lines;
string line;
while (getline(file, line)) {
lines.push_back(line);
}
file.close();
lines.pop_back();
for (const string& line : lines) {
outputFile << line << endl;
}
outputFile.close();
cout << "ning oxirgi qatori " << fileName << " o'chirildi. Natija saqlanadi " << outputFileName << endl;
return 0;
}

  1. Matnli fayl berilgan. Undagi birinchi va oxirgi qatorlarini o’chiring. Natijani boshqa faylga yozing.

#include


#include
#include
int main() {
std::ifstream inputFile("input.txt");
std::ofstream outputFile("output.txt");
if (!inputFile.is_open()) {
std::cerr << "Kirish faylini ochib bo‘lmadi";
return 1;
}
std::string line;
int lineCount = 0;
while (std::getline(inputFile, line)) {
if (lineCount != 0 && !inputFile.eof()) {
outputFile << line << std::endl;
}
++lineCount;
}
inputFile.close();
outputFile.close();
return 0;
}



  1. Matnli fayl berilgan. A va a belgilari bilan boshlanadigan qatorlar sonini toping.

#include


#include
#include
int main() {
std::ifstream inputFile("input.txt");
if (!inputFile.is_open()) {
std::cerr << "Kirish faylini ochib bo‘lmadi";
return 1;
}
std::string line;
int lineCount = 0;
while (std::getline(inputFile, line)) {
if ((line[0] == 'A') || (line[0] == 'a')) {
++lineCount;
}
}
inputFile.close();
std::cout << "A yoki an bilan boshlanadigan qatorlar soni: " << lineCount << std::endl;
return 0;
}



  1. Matnli fayl berilgan. Undagi eng uzun qatorni toping.

#include


#include
#include

int main() {


std::ifstream inputFile("input.txt");
if (!inputFile.is_open()) {
std::cerr << "Faylni ochib bo`lmadi";
return 1;
}
std::string line;
std::string longestLine;
while (std::getline(inputFile, line)) {
if (line.length() > longestLine.length()) {
longestLine = line;
}
}
inputFile.close();
std::cout << "Eng uzun qator: " << longestLine << std::endl;
return 0;
}



  1. Matnli fayl berilgan. Bu faylda T belgisi bilan boshlanadigan qator bor yoki yo’qligini aniqlang. Agar mavjud bo’lsa ulaning birinchisini toping.

#include


#include
#include

int main() {


std::ifstream inputFile("input.txt");

if (!inputFile.is_open()) {


std::cerr << "Kirish faylini ochib bo‘lmadi";
return 1;
}
std::string line;
bool foundT = false;
while (std::getline(inputFile, line)) {
if (line.length() > 0 && line[0] == 'T') {
foundT = true;
std::cout << "T harfi bilan boshlangan birinchi qator: " << line << std::endl;
break;
}
}
inputFile.close();
if (!foundT) {
std::cout << "T harfi bilan boshlangan qatorlar topilmadi." << std::endl;
}

return 0;


}



  1. Matnli fayl berilgan.. Birinchi qatorning eng oxirgi belgisini konsolga chiqaring.

#include


#include
#include

int main() {


std::ifstream inputFile("input.txt");

if (!inputFile.is_open()) {


std::cerr << "Faylni ochib bo`lmadi";
return 1;
}
std::string line;
if (std::getline(inputFile, line)) {

std::cout << "Birinchi qatorning oxirgi belgisi: " << line.back() << std::endl;


} else {
std::cout << "Fayl boʻsh" << std::endl;
}
inputFile.close();

return 0;


}



  1. Ushbu : familiya, amplua, yosh, o’yinlar soni va gollar soni- maydonlariga ega Player sinfini tuzing. Elementlari Player obyektidan iborat massiv yarating. Eng yaxshi to’purarni va kamida 5-ta o’yin o’ynagan futbolchilar haqidagi ma’lumotni konsolga chiqaring.

#include


#include

class Player {


public:
std::string Ismi;
std::string pozitsiyasi;
int yoshi;
int o`yinlar soni;
int gollar soni;
};
int main() {
const int numPlayers = 10;
Player players[numPlayers];
players[0].Ismi = "Cristiano Ronaldo";
players[0].pozitsiyasi = "Forward";
players[0].yoshi = 36;
players[0].o`yinlar soni = 800;
players[0].gollar soni = 700;
players[1].Ismi = "Lionel Messi";
players[1].pozitsiyasi = "Forward";
players[1].yoshi = 34;
players[1].o`yinlar soni = 900;
players[1].gollar soni = 750;
players[2].Ismi = "Neymar Jr.";
players[2].pozitsiyasi = "Forward";
players[2].yoshi = 29;
players[2].o`yinlar soni = 600;
players[2].gollar soni = 300;
players[3].Ismi = "Kevin De Bruyne";
players[3].pozitsiyasi = "Midfielder";
players[3].yoshi = 30;
players[3].o`yinlar soni = 400;
players[3].gollar soni = 100;
players[4].Ismi = "Robert Lewandowski";
players[4].pozitsiyasi = "Forward";
players[4].yoshi = 32;
players[4].o`yinlar soni = 500;
players[4].gollar soni = 400;
players[5].Ismi = "Virgil van Dijk";
players[5].pozitsiyasi = "Defender";
players[5].yoshi = 29;
players[5].o`yinlar soni = 300;
players[5].gollar soni = 25;
return 0;
}




  1. Download 59.58 Kb.

    Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   13




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