Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Kiểu dữ liệu trong C++

Char dãy giá trị từ -127 đến 127 or 0 đên 255;


Unsigned char (không có dấu) giá trị từ 0 đến 255: độ rộng bít là 1 byte

//QUAN LY DONG VAT!!!


#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;


class Animal {
//Khai bao thuoc tinh properties
private:
string type;
float dimention;
public:
// Constructors
Animal(string Type, float Dim);
// set methods
void setType(string Type);
void setDimension(float Dim);
// get methods
string getType();
float getDimension();
float getWeight();
float getSpeed();
};

Animal::Animal(string Type, float Dim) {


type = Type;
dimention = Dim;
}

void Animal::setType(string Type) {


type = Type;
}

void Animal::setDimension(float Dim) {


dimention = Dim;
}

string Animal::getType() {
return type;
}

float Animal::getDimension() {
return dimention;
}

float Animal::getWeight() {
if (type == "Dog") {
return dimention * 20;
}
else if (type == "Horse") {
return dimention * 100;
}
else if (type == "Fish") {
return dimention * 10;
}
else if (type == "Bird") {
return dimention * 10;
}
else {
return -1;
}
}

float Animal::getSpeed() {
if (type == "Dog") {
return dimention * 20;
}
else if (type == "Horse") {
return dimention * 20;
}
else if (type == "Fish") {
return dimention * 7;
}
else if (type == "Bird") {
return dimention * 15;
}
else {
return -1;
}
}

void addAnimalScreen(vector<Animal> &AnimalVector) {


float dim;
int choose;
string AnimalType;
bool flag = true;
cout << "Chon con vat ban muon them:\n";
while (flag)
{
cout << "1: Them Cho.\n";
cout << "2: Them Ngua.\n";
cout << "3: Them Chim.\n";
cout << "4: Them Ca.\n";
cout << "0: Quay lai menu chinh.\n";
cout << "Nhap loai dong vat: ";
cin >> choose;

switch (choose) {
case 0: {
flag = false;
break;
};
case 1: {
cout << "Hay nhap vao chieu cao cua con cho: ";
cin >> dim;
AnimalVector.push_back(Animal("Dog", dim));
break;
}
case 2: {
cout << "Hay nhap vao chieu cao cua con ngua: ";
cin >> dim;
AnimalVector.push_back(Animal("Horse", dim));
break;
}
case 3: {
cout << "Hay nhap vao chieu dai canh cua con chim: ";
cin >> dim;
AnimalVector.push_back(Animal("Bird", dim));
break;
}
case 4: {
cout << "Hay nhap vao chieu dai cua con ca: ";
cin >> dim;
AnimalVector.push_back(Animal("Fish", dim));
break;
}
}
if (flag == false) {
break;
}
}
}

void printAllAnimal(vector<Animal> AnimalVector) {


vector<Animal>::iterator animal_prt;
cout << "Danh sach cac con vat:\n";
for (animal_prt = AnimalVector.begin(); animal_prt < AnimalVector.end(); animal_prt++) {
cout << "Mot con " << (*animal_prt).getType() << " voi Khoi luong: " << (*animal_prt).getWeight() << " va Toc do:
" << (*animal_prt).getSpeed() << endl;
}
}

void PrintRandomAnimal(vector<Animal> AnimalVector) {


int index = 0;
cout << "Nhap chi so cua con vat: ";
cin >> index;
if (index < size(AnimalVector)){
cout << "Con vat o vi tri " << index << " la con " << AnimalVector[index].getType() << " co:\n";
cout << "- Kich thuoc: " << AnimalVector[index].getDimension() << endl;
cout << "- Khoi luong: " << AnimalVector[index].getWeight() << endl;
cout << "- Toc do: " << AnimalVector[index].getSpeed() << endl;
}
else {
cout << "Chi so lon hon so luong con vat toi da(" << size(AnimalVector) << " con).\n";
}
}

int main()
{
float dim;
int choose;
string AnimalType;
bool flag = true;
vector<Animal> AnimalManagementVector;
while (flag) {
cout << "CHUONG TRINH QUAN LY CON VAT:\n";
cout << "1: Them con vat moi.\n";
cout << "2: Xem toan bo con vat.\n";
cout << "3: Xem ngau nhien.\n";
cout << "0: Thoat khoi chuong trinh.\n";
cout << " Lua chon: ";
cin >> choose;

switch (choose) {
case 0:{
cout << "Good bye!\n";
flag = false;
break;
}
case 1: {
addAnimalScreen(AnimalManagementVector);
break;
}
case 2: {
printAllAnimal(AnimalManagementVector);
break;
}
case 3: {
PrintRandomAnimal(AnimalManagementVector);
break;
}
}
}
}

