Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions 1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,40 @@
using namespace std;

class container {

int size;
int size;
public:
float* p;
container(int s) :size(s){}
const int& getsize() { return size;}

container(int s) : size(s) {}
int& getsize() { return size; } // Return non-constant reference
};

class vector :public container {

int call_num;
class MyVector : public container {
int call_num;
int len;
public:
explicit vector(int l) :len(l),size(1 * 100){
p = new float();
}
int len;
int& getlen() const {
call_num ++;
return len;
}
~vector() = default;
explicit MyVector(int l) : container(1 * 100), len(l), call_num(0) {
p = new float();
}

int& getlen() {
call_num++;
return len;
}

~MyVector() = default;
float* p;
};

int main() {
container c1(100);
MyVector v1(100);
container& r1 = v1;
container c2(100);
c2.getsize() = 20;
cout << c2.getsize() << endl;

MyVector v2(100);
v2.getlen() = 40;
cout << v2.getlen() << endl;

container c1(100);
vector v1 = c1;
container& r1 = v1;
container c2 = 100;
c2.getsize() = 20;
cout << c2.getsize();
vector v2 = 100;
v2.getlen = 40;
cout << v2.getlen();
}
return 0;
}
18 changes: 10 additions & 8 deletions 2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

using namespace std;

// count all the specific char in the whole array of strings
int countAllSpecificChars(string sArr[], int arrLength, char specificChar) {
int count;
for (int i = 0; i <= arrLength; ++i)
for (int j = 0; j <= sArr[i].size(); ++j)
// if the jth char of the string is the specific char
if (sArr[i][j] = specificChar)
int count = 0;
for (int i = 0; i < arrLength; ++i) {
for (int j = 0; j < sArr[i].size(); ++j) {
if (sArr[i][j] == specificChar) {
count++;
}
}
}
return count;
}

Expand All @@ -22,6 +23,7 @@ int main() {
"class"
};
char findIt;
cin >> findIt;
cin >> findIt;
cout << countAllSpecificChars(sArr, 4, findIt);
}
return 0;
}
252 changes: 138 additions & 114 deletions 3.cpp
Original file line number Diff line number Diff line change
@@ -1,136 +1,160 @@
#include<stdio.h>
#include<stdlib.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 200

int arr[MAX_SIZE];

typedef struct alfa * alfaptr;
typedef struct alfa *alfaptr;

struct alfa {
long long x;
alfaptr next;
long long x;
alfaptr next;
};

alfaptr rear = NULL, front = NULL;
void push(int x)
{
alfaptr node;
node = (alfaptr)malloc(sizeof(struct alfa));
node->x = x;
if (!front)
front = node;
else {
rear->next = node;
rear = node;
}

void push(int x) {
alfaptr node;
node = (alfaptr)malloc(sizeof(struct alfa));
node->x = x;
node->next = NULL; // Initialize next pointer to NULL

if (!front)
front = node;
else {
rear->next = node;
}
rear = node; // Update rear pointer
}

void pop()
{
alfaptr node;
if (!front)
printf("ERROR1");
else
{
node = front->next;
front = node;
}
void pop() {
if (!front)
printf("ERROR1\n");
else {
alfaptr node = front;
front = front->next;
free(node);
}
}
void search(int x)
{
alfaptr node = front;
int counter = 0;
while (node)
if (node->x == x)
printf("%d", counter);
else {
printf("ERROR2");
break;
}
node = node->next;

void search(int x) {
alfaptr node = front;
int counter = 0;
while (node) {
if (node->x == x) {
printf("%d\n", counter);
return;
}
node = node->next;
counter++;
}
printf("ERROR2\n");
}

void rpop() {//pop last element
alfaptr node = front;
while (node)
node = node->next;
free(rear);
rear = node;
void rpop() { // pop last element
alfaptr node = front;
alfaptr prev = NULL;

if (!front) {
printf("ERROR1\n");
return;
}

while (node->next) {
prev = node;
node = node->next;
}

if (prev)
prev->next = NULL;
else
front = NULL;

rear = prev; // Update rear pointer

free(node);
}

void set()
{
alfaptr node = front;
for (int i = 0; i < MAX_SIZE && node; i++, node = node->next)
arr[i] = node->x;
void set() {
alfaptr node = front;
int i = 0;

while (i < MAX_SIZE && node) {
arr[i] = node->x;
node = node->next;
i++;
}
}

int size()
{
alfaptr node = front;
int count;
while (node)
count++;node = node->next;
return count;
int size() {
alfaptr node = front;
int count = 0;

while (node) {
count++;
node = node->next;
}
return count;
}

void show()
{
if (!front) {
for (int i = 0; i < MAX_SIZE; i++)
printf("%d ", arr[i]);
}
else
{
printf("ERROR3");
}
void show() {
if (!front) {
for (int i = 0; i < MAX_SIZE; i++)
printf("%d ", arr[i]);
} else {
printf("ERROR3\n");
}
}

int average()
{

alfaptr node = front;
int sum = 0, count;
while (node) {
sum += node->x;
count++;
node = node->next;
}
return sum / count;
int average() {
alfaptr node = front;
int sum = 0, count = 0;

while (node) {
sum += node->x;
count++;
node = node->next;
}

return count > 0 ? sum / count : 0; // Check for division by zero
}

void main()
{
int cmd;
long long int x;
while (true)
{
scanf("%d", &cmd);
switch (cmd)
{
case 1://push
scanf("%lld", &x);
push(x);
break;
case 2://pop
pop();
break;
case 3://rpop
rpop();
break;
case 4://search
scanf("%lld", &x);
search(x);
break;
case 5://set
set();
break;
case 6://show
show();
break;
case 7://size
printf("%d", size());
break;
case 10:
exit(0);
}
}
}
int main() {
int cmd;
long long int x;

while (1) {
scanf("%d", &cmd);

switch (cmd) {
case 1: // push
scanf("%lld", &x);
push(x);
break;
case 2: // pop
pop();
break;
case 3: // rpop
rpop();
break;
case 4: // search
scanf("%lld", &x);
search(x);
break;
case 5: // set
set();
break;
case 6: // show
show();
break;
case 7: // size
printf("%d\n", size());
break;
case 10:
exit(0);
}
}

return 0;
}
Binary file added 8.exe
Binary file not shown.
2 changes: 0 additions & 2 deletions README.md

This file was deleted.