diff --git a/1.cpp b/1.cpp index 190c209..e4b1aca 100644 --- a/1.cpp +++ b/1.cpp @@ -2,39 +2,48 @@ using namespace std; class container { - +protected: int size; public: float* p; - container(int s) :size(s){} - const int& getsize() { return size;} - + container(int s) :size(s) {} + const int& getsize()const { return size; } + int& getsize() { + return size; + } }; class vector :public container { - + int len; int call_num; public: - explicit vector(int l) :len(l),size(1 * 100){ + explicit vector(int l) :len(l), container(1 * 100) { p = new float(); } - int len; - int& getlen() const { - call_num ++; + vector(container obj) :len(0),container(obj.getsize()){ + p = new float(); + } + const int& getlen() const { + return len; + } + int& getlen() { + call_num++; return len; } - ~vector() = default; + ~vector() { + delete p; + } }; int main() { container c1(100); - vector v1 = c1; + vector v1 =c1; container& r1 = v1; container c2 = 100; c2.getsize() = 20; cout << c2.getsize(); - vector v2 = 100; - v2.getlen = 40; + vector v2 = (vector)100; + v2.getlen() = 40; cout << v2.getlen(); } \ No newline at end of file diff --git a/2.cpp b/2.cpp index 6a27746..a9cd066 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 #define MAX_SIZE 200 int arr[MAX_SIZE]; - +#pragma warning (disable:4996) typedef struct alfa * alfaptr; struct alfa { @@ -10,14 +10,18 @@ struct alfa { alfaptr next; }; alfaptr rear = NULL, front = NULL; -void push(int x) -{ +void push(int x) { alfaptr node; node = (alfaptr)malloc(sizeof(struct alfa)); node->x = x; + node->next = NULL; if (!front) + { front = node; - else { + rear = node; + } + else + { rear->next = node; rear = node; } @@ -30,57 +34,76 @@ void pop() printf("ERROR1"); else { - node = front->next; - front = node; + node = front; + front = front->next; + free(node); } } void search(int x) { alfaptr node = front; int counter = 0; - while (node) - if (node->x == x) + while (node) { + if (node->x == x) { printf("%d", counter); - else { - printf("ERROR2"); - break; + return; } node = node->next; + counter++; + } + printf("ERROR2"); } void rpop() {//pop last element alfaptr node = front; - while (node) - node = node->next; - free(rear); - rear = node; + if (!front) + printf("ERROR1"); + else if (front == rear) { // there is only one element in the list + free(front); + front = NULL; + rear = NULL; + } + else { + while (node->next != rear) { + node = node->next; + } + node->next = NULL; + free(rear); + rear = node; + } } -void set() -{ +void set(){ alfaptr node = front; 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; + int count = 0; + while (node) { + count++; + node = node->next; + } return count; } void show() { if (!front) { - for (int i = 0; i < MAX_SIZE; i++) + for (int i = 0; i < MAX_SIZE&&arr[i]!=0; i++) printf("%d ", arr[i]); + printf("\n"); } else { - printf("ERROR3"); + alfaptr node = front; + while (node) { + printf("%lld ", node->x); + node = node->next; + } + printf("\n"); } } @@ -88,7 +111,7 @@ int average() { alfaptr node = front; - int sum = 0, count; + int sum = 0, count=0; while (node) { sum += node->x; count++; @@ -129,6 +152,9 @@ void main() case 7://size printf("%d", size()); break; + case 8: + printf("%d", average()); + break; case 10: exit(0); } diff --git a/4.cpp b/4.cpp index a9a32f2..95b1838 100644 --- a/4.cpp +++ b/4.cpp @@ -2,8 +2,9 @@ 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[0]; + float* ptr2 = ptr1 + 3; printf("%f", *ptr2 - *ptr1); return 0; -} \ No newline at end of file +} +//output:78.000000 \ No newline at end of file diff --git a/5.cpp b/5.cpp index e9a1737..948d0d0 100644 --- a/5.cpp +++ b/5.cpp @@ -2,9 +2,11 @@ int main() { int arr[] = { 10, 20, 30, 40, 50, 60 }; - int *ptr1 = arr; - int *ptr2 = arr + 5; + int* ptr1 = arr; + int* ptr2 = arr + 5; printf("%d\n", (*ptr2 - *ptr1)); printf("%c", (char)(*ptr2 - *ptr1)); return 0; -} \ No newline at end of file +} +//output://50 +//2 \ No newline at end of file diff --git a/6.cpp b/6.cpp index bb721c3..7f599b1 100644 --- a/6.cpp +++ b/6.cpp @@ -1,11 +1,12 @@ #include int main() { - int a; - char *x; - x = (char *)&a; - a = 512; - x[0] = 1; - printf("%d\n", a); - return 0; + int a; + char* x; + x = (char*)&a; + a = 512; + x[0] = 1; + printf("%d\n", a); + return 0; } +//output:513 diff --git a/7.cpp b/7.cpp index 7b065a0..cf488e9 100644 --- a/7.cpp +++ b/7.cpp @@ -2,9 +2,10 @@ int main() { int arr[] = { 1, 2, 3, 4, 5 }; - int *p = arr; - ++*p; + int* p = arr; + ++* p; p += 2; printf("%d", *p); return 0; -} \ No newline at end of file +} +//3 \ No newline at end of file diff --git a/8.cpp b/8.cpp index 76b870c..f6de89e 100644 --- a/8.cpp +++ b/8.cpp @@ -1,13 +1,14 @@ #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)); - + } +//output:Be WooW \ No newline at end of file