//QUAN LY DONG VAT!!!


#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

class DongVat {
private:
string loai;
float dai;
public:
//Contructors
DongVat(string loai, float Dai);
void Setloai(string Loai);
void Setdai(float Dai);
string Getloai();
float Getdai();
float Getcannang();
float Gettocdo();
};

DongVat::DongVat(string Loai, float Dai) {


loai = Loai;
dai = Dai;
}

void DongVat::Setloai(string Loai) {


loai = Loai;
}

void DongVat::Setdai(float Dai) {


dai = Dai;
}

string DongVat::Getloai() {
return loai;
}

float DongVat::Getdai() {
return dai;
}

float DongVat::Getcannang() {
if (loai == "Cho") {
return dai * 20;
}
else if (loai == "Ngua") {
return dai * 100;
}
else if (loai == "Ca") {
return dai * 10;
}
else if (loai == "Chim") {
return dai * 10;
}
else {
return -1;
}
}

float DongVat::Gettocdo() {
if (loai == "Cho") {
return dai * 20 * 3.6;
}
else if (loai == "Ngua") {
return dai * 20 * 3.6;
}
else if (loai == "Ca") {
return dai * 7 * 3.6;
}
else if (loai == "Chim") {
return dai * 15 * 3.6;
}
else {
return -1;
}
}

void Themdongvat(vector<DongVat>& DongVatvector) {


float dai;
int chon;
bool flag = true;

cout << "Con vat ban muon them vao:\n";


while (flag)
{
cout << "1. Them con Cho.\n";
cout << "2. Them con Ngua.\n";
cout << "3. Them con Ca.\n";
cout << "4. Them con Chim.\n";
cout << "0. Quay ve man hinh chinh.\n";
cout << " Nhap loai dong vat.";
cin >> chon;

switch (chon) {
case 0: {
flag = false;
break;
}
case 1: {
cout << "Nhap chieu dai cua con Cho: ";
cin >> dai;
DongVatvector.push_back(DongVat("Cho", dai));
break;
}
case 2: {
cout << "Nhap chieu dai cua con Ngua: ";
cin >> dai;
DongVatvector.push_back(DongVat("Ngua", dai));
break;
}
case 3: {
cout << "Nhap chieu dai cua con Ca: ";
cin >> dai;
DongVatvector.push_back(DongVat("Ca", dai));
break;
}
case 4: {
cout << "Nhap do dai sai canh cua con Chim: ";
cin >> dai;
DongVatvector.push_back(DongVat("Chim", dai));
break;
}
}
if (flag == false) {
break;
}
}
}

void Toanbodongvat(vector<DongVat>& DongVatvector){


vector<DongVat>::iterator dongvat_prt;
cout << "Danh sach con vat duoc them vao: \n";
for (dongvat_prt = DongVatvector.begin(); dongvat_prt <= DongVatvector.end(); dongvat_prt++) {
cout << "Mot con " << (*dongvat_prt).Getloai() << " co chieu dai "
<< (*dongvat_prt).Getdai() << " m. Co can nang "
<< (*dongvat_prt).Getcannang() << " Kg. Co van toc "
<< (*dongvat_prt).Gettocdo() << " Km/h.\n";
}
}

void Xemmotcondongvat(vector<DongVat>& DongVatvector) {


int sothem;
cout << " Ban da them vao " << size(DongVatvector) << " con vat." << endl;
cout << " Luc cho con ban muon xem: ";
cin >> sothem;
if (sothem <= size(DongVatvector)) {
cout << "Con vat thu " << sothem << " la con " << DongVatvector[sothem].Getloai()
<< "\n- Co chieu dai: " << DongVatvector[sothem].Getdai() << " m."
<< "\n - Co Can nang: " << DongVatvector[sothem].Getcannang() << " Kg."
<< "\n - Co van toc: " << DongVatvector[sothem].Gettocdo() << " Km/h.";
}
else {
cout << "Chi so lon hon so luong con vat toi da(" << size(DongVatvector) << " con).\n";
}
}

int main() {
int chon;
bool flag = true;
vector<DongVat> Sucvatvector;

cout << "CHUONG TRINH QUAN LY DONG VAT.\n";


cout << "Cac chuc nang:\n";
while (flag) {
cout << "1. Them con vat.\n";
cout << "2. Hien thi toan bo.\n";
cout << "3. Hien thi ngau nhien mot con.\n";
cout << "0. Thoat.\n";
cout << "Lua chon tinh nang: ";
cin >> chon;

switch (chon) {
case 0: {
cout << "Good bye!!!";
flag = false;
break;
}
case 1: {
Themdongvat(Sucvatvector);
break;
}
case 2: {
Toanbodongvat(Sucvatvector);
break;
}
case 3: {
Xemmotcondongvat(Sucvatvector);
break;
}
}
}
}

