Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pooya golestanfar debuged your code 40129703 #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
36 changes: 35 additions & 1 deletion 1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ using namespace std;
class container {

int size;
friend class vector;
public:
float* p;
container(int s) :size(s){}
const int& getsize() { return size;}
container() { size = 0; }
float* p;
container(int s) :size(s) {}
int& getsize() { return size; }

protected:
int size;
};

class vector :public container {

int call_num;
int call_num;
public:
explicit vector(int l) :len(l),size(1 * 100){
p = new float();
Expand All @@ -24,6 +32,23 @@ class vector :public container {
return len;
}
~vector() = default;
explicit vector(int l) :len(l), container(1 * 100) {
p = new float[size];
}
vector(const container& con) :container(1 * 100) {

this->size = size;
this->p = new float[con.size];
len = 0;

}
int len;
//len is useless!!
int& getlen() {
call_num++;
return len;
}
~vector() = default;
};

int main() {
Expand All @@ -37,4 +62,13 @@ int main() {
vector v2 = 100;
v2.getlen = 40;
cout << v2.getlen();
}
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();
}
8 changes: 5 additions & 3 deletions 2.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#include <iostream>
#include <string>

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)
int count=0;
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)
if (sArr[i][j] == specificChar)
count++;
return count;
}

int main() {
string sArr[4] = {
"I am",
Expand All @@ -24,4 +26,4 @@ int main() {
char findIt;
cin >> findIt;
cout << countAllSpecificChars(sArr, 4, findIt);
}
}
131 changes: 130 additions & 1 deletion 3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
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)
Expand All @@ -21,6 +24,19 @@ void push(int x)
rear->next = node;
rear = node;
}
alfaptr node;
node = (alfaptr)malloc(sizeof(struct alfa));
node->next = NULL;
node->x = x;
if (!front) {
front = node;
rear = front;
}

else {
rear->next = node;
rear = node;
}
}

void pop()
Expand All @@ -33,6 +49,16 @@ void pop()
node = front->next;
front = node;
}

if (!front)
printf("ERROR1");
else
{
alfaptr temp = front->next;
delete front;
front = temp;

}
}
void search(int x)
{
Expand All @@ -46,6 +72,17 @@ void search(int x)
break;
}
node = node->next;
alfaptr node = front;
int counter = 0;
while (node) {
if (node->x == x)
printf("%d", counter);
else {
printf("ERROR2");
break;
}
node = node->next;
}
}

void rpop() {//pop last element
Expand All @@ -54,13 +91,34 @@ void rpop() {//pop last element
node = node->next;
free(rear);
rear = node;
alfaptr node = front;

if (front) {
while (node->next && node->next->next)
node = node->next;
}
else {
return;
}
if (node == front&& node->next==NULL) {
rear = NULL;
front = NULL;
}
else {
free(rear);
node->next = NULL;
rear = node;
}
}

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

int size()
Expand All @@ -70,6 +128,13 @@ int size()
while (node)
count++;node = node->next;
return count;
alfaptr node = front;
int count=0;
while (node) {
count++;
node = node->next;
}
return count;
}

void show()
Expand All @@ -82,9 +147,28 @@ void show()
{
printf("ERROR3");
}
//if (front) {
// alfaptr temp = front;
// while (temp) {
// printf("%d\n", temp->x);
// temp = temp->next;
//
// }
//}
//return;
//or ---------------------(there can be bugs in some situations in this)
if (front) {
for (int i = 0; i < size(); i++)
printf("%d ", arr[i]);
}
else
{
printf("ERROR3");
}
}

int average()
float average()
{

alfaptr node = front;
Expand All @@ -95,9 +179,18 @@ int average()
node = node->next;
}
return sum / count;
alfaptr node = front;
int sum = 0, count=0;
while (node) {
sum += node->x;
count++;
node = node->next;
}
return sum * 1.0 / count;
}

void main()
int main()
{
int cmd;
long long int x;
Expand Down Expand Up @@ -133,4 +226,40 @@ void main()
exit(0);
}
}
}
int cmd;
long long int x;
while (true)
{
scanf_s("%d", &cmd);
switch (cmd)
{
case 1://push
scanf_s("%lld", &x);
push(x);
break;
case 2://pop
pop();
break;
case 3://rpop
rpop();
break;
case 4://search
scanf_s("%lld", &x);
search(x);
break;
case 5://set
set();
break;
case 6://show
show();
break;
case 7://size
printf("%d", size());
break;
case 8:
printf("%f", average());
case 10:
exit(0);
}
}
}
4 changes: 3 additions & 1 deletion 4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ int main()
float *ptr2 = ptr1 + 3;
printf("%f", *ptr2 - *ptr1);
return 0;
}
}
}
// output : 78.000000
5 changes: 4 additions & 1 deletion 5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ int main()
printf("%d\n", (*ptr2 - *ptr1));
printf("%c", (char)(*ptr2 - *ptr1));
return 0;
}
}
}
//output : 50
// 2
2 changes: 2 additions & 0 deletions 6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ int main()
printf("%d\n", a);
return 0;
}
// output : 513
//
4 changes: 3 additions & 1 deletion 7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ int main()
p += 2;
printf("%d", *p);
return 0;
}
}
}
//output: 3
2 changes: 1 addition & 1 deletion 8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ int main() {
printf("%c%c%c%c\n", **str, *(*(str + 1) + 1), *((str + 2)[-1] + 1), **&*(&str[-1] + 1));



}
//output : Be WooW