From 544120168f30f811aa64f80a996743daa20bac8e Mon Sep 17 00:00:00 2001 From: parsa nikbakht <133770139+Nikm2023@users.noreply.github.com> Date: Tue, 16 May 2023 16:46:39 +0430 Subject: [PATCH 1/4] Update 1.cpp --- 1.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/1.cpp b/1.cpp index 190c209..97b21c7 100644 --- a/1.cpp +++ b/1.cpp @@ -7,19 +7,22 @@ class container { public: float* p; container(int s) :size(s){} - const int& getsize() { return size;} + int& getsize() { return size;} }; class vector :public container { - int call_num; + static int call_num; public: - explicit vector(int l) :len(l),size(1 * 100){ + explicit vector(int l) :len(l), container(100 * l){ + p = new float(); + } + vector(container c1):len(c1.getsize() / 100),container(c1.getsize()){ p = new float(); } int len; - int& getlen() const { + int& getlen() { call_num ++; return len; } @@ -35,6 +38,6 @@ int main() { c2.getsize() = 20; cout << c2.getsize(); vector v2 = 100; - v2.getlen = 40; + v2.getlen() = 40; cout << v2.getlen(); } \ No newline at end of file From 94b4075d88721c5ba39b4ce2d8ace5a188187584 Mon Sep 17 00:00:00 2001 From: parsa nikbakht <133770139+Nikm2023@users.noreply.github.com> Date: Tue, 16 May 2023 16:52:17 +0430 Subject: [PATCH 2/4] Update 2.cpp --- 2.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/2.cpp b/2.cpp index 6a27746..23cea79 100644 --- a/2.cpp +++ b/2.cpp @@ -5,11 +5,11 @@ 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; } From f8dd379ddd22c5baaf59e7f2c2f801f4296c5f9c Mon Sep 17 00:00:00 2001 From: parsa nikbakht <133770139+Nikm2023@users.noreply.github.com> Date: Tue, 16 May 2023 18:11:56 +0430 Subject: [PATCH 3/4] Update 3.cpp --- 3.cpp | 84 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 39 deletions(-) diff --git a/3.cpp b/3.cpp index c8732ef..6f8676d 100644 --- a/3.cpp +++ b/3.cpp @@ -3,79 +3,85 @@ #define MAX_SIZE 200 int arr[MAX_SIZE]; -typedef struct alfa * alfaptr; +typedef struct alfa* alfaptr; struct alfa { - long long x; + long long int x; alfaptr next; }; -alfaptr rear = NULL, front = NULL; -void push(int x) +alfaptr rear = NULL, front = NULL, tail = NULL; +void push(long long int x) { alfaptr node; node = (alfaptr)malloc(sizeof(struct alfa)); node->x = x; - if (!front) - front = node; + if (!front) { + rear = tail = front = node; + node->next = NULL; + } else { - rear->next = node; rear = node; + rear->next = tail; + tail = rear; } } void pop() { - alfaptr node; - if (!front) + if (!front) { printf("ERROR1"); - else - { - node = front->next; - front = node; + return; + } + if (front == rear) { + front = rear = tail = NULL; + return; } + alfaptr tmp = rear; + for (; tmp->next != front; tmp = tmp->next){} + tmp->next = NULL; + front = tmp; } -void search(int x) +int search(long long int x) { - alfaptr node = front; + alfaptr node = rear; int counter = 0; - while (node) + while (node) { if (node->x == x) - printf("%d", counter); - else { - printf("ERROR2"); - break; - } + counter++; node = node->next; + } + return counter; } void rpop() {//pop last element - alfaptr node = front; - while (node) - node = node->next; - free(rear); - rear = node; + alfaptr tmp = rear; + tail = rear = tmp->next; + free(tmp); } void set() { - alfaptr node = front; + alfaptr node = rear; for (int i = 0; i < MAX_SIZE && node; i++, node = node->next) arr[i] = node->x; } int size() { - alfaptr node = front; - int count; - while (node) - count++;node = node->next; + alfaptr node = rear; + int count = 0; + while (node) { + count++; + node = node->next; + } return count; } void show() { - if (!front) { - for (int i = 0; i < MAX_SIZE; i++) + if (front) { + alfaptr node = rear; + for (int i = 0; i < MAX_SIZE && node; i++, node = node->next) printf("%d ", arr[i]); } else @@ -88,7 +94,7 @@ int average() { alfaptr node = front; - int sum = 0, count; + int sum = 0, count = 0; while (node) { sum += node->x; count++; @@ -103,11 +109,11 @@ void main() long long int x; while (true) { - scanf("%d", &cmd); + scanf_s("%d", &cmd); switch (cmd) { case 1://push - scanf("%lld", &x); + scanf_s("%lld", &x); push(x); break; case 2://pop @@ -117,8 +123,8 @@ void main() rpop(); break; case 4://search - scanf("%lld", &x); - search(x); + scanf_s("%lld", &x); + printf("%d\n", search(x)); break; case 5://set set(); @@ -127,7 +133,7 @@ void main() show(); break; case 7://size - printf("%d", size()); + printf("%d\n", size()); break; case 10: exit(0); From 10e217eff6d5b9e80d02afd0a82b8786ffcd3660 Mon Sep 17 00:00:00 2001 From: parsa nikbakht <133770139+Nikm2023@users.noreply.github.com> Date: Tue, 16 May 2023 21:05:35 +0430 Subject: [PATCH 4/4] changed --- 4.cpp | 4 ++-- 5.cpp | 8 ++++---- 6.cpp | 6 +++--- 7.cpp | 6 +++--- 8.cpp | 8 +++----- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/4.cpp b/4.cpp index a9a32f2..a60921b 100644 --- a/4.cpp +++ b/4.cpp @@ -2,8 +2,8 @@ int main() { float arr[5] = { 12.5, 10.0, 13.5, 90.5, 0.5 }; - float *ptr1 = &arr[0]; - float *ptr2 = ptr1 + 3; + float* ptr1 = arr; + float* ptr2 = arr + 3; printf("%f", *ptr2 - *ptr1); return 0; } \ No newline at end of file diff --git a/5.cpp b/5.cpp index e9a1737..e815099 100644 --- a/5.cpp +++ b/5.cpp @@ -2,9 +2,9 @@ int main() { int arr[] = { 10, 20, 30, 40, 50, 60 }; - int *ptr1 = arr; - int *ptr2 = arr + 5; - printf("%d\n", (*ptr2 - *ptr1)); - printf("%c", (char)(*ptr2 - *ptr1)); + int* ptr1 = arr; + int* ptr2 = &arr[5]; + printf("%d\n", *ptr2 - *ptr1); + printf("%c", (float)(*ptr2 - *ptr1)); return 0; } \ No newline at end of file diff --git a/6.cpp b/6.cpp index bb721c3..0ebacaa 100644 --- a/6.cpp +++ b/6.cpp @@ -1,9 +1,9 @@ #include int main() { - int a; - char *x; - x = (char *)&a; + char a; + char* x; + x = &a; a = 512; x[0] = 1; printf("%d\n", a); diff --git a/7.cpp b/7.cpp index 7b065a0..d04dcde 100644 --- a/7.cpp +++ b/7.cpp @@ -2,9 +2,9 @@ int main() { int arr[] = { 1, 2, 3, 4, 5 }; - int *p = arr; - ++*p; - p += 2; + int* p = arr; + ++* p; + *p += 2; printf("%d", *p); return 0; } \ No newline at end of file diff --git a/8.cpp b/8.cpp index 76b870c..cabd8bb 100644 --- a/8.cpp +++ b/8.cpp @@ -1,13 +1,11 @@ #include -const char * f(const char **p) { +const char* f(const char** p) { auto q = (p + sizeof(char))[1]; return q; } int main() { - const char * str[] = { "Wish","You","Best",":D" }; + const char* str[] = { "Wish","You","Best",":D" }; printf("%c%c ", *f(str), *(f(str) + 1)); printf("%c%c%c%c\n", **str, *(*(str + 1) + 1), *((str + 2)[-1] + 1), **&*(&str[-1] + 1)); - - - + // I have no idea :) }