0% found this document useful (0 votes)
37 views7 pages

Prelab 3

Uploaded by

phamviethungf9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views7 pages

Prelab 3

Uploaded by

phamviethungf9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

bài 1

struct node {
int data;
node* next;
};

node* createLinkedList(int n); // The implementation is provided implicitly

bool isEqual(node* head1, node* head2) {


while (head1 != nullptr && head2 != nullptr) {
if (head1->data != head2->data) {
return false;
}
head1 = head1->next;
head2 = head2->next;
}
return head1 == nullptr && head2 == nullptr;
}

int main() {
int n = 0;
cin >> n;
node* head1 = createLinkedList(n);

int m = 0;
cin >> m;
node* head2 = createLinkedList(m);

cout << isEqual(head1, head2) << endl;

return 0;
}
bài 2
#include <iostream>
using namespace std;

struct node
{
int data;
node *next;
};
node *createLinkedList(int n); // The implementation is provided implicitly

int countNode(node* head)


{
int count = 0;
while (head != nullptr)
{
count++;
head = head ->next;
}
return count;
}

void print(node *head)


{
while (head != nullptr)
{
cout << head->data << endl;
head = head->next;
}
}
int main()
{
int n;
cin >> n;
node* head = createLinkedList(n);
print(head);
cout<<endl;
cout << countNode(head);
return 0;
}
bài 3
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *createLinkedList(int n)
{
node* head = nullptr;
node* tail = nullptr;

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


int value;
cin >> value;
node* newNode = new node{ value, nullptr };
if (head == nullptr) {
head = newNode;
tail = newNode;
}
else {
newNode->next = head;
head = newNode;
}
}

return head;
}
void print(node *head)
{
while (head != nullptr)
{
cout << head->data << endl;
head = head->next;
}
}
int main()
{
int n = 0;
cin >> n;
if (n > 0)
{
node *head = createLinkedList(n);
print(head);
}
else
{
cout << "Invalid n" << endl;
}
return 0;
}

OOP
//1
#include<iostream>
using namespace std;

class Course {
private:
int ID;
int numOfStudent;
int numOfTeacher;
int numOfTA;
public:
void getinfo();
void disinfo();
};
void Course::getinfo() {
cout << "ID: ";
cin >> ID;
cout << "Number of Students: ";
cin >> numOfStudent;
cout << "Number of Teachers: ";
cin >> numOfTeacher;
cout << "Number of TAs: ";
cin >> numOfTA;
}
void Course::disinfo()
{
cout<<endl;
cout<< "CourseID = "<< ID << endl;
cout<< "Number of student = " << numOfStudent << endl;
cout<< "Number of teacher = " << numOfTeacher << endl;
cout<< "Number of TA = " << numOfTA<< endl;
}
int main() {
int n;

cin >> n;

Course* courses = new Course[n];


for (int i = 0; i < n; i++) {
cout << "Course " << i+1 << ":" << endl;
courses[i].getinfo();
cout << endl;
}
cout<< "ID: Number of Students: Number of Teachers: Number of TAs: ID: Number
of Students: Number of"<<endl;
cout<<"Teachers: Number of TAs:";
for (int i = 0; i < n; i++) {
courses[i].disinfo();
}
delete[] courses;
return 0;
}
// cau 2
#include <iostream>
using namespace std;
class Integer {
private:
int val;

public:
Integer(int val) {
this->val = val;
}

void setValue(int val) {


this->val = val
}

int getValue() {
return this->val;
}
~Integer() {}
};

int main() {
cout << "Constructor test" <<endl;
Integer i(10);
Integer i2(-10);
Integer *i3 = new Integer(20);
delete i3;
return 0;
}
// cau3
template<typename T>
class Array {
public:
Array(int size, T initValue);
~Array();
void setAt(int idx, const T& value);
T getAt(int idx);
T& operator[](int idx);

void print();

private:
int size;
T* p;
};

template<typename T>
Array<T>::Array(int size, T initValue) {
this->size = size;
this->p = new T[size];
for (int i = 0; i < size; ++i) {
this->p[i] = initValue;
}
}

template<typename T>
Array<T>::~Array() {
delete[] this->p;
}

template<typename T>
void Array<T>::setAt(int idx, const T& value) {
if (idx < 0 || idx >= this->size) {
throw -1;
}
this->p[idx] = value;
}

template<typename T>
T Array<T>::getAt(int idx) {
if (idx < 0 || idx >= this->size) {
throw -1;
}
return this->p[idx];
}

template<typename T>
T& Array<T>::operator[](int idx) {
if (idx < 0 || idx >= this->size) {
throw -1;
}
return this->p[idx];
}

template<typename T>
void Array<T>::print() {
for (int i = 0; i < this->size; ++i) {
cout << (i > 0 ? " " : "") << this->p[i];
}
}

//cau 4
#include <iostream>
using namespace std;
class ClockType {
public:
ClockType(int hour, int minute, int second); // constructor with parameters
ClockType(); // default constructor
void printTime() const;
private:
int hr;
int min;
int sec;
};

ClockType::ClockType(int hour, int minute, int second) {


if (hour >= 0 && hour < 24)
hr = hour;
else
hr = 0;

if (minute >= 0 && minute < 60)


min = minute;
else
min = 0;
if (second >= 0 && second < 60)
sec = second;
else
sec = 0;
}

ClockType::ClockType() {
hr = 0;
min = 0;
sec = 0;
}

void ClockType::printTime() const {


if (hr < 10)
cout << "0";
cout << hr << ":";

if (min < 10)


cout << "0";
cout << min << ":";

if (sec < 10)


cout << "0";
cout << sec << endl;
}

int main() {
ClockType myClock(5, 12, 40);
myClock.printTime();

ClockType defaultClock;
defaultClock.printTime();

return 0;
}

//// cau 5
#include <iostream>
using namespace std;

class Room {
public:
Room(double length, double breadth, double height); // constructor
double calculateArea(); // tính diện tích
double calculateVolume(); // tính thể tích
private:
double length;
double breadth;
double height;
};

// Constructor implementation
Room::Room(double length, double breadth, double height) {
this->length = length;
this->breadth = breadth;
this->height = height;
}

double Room::calculateArea() {
return this->length * this->breadth;
}

double Room::calculateVolume() {
return this->length * this->breadth * this->height;
}

int main() {
cout << "Constructor test" << endl;
Room r(20, 3, 4);
Room *r2 = new Room(10.5, 5.5, 5.4);

cout << "Area of room 1: " << r2->calculateArea() << endl;


cout << "Volume of room 1: " << r2->calculateVolume() << endl;
delete r2;
return 0;
}
// cau 6

void ClockType::setTime(int hr, int min, int sec) {


this->hr = (hr >= 0 && hr < 24) ? hr : 0;
this->min = (min >= 0 && min < 60) ? min : 0;
this->sec = (sec >= 0 && sec < 60) ? sec : 0;
}

You might also like