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

debugging and outputs #26

Open
wants to merge 8 commits 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
23 changes: 13 additions & 10 deletions 1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
using namespace std;

class container {

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

};

class vector :public container {

int call_num;
public:
explicit vector(int l) :len(l),size(1 * 100){
p = new float();
explicit vector(int l) :len(l) {
size = 1 * 100;
p = new float[size];
}
int len;
int& getlen() const {
call_num ++;
int& getlen() {
call_num++; //you can not change this object because this function is const
return len;
}
~vector() = default;
Expand All @@ -29,12 +31,13 @@ class vector :public container {
int main() {

container c1(100);
vector v1 = c1;
//vector v1 = c1; you can not put a parent in its child
vector v1(100);
container& r1 = v1;
container c2 = 100;
c2.getsize() = 20;
cout << c2.getsize();
vector v2 = 100;
v2.getlen = 40;
vector v2(100); // can not be implicit like this : vector v2 = 100;
v2.getlen() = 40;
cout << v2.getlen();
}
8 changes: 4 additions & 4 deletions 2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
59 changes: 38 additions & 21 deletions 3.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include<stdio.h>
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 200
int arr[MAX_SIZE];

typedef struct alfa * alfaptr;
typedef struct alfa* alfaptr;

struct alfa {
long long x;
Expand All @@ -15,19 +16,22 @@ void push(int x)
alfaptr node;
node = (alfaptr)malloc(sizeof(struct alfa));
node->x = x;
if (!front)
if (front) {
alfaptr temp = front;
front = node;
front->next = front;
}
else {
rear->next = node;
rear = node;
front = node;
}
}

void pop()
{
alfaptr node;
if (!front)
printf("ERROR1");
printf("ERROR1\n");
else
{
node = front->next;
Expand All @@ -38,19 +42,27 @@ void search(int x)
{
alfaptr node = front;
int counter = 0;
while (node)
if(node == NULL) {
printf("ERROR2");
return;
}
while (node) {
if (node->x == x)
printf("%d", counter);
else {
printf("ERROR2");
break;
}
printf("%d\n", counter);
node = node->next;
}
}

void rpop() {//pop last element
alfaptr node = front;
while (node)
if (!node)
return;
if (!node->next) {
free(rear);
rear = front;
return;
}
while (node->next->next)
node = node->next;
free(rear);
rear = node;
Expand All @@ -59,36 +71,38 @@ void rpop() {//pop last element
void set()
{
alfaptr node = front;
for (int i = 0; i < MAX_SIZE && node; i++, node = node->next)
for (int i = 0; (i < MAX_SIZE) && (node != NULL); 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) {
if (front) {
for (int i = 0; i < MAX_SIZE; i++)
printf("%d ", arr[i]);
}
else
{
printf("ERROR3");
printf("ERROR3\n");
}
}

int average()
double average()
{

alfaptr node = front;
int sum = 0, count;
int sum = 0, count = 0;
while (node) {
sum += node->x;
count++;
Expand Down Expand Up @@ -127,7 +141,10 @@ void main()
show();
break;
case 7://size
printf("%d", size());
printf("%d\n", size());
break;
case 8://average
printf("%d\n", average());
break;
case 10:
exit(0);
Expand Down
7 changes: 4 additions & 3 deletions 4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
//output : 78.000000
8 changes: 5 additions & 3 deletions 5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
//output : 50 //first line
//output : 2 //second lise
5 changes: 3 additions & 2 deletions 6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
int main()
{
int a;
char *x;
x = (char *)&a;
char* x;
x = (char*)&a;
a = 512;
x[0] = 1;
printf("%d\n", a);
return 0;
}
//output : 513
7 changes: 4 additions & 3 deletions 7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
//output : 3
7 changes: 4 additions & 3 deletions 8.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include<stdio.h>
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