diff --git a/1.cpp b/1.cpp index 190c209..29f1b32 100644 --- a/1.cpp +++ b/1.cpp @@ -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(); -} \ No newline at end of file + return 0; +} diff --git a/2.cpp b/2.cpp index 6a27746..d2285d1 100644 --- a/2.cpp +++ b/2.cpp @@ -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; } @@ -22,6 +23,7 @@ int main() { "class" }; char findIt; - cin >> findIt; + cin >> findIt; cout << countAllSpecificChars(sArr, 4, findIt); -} \ No newline at end of file + return 0; +} diff --git a/3.cpp b/3.cpp index c8732ef..3dd0ed9 100644 --- a/3.cpp +++ b/3.cpp @@ -1,136 +1,160 @@ -#include -#include +#include +#include + #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); - } - } -} \ No newline at end of file +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; +} diff --git a/8.exe b/8.exe new file mode 100644 index 0000000..39235c1 Binary files /dev/null and b/8.exe differ diff --git a/README.md b/README.md deleted file mode 100644 index 3a019e5..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# APLabGitPractice - باید که کد های این مخزن رو بررسی و دیباگ کنید و کد اصلاح شده رو اینجا ارسال کنید