diff --git a/practices/c/level1/p01_runningLetter/runningLetter.c b/practices/c/level1/p01_runningLetter/runningLetter.c new file mode 100644 index 00000000..d51a0cc1 --- /dev/null +++ b/practices/c/level1/p01_runningLetter/runningLetter.c @@ -0,0 +1,32 @@ +#include +#include + +int main() +{ + int n = 0, pos=0; + + while (TRUE) + { + if (n>30) + { + pos = 60 - n; + } + else + { + pos = n; + } + for (int i = 0; i +#include + +int main() +{ + int n,n_sqrt; + scanf("%d",&n); + n_sqrt=sqrt(n); + for(int i=2;i + +int main() +{ + int i=0; + while(++i) + { + if((i/12.0+i/6.0+i/7.0+5.0+i/2.0+4.0)==i) + { + printf("\n%d",i-4); + break; + } + } + return 0; +} \ No newline at end of file diff --git a/practices/c/level1/p04_ narcissus/narcissus.c b/practices/c/level1/p04_ narcissus/narcissus.c new file mode 100644 index 00000000..b21369da --- /dev/null +++ b/practices/c/level1/p04_ narcissus/narcissus.c @@ -0,0 +1,16 @@ +#include + +int main() +{ + for(int i=100;i<1000;i++) + { + int a=i/100; + int b=(i-a*100)/10; + int c=i%10; + if(a*a*a+b*b*b+c*c*c==i) + { + printf("%4d",i); + } + } + return 0; +} \ No newline at end of file diff --git a/practices/c/level1/p05_allPrimes/allPrimes.c b/practices/c/level1/p05_allPrimes/allPrimes.c new file mode 100644 index 00000000..f8af3453 --- /dev/null +++ b/practices/c/level1/p05_allPrimes/allPrimes.c @@ -0,0 +1,34 @@ +#include + +int main() +{ + int a[999]; + for(int i=0;i<999;i++) + { + a[i]=i+2; + } + for(int i=0;i<999;i++) + { + for(int j=i;j<999;j++) + { + int n=(i+2)*(j+2); + if(n<=1000) + { + a[n-2]=0; + } + else + { + break; + } + } + } + for(int i=0;i<999;i++) + { + if(a[i]!=0) + { + printf("%4d",a[i]); + } + } + + return 0; +} \ No newline at end of file diff --git a/practices/c/level1/p06_Goldbach/Goldbach.c b/practices/c/level1/p06_Goldbach/Goldbach.c new file mode 100644 index 00000000..e41b69a4 --- /dev/null +++ b/practices/c/level1/p06_Goldbach/Goldbach.c @@ -0,0 +1,42 @@ +#include +#include + +int isPrime(int n) +{ + int n_sqrt=sqrt(n); + for(int i=2;i + +void encrypt(char*); +void decrypt(char*); + +int main() +{ + char a[10000]; + char choice; + printf("D:decrypt E:encrypt\n"); + scanf("%c",&choice); + if(choice=='D'||choice=='d') + { + printf("Text:"); + scanf("%s",a); + encrypt(a); + printf("%s\n",a); + } + else if(choice=='E'||choice=='e') + { + printf("Encrypted text:"); + scanf("%s",a); + decrypt(a); + printf("%s\n",a); + } + else + { + printf("Wrong input."); + } +} + +void encrypt(char* str) +{ + char *ch=str; + while(*ch!='\0') + { + *(ch)^='#'; + ++*(ch); + ++ch; + } +} + +void decrypt(char* str) +{ + char *ch=str; + while(*ch!='\0') + { + --*(ch); + *(ch)^='#'; + ++ch; + } +} \ No newline at end of file diff --git a/practices/c/level1/p08_hanoi/hanoi.c b/practices/c/level1/p08_hanoi/hanoi.c new file mode 100644 index 00000000..8f0abe5c --- /dev/null +++ b/practices/c/level1/p08_hanoi/hanoi.c @@ -0,0 +1,23 @@ +#include + +void hanoi(char,char,char,int); + +int main() +{ + hanoi('A','B','C',64); + return 0; +} + +void hanoi(char source,char temp,char target,int number) +{ + if(number==1) + { + printf("move from %c to %c\n",source,target); + } + else + { + hanoi(source,target,temp,number-1); + hanoi(source,temp,target,1); + hanoi(temp,source,target,number-1); + } +} \ No newline at end of file diff --git a/practices/c/level1/p09_maze/maze.c b/practices/c/level1/p09_maze/maze.c new file mode 100644 index 00000000..4d9531e0 --- /dev/null +++ b/practices/c/level1/p09_maze/maze.c @@ -0,0 +1,93 @@ +#include +#include + +char map[11][12]= +{ + "OOOOOOOOOOO", + "OKO O O", + "O O O O O O", + "O O O O O", + "OOOOO O O O", + "O O O", + "O OOOOOOO O", + "O O OO O", + "O O O OOOOO", + "O O ", + "OOOOOOOOOOO" +}; +int x=1; +int y=1; + +void print(); + +int main() +{ + print(); + while (true) + { + if (GetKeyState(VK_DOWN) < 0) + { + if (map[y+1][x] == ' ') + { + map[y][x] = ' '; + y += 1; + map[y][x] = 'K'; + print(); + } + Sleep(100); + } + if (GetKeyState(VK_UP) < 0) + { + if (map[y -1][x] == ' ') + { + map[y][x] = ' '; + y -= 1; + map[y][x] = 'K'; + print(); + } + Sleep(100); + } + if (GetKeyState(VK_RIGHT) < 0) + { + if (map[y ][x+1] == ' ') + { + map[y][x] = ' '; + x += 1; + map[y][x] = 'K'; + print(); + } + Sleep(100); + } + if (GetKeyState(VK_LEFT) < 0) + { + if (map[y ][x-1] == ' ') + { + map[y][x] = ' '; + x-=1; + map[y][x] = 'K'; + print(); + } + Sleep(100); + } + + if (x == 10 && y == 9) + { + system("cls"); + printf("You win!\n"); + Sleep(1000); + break; + } + } + return 0; +} + +void print() +{ + system("cls"); + for (int i = 0; i < 11; i++) + { + printf("%s\n", map[i]); + } +} + + diff --git a/practices/c/level1/p10_pushBoxes/ctest.cpp b/practices/c/level1/p10_pushBoxes/ctest.cpp new file mode 100644 index 00000000..ef800bb5 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/ctest.cpp @@ -0,0 +1,322 @@ +#include +#include + +struct Point +{ + int x; + int y; +}; + +//Func declr +void welcome(); +void loadmap(); +void game(); +void move(); +void refreshmap(); +void ending(); + +//Global +int level = 0; +int totalscore; +int step=0; +char map[9][9]; +FILE * fmap; +Point person, box[9], target[9]; +int numberofbox=0; + +int main() +{ + while (true) + { + welcome(); + if (level == -1) break; + + totalscore = 0; + while (true) + { + loadmap(); + if (level == -2) break; + + game(); + + if(level== 10) + { + ending(); + } + } + } + return 0; +} + +void welcome() +{ + system("cls"); + printf("***\nGame:PushBoxes\n***\n"); + printf("Input the level you want to start (1~10,and -1 to quit)\n-> "); + scanf("%d", &level); +} + +void loadmap() +{ + + int index_box = 0, index_target = 0; + char mapname[10] = "pb_00.txt"; + mapname[3] = '0' + level / 10; + mapname[4] = '0' + level % 10; + + fmap = fopen(mapname, "r"); + if (fmap == NULL) + { + printf("Map reading error.\n"); + Sleep(2000); + level = -2; + return; + } + + system("cls"); + printf("Loading..."); + + for (int i = 0; i<9; i++) + { + for (int j = 0; j<9; j++) + { + + while( + ( map[i][j] = fgetc(fmap) ) && ( map[i][j]=='\r' || map[i][j]=='\n' ) + ); + + if (map[i][j] == 'P') + { + person.x = j; + person.y = i; + } + if (map[i][j] == 'B') + { + box[index_box].x = j; + box[index_box].y = i; + index_box++; + } + if (map[i][j] == 'T') + { + target[index_target].x = j; + target[index_target].y = i; + index_target++; + } + } + } + for(int i=index_box;i<9;i++) + { + box[i].x=0; + box[i].y=0; + target[i].x=0; + target[i].y=0; + } + numberofbox = index_box; + fclose(fmap); +} + +void game() +{ + step = 0; + refreshmap(); + while(true) + { + move(); + + + //win + int counter = 0; + int i, j; + for (i = 0; i < 9; i++) + { + for (j = 0; j < 9; j++) + { + if (box[i].x == target[j].x && box[i].y == target[j].y && box[i].x!=0) //box.x!=0 ensure that box is inited. + { + counter++; + } + } + } + if (counter == numberofbox) + { + printf(" Pass!\n"); + if (step<100) + { + totalscore += 100 - step; + printf(" You get %d points.\n", 100 - step); + } + else + { + printf(" You get 0 point.\n"); + } + printf(" Total : %d\n", totalscore); + + Sleep(5000); + level++; + break; + } + } + +} + +void move() +{ + int ismoved=0; + Point currentperson = person; + + if (GetKeyState(VK_UP) < 0) + { + if (map[person.y - 1][person.x] == ' ' || map[person.y - 1][person.x] == 'T') + { + person.y--; + step++; + ismoved=1; + } + else if (map[person.y - 1][person.x] == 'B' && (map[person.y - 2][person.x] == ' ' || map[person.y - 2][person.x] == 'T')) + { + int i; + for (i = 0; i < 9; i++) + { + if (box[i].x == person.x && box[i].y == person.y - 1) + { + map[box[i].y][box[i].x] = ' '; + box[i].y--; + map[box[i].y][box[i].x] = 'B'; + } + } + person.y--; + step++; + ismoved=1; + } + + + Sleep(50); + } + if (GetKeyState(VK_DOWN) < 0) + { + if (map[person.y + 1][person.x] == ' ' || map[person.y + 1][person.x] == 'T') + { + person.y++; + step++; + ismoved=1; + } + else if (map[person.y + 1][person.x] == 'B' && (map[person.y + 2][person.x] == ' ' || map[person.y + 2][person.x] == 'T')) + { + int i; + for (i = 0; i < 9; i++) + { + if (box[i].x == person.x && box[i].y == person.y + 1) + { + map[box[i].y][box[i].x] = ' '; + box[i].y++; + map[box[i].y][box[i].x] = 'B'; + } + } + person.y++; + step++; + ismoved=1; + } + Sleep(50); + } + if (GetKeyState(VK_LEFT) < 0) + { + if (map[person.y][person.x - 1] == ' ' || map[person.y][person.x - 1] == 'T') + { + person.x--; + step++; + ismoved=1; + } + else if (map[person.y][person.x - 1] == 'B' && (map[person.y][person.x - 2] == ' ' || map[person.y][person.x - 2] == 'T')) + { + int i; + for (i = 0; i < 9; i++) + { + if (box[i].x == person.x - 1 && box[i].y == person.y) + { + map[box[i].y][box[i].x] = ' '; + box[i].x--; + map[box[i].y][box[i].x] = 'B'; + } + } + person.x--; + step++; + ismoved=1; + } + Sleep(50); + } + if (GetKeyState(VK_RIGHT) < 0) + { + if (map[person.y][person.x + 1] == ' ' || map[person.y][person.x + 1] == 'T') + { + person.x++; + step++; + ismoved=1; + } + else if (map[person.y][person.x + 1] == 'B' && (map[person.y][person.x + 2] == ' ' || map[person.y][person.x + 2] == 'T')) + { + int i; + for (i = 0; i < 9; i++) + { + if (box[i].x == person.x + 1 && box[i].y == person.y) + { + map[box[i].y][box[i].x] = ' '; + box[i].x++; + map[box[i].y][box[i].x] = 'B'; + } + } + person.x++; + step++; + ismoved=1; + } + Sleep(50); + } + + int i, istarget = 0; + for (i = 0; i < 3; i++) + { + if (target[i].x == currentperson.x &&target[i].y == currentperson.y) + { + istarget = 1; + } + } + if (istarget) + { + map[currentperson.y][currentperson.x] = 'T'; + } + else + { + map[currentperson.y][currentperson.x] = ' '; + } + map[person.y][person.x] = 'P'; + if(ismoved) + { + refreshmap(); + } +} + +void refreshmap() +{ + system("cls"); + for (int i = 0; i<9; i++) + { + for (int j = 0; j<9; j++) + { + printf("%c", map[i][j]); + } + printf("\n"); + } +} + +void ending() +{ + printf("You have passed all\n"); + printf("Total:%d points.\n",totalscore) ; +} + + + + + + + + diff --git a/practices/c/level1/p10_pushBoxes/pb_01.txt b/practices/c/level1/p10_pushBoxes/pb_01.txt new file mode 100644 index 00000000..175fe8aa --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pb_01.txt @@ -0,0 +1,10 @@ + OOO + OTO + OBOOOO +OOOP BTO +OTB OOO +OOOOBO + OTO + OOO + + \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/pb_02.txt b/practices/c/level1/p10_pushBoxes/pb_02.txt new file mode 100644 index 00000000..23d5289a --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pb_02.txt @@ -0,0 +1,9 @@ +OOOOO +O O +O B O OOO +O BPO OTO +OOOBOOOTO + OO TO + O O O + O OOOO + OOOOO \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/pb_03.txt b/practices/c/level1/p10_pushBoxes/pb_03.txt new file mode 100644 index 00000000..762c38f5 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pb_03.txt @@ -0,0 +1,9 @@ +OOOOOOOOO +OP BTO +OOOOOOOOO + + + + + + \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/pb_04.txt b/practices/c/level1/p10_pushBoxes/pb_04.txt new file mode 100644 index 00000000..762c38f5 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pb_04.txt @@ -0,0 +1,9 @@ +OOOOOOOOO +OP BTO +OOOOOOOOO + + + + + + \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/pb_05.txt b/practices/c/level1/p10_pushBoxes/pb_05.txt new file mode 100644 index 00000000..762c38f5 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pb_05.txt @@ -0,0 +1,9 @@ +OOOOOOOOO +OP BTO +OOOOOOOOO + + + + + + \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/pb_06.txt b/practices/c/level1/p10_pushBoxes/pb_06.txt new file mode 100644 index 00000000..762c38f5 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pb_06.txt @@ -0,0 +1,9 @@ +OOOOOOOOO +OP BTO +OOOOOOOOO + + + + + + \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/pb_07.txt b/practices/c/level1/p10_pushBoxes/pb_07.txt new file mode 100644 index 00000000..762c38f5 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pb_07.txt @@ -0,0 +1,9 @@ +OOOOOOOOO +OP BTO +OOOOOOOOO + + + + + + \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/pb_08.txt b/practices/c/level1/p10_pushBoxes/pb_08.txt new file mode 100644 index 00000000..762c38f5 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pb_08.txt @@ -0,0 +1,9 @@ +OOOOOOOOO +OP BTO +OOOOOOOOO + + + + + + \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/pb_09.txt b/practices/c/level1/p10_pushBoxes/pb_09.txt new file mode 100644 index 00000000..762c38f5 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pb_09.txt @@ -0,0 +1,9 @@ +OOOOOOOOO +OP BTO +OOOOOOOOO + + + + + + \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/pb_10.txt b/practices/c/level1/p10_pushBoxes/pb_10.txt new file mode 100644 index 00000000..762c38f5 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pb_10.txt @@ -0,0 +1,9 @@ +OOOOOOOOO +OP BTO +OOOOOOOOO + + + + + + \ No newline at end of file diff --git a/practices/c/level1/p11_linkedList/linklist.c b/practices/c/level1/p11_linkedList/linklist.c new file mode 100644 index 00000000..e2cf051b --- /dev/null +++ b/practices/c/level1/p11_linkedList/linklist.c @@ -0,0 +1,112 @@ +#include +#include + +struct node +{ + int value; + node *pnext; +}; + +node *create(int); +void print(node*); +void reverse(node*); +void find(node*,int); +void findsec(node*, int); + +int main() +{ + node *head = create(10); + print(head); + reverse(head); + print(head); + printf("After reverse:\n"); + find(head, 5); + findsec(head, 5); + + return 0; +} + +node *create(int length) +{ + node *head = (node*)malloc(sizeof(node)); + head->pnext = NULL; + node *tail = head; + node *data; + for (int i = 0; i < length; i++) + { + data = (node*)malloc(sizeof(node)); + data->value = -i*i+8*i-10; + tail->pnext = data; + tail = data; + } + tail->pnext = NULL; + return head; +} + +void print(node *head) +{ + node *read = head; + while (read = read->pnext) + { + printf("%-4d", read->value); + } + printf("\n"); +} + +void reverse(node *head) +{ + node *pre = head; + node *now = head->pnext; + node *next = now->pnext; + pre = NULL; + while (next) + { + now->pnext = pre; + pre = now; + now = next; + next = next->pnext; + } + now->pnext = pre; + head->pnext = now; +} + +void find(node *head, int num) +{ + node *read = head; + int index=0; + while (read=read->pnext) + { + index++; + if (read->value == num) + { + printf("%d\n", index); + return; + } + } + printf("-1"); +} + +void findsec(node *head, int num) +{ + node *read = head; + int index = 0; + int found = 0; + while (read = read->pnext) + { + index++; + if (read->value == num) + { + if (found == 0) + { + found++; + } + else + { + printf("%d\n", index); + return; + } + + } + } + printf("-1"); +} \ No newline at end of file diff --git a/practices/c/level1/p12_warehouse/DATA.txt b/practices/c/level1/p12_warehouse/DATA.txt new file mode 100644 index 00000000..27d58a91 --- /dev/null +++ b/practices/c/level1/p12_warehouse/DATA.txt @@ -0,0 +1,4 @@ +dd1009#7 +dd1010#1 +dd1011#45 +dd2001#17 diff --git a/practices/c/level1/p12_warehouse/warehouse.c b/practices/c/level1/p12_warehouse/warehouse.c new file mode 100644 index 00000000..ef24f0d0 --- /dev/null +++ b/practices/c/level1/p12_warehouse/warehouse.c @@ -0,0 +1,184 @@ +#include +#include +#include +#ifdef _WIN32 +#define clear(); system("cls"); +#endif +#ifdef linux +#define clear(); system("clear"); +#endif + +struct item +{ + char type[255]; + int amount; +}list[1024]={0}; +int length=0; + +void listout(); +void putout(); +void putin(); +void writeout(); + +int main() +{ + FILE* fp; + int i=0; + + //-----read + fp=fopen("DATA.txt","r"); + char buffer[255]; + while(1) + { + fgets(buffer, 255, fp);//char *fgets(char *buf, int bufsize, FILE *stream); + if(*buffer=='0') + { + break; + } + char* b_type; + char* b_amount; + b_type=strtok(buffer,"#"); + b_amount=strtok(NULL,""); + strcpy(list[i].type,b_type); + list[i].amount=atoi(b_amount); + i++; + length=i; + } + //------------------------------------- + + //-----menu + while(1) + { + clear(); + int n; + printf("1.list\n2.Input\n3.Output\n4.Exit\n-->"); + scanf("%d",&n); + switch(n) + { + case 1: + listout(); + getchar(); + getchar(); + break; + case 2: + putin(); + getchar(); + getchar(); + break; + case 3: + putout(); + getchar(); + getchar(); + break; + case 4: + writeout(); + return 0; + break; + default: + printf("No such option to choose.\n"); + getchar(); + getchar(); + } + } + + + + + return 0; +} + +void listout() +{ + int j; + for(j=0;jlist[j].amount) + { + printf("No enough items"); + } + else + { + list[j].amount-=amount; + printf("Output complete.\n"); + } + return; + } + } + printf("No such item.\n"); +} + +void putin() +{ + char type[255]; + int amount,j; + printf("Item type:"); + scanf("%s",type); + for(j=0;j +using std::cin; +using std::cout; +using std::endl; + +Queue::Queue() +{ + data=new int[100]; + head=data; + tail=data; +} + +Queue::~Queue() +{ + delete[] data; +} + +bool Queue::isFull() +{ + return this->head+1==this->tail||this->head-this->tail==100?true:false; +} + +bool Queue::isEmpty() +{ + return this->head==this->tail?true:false; +} + +void Queue::append(int n) +{ + if(!this->isFull()) + { + head++; + if(this->head-this->data==100) + { + this->head=this->data; + } + *(this->head)=n; + } + else + { + cout<<"error:The queue is full."<isEmpty()) + { + tail++; + if(this->tail-this->data==100) + { + this->tail=this->data; + } + } + else + { + cout<<"error:The queue is empty."< +#include"Queue.h" +using std::cout; +using std::endl; + +int main() +{ + Queue queue; + queue.append(2); + queue.append(4); + queue.append(8); + cout<<"IsFull?->"<"< +using std::cin; +using std::cout; +using std::endl; + +template +class Stack +{ + public: + Stack(int); + ~Stack(); + void push(T); + void pop(); + bool isFull(); + bool isEmpty(); + private: + int *data,*top,length; +}; + + +template +Stack::Stack(int len) +{ + data=new T[len]; + this->top=this->data; + this->length=len; +} + +template +Stack::~Stack() +{ + delete [] this->data; +} + +template +bool Stack::isFull() +{ + return this->top-this->data==this->length; +} + +template +bool Stack::isEmpty() +{ + return this->top==this->data; +} + +template +void Stack::push(T value) +{ + if(!isFull()) + { + *top++=value; + } + else + { + cout<<"error:the stack is full."< +void Stack::pop() +{ + if(!isEmpty()) + { + top--; + } + else + { + cout<<"error:the stack is empty."< stack(95); + for(int i=0;i<101;i++) + { + stack.push(i); + } + cout< +using std::cin; +using std::cout; +using std::endl; + +template +class SafeArray +{ + public: + SafeArray(int); + ~SafeArray(); + T& operator[](int); + private: + T * data; + int length; +}; + +template +SafeArray::SafeArray(int len) +{ + data=new T[len]; + length=len; +} + +template +SafeArray::~SafeArray() +{ + delete [] this->data; +} + +template +T& SafeArray::operator[](int index) +{ + if(index>=length||index<0) + { + cout<<"error:index out of range,return the first element("<<*data<<")"< array(100); + for(int i=0;i<10;i++) + { + array[i]=i; + cout< +#include +#include +#include +using namespace std; + +struct Record{ + int id; + string name; + double score; +}; + +vector students; + +void loadformfile(); +void listout(); +void addstu(); +void delstu(); +void record(int); +void savetofile(); + +int main(){ + loadformfile(); + while(ture){ + sys("clr"); + cout<<"1.list"<"<>choice; + switch (choice){ + case 1: + listout(); + break; + case 2: + addstu(); + break; + case 3: + delstu(); + break; + case 4: + cout<<"Input the number you would start."<>c){ + f>>id>>name>>score; + Record newrecord ={id,name,score}; + students.append(newrecord); + } +} + +void listout(){ + for(auto it=students.begin();it!=stu.end();it++){ + cout<id<<" "<name<<" "<score<id,name,-1}; + students.append(newrecord); +} + +void delstu(){ + int id; + cout<<"Input the id of the student:"<id){ + students.erase(id) + }else{ + cout<<"No this student"<>it->score; + cout<<"Would you like to continue?(y/n)"<id<<" "<name<<" "<score< +#include +using namespace std; + +class Shape{ + public: + virtual void show()=0; +}; + +class Circle:public Shape{ + public: + Circle(int,int,int); + void show(); + private: + double x; + double y; + double r; +}; + +class Rect:public Shape{ + public: + Rect(int,int,int,int); + void show(); + private: + double left; + double top; + double right; + double bottom; +}; + +class Canvas{ + private: + vector shapevector; + public: + void print(); + void add(Shape*); +}; +Circle::Circle(int a,int b,int c){ + x=a; + y=b; + r=c; +} + +void Circle::show(){ + cout<<"Circle:centre("<x<<","<y<<"),radius:"<r<left<<","<top<<"),("<right<<","<bottom<<")"<shapevector.begin();it!=this->shapevector.end();it++){ + (*it)->show(); + } +} + +void Canvas::add(Shape* s){ + shapevector.push_back(s); +} + +int main(){ + Circle c(5,5,2); + Rect r(1,4,4,1); + Canvas canvas; + canvas.add(&c); + canvas.add(&r); + + canvas.print(); + return 0; +} diff --git a/practices/cpp/level1/p06_CircleAndPoint/circleandpoint.cpp b/practices/cpp/level1/p06_CircleAndPoint/circleandpoint.cpp new file mode 100644 index 00000000..aa7d8e77 --- /dev/null +++ b/practices/cpp/level1/p06_CircleAndPoint/circleandpoint.cpp @@ -0,0 +1,70 @@ +/* +题目:Circle & Point + +功能要求: + +实现Point类 +利用组合Point实现Circle类 +通过移动圆心,来实现圆的移动 +*/ +#include +using namespace std; + +class Point{ + private: + double x; + double y; + public: + Point(); + Point(double,double); + Point(const Point&); + void move(double,double); +}; + +Point::Point(){ + x=0; + y=0; +} + +Point::Point(double a,double b){ + x=a; + y=b; +} + +Point::Point(const Point& p){ + x=p.x; + y=p.y; +} + +void Point::move(double a,double b){ + this->x+=a; + this->y+=b; +} + +class Circle{ + private: + Point centre; + double radius; + public: + Circle(Point,double); + void move(double,double); +}; + +Circle::Circle(Point p,double r) +{ + centre=p; + radius=r; +} + + +void Circle::move(double a,double b){ + this->centre.move(a,b); +} + + +int main(){ + Point p(5,5); + Circle c(p,1); + c.move(2,3); + return 0; +} diff --git a/practices/cpp/level1/p07_Circuit/circuit.cpp b/practices/cpp/level1/p07_Circuit/circuit.cpp new file mode 100644 index 00000000..26ae2a56 --- /dev/null +++ b/practices/cpp/level1/p07_Circuit/circuit.cpp @@ -0,0 +1,116 @@ +#include +#include +using namespace std; + +class Device{ + public: + virtual void setOn()=0; + virtual void setOff()=0; +}; + +class Bulb:public Device{ + public: + void setOn(); + void setOff(); +}; + +class Fan:public Device{ + public: + void setOn(); + void setOff(); +}; + +class Switch{ + private: + bool key; + public: + void setOn(); + void setOff(); + bool isConnected(); +}; + +class Circult{ + private: + vector devicevector; + vector switchvector; + vector circuitvector; + public: + void addDevice(Device*); + void addSwitch(Switch*); + void addCircuit(Circult*); + bool isConnected(); +}; +void Bulb::setOn(){ + cout<<"Bulb is on!."<key=true; +} + +void Switch::setOff(){ + this->key=false; +} + +bool Switch::isConnected(){ + return this->key; +} + +void Circult::addDevice(Device* d){ + this->devicevector.push_back(d); +} +void Circult::addSwitch(Switch* s){ + this->switchvector.push_back(s); +} + +void Circult::addCircuit(Circult* c){ + this->circuitvector.push_back(c); +} + +bool Circult::isConnected(){ + for(auto it=switchvector.begin();it!=switchvector.end();it++){ + if((*it)->isConnected()==false){ + return false; + } + } + for(auto it=circuitvector.begin();it!=circuitvector.end();it++){ + if((*it)->isConnected()==false){ + return false; + } + } + return true; +} + +int main(){ + Switch s1,s2; + s1.setOn(); + s2.setOn(); + Bulb b; + Fan f; + Circult c1,c2,c3; + //circuit setting + c3.addDevice(&f); + + c2.addSwitch(&s2); + c2.addDevice(&b); + + c1.addSwitch(&s1); + c1.addCircuit(&c2); + c1.addCircuit(&c3); + + std::cout< +#include +using namespace std; + +class Employee{ + protected: + string name; + int age; + int level; + public: + Employee(string,int,int); + virtual void salary(); +}; + +class Sales :public Employee{ + private: + int sales; + public: + Sales(string,int,int,int); + void salary(); +}; + +Employee::Employee(string n,int a,int l){ + name=n; + age=a; + level=l; +} + +void Employee::salary(){ + cout<name<<"'s salary is"<level*1000<<"yuan ."<name<<"'s salary is"<level*1000+sales*0.2<<"yuan ."<salary(); + e=new Sales("Bob",27,5,3600); + e->salary(); + return 0; +} diff --git a/practices/cpp/level1/p09_Tree/tree.cpp b/practices/cpp/level1/p09_Tree/tree.cpp new file mode 100644 index 00000000..86dc935e --- /dev/null +++ b/practices/cpp/level1/p09_Tree/tree.cpp @@ -0,0 +1,58 @@ +/* +题目:Tree + +功能要求: + +可以在树的某个节点上append任意数量的子节点 +每个节点都可以保存一个int型的值 +可获取节点的parent(父节点) +可计算节点的子孙节点总数 +*/ + +#include +#include +using std::cin; +using std::cout; +using std::endl; +using std::vector; + +class Tree{ + private: + vector children; + Tree* parent; + int value; + public: + Tree(int); + void app(Tree*); + Tree* getParent(); + int countChildren(); +}; + +Tree::Tree(int n){ + parent=NULL; + value=n; +} + +void Tree::app(Tree* t){ + t->parent=this; + children.push_back(t); +} + +Tree* Tree::getParent(){ + return this->parent; +} + +int Tree::countChildren(){ + vector::iterator it; + for(it=this->children.begin();it!=this->children.end();it++){} + return it-children.begin(); +} + +int main(){ + Tree t1(1),t2(3),t3(3),t4(5),t5(5); + t2.app(&t4); + t2.app(&t5); + t1.app(&t2); + t1.app(&t3); + return 0; +} diff --git a/practices/cpp/level1/p10_Iterator/Iterator.cpp b/practices/cpp/level1/p10_Iterator/Iterator.cpp new file mode 100644 index 00000000..cb01e2d1 --- /dev/null +++ b/practices/cpp/level1/p10_Iterator/Iterator.cpp @@ -0,0 +1,43 @@ +#include +#include"Iterator.h" + +Iterator::Iterator() +{ + current=0; +} + +Iterator::Iterator(int *o,int *c) +{ + current=c; + origin=o; +} + +Iterator& Iterator::operator=(Iterator i) +{ + this->origin=i.origin; + this->current=i.current; + return *this; +} + +bool Iterator::operator==(Iterator i) +{ + return this->current==i.current; +} + +bool Iterator::operator!=(Iterator i) +{ + return this->current!=i.current; +} + +Iterator& Iterator::operator++() +{ + this->current+=1; + if(this->current-this->origin==100) + this->current-=100; + return *this; +} + +int Iterator::operator*() +{ + return *current; +} \ No newline at end of file diff --git a/practices/cpp/level1/p10_Iterator/Iterator.h b/practices/cpp/level1/p10_Iterator/Iterator.h new file mode 100644 index 00000000..ddf7c86e --- /dev/null +++ b/practices/cpp/level1/p10_Iterator/Iterator.h @@ -0,0 +1,16 @@ +#pragma once +class Iterator +{ + + private: + int *origin; + int *current; + public: + Iterator(); + Iterator(int*,int*); + Iterator& operator=(Iterator); + bool operator==(Iterator); + bool operator!=(Iterator); + Iterator& operator++(); + int operator*(); +}; \ No newline at end of file diff --git a/practices/cpp/level1/p10_Iterator/Queue.cpp b/practices/cpp/level1/p10_Iterator/Queue.cpp new file mode 100644 index 00000000..0447d747 --- /dev/null +++ b/practices/cpp/level1/p10_Iterator/Queue.cpp @@ -0,0 +1,74 @@ +#include +#include"Queue.h" +#include"Iterator.h" +using std::cin; +using std::cout; +using std::endl; + +Queue::Queue() +{ + data=new int[100]; + head=data; + tail=data; +} + +Queue::~Queue() +{ + delete[] data; +} + +bool Queue::isFull() +{ + return this->head+1==this->tail||this->head-this->tail==100?true:false; +} + +bool Queue::isEmpty() +{ + return this->head==this->tail?true:false; +} + +void Queue::append(int n) +{ + if(!this->isFull()) + { + head++; + if(this->head-this->data==100) + { + this->head=this->data; + } + *(this->head)=n; + } + else + { + cout<<"error:The queue is full."<isEmpty()) + { + cout<<"Pop:"<tail<tail-this->data==100) + { + this->tail=this->data; + } + } + else + { + cout<<"error:The queue is empty."<data,this->tail+1); + return newi; +} + +Iterator Queue::end() +{ + Iterator newi(this->data,this->head+1); + return newi; +} \ No newline at end of file diff --git a/practices/cpp/level1/p10_Iterator/Queue.h b/practices/cpp/level1/p10_Iterator/Queue.h new file mode 100644 index 00000000..5b473831 --- /dev/null +++ b/practices/cpp/level1/p10_Iterator/Queue.h @@ -0,0 +1,17 @@ +#pragma once +#include"Iterator.h" + +class Queue +{ + public: + Queue(); + ~Queue(); + void append(int); + void pop(); + bool isFull(); + bool isEmpty(); + Iterator begin(); + Iterator end(); + private: + int *data,*head,*tail; +}; \ No newline at end of file diff --git a/practices/cpp/level1/p10_Iterator/main.cpp b/practices/cpp/level1/p10_Iterator/main.cpp new file mode 100644 index 00000000..97a867ba --- /dev/null +++ b/practices/cpp/level1/p10_Iterator/main.cpp @@ -0,0 +1,20 @@ +#include +#include"Queue.h" +#include"Iterator.h" +using std::cin; +using std::cout; +using std::endl; + +int main() +{ + Queue queue; + for(int i=0;i<50;i++) + { + queue.append(i); + } + for(Iterator it=queue.begin();it!=queue.end();++it) + { + cout<<*it< +#include +#include +#include + +//for debug +#include +using std::cin; +using std::cout; +using std::endl; + +const int STAGE_X=540; +const int STAGE_Y=720; +const int PLANE_L=64; +const int FIRE_L=32; + + +void handlePlayerInput(sf::Keyboard::Key, bool); +void update(); +void fire(); +void spawnEnemy(); +void boom(sf::Vector2f); +void fireenemy(sf::Vector2f); + +//movement params +bool isMovingUp=false; +bool isMovingDown=false; +bool isMovingLeft=false; +bool isMovingRight=false; +bool FiringKey=false; + +//count +int score=0; +sf::Text counter; +sf::Font font; + +int life=3; +int level=1; + + + +sf::Texture t_hero; +sf::Sprite hero; +sf::Texture t_herofire; +sf::Sprite herofire; +sf::Texture t_enemy; +sf::Sprite enemy; +sf::Texture t_enemyfire; +sf::Sprite enemyfire; +sf::Texture t_boom1; +sf::Sprite boom1; +sf::Texture t_boom2; +sf::Sprite boom2; +sf::Music ks; + +std::vector herofirevector; +std::vectorenemyvector; +std::vector< std::pair > boomvector; +std::vectorenemyfirevector; + +int main() +{ + sf::RenderWindow window(sf::VideoMode(STAGE_X, STAGE_Y), "SFML works!"); + + //load hero + t_hero.loadFromFile("assets/hero.png"); + hero.setTexture(t_hero); + hero.setScale(0.5,0.5); //PLANE--------(128,128)*0.5=(64,64) + hero.setPosition(540*0.5-32,720-64); + + //load herofire + t_herofire.loadFromFile("assets/herofire.png"); + herofire.setTexture(t_herofire); + + //load enemy + t_enemy.loadFromFile("assets/enemy.png"); + enemy.setTexture(t_enemy); + enemy.setScale(0.5,0.5); + + //load enemyfire + t_enemyfire.loadFromFile("assets/enemyfire.png"); + enemyfire.setTexture(t_enemyfire); + + //load boom + t_boom1.loadFromFile("assets/boom1.png"); + t_boom2.loadFromFile("assets/boom2.png"); + boom1.setTexture(t_boom1); + boom2.setTexture(t_boom2); + + //load bgm + sf::Music bgm; + bgm.openFromFile("assets/bgm1.ogg"); + bgm.setVolume(20); + bgm.play(); + + //load kill sound + ks.openFromFile("assets/killed.ogg"); + + //load font + font.loadFromFile("assets/arial.ttf"); + + //set main clock + sf::Clock spawnclock; + float interval=0; + + while (window.isOpen()) + { + //handle event + sf::Event event; + while (window.pollEvent(event)) + { + switch (event.type) + { + case sf::Event::KeyPressed: + handlePlayerInput(event.key.code, true); + break; + case sf::Event::KeyReleased: + handlePlayerInput(event.key.code, false); + break; + case sf::Event::Closed: + window.close(); + break; + } + } + //level control + level=1+score/400; + if(level>8) + { + //PASS ALL LEVEL + break; + } + //spawn enemy + if(spawnclock.getElapsedTime().asSeconds()>interval) + { + spawnEnemy(); + srand(time(0)); + interval=2-level*0.1+rand()%(2); + spawnclock.restart(); + } + update(); + + /************************ + *RENDER REGION + */ + window.clear(); + //draw hero + window.draw(hero); + //draw herofire + for(std::vector::iterator it=herofirevector.begin();it!=herofirevector.end();it++) + { + herofire.setPosition(*(it)); + window.draw(herofire); + } + //draw enemy + for(std::vector::iterator it=enemyvector.begin();it!=enemyvector.end();it++) + { + enemy.setPosition(*(it)); + window.draw(enemy); + } + //draw enemyfire + for(std::vector::iterator it=enemyfirevector.begin();it!=enemyfirevector.end();it++) + { + enemyfire.setPosition(*(it)); + window.draw(enemyfire); + } + //renderboom + for(std::vector< std::pair >::iterator it=boomvector.begin();it!=boomvector.end();) + { + if(it->second<=0) + { + it=boomvector.erase(it); + continue; + } + if(it->second%2==0) + { + boom2.setPosition(it->first); + window.draw(boom2); + it->second-=1; + } + else if(it->second%2==1) + { + boom1.setPosition(it->first); + window.draw(boom1); + it->second-=1; + } + ++it; + } + + //write score on screen + //int sprintf( char *buffer, const char *format, [ argument] … ); + char buf[64]; + sprintf( buf, "Score:%d\nLife:%d\nLevel:%d", score,life,level); + sf::String text(buf); + counter.setString(text); + counter.setFont(font); + counter.setCharacterSize(20); + counter.setColor(sf::Color::Blue); + window.draw(counter); + + //render for gameover + if(life<=0) + { + + boomvector.push_back(std::pair(hero.getPosition(),1200)); + + sprintf( buf, "Game Over!\nYour score:%d", score); + sf::String text(buf); + counter.setString(text); + counter.setFont(font); + counter.setCharacterSize(35); + counter.setColor(sf::Color::Red); + counter.setPosition(STAGE_X/4,STAGE_Y/2); + window.draw(counter); + + window.display(); + sf::Clock endclock; + while(endclock.getElapsedTime().asSeconds()<3) + { + //wait 3 second to look score + } + break; + } + //render all + window.display(); + /************************* + *REGION END + */ + + + } + + return 0; +} + +void handlePlayerInput(sf::Keyboard::Key key,bool isPressed) +{ + if (key == sf::Keyboard::W) + isMovingUp = isPressed; + else if (key == sf::Keyboard::S) + isMovingDown = isPressed; + else if (key == sf::Keyboard::A) + isMovingLeft = isPressed; + else if (key == sf::Keyboard::D) + isMovingRight = isPressed; + else if (key == sf::Keyboard::Space) + { + if(isPressed&&!FiringKey) + { + FiringKey=1; + fire(); + } + else if(!isPressed&&FiringKey) + { + FiringKey=0; + } + } +} + +void update() +{ + //hero move + sf::Vector2f movement(0.f, 0.f); + if (isMovingUp&&hero.getPosition().y>0) + movement.y -= 0.15f; + if (isMovingDown&&hero.getPosition().y0) + movement.x -= 0.15f; + if (isMovingRight&&hero.getPosition().x::iterator it=herofirevector.begin();it!=herofirevector.end();) + { + sf::Vector2f location=*(it); + if(location.x<0||location.x>STAGE_X||location.y<0||location.y>STAGE_Y) + { + it=herofirevector.erase(it); + } + else + { + sf::Vector2f movement(0,-0.2f); + *(it)+=movement; + it++; + } + } + //enemy move + for(std::vector::iterator it=enemyvector.begin();it!=enemyvector.end();) + { + sf::Vector2f location=*(it); + if(location.x<0||location.x+PLANE_L>STAGE_X||location.y<0||location.y>STAGE_Y) + { + it=enemyvector.erase(it); + } + else + { + sf::Vector2f movement(0,0.1f+level*0.02f); + *(it)+=movement; + it++; + } + } + //enemyfire move + for(std::vector::iterator it=enemyfirevector.begin();it!=enemyfirevector.end();) + { + sf::Vector2f location=*(it); + if(location.x<0||location.x>STAGE_X||location.y<0||location.y>STAGE_Y) + { + it=enemyfirevector.erase(it); + } + else + { + sf::Vector2f movement(0,0.2f+level*0.04f); + *(it)+=movement; + it++; + } + } + //kill enemy + for(std::vector::iterator it=herofirevector.begin();it!=herofirevector.end();) + { + bool killed=0; + sf::Vector2f fireett=*(it); + for(std::vector::iterator itt=enemyvector.begin();itt!=enemyvector.end();) + { + sf::Vector2f enemyett=*(itt); + if(fireett.x+FIRE_L/2>enemyett.x&&fireett.x+FIRE_L/2enemyett.y&&fireett.y+FIRE_L/2::iterator it=enemyvector.begin();it!=enemyvector.end();it++) + { + sf::Vector2f location=*(it); + if(location.y>=199.9&&location.y<=200.01) + { + fireenemy(location); + continue; + } + } + //kill self + for(std::vector::iterator it=enemyfirevector.begin();it!=enemyfirevector.end();) + { + sf::Vector2f fireett=*(it); + sf::Vector2f heroett=hero.getPosition(); + if(fireett.x+FIRE_L/2>heroett.x&&fireett.x+FIRE_L/2heroett.y&&fireett.y+FIRE_L/2::iterator it=enemyvector.begin();it!=enemyvector.end();) + { + sf::Vector2f enemyett=*(it); + sf::Vector2f heroett=hero.getPosition(); + if(enemyett.x+FIRE_L/2>heroett.x&&enemyett.x+FIRE_L/2heroett.y&&enemyett.y+FIRE_L/2 newboom; + newboom.first=pos; + newboom.second=450; + boomvector.push_back(newboom); + ks.play(); + score+=20; +} + +void fireenemy(sf::Vector2f pos) +{ + sf::Vector2f newfire=pos; + newfire.x+=PLANE_L/2-FIRE_L/2; + newfire.y+=PLANE_L; + enemyfirevector.push_back(newfire); +} \ No newline at end of file