diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..d512167 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++.exe build active file", + "command": "C:\\MinGW64-2.0\\bin\\g++.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/1.cpp b/1.cpp index fedde2c..82e05d5 100644 --- a/1.cpp +++ b/1.cpp @@ -1,40 +1,39 @@ #include #include -#include +#include using namespace std; -long long *b; +unsigned long long *b; -long long int factorial(int n) +unsigned long long int factorial(int n) { return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n; } -long long int *producingTheFactorialFractions() +unsigned long long int *producingTheFactorialFractions() { - long long b[10]; + b = new unsigned long long[10]; for (int i = 10; i >= 0; i--) { - b[i] += (int)pow(factorial(10), 2.0) / (i + 1); + b[i] = (int)pow(factorial(i), 2.0) / (i + 1); } return b; } -void checkZeros(long long *a) +void checkZeros(unsigned long long *a) { for (int i = 9; i >= 0; i--) { - if (a[i] = 0) + if (a[i] == 0) cout << "Zero Found" << endl; } } int main() { - - long long int *a; + unsigned long long int *a; a = producingTheFactorialFractions(); checkZeros(a); for (int i = 0; i < 10; i++) @@ -43,8 +42,6 @@ int main() } delete a; - cout<<"hello"; - cout<<"Bye"; - - + cout << "hello"; + cout << "Bye"; } \ No newline at end of file diff --git a/1.exe b/1.exe new file mode 100644 index 0000000..4449a44 Binary files /dev/null and b/1.exe differ diff --git a/2.cpp b/2.cpp index 6a27746..1855163 100644 --- a/2.cpp +++ b/2.cpp @@ -4,23 +4,28 @@ 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 countAllSpecificChars(string sArr[], int arrLength, char specificChar) +{ + 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) count++; + } + } return count; } -int main() { +int main() +{ string sArr[4] = { - "I am", - "in", - "ap", - "class" - }; + "I am", + "in", + "ap", + "class"}; char findIt; cin >> findIt; cout << countAllSpecificChars(sArr, 4, findIt); diff --git a/2.exe b/2.exe new file mode 100644 index 0000000..913893a Binary files /dev/null and b/2.exe differ diff --git a/3.cpp b/3.cpp index 77eb722..07d9d00 100644 --- a/3.cpp +++ b/3.cpp @@ -1,23 +1,31 @@ -#include -#include +#include +using namespace std; #define MAX_SIZE 200 int arr[MAX_SIZE]; -typedef struct alfa * alfaptr; +typedef struct alfa *alfaptr; -struct alfa { - long long x; +struct alfa +{ + int x; alfaptr next; }; + alfaptr rear = NULL, front = NULL; + void push(int x) { alfaptr node; - node = (alfaptr)malloc(sizeof(struct alfa)); + node = new alfa; node->x = x; + node->next = NULL; if (!front) + { front = node; - else { + rear = node; + } + else + { rear->next = node; rear = node; } @@ -25,35 +33,65 @@ void push(int x) void pop() { - alfaptr node; if (!front) - printf("ERROR1"); + cout << "ERROR 1" << endl; else { - node = front->next; - front = node; + if (front == rear) + { + delete front; + front = NULL; + rear = NULL; + } + else + { + alfaptr node; + node = front->next; + delete front; + front = node; + } } } + void search(int x) { alfaptr node = front; - int counter = 0; + int s_counter = 0; while (node) + { if (node->x == x) - printf("%d", counter); - else { - printf("ERROR2"); - break; + { + cout << s_counter << endl; + return; } - node = node->next; + else + { + s_counter++; + node = node->next; + } + } + cout << "ERROR 2" << endl; } -void rpop() {//pop last element - alfaptr node = front; - while (node) - node = node->next; - free(rear); - rear = node; +void rpop() +{ // pop last element + if (rear == front) + { + delete front; + front = NULL; + rear = NULL; + } + else + { + alfaptr node = front; + while (node->next->next) + { + node = node->next; + } + delete rear; + rear = node; + rear->next = NULL; + } } void set() @@ -66,21 +104,26 @@ void set() int size() { alfaptr node = front; - int count; + int counter = 0; while (node) - count++;node = node->next; - return count; + { + counter++; + node = node->next; + } + node = node->next; + return counter; } void show() { - if (!front) { + if (!front) + { for (int i = 0; i < MAX_SIZE; i++) - printf("%d ", arr[i]); + cout << arr[i] << endl; } else { - printf("ERROR3"); + cout << "ERROR3" << endl; } } @@ -88,8 +131,9 @@ int average() { alfaptr node = front; - int sum = 0, count; - while (node) { + int sum = 0, count = 0; + while (node) + { sum += node->x; count++; node = node->next; @@ -97,37 +141,37 @@ int average() return sum / count; } -void main() +int main() { int cmd; long long int x; while (true) { - scanf("%d", &cmd); + cin >> cmd; switch (cmd) { - case 1://push - scanf("%lld", &x); + case 1: // push + cin >> x; push(x); break; - case 2://pop + case 2: // pop pop(); break; - case 3://rpop + case 3: // rpop rpop(); break; - case 4://search - scanf("%lld", &x); + case 4: // search + cin >> x; search(x); break; - case 5://set + case 5: // set set(); break; - case 6://show + case 6: // show show(); break; - case 7://size - printf("%d", size()); + case 7: // size + cout << size() << endl; break; case 10: exit(0); diff --git a/3.exe b/3.exe new file mode 100644 index 0000000..e07ec70 Binary files /dev/null and b/3.exe differ diff --git a/4.cpp b/4.cpp index a9a32f2..22a71ca 100644 --- a/4.cpp +++ b/4.cpp @@ -1,9 +1,10 @@ -#include +#include +using namespace std; int main() { - float arr[5] = { 12.5, 10.0, 13.5, 90.5, 0.5 }; + float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5}; float *ptr1 = &arr[0]; float *ptr2 = ptr1 + 3; - printf("%f", *ptr2 - *ptr1); + cout << (*ptr2 - *ptr1) << endl; return 0; } \ No newline at end of file diff --git a/4.exe b/4.exe new file mode 100644 index 0000000..7c34d06 Binary files /dev/null and b/4.exe differ diff --git a/5.cpp b/5.cpp index 1be3d10..abfd252 100644 --- a/5.cpp +++ b/5.cpp @@ -1,10 +1,12 @@ -#include +#include +using namespace std; + int main() { - int arr[] = { 10, 20, 30, 40, 50, 60 }; + 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)); + cout << (*ptr2 - *ptr1) << endl; + cout << (char)(*ptr2 - *ptr1) << endl; return 0; } \ No newline at end of file diff --git a/5.exe b/5.exe new file mode 100644 index 0000000..833893e Binary files /dev/null and b/5.exe differ diff --git a/6.cpp b/6.cpp index f8b3141..fd05637 100644 --- a/6.cpp +++ b/6.cpp @@ -1,4 +1,6 @@ -#include +#include +using namespace std; + int main() { int a; @@ -6,6 +8,6 @@ int main() x = (char *)&a; a = 512; x[0] = 1; - printf("%d\n", a); + cout< +#include +using namespace std; + int main() { int arr[] = { 1, 2, 3, 4, 5 }; int *p = arr; ++*p; p += 2; - printf("%d", *p); + cout<<*p< -const char * f(const char **p) { +#include +using namespace std; + +const char *f(const char **p) +{ auto q = (p + sizeof(char))[1]; return q; } -int main() { - 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)); - - - +int main() +{ + const char *str[] = {"Wish", "You", "Best", ":D"}; + cout << *f(str) << *(f(str) + 1) << endl; + cout << **str << *(*(str + 1) + 1) << *((str + 2)[-1] + 1) << **&*(&str[-1] + 1) << endl; } diff --git a/8.exe b/8.exe new file mode 100644 index 0000000..a4b5147 Binary files /dev/null and b/8.exe differ