Dưới đây là một đoạn code C++ để giải quyết yêu cầu của bạn:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

class TailProgram {
public:
void printLastLines(const std::string& filename, int n) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cout << "Không thể mở file!" << std::endl;
return;
}

std::vector<std::string> lines;
std::string line;

while (std::getline(file, line)) {


lines.push_back(line);
if (lines.size() > n) {
lines.erase(lines.begin());
}
}

for (const auto& line : lines) {


std::cout << line << std::endl;
}

file.close();
}
};

int main(int argc, char* argv[]) {


if (argc < 3) {
std::cout << "Sử dụng: tail <tên_file> <số_dòng_cuối_cùng>" << std::endl;
return 1;
}

std::string filename = argv[1];


int n = std::stoi(argv[2]);

TailProgram tail;
tail.printLastLines(filename, n);

return 0;
}
Copy
Trong chương trình này, chúng ta sử dụng một lớp TailProgram để thực hiện các thao tác liên quan
đến việc in ra n dòng cuối cùng của một tệp tin. Hàm printLastLines nhận vào tên file và số dòng
cuối cùng cần in. Trong hàm này, chúng ta mở file và đọc từng dòng, lưu trữ n dòng cuối cùng vào
một vector. Sau đó, chúng ta in ra các dòng đã lưu trữ trong vector.

Trong hàm main, chúng ta kiểm tra số lượng đối số đầu vào của chương trình. Nếu không đủ đối số,
chúng ta in ra một thông báo hướng dẫn sử dụng. Sau đó, chúng ta lấy tên file và số dòng cuối cùng
từ các đối số và gọi phương thức printLastLines của lớp TailProgram.

Để biên dịch và chạy chương trình, bạn có thể sử dụng các bước sau:

1. Lưu code vào một tệp tin có tên "tail.cpp".


2. Mở Terminal và di chuyển đến thư mục chứa tệp tin "tail.cpp".
3. Biên dịch code bằng cách chạy lệnh sau: g++ tail.cpp -o tail.
4. Chạy chương trình bằng cách nhập lệnh sau: ./tail <tên_file> <số_dòng_cuối_cùng>.
Ví dụ: ./tail input.txt 5.

Chương trình sẽ in ra n dòng cuối cùng của tệp tin "input.txt" (trong ví dụ trên là 5 dòng).

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <thread>
#include <string>

using namespace std;

// Hàm sắp xếp một dãy số nguyên tăng dần


void sortRange(std::vector<int>& numbers) {
std::sort(numbers.begin(), numbers.end());
}

int main(int argc, char* argv[]) {


if (argc != 2) {
std::cout << "Su dung: ./program_name <so luong thread>" << std::endl;
return 1;
}

int numThreads = stoi(argv[1]);

// Đọc dữ liệu từ file input.txt


std::ifstream inputFile("input.txt");
if (!inputFile.is_open()) {
std::cout << "khong the mo file.txt" << std::endl;
return 1;
}

std::vector<int> numbers;

int number;
while (inputFile >> number) {
numbers.push_back(number);
}

inputFile.close();

// Chia dãy số thành các dãy con


int chunkSize = numbers.size() / numThreads;
int extraElements = numbers.size() % numThreads;

std::vector<std::vector<int>> chunks(numThreads);

auto start = numbers.begin();


for (int i = 0; i < numThreads; ++i) {
auto end = start + chunkSize;
if (extraElements > 0) {
end++;
extraElements--;
}
chunks[i] = std::vector<int>(start, end);
start = end;
}

// Sắp xếp các dãy con bằng các thread


std::vector<std::thread> threads;
for (int i = 0; i < numThreads; ++i) {
threads.push_back(std::thread(sortRange, std::ref(chunks[i])));
}
// Đợi tất cả các thread hoàn thành
for (auto& thread : threads) {
thread.join();
}

// Trộn các dãy con đã sắp xếp thành một dãy số hoàn chỉnh
std::vector<int> sortedNumbers;
for (const auto& chunk : chunks) {
sortedNumbers.insert(sortedNumbers.end(), chunk.begin(), chunk.end());
}

// Ghi kết quả ra file output.txt


std::ofstream outputFile("output.txt");
if (!outputFile.is_open()) {
std::cout << "Khong the tao file output.txt" << std::endl;
return 1;
}

for (const auto& number : sortedNumbers) {


outputFile << number << ' ';
}

outputFile.close();

std::cout << "Hoan thanh sap xep va ket qua." << std::endl;

return 0;
}

You might also like