Skip to content
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
26 changes: 20 additions & 6 deletions 1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,47 @@ class container {
float* p;
container(int s) :size(s){}
const int& getsize() { return size;}
void setsize(int s){
size=s;
}

};

class vector :public container {

int call_num;
public:
explicit vector(int l) :len(l),size(1 * 100){
vector():len(0),container(0){
p=NULL;
}
vector(int l) :len(l),container(1 * 100){
p = new float();
}
int len;
int& getlen() const {
int& getlen() {
call_num ++;
return len;
}
void setlen(int l){
len=l;
}
~vector() = default;
vector& operator=(container& c){
setsize(c.getsize());
len=0;
return *this;
}
};

int main() {

container c1(100);
vector v1 = c1;
vector v1;
v1=c1;
container& r1 = v1;
container c2 = 100;
c2.getsize() = 20;
c2.setsize(20);
cout << c2.getsize();
vector v2 = 100;
v2.getlen = 40;
v2.setlen(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
153 changes: 82 additions & 71 deletions 3.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 200
int arr[MAX_SIZE];

int arr[MAX_SIZE]= {0};
//we want to create a linklist
typedef struct alfa * alfaptr;

struct alfa {
long long x;
struct alfa {//we have a struct(alfa) have one data of long long int and one pointer
long long int x;
alfaptr next;
};
alfaptr rear = NULL, front = NULL;
void push(int x)
{
alfaptr node;
node = (alfaptr)malloc(sizeof(struct alfa));
alfaptr rear = NULL, front = NULL;//add new node
void push(int x) { //this function push new node to our linklist
alfaptr node;//we have a pointer that save the address of new node in it
node = (alfaptr)malloc(sizeof(struct alfa));//get memory for new node
node->x = x;
if (!front)
if (!front) { //if the new node is the first node of linklist
front = node;
else {
rear=node;
} else {
rear->next = node;
rear = node;
rear = node;//the rear pointer should point to the new node
rear->next=NULL;
}
}

void pop()
{
alfaptr node;
if (!front)
void pop() { //remove the first node
alfaptr node,ptr=front;
if (!front)//the linklist is empty
printf("ERROR1");
else
{
if(front->next==NULL) {
free(front);
front=NULL;
rear=NULL;
return;
} else {
node = front->next;
front = node;
free(ptr);
}
}
void search(int x)
{
void search(int x) { //search one data in our list while find one data that not equal to this data
alfaptr node = front;
int counter = 0;
while (node)
Expand All @@ -45,50 +49,57 @@ void search(int x)
printf("ERROR2");
break;
}
node = node->next;
node = node->next;
}

void rpop() {//pop last element
void rpop() {//pop last element(remove the node from the last of the linklist)
alfaptr node = front;
while (node)
if(front->next==NULL) {
free(front);
front=NULL;
rear=NULL;
return;
}
while (node->next->next!=NULL)
node = node->next;
free(rear);
rear = node;
node->next=NULL;
}

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()
{
int size() { //this function count the nodes and return the number of nodes
alfaptr node = front;
int count;
while (node)
count++;node = node->next;
int count=0;
if(front==NULL)
return count;
while (node!=NULL) {
count++;
node = node->next;
}
return count;
}

void show()
{
if (!front) {
void show() {//show the all member of arr if the linklist is not empty
if (!front)
printf("ERROR3");
else {
for (int i = 0; i < MAX_SIZE; i++)
printf("%d ", arr[i]);
}
else
{
printf("ERROR3");
}
}

int average()
{
int average() {//this function measure the average number and return it

alfaptr node = front;
int sum = 0, count;
int sum = 0, count=0;
if(front==NULL)//default average
return 0;
while (node) {
sum += node->x;
count++;
Expand All @@ -97,40 +108,40 @@ int average()
return sum / count;
}

void main()
{
int main() {
int cmd;
long long int x;
while (true)
{
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);
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 9:
printf("%d", average());
case 10:
exit(0);
}
}
return 0;
}
6 changes: 5 additions & 1 deletion 4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ int main()
float *ptr2 = ptr1 + 3;
printf("%f", *ptr2 - *ptr1);
return 0;
}
}/*this program first build a float_array contain of
5 memberes and then we have pointer1 that have the address
of the first house of array and the second pointer that have the
address of the fourth house of array and then we print the defference
between the tow number that these these two pointers point to and the output is 78*/
11 changes: 8 additions & 3 deletions 5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ 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));
printf("%d\n", (*ptr2 - *ptr1));//the output is 50
printf("%c", (char)(*ptr2 - *ptr1));//the output is 2
return 0;
}
}/*we have a array of 6 number,we have the first
pointer that point to the first house of array and
the second pointer that point the sixth house of array
then we print the deference between the number that pointer
2 point and the number that pointer 1 point,then we print the
fifth char of askii table*/
5 changes: 3 additions & 2 deletions 6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ int main()
x = (char *)&a;
a = 512;
x[0] = 1;
printf("%d\n", a);
printf("%d\n", a);//print 513
return 0;
}
}/*we have a int named a
than we ++a and then print it*/
6 changes: 3 additions & 3 deletions 7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int *p = arr;
++*p;
p += 2;
printf("%d", *p);
++*p;//first ++p and then *p
p += 2;//arr[0]+2
printf("%d", *p);//the output is 3
return 0;
}
9 changes: 5 additions & 4 deletions 8.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include<stdio.h>
const char * f(const char **p) {
auto q = (p + sizeof(char))[1];
const char * f(const char **p) {//this fuction get char **
auto q = (p + sizeof(char))[1];//move the pointer forward one house and then again move it forward one house
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));
printf("%c%c ", *f(str), *(f(str) + 1));//the *f(str)==B , the f(str)+1 the address of the house B and *(f(str)+1)==e the result is Be
printf("%c%c%c%c\n", **str, *(*(str + 1) + 1), *((str + 2)[-1] + 1), **&*(&str[-1] + 1));/***str==w ,*(str + 1)=the address of the house of "you"
*(*(str + 1) + 1)==O ,*((str + 2)[-1] + 1)==o , **&*(&str[-1] + 1)==w the result is woow*/



Expand Down