diff --git a/Diophantus.c b/Diophantus.c new file mode 100644 index 00000000..4f2275bc --- /dev/null +++ b/Diophantus.c @@ -0,0 +1,18 @@ +#include +int main(int argc, char** argv) +{ + int fatherAge=0; + for(double i=1.0;i<100;i++) //儿子年龄 + { + for(double j=1;j<200;j++) //父亲年龄 + { + if((1.0/6.0*j+1.0/12.0*j+1.0/7.0*j+5+i+4==j)&&(2*i==j)) + { + fatherAge=j; + break; + } + } + } + printf("儿子死时丢番图年龄为%d",FatherAge-4); + return 0; +} diff --git a/Encrypted.c b/Encrypted.c new file mode 100644 index 00000000..50f35036 --- /dev/null +++ b/Encrypted.c @@ -0,0 +1,52 @@ +#include +#include +#include + +void Encrypte(char* p) +{ + int i = 0; + while (p[i] != '\0') + { + p[i] = p[i] + '3' - '1'; + printf("%c", p[i]); + i++; + } + printf("\n"); +} + +void Decrypte(char* p) +{ + int i = 0; + while (p[i] != '\0') + { + p[i] = p[i] - '3' + '1'; + printf("%c", p[i]); + i++; + } + printf("\n"); +} + +int main() +{ + char p[10000]; + int Judge = 0; + printf("Please input the string need opreating\n"); + scanf("%s", p); + printf("En or De?\nPleaes input 1(En) or 2(De)\n"); + scanf("%d", &Judge); + if (Judge == 1) + { + Encrypte(p); + } + else if (Judge == 2) + { + Decrypte(p); + } + else + { + printf("Input Error!"); + return -1; + } + return 0; +} + diff --git a/Goldbach.c b/Goldbach.c new file mode 100644 index 00000000..86b8ad4e --- /dev/null +++ b/Goldbach.c @@ -0,0 +1,85 @@ +#include + +/*int pd(int n) +{ + for(int i=2;i<=n/2;i++) //This program is used for drawing a table + { + if(n%i==0) + return 0; + } + return 1; +} + + +int main(int argc, char** argv) +{ + int a[100]; + int tot=0; + printf("a[0]=2\n"); + for(int i=3;i<100;i++) + { + if(pd(i)) + { + a[tot++]=i; + printf("a[%d]=%d;\n",tot,i); + } + } + return 0; +} */ + +int main() +{ + int a[25]; //打表 + int judge = 0; + a[0] = 2; + a[1] = 3; + a[2] = 5; + a[3] = 7; + a[4] = 11; + a[5] = 13; + a[6] = 17; + a[7] = 19; + a[8] = 23; + a[9] = 29; + a[10] = 31; + a[11] = 37; + a[12] = 41; + a[13] = 43; + a[14] = 47; + a[15] = 53; + a[16] = 59; + a[17] = 61; + a[18] = 67; + a[19] = 71; + a[20] = 73; + a[21] = 79; + a[22] = 83; + a[23] = 89; + a[24] = 97; + for (int i = 4; i <=100; i+=2) + { + for (int j = 0; j < 25; j++) + { + for (int k = 0; k < 25; k++) + { + if (a[j] + a[k] == i) + { + judge = 1; + break; + } + } + if (judge==1) + { + cout << i << "满足哥德巴赫猜想" << endl; + break; + } + else if (judge==0) + { + return -1; + printf("Goldbach do not exists"); + } + judge = 0; + } + return 0; +} + diff --git a/Running Letter.c b/Running Letter.c new file mode 100644 index 00000000..ece4e0aa --- /dev/null +++ b/Running Letter.c @@ -0,0 +1,34 @@ +#include +#include + +int main(int argc, char** argv) +{ + int i = 0; + int j = 0; + while (1) + { + printf("R"); + Sleep(30); + system("cls"); + int op = 1; + for (i = 0; i<79&&i>=0; i+=op) + { + for (j = 0; j < i; j++) + { + printf(" "); + } + printf("R"); + Sleep(30); + system("cls"); + if (i == 78) + { + op = -1; + } + if (i == 0) + { + op = 1; + } + } + } + return 0; +} \ No newline at end of file diff --git a/allPrimes.c b/allPrimes.c new file mode 100644 index 00000000..5ab0fa57 --- /dev/null +++ b/allPrimes.c @@ -0,0 +1,207 @@ +#include +#include +#include +/*int pd(int n) +{ + for(int i=2;i<=sqrt(n);i++) //此程序用来打表 + { + if(n%i==0) + return 0; + } + return 1; +} + + +int main(int argc, char** argv) +{ + int a[1000]; + int tot=0; + printf("a[0]=2\n"); + for(int i=3;i<1000;i++) + { + if(pd(i)) + { + a[tot++]=i; + printf("a[%d]=%d;\n",tot,i); + } + } + return 0; +} */ +int main() +{ + clock_t t1=clock(); + int a[500]; + a[0]=2; + a[1]=3; + a[2]=5; + a[3]=7; + a[4]=11; + a[5]=13; + a[6]=17; + a[7]=19; + a[8]=23; + a[9]=29; + a[10]=31; + a[11]=37; + a[12]=41; + a[13]=43; + a[14]=47; + a[15]=53; + a[16]=59; + a[17]=61; + a[18]=67; + a[19]=71; + a[20]=73; + a[21]=79; + a[22]=83; + a[23]=89; + a[24]=97; + a[25]=101; + a[26]=103; + a[27]=107; + a[28]=109; + a[29]=113; + a[30]=127; + a[31]=131; + a[32]=137; + a[33]=139; + a[34]=149; + a[35]=151; + a[36]=157; + a[37]=163; + a[38]=167; + a[39]=173; + a[40]=179; + a[41]=181; + a[42]=191; + a[43]=193; + a[44]=197; + a[45]=199; + a[46]=211; + a[47]=223; + a[48]=227; + a[49]=229; + a[50]=233; + a[51]=239; + a[52]=241; + a[53]=251; + a[54]=257; + a[55]=263; + a[56]=269; + a[57]=271; + a[58]=277; + a[59]=281; + a[60]=283; + a[61]=293; + a[62]=307; + a[63]=311; + a[64]=313; + a[65]=317; + a[66]=331; + a[67]=337; + a[68]=347; + a[69]=349; + a[70]=353; + a[71]=359; + a[72]=367; + a[73]=373; + a[74]=379; + a[75]=383; + a[76]=389; + a[77]=397; + a[78]=401; + a[79]=409; + a[80]=419; + a[81]=421; + a[82]=431; + a[83]=433; + a[84]=439; + a[85]=443; + a[86]=449; + a[87]=457; + a[88]=461; + a[89]=463; + a[90]=467; + a[91]=479; + a[92]=487; + a[93]=491; + a[94]=499; + a[95]=503; + a[96]=509; + a[97]=521; + a[98]=523; + a[99]=541; + a[100]=547; + a[101]=557; + a[102]=563; + a[103]=569; + a[104]=571; + a[105]=577; + a[106]=587; + a[107]=593; + a[108]=599; + a[109]=601; + a[110]=607; + a[111]=613; + a[112]=617; + a[113]=619; + a[114]=631; + a[115]=641; + a[116]=643; + a[117]=647; + a[118]=653; + a[119]=659; + a[120]=661; + a[121]=673; + a[122]=677; + a[123]=683; + a[124]=691; + a[125]=701; + a[126]=709; + a[127]=719; + a[128]=727; + a[129]=733; + a[130]=739; + a[131]=743; + a[132]=751; + a[133]=757; + a[134]=761; + a[135]=769; + a[136]=773; + a[137]=787; + a[138]=797; + a[139]=809; + a[140]=811; + a[141]=821; + a[142]=823; + a[143]=827; + a[144]=829; + a[145]=839; + a[146]=853; + a[147]=857; + a[148]=859; + a[149]=863; + a[150]=877; + a[151]=881; + a[152]=883; + a[153]=887; + a[154]=907; + a[155]=911; + a[156]=919; + a[157]=929; + a[158]=937; + a[159]=941; + a[160]=947; + a[161]=953; + a[162]=967; + a[163]=971; + a[164]=977; + a[165]=983; + a[166]=991; + a[167]=997; + for(int i=0;i<168;i++) + printf("%d ",a[i]); + clock_t t2=clock(); + printf("\nrunning this program costs %d milliseconds\n",t2-t1); + return 0; +} diff --git a/hanio.c b/hanio.c new file mode 100644 index 00000000..1e13eb2d --- /dev/null +++ b/hanio.c @@ -0,0 +1,26 @@ +#include +//第一个塔为初始塔,中间的塔为借用塔,最后一个塔为目标塔 +int i=1;//记录步数 +void move(int n,char from,char to) //将编号为n的盘子由from移动到to +{printf("第%d步:将%d号盘子%c---->%c\n",i++,n,from,to); +} +void hanoi(int n,char from,char denpend_on,char to) +{ + if (n==1) + move(1,from,to);// + else + { + hanoi(n-1,from,to,denpend_on); + move(n,from,to); + hanoi(n-1,denpend_on,from,to); + } +} +void main() +{ + printf("请输入盘子的个数:\n"); + int n; + scanf("%d",&n); + char x='A',y='B',z='C'; + printf("盘子移动情况如下:\n"); + hanoi(n,x,y,z); +} diff --git a/maze.c b/maze.c new file mode 100644 index 00000000..24995966 --- /dev/null +++ b/maze.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include + +int map[25][25] = { + { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, + { 2,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0 }, + { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, +}; +int i, j; +void print() +{ + for (i = 0; i < 25; i++) + { + for (j = 0; j < 25; j++) + { + if (map[i][j] == 1) + printf("#"); + if (map[i][j] == 0) + printf(" "); + if (map[i][j] == 2) + printf("E"); + if (j == 24) + { + printf("\n"); + } + } + } +} +int main() +{ + print(); + int m = 1, n = 0; + while (m!=24&&n!=23) + { + if (GetKeyState(VK_UP) < 0&& map[m - 1][n]==0) + { + map[m - 1][n] = 2; + map[m][n] = 0; + --m; + system("cls"); + print(); + } + if (GetKeyState(VK_DOWN) < 0 && map[m + 1][n] == 0) + { + map[m + 1][n] = 2; + map[m][n] = 0; + ++m; + system("cls"); + print(); + } + if (GetKeyState(VK_LEFT) < 0 && map[m][n-1] == 0) + { + map[m][n - 1] = 2; + map[m][n] = 0; + --n; + system("cls"); + print(); + } + if (GetKeyState(VK_RIGHT) < 0 && map[m][n+1] == 0) + { + map[m][n + 1] = 2; + map[m][n] = 0; + ++n; + system("cls"); + print(); + } + } + system("cls"); + printf("You win!"); + return 0; +} + diff --git a/narcissus.c b/narcissus.c new file mode 100644 index 00000000..eb706454 --- /dev/null +++ b/narcissus.c @@ -0,0 +1,12 @@ +#include +#include +using namespace std; +int main(int argc, char** argv) +{ + for(int i=100;i<1000;i++) + { + if((pow(i/100,3)+pow(i%100/10,3)+pow(i%100%10,3)==i)) + cout< +#include +int main(int argc, char** argv) + { + while(1) + { + + printf("R"); + Sleep(30); + system("cls"); + for(int i=0;i<79;i++) + { + for(int j=0;j=0;i--) + { + for(int j=0;j +#include +#include +#include +#include + +int map[25][25] = { + { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, + { 2,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 }, + { 1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0 }, + { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, +}; +int i, j; +void print() +{ + for (i = 0; i < 25; i++) + { + for (j = 0; j < 25; j++) + { + if (map[i][j] == 1) + printf("#"); + if (map[i][j] == 0) + printf(" "); + if (map[i][j] == 2) + printf("E"); + if (j == 24) + { + printf("\n"); + } + } + } +} +int main() +{ + print(); + int m = 1, n = 0; + while (m!=24&&n!=23) + { + if (GetKeyState(VK_UP) < 0&& map[m - 1][n]==0) + { + map[m - 1][n] = 2; + map[m][n] = 0; + --m; + system("cls"); + print(); + } + if (GetKeyState(VK_DOWN) < 0 && map[m + 1][n] == 0) + { + map[m + 1][n] = 2; + map[m][n] = 0; + ++m; + system("cls"); + print(); + } + if (GetKeyState(VK_LEFT) < 0 && map[m][n-1] == 0) + { + map[m][n - 1] = 2; + map[m][n] = 0; + --n; + system("cls"); + print(); + } + if (GetKeyState(VK_RIGHT) < 0 && map[m][n+1] == 0) + { + map[m][n + 1] = 2; + map[m][n] = 0; + ++n; + system("cls"); + print(); + } + } + system("cls"); + printf("You win!"); + return 0; +} + diff --git a/practices/c/level1/p10_pushBoxes/pushbox.c b/practices/c/level1/p10_pushBoxes/pushbox.c new file mode 100644 index 00000000..e6aa3c6c --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pushbox.c @@ -0,0 +1,226 @@ +// pushbox.cpp : 定义控制台应用程序的入口点。 + + +#include "stdafx.h" +#include +int a[6][6]; +int i = 0, j = 0; +int level; +int levelNum = 0; +int scan(int n,const char b[6]) +{ + FILE* fp = fopen(b, "r"); + if (fp == NULL) + { + printf("Files not found"); + return -1; + } + for (i = 0; inum), &(p1->value)); //录入数据 + } + while (p1->num != 0) //只要学号不为0,就继续录入下一个节点 + { + n += 1; //节点总数增加1个 + if (n == 1) //如果节点总数是1,则head指向刚创建的节点p1 + { + head = p1; + p2->next = NULL; //此时的p2就是p1,也就是p1->next指向NULL。 + } + else + { + p2->next = p1; //指向上次下面刚刚开辟的新节点 + } + + p2 = p1; //把p1的地址给p2保留,然后p1产生新的节点 + + p1 = (struct student *) malloc(LEN); + printf("Please input %d node -- num,value: ", n + 1); + scanf("%d %f", &(p1->num), &(p1->value)); + } + p2->next = NULL; //此句就是根据单向链表的最后一个节点要指向NULL + + free(p1); //p1->num为0的时候跳出了while循环,并且释放p1 + p1 = NULL; //特别不要忘记把释放的变量清空置为NULL,否则就变成"野指针",即地址不确定的指针 + return head; //返回创建链表的头指针 +} + +struct student *reverse(struct student *head) +{ + struct student *p; //临时存储 + struct student *p1; //存储返回结果 + struct student *p2; //源结果节点一个一个取 + + p1 = NULL; //开始颠倒时,已颠倒的部分为空 + p2 = head; //p2指向链表的头节点 + while (p2 != NULL) + { + p = p2->next; + p2->next = p1; + p1 = p2; + p2 = p; + } + head = p1; + return head; +} + +void search(int n,struct student *head) +{ + struct student *p1=head; + struct student *p2=head; + struct student *p; + int tot = 0; + while (p1!=NULL) + { + if (p1->value == n) + { + a[tot++]= p1->num; + } + p = p1; + p2 = p; + p1 = p2->next; + } + a[tot]=-1; +} +int main(int argc, char *argv[]) +{ + struct student *head; + printf("Take turns to input the value\nIf you want to stop,input 0 0\n"); + head = Create(); + int judge; + while (1) + { + a[100] = { 0 }; + int judge2 = 0, i = 0, number = 0; + printf("What do you what to do?\n1:Reverse 2:Search -1:quit\n"); + scanf("%d", &judge); + if (judge == 1) + { + head=reverse(head); + printf("Operation succeeded!"); + } + else if (judge == 2) + { + printf("Input the number you want to search\n"); + scanf("%d", &number); + search(number, head); + while (a[i] != -1&&a[i]!=0) + { + printf("No.%d's value is %d\n", a[i], number); + ++i; + judge2 = 1; + } + if (judge2 == 0) + { + printf("-1\nComplete\n"); + } + else + { + printf("Complete\n"); + } + } + else if (judge == -1) + { + return 0; + } + else + { + printf("Input Error!\nPlease input again!\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/practices/c/level1/p12_warehouse/warehouse.c b/practices/c/level1/p12_warehouse/warehouse.c new file mode 100644 index 00000000..792c0ebd --- /dev/null +++ b/practices/c/level1/p12_warehouse/warehouse.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include + +int n,m,v[1000]; +struct Data +{ + int num; + char name[10]; +}; +Data goods[1000],temp[1000]; +void loading(); +void saving(); +void working(); +int main() +{ + loading(); + working(); + saving(); + return 0; +} +void loading() +{ + FILE *f=fopen("data.txt","r"); + if(f==NULL) + { + printf("No data!\n"); + return; + } + else + { + fscanf(f,"%d",&n); + for (int i=1;i<=n;i++) + { + fscanf(f,"%s %d",goods[i].name,&goods[i].num); + } + } + fclose(f); +} +void working() +{ + while (1) + { + printf("1 show list\n"); + printf("2 append\n"); + printf("3 pop\n"); + printf("4 exit\n"); + char ch=getch(); + if (ch<'1'||ch>'6') + continue; + int i,j,k=ch-'0'; + if (k==1) { + for (i=1;i<=n;i++) + { + printf("%d:%s %d\n",i,goods[i].name,goods[i].num); + } + Sleep(3000); + } + if (k==2) + { + printf("Please input the number of goods you want to add:\n"); + scanf("%d",&m); + printf("Please input some goods:\n"); + for (i=n+1;i<=n+m;i++) + { + scanf("%s %d",goods[i].name,&goods[i].num); + } + n+=m; + } + if (k==3) + { + printf("Please input some goods:\n"); + + scanf("%d",&j); + for (i=1;i<=n-j;i++) + { + temp[j+i-1]=goods[j+i]; + goods[j+i-1]=temp[j+i-1]; + } + } + if (k==4) + break; + } +} +void saving() +{ + FILE *f=fopen("data.txt","w"); + fprintf(f,"%d\n",n); + for (int i=1;i<=n;++i) { + fprintf(f,"%s %d\n",goods[i].name,goods[i].num); + } + fclose(f); +} diff --git a/practices/cpp/level1/p01_Queue/main_quene.cpp b/practices/cpp/level1/p01_Queue/main_quene.cpp new file mode 100644 index 00000000..c31cb38c --- /dev/null +++ b/practices/cpp/level1/p01_Queue/main_quene.cpp @@ -0,0 +1,13 @@ +#include +#include"Queue.h" +using namespace std; + +int main() +{ + Queue queue; + while(!isFull()) + { + append(100); + } + return 0; + } diff --git a/practices/cpp/level1/p01_Queue/queue.cpp b/practices/cpp/level1/p01_Queue/queue.cpp new file mode 100644 index 00000000..f18d3862 --- /dev/null +++ b/practices/cpp/level1/p01_Queue/queue.cpp @@ -0,0 +1,35 @@ +#include "Queue.h" +bool Queue::isFull() +{ + if(data[100]!=0) + { + return true; + } + else + return false; +} +bool Quene::isEmpty() +{ + return data==tail; +} +void Queue::append(int item) +{ + if ((tail+1)%100==head) + { + cout<<"Full!\n"; + return; + } + tail++; + if (tail==MAXQUEUE) + tail=0; + data[tail]=item; +} +void Queue::pop() +{ + if (head!=tail) + { + head++; + if (head==MAXQUEUE) + head=0; + } +} diff --git a/practices/cpp/level1/p01_Queue/queue.h b/practices/cpp/level1/p01_Queue/queue.h new file mode 100644 index 00000000..8f03fab9 --- /dev/null +++ b/practices/cpp/level1/p01_Queue/queue.h @@ -0,0 +1,17 @@ +#ifndef Queue_H +#define Queue_H + +class Queue +{ + public: + bool isFull(); + void append(int item); + bool isEmpty(); + protected: + private: + int tail=0; + int head=0; + int data[100]={0}; +}; + +#endif diff --git a/practices/cpp/level1/p02_Stack/main_stack.cpp b/practices/cpp/level1/p02_Stack/main_stack.cpp new file mode 100644 index 00000000..d1d9bd5a --- /dev/null +++ b/practices/cpp/level1/p02_Stack/main_stack.cpp @@ -0,0 +1,18 @@ +#include +#include"stack.h" + +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char** argv) { + stack stack1; + stack1.push(123); + if(stack1.isEmpty()==1) + { + printf("The stack is empty"); + } + else + { + printf("The stack is not empty"); + } + return 0; +} diff --git a/practices/cpp/level1/p02_Stack/stack.cpp b/practices/cpp/level1/p02_Stack/stack.cpp new file mode 100644 index 00000000..0e7cd379 --- /dev/null +++ b/practices/cpp/level1/p02_Stack/stack.cpp @@ -0,0 +1,26 @@ +#include "stack.h" +void stack::push(int temp) +{ + data[tail]=temp; + tail++; +} +void stack::pop() +{ + tail--; +} +bool stack::isFull() +{ + if(data[MAXDATA]!=0) + { + return true; + } + return false; +} +bool stack::isEmpty() +{ + if(data[0]==0) + { + return true; + } + return false; +} diff --git a/practices/cpp/level1/p02_Stack/stack.h b/practices/cpp/level1/p02_Stack/stack.h new file mode 100644 index 00000000..ce14fae7 --- /dev/null +++ b/practices/cpp/level1/p02_Stack/stack.h @@ -0,0 +1,18 @@ +#ifndef STACK_H +#define STACK_H +#define MAXDATA 100 + +class stack +{ + public: + void push(int temp); + void pop(); + bool isFull(); + bool isEmpty(); + private: + int data[100]={0}; + int tail=0; + int head=0; +}; + +#endif diff --git a/practices/cpp/level1/p03_SafeArray/safearray.cpp b/practices/cpp/level1/p03_SafeArray/safearray.cpp new file mode 100644 index 00000000..1fae20cb --- /dev/null +++ b/practices/cpp/level1/p03_SafeArray/safearray.cpp @@ -0,0 +1,40 @@ +#include "SafeArray.h" +#include + +SafeArray::SafeArray() +{ + data=NULL; + totalLength=0; +} + +SafeArray::~SafeArray() +{ + delete []data; + totalLength=0; + data=NULL; +} + +void SafeArray::create(int length) +{ + totalLength=length; + data=new int[length]; +} + +bool SafeArray::write(int index,int number) +{ + if (index<0||index>=totalLength) + { + return false; + } + data[index]=number; + return true; +} + +int SafeArray::read(int index){ + if (index<0||index>=totalLength) + { + printf("越界"); + return -1; + } + return data[index]; +} diff --git a/practices/cpp/level1/p03_SafeArray/safearray.h b/practices/cpp/level1/p03_SafeArray/safearray.h new file mode 100644 index 00000000..c0118967 --- /dev/null +++ b/practices/cpp/level1/p03_SafeArray/safearray.h @@ -0,0 +1,12 @@ +class SafeArray{ + private: + int totalLength; + int *data; + + public: + SafeArray(); + ~SafeArray(); + void create(int length); + bool write(int index,int number); + int read(int index); +}; diff --git a/practices/cpp/level1/p03_SafeArray/safearray_main.cpp b/practices/cpp/level1/p03_SafeArray/safearray_main.cpp new file mode 100644 index 00000000..8f73e4ba --- /dev/null +++ b/practices/cpp/level1/p03_SafeArray/safearray_main.cpp @@ -0,0 +1,11 @@ +#include +#include +using namespace std; + +int main() +{ + int length; + cin>>length; + SafeArray safearray; + safearray.create(length); + } diff --git a/practices/cpp/level1/p04_cppScoreManagement/CLASS.h b/practices/cpp/level1/p04_cppScoreManagement/CLASS.h new file mode 100644 index 00000000..0579b778 --- /dev/null +++ b/practices/cpp/level1/p04_cppScoreManagement/CLASS.h @@ -0,0 +1,22 @@ +#ifndef CLASSES_H +#define CLASSES_H + +#define MAXN 150 +class Classes +{ + private: + struct + { + char name[20]; + char studentNumber[20]; + int grade; + }student[151]; + int totalNumber; + + public: + void showList(); + void addStudent(); + void deleteStudent(); + void enteringGrade(); +}; +#endif diff --git a/practices/cpp/level1/p04_cppScoreManagement/class.cpp b/practices/cpp/level1/p04_cppScoreManagement/class.cpp new file mode 100644 index 00000000..822e75ca --- /dev/null +++ b/practices/cpp/level1/p04_cppScoreManagement/class.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include "CLASS.h" + +void Classes::showList() +{ + for (int i=1;i<=totalNumber;i++) + { + if (student[i].name[0]!=0) + { + printf("%s %s %d\n",student[i].studentNumber,student[i].name,student[i].grade); + } + } +} + + +void Classes::deleteStudent() +{ + MAXN=150; + char tmpName[20],tmpNumber[20]; + int order=0; + while(1) + { + if (totalNumber==0) + { + printf("Faliure!\n"); + return; + } + loop: + printf("Input name & student number:"); + scanf("%s %s",tmpName,tmpNumber); + for (int i=1;i<=totalNumber;i++) + { + if (strcmp(tmpName,student[i].studentNumber)==0) + { + totalNumber--; + student[i].name[0]=0; + goto loop; + } + } + printf("Not Exist!\n"); + printf("Continue?1=YES/0=NO\n"); + scanf("%d",&order); + if (order==0) break; + } +} + +void Classes::enteringGrade(){ + MAXN=150; + int tmpGrade=0,order=0; + char tmpName[20],tmpNumber[20]; + while(1){ + if (totalNumber==0){ + printf("No info!\n"); + return; + } + loop: + printf("Input Name & Number:"); + scanf("%s %s",tmpName,tmpNumber); + for (int i=1;i<=totalNumber;i++){ + if (strcmp(tmpName,student[i].studentNumber)==0){ + printf("Input grade:"); + scanf("%d",&tmpGrade); + student[i].grade=tmpGrade; + goto loop; + } + } + printf("Not exist!\n"); + printf("Continue?1=YES/0=NO\n"); + scanf("%d",&order); + if (order==0) break; + } +} +void Classes::addStudent(){ + char tmpName[20],tmpNumber[20]; + int order=0; + while(1) + { + if (totalNumber>=MAXN) + { + printf("Full!\n"); + return; + } + loop: + printf("Input name&student number"); + scanf("%s %s",tmpName,tmpNumber); + for (int i=1;i<=totalNumber;i++){ + if (strcmp(tmpName,student[i].studentNumber)==0){ + printf("Exists!\n"); + goto loop; + } + } + totalNumber++; + for (int i=1;i<=totalNumber;i++){ + if (student[i].name[0]==0){ + strcpy(student[i].name,tmpName); + strcpy(student[i].studentNumber,tmpNumber); + break; + } + } + printf("Continue?1=YES 0=NO\n"); + scanf("%d",&order); + if (order==0) break; + } +} + diff --git a/practices/cpp/level1/p04_cppScoreManagement/main_class.cpp b/practices/cpp/level1/p04_cppScoreManagement/main_class.cpp new file mode 100644 index 00000000..634b9643 --- /dev/null +++ b/practices/cpp/level1/p04_cppScoreManagement/main_class.cpp @@ -0,0 +1,31 @@ +#include +#include +#include "Classes.h" +using namespace std; + +int main() +{ + int a; + scanf("%d",a); + while(1) + { + + cout<<"1:showlist\n2:add student\n3:delete student\n4:entering"<isPassable = 1; +} +void Switch::turnoff() +{ + this->isPassable = 0; +} + +Fan::Fan() +{ + isPassable = 1; + rotateSpeed = 10; + isSwitch = 0; +} +void Fan::turnon(){ + isPassable = 1; +} +void Fan::turnoff(){ + isPassable = 0; +} + +Bulb::Bulb() +{ + isPassable = 1; + light = 10; + isSwitch = 0; +} + +void Bulb::turnon(){ + isPassable = 1; +} +void Bulb::turnoff(){ + isPassable = 0; +} diff --git a/practices/cpp/level1/p07_Circuit/Cell.h b/practices/cpp/level1/p07_Circuit/Cell.h new file mode 100644 index 00000000..9ab18352 --- /dev/null +++ b/practices/cpp/level1/p07_Circuit/Cell.h @@ -0,0 +1,50 @@ +#ifndef CELL_H +#define CELL_H + + +class Cell +{ + public: + Cell() + { + isPassable = 0; + } + virtual void turnon()=0; + virtual void turnoff()=0; + bool isSwitch; + protected: + bool isPassable; +}; + +class Switch : public Cell +{ + public: + Switch(); + void turnon(); + void turnoff(); +}; + + +class Fan : public Cell +{ + public: + Fan(); + void turnon(); + void turnoff(); + int getRotateSpeed(); + private: + int rotateSpeed; +}; + +class Bulb : public Cell +{ + public: + Bulb(); + void turnon(); + void turnoff(); + int getLight(); + private: + int light; +}; + +#endif // CELL_H diff --git a/practices/cpp/level1/p07_Circuit/Circuit.cpp b/practices/cpp/level1/p07_Circuit/Circuit.cpp new file mode 100644 index 00000000..59451dfd --- /dev/null +++ b/practices/cpp/level1/p07_Circuit/Circuit.cpp @@ -0,0 +1,25 @@ +#include "Circuit.h" +using namespace std; + +void Circuit::addCell(Cell* cell) +{ + Element.push_back(cell); +} + +void Circuit::on() +{ + for(unsigned i=0;i != Element.size();i++) + { + Element[i]->turnon(); + } +} + +void Circuit::off() +{ + for(unsigned i=0;i != Element.size();i++) + { + Element[i]->turnoff(); + } +} + + diff --git a/practices/cpp/level1/p07_Circuit/Circuit.h b/practices/cpp/level1/p07_Circuit/Circuit.h new file mode 100644 index 00000000..60e49eab --- /dev/null +++ b/practices/cpp/level1/p07_Circuit/Circuit.h @@ -0,0 +1,17 @@ +#ifndef CIRCUIT_H +#define CIRCUIT_H +#include "Cell.h" +#include +using namespace std; + +class Circuit +{ + public: + void addCell(Cell* cell); + void off(); + void on(); + private: + vector Element; +}; + +#endif // CIRCUIT_H diff --git a/practices/cpp/level1/p07_Circuit/main.cpp b/practices/cpp/level1/p07_Circuit/main.cpp new file mode 100644 index 00000000..8812fea1 --- /dev/null +++ b/practices/cpp/level1/p07_Circuit/main.cpp @@ -0,0 +1,18 @@ +#include +#include "Cell.h" +#include "Circuit.h" +using namespace std; + + + +int main() +{ + Circuit circuit; + Fan f1,f2; + Bulb b1,b2; + circuit.addCell(&f1); + circuit.addCell(&b2); + circuit.on(); + circuit.off(); + return 0; +} diff --git a/practices/cpp/level1/p08_EmployeeAndSales/Employee.cpp b/practices/cpp/level1/p08_EmployeeAndSales/Employee.cpp new file mode 100644 index 00000000..1f82adff --- /dev/null +++ b/practices/cpp/level1/p08_EmployeeAndSales/Employee.cpp @@ -0,0 +1,24 @@ +#include "Employee.h" + +Employee::Employee(int t_level=1) +{ + level = t_level; +} + +double Employee::getSalary() +{ + salary = level * 1000; + return salary; +} + +Sales::Sales(int t_level = 1,double t_salesValue = 6000) +{ + level = t_level; + salesValue = t_salesValue; +} + +double Sales::getSalary() +{ + salary = salesValue*0.2 + level * 1000.0; + return salary; +} diff --git a/practices/cpp/level1/p08_EmployeeAndSales/Employee.h b/practices/cpp/level1/p08_EmployeeAndSales/Employee.h new file mode 100644 index 00000000..b2db1809 --- /dev/null +++ b/practices/cpp/level1/p08_EmployeeAndSales/Employee.h @@ -0,0 +1,25 @@ +#ifndef EMPLOYEE_H +#define EMPLOYEE_H +#include + +class Employee +{ + public: + Employee(int t_level); + std::string name; + unsigned age; + int level; + virtual double getSalary(); + protected: + double salary; +}; + +class Sales : public Employee +{ + public: + Sales(int t_level,double t_salesValue); + double getSalary(); + private: + double salesValue; +}; +#endif // EMPLOYEE_H diff --git a/practices/cpp/level1/p08_EmployeeAndSales/main.cpp b/practices/cpp/level1/p08_EmployeeAndSales/main.cpp new file mode 100644 index 00000000..6f81451f --- /dev/null +++ b/practices/cpp/level1/p08_EmployeeAndSales/main.cpp @@ -0,0 +1,12 @@ +#include +#include "Employee.h" +using namespace std; + +int main() +{ + Sales s1 = Sales(5,6000); + Employee e1 = Employee(6); + double e1_salary = e1.getSalary(); + double s1_salary = s1.getSalary(); + cout<sonNode = new Tree; + sonNode->value = tvalue; + sonNode->brotherNode = NULL; + sonNode->sonNode = NULL; +} + +void Tree::appendBrother(int tvalue) +{ + this->brotherNode = new Tree; + brotherNode->value = tvalue; + brotherNode->brotherNode = NULL; + brotherNode->sonNode = NULL; +} + +int Tree::calculate() +{ + int sum = 0; + Tree* tNode = this->sonNode; + while(tNode) + { + sum++; + tNode = tNode->sonNode; + } + return sum; +} + +int Tree::getValue() +{ + return value; +} diff --git a/practices/cpp/level1/p09_Tree/Tree.h b/practices/cpp/level1/p09_Tree/Tree.h new file mode 100644 index 00000000..688b9043 --- /dev/null +++ b/practices/cpp/level1/p09_Tree/Tree.h @@ -0,0 +1,21 @@ +#ifndef TREE_H +#define TREE_H + + +class Tree +{ + public: + Tree(); + Tree(int tvalue); + ~Tree(); + Tree* sonNode; + Tree* brotherNode; + void appendSon(int tvalue); + void appendBrother(int tvalue); + int calculate(); + int getValue(); + private: + int value; +}; + +#endif // TREE_H diff --git a/practices/cpp/level1/p09_Tree/main.cpp b/practices/cpp/level1/p09_Tree/main.cpp new file mode 100644 index 00000000..b3b75bb0 --- /dev/null +++ b/practices/cpp/level1/p09_Tree/main.cpp @@ -0,0 +1,14 @@ +#include +#include "Tree.h" +using namespace std; + +int main() +{ + Tree root = Tree(0); + root.appendSon(15); + root.appendBrother(20); + root.sonNode->appendSon(10); + int sum = root.calculate(); + cout<type = type; +} +int Bullet::getType(){ + return type; +} +Bullet::Bullet(const Bullet& bullet){ + this->type = bullet.type; + this->setTexture(*bullet.getTexture()); +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Bullet.h b/practices/cpp/level1/p11_Fighters/Bullet.h new file mode 100644 index 00000000..6c12eda3 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Bullet.h @@ -0,0 +1,16 @@ +#ifndef BULLET_H +#define BULLET_H +#include +#include +#define HERO_BULLET 1 +#define ENEMY_BULLET 2 +class Bullet:public sf::Sprite{ +public: + Bullet(){}; + Bullet(const Bullet& bullet); + Bullet(int type); + int getType(); +private: + int type; +}; +#endif \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Controller.cpp b/practices/cpp/level1/p11_Fighters/Controller.cpp new file mode 100644 index 00000000..384ee9cb --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Controller.cpp @@ -0,0 +1,21 @@ +#include "Controller.h" + +void Controller::control(Plane &plane){ + if((sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) && (plane.getPosition().y - plane.getTexture()->getSize().y / 2 > 0)){ + plane.move(0, -SPEED); + } + if((sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) && (plane.getPosition().y + plane.getTexture()->getSize().y / 2 < 800)){ + plane.move(0, SPEED); + } + + if((sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) && (plane.getPosition().x - plane.getTexture()->getSize().x / 2 > 0)){ + plane.move(-SPEED, 0); + } + + if((sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) && (plane.getPosition().x + plane.getTexture()->getSize().x / 2 < 480)){ + plane.move(SPEED, 0); + } + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){ + plane.fire(); + } +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Controller.h b/practices/cpp/level1/p11_Fighters/Controller.h new file mode 100644 index 00000000..4a20731b --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Controller.h @@ -0,0 +1,9 @@ +#ifndef CONTROLLER_H +#define CONTROLLER_H +#include "Sky.h" +#define SPEED 5 +class Controller{ +public: + void control(Plane &plane); +}; +#endif \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Firer.cpp b/practices/cpp/level1/p11_Fighters/Firer.cpp new file mode 100644 index 00000000..2dab55e1 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Firer.cpp @@ -0,0 +1,13 @@ +#include "Sky.h" + +Firer::Firer(Sky &sky , LoadTexture &loadtexture){ + this->sky = &sky; + this->loadtexture = &loadtexture; +} +void Firer::fire(sf::Vector2f position , int type){ + bullet = new Bullet(type); + bullet->setTexture(*loadtexture->getTextureByName("bullet2")); + bullet->setOrigin(bullet->getTexture()->getSize().x / 2 ,bullet->getTexture()->getSize().y / 2); + bullet->setPosition(position); + sky->add(bullet); +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Firer.h b/practices/cpp/level1/p11_Fighters/Firer.h new file mode 100644 index 00000000..f74df0fd --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Firer.h @@ -0,0 +1,9 @@ +#ifndef FIRER_H +#define FIRER_H +#include +#include +#include "LoadTexture.h" +// xun huan yinyong priblem +// move the content to sky.h + +#endif \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/LoadTexture.cpp b/practices/cpp/level1/p11_Fighters/LoadTexture.cpp new file mode 100644 index 00000000..8f9a32d9 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/LoadTexture.cpp @@ -0,0 +1,22 @@ +#include "LoadTexture.h" + +void LoadTexture::loadFromFile(string path){ + initLoad(); + sf::Image image; + sf::Texture *texture; + image.loadFromFile(path); + for (int i = 0 ; i < imageVector.size() ; i ++){ + sf::IntRect r(imageVector.at(i)->position[0] , imageVector.at(i)->position[1] , imageVector.at(i)->position[2] , imageVector.at(i)->position[3]); + texture = new sf::Texture(); + texture->loadFromImage(image , r); + textureVector.push_back(texture); + } +} +sf::Texture* LoadTexture::getTextureByName(string name){ + for (int i = 0 ; i < imageVector.size() ; i++){ + if (imageVector.at(i)->name == name){ + return textureVector.at(i); + } + } + std::cout<<"no texture matched!\n"; +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/LoadTexture.h b/practices/cpp/level1/p11_Fighters/LoadTexture.h new file mode 100644 index 00000000..b6dfcbe0 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/LoadTexture.h @@ -0,0 +1,321 @@ +#ifndef LOADTEXTURE_H +#define LOADTEXTURE_H +#include +#include +#include +#include +#include +using namespace std; +struct Position{ + string name; + int position[4]; +}; +typedef struct Position ImagePosition; + +class LoadTexture{ + public: + void loadFromFile(string path); + sf::Texture* getTextureByName(string name); + void initLoad(){ +imagePosition = new ImagePosition; +imagePosition -> name = "enemy3_down6"; +imagePosition -> position[0] = 0; +imagePosition -> position[1] = 747; +imagePosition -> position[2] = 166; +imagePosition -> position[3] = 261; +imageVector.push_back(imagePosition); +imagePosition = new ImagePosition; +imagePosition -> name = "enemy3_hit"; +imagePosition -> position[0] = 166; +imagePosition -> position[1] = 750; +imagePosition -> position[2] = 169; +imagePosition -> position[3] = 258; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy3_n1"; +imagePosition -> position[0] = 335; +imagePosition -> position[1] = 750; +imagePosition -> position[2] = 169; +imagePosition -> position[3] = 258; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy3_n2"; +imagePosition -> position[0] = 504; +imagePosition -> position[1] = 750; +imagePosition -> position[2] = 169; +imagePosition -> position[3] = 258; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy3_down1"; +imagePosition -> position[0] = 0; +imagePosition -> position[1] = 486; +imagePosition -> position[2] = 165; +imagePosition -> position[3] = 261; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy3_down2"; +imagePosition -> position[0] = 0; +imagePosition -> position[1] = 225; +imagePosition -> position[2] = 165; +imagePosition -> position[3] = 261; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy3_down5"; +imagePosition -> position[0] = 673; +imagePosition -> position[1] = 748; +imagePosition -> position[2] = 166; +imagePosition -> position[3] = 260; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy3_down3"; +imagePosition -> position[0] = 839; +imagePosition -> position[1] = 748; +imagePosition -> position[2] = 165; +imagePosition -> position[3] = 260; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy3_down4"; +imagePosition -> position[0] = 165; +imagePosition -> position[1] = 486; +imagePosition -> position[2] = 165; +imagePosition -> position[3] = 261; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "hero1"; +imagePosition -> position[0] = 0; +imagePosition -> position[1] = 99; +imagePosition -> position[2] = 102; +imagePosition -> position[3] = 126; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "hero2"; +imagePosition -> position[0] = 165; +imagePosition -> position[1] = 360; +imagePosition -> position[2] = 102; +imagePosition -> position[3] = 126; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "hero_blowup_n1"; +imagePosition -> position[0] = 165; +imagePosition -> position[1] = 234; +imagePosition -> position[2] = 102; +imagePosition -> position[3] = 126; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "hero_blowup_n2"; +imagePosition -> position[0] = 330; +imagePosition -> position[1] = 624; +imagePosition -> position[2] = 102; +imagePosition -> position[3] = 126; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "hero_blowup_n3"; +imagePosition -> position[0] = 330; +imagePosition -> position[1] = 498; +imagePosition -> position[2] = 102; +imagePosition -> position[3] = 126; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "hero_blowup_n4"; +imagePosition -> position[0] = 432; +imagePosition -> position[1] = 624; +imagePosition -> position[2] = 102; +imagePosition -> position[3] = 126; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy2"; +imagePosition -> position[0] = 0; +imagePosition -> position[1] = 0; +imagePosition -> position[2] = 69; +imagePosition -> position[3] = 99; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy2_hit"; +imagePosition -> position[0] = 432; +imagePosition -> position[1] = 525; +imagePosition -> position[2] = 69; +imagePosition -> position[3] = 99; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "ufo2"; +imagePosition -> position[0] = 102; +imagePosition -> position[1] = 118; +imagePosition -> position[2] = 60; +imagePosition -> position[3] = 107; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy2_down1"; +imagePosition -> position[0] = 534; +imagePosition -> position[1] = 655; +imagePosition -> position[2] = 69; +imagePosition -> position[3] = 95; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy2_down2"; +imagePosition -> position[0] = 603; +imagePosition -> position[1] = 655; +imagePosition -> position[2] = 69; +imagePosition -> position[3] = 95; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy2_down3"; +imagePosition -> position[0] = 672; +imagePosition -> position[1] = 653; +imagePosition -> position[2] = 69; +imagePosition -> position[3] = 95; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy2_down4"; +imagePosition -> position[0] = 741; +imagePosition -> position[1] = 653; +imagePosition -> position[2] = 69; +imagePosition -> position[3] = 95; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "ufo1"; +imagePosition -> position[0] = 267; +imagePosition -> position[1] = 398; +imagePosition -> position[2] = 58; +imagePosition -> position[3] = 88; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "bomb"; +imagePosition -> position[0] = 810; +imagePosition -> position[1] = 691; +imagePosition -> position[2] = 63; +imagePosition -> position[3] = 57; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy1_down1"; +imagePosition -> position[0] = 267; +imagePosition -> position[1] = 347; +imagePosition -> position[2] = 57; +imagePosition -> position[3] = 51; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy1_down2"; +imagePosition -> position[0] = 873; +imagePosition -> position[1] = 697; +imagePosition -> position[2] = 57; +imagePosition -> position[3] = 51; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy1_down3"; +imagePosition -> position[0] = 267; +imagePosition -> position[1] = 296; +imagePosition -> position[2] = 57; +imagePosition -> position[3] = 51; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy1_down4"; +imagePosition -> position[0] = 930; +imagePosition -> position[1] = 697; +imagePosition -> position[2] = 57; +imagePosition -> position[3] = 51; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "game_pause_nor"; +imagePosition -> position[0] = 267; +imagePosition -> position[1] = 251; +imagePosition -> position[2] = 60; +imagePosition -> position[3] = 45; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "game_pause_pressed"; +imagePosition -> position[0] = 810; +imagePosition -> position[1] = 646; +imagePosition -> position[2] = 60; +imagePosition -> position[3] = 45; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "enemy1"; +imagePosition -> position[0] = 534; +imagePosition -> position[1] = 612; +imagePosition -> position[2] = 57; +imagePosition -> position[3] = 43; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "bullet1"; +imagePosition -> position[0] = 1004; +imagePosition -> position[1] = 987; +imagePosition -> position[2] = 9; +imagePosition -> position[3] = 21; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +imagePosition = new ImagePosition; +imagePosition -> name = "bullet2"; +imagePosition -> position[0] = 69; +imagePosition -> position[1] = 78; +imagePosition -> position[2] = 9; +imagePosition -> position[3] = 21; +if (imagePosition != NULL){ + imageVector.push_back(imagePosition); +} +} + private: + vector textureVector; + vector imageVector; + ImagePosition* imagePosition; +}; +#endif \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Plane.cpp b/practices/cpp/level1/p11_Fighters/Plane.cpp new file mode 100644 index 00000000..3b1796d2 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Plane.cpp @@ -0,0 +1,67 @@ +#include "Sky.h" +#include "math.h" +Plane::Plane(int type){ + ifStop = false; + ifKilled = false; + this->type = type; + planeClock.restart(); + bulletSoundBuffer.loadFromFile("resource/sounds/bullet.ogg"); + bulletSound.setBuffer(bulletSoundBuffer); + nowTexture = 0; + downSoundbuffer.loadFromFile("resource/sounds/enemy1_down.ogg"); + downSound.setBuffer(downSoundbuffer); +} +void Plane::fire(){ + if((planeClock.getElapsedTime().asSeconds() > sf::seconds(0.2f).asSeconds()) || (this->type != HERO)){ + planeFirer->fire(this->getPosition() , this->getType()); + planeClock.restart(); + bulletSound.play(); + } +} +int Plane::getType(){ + return type; +} +void Plane::setFirer(Firer &firer){ + this->planeFirer = &firer; +} +void Plane::kill(Sky *sky){ + ifKilled = true; + textureClock.restart(); + this->setTexture(*textureVector->at(0)); + downSound.play(); +} +bool Plane::ifCouldErase(){ + if(ifStop && ifKilled){ + return true; + }else{ + return false; + } +} +bool Plane::ifkill(){ + return ifKilled; +} +void Plane::moveThis(){ + if (this->type == ENEMY_1){ + if (this->ifKilled){ + if (textureClock.getElapsedTime().asSeconds() > sf::seconds(timePerTexture).asSeconds()){ + nowTexture ++; + textureClock.restart(); + if (nowTexture > 3){ + ifStop = true; + return; + } + this->setTexture(*textureVector->at(nowTexture)); + return; + } + return; + } + this->move(0 , 3); + } +} +void Plane::setTextureVector(std::vector *v){ + this->textureVector = v; +} +void Plane::setTimePerTexture(float time){ + timePerTexture = time; +} + diff --git a/practices/cpp/level1/p11_Fighters/Plane.h b/practices/cpp/level1/p11_Fighters/Plane.h new file mode 100644 index 00000000..0b9f16aa --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Plane.h @@ -0,0 +1,6 @@ +#ifndef PLANE_H +#define PLANE_H +#include +#include +// move the content to sky.h +#endif \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/Sky.cpp b/practices/cpp/level1/p11_Fighters/Sky.cpp new file mode 100644 index 00000000..5cb86b65 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Sky.cpp @@ -0,0 +1,165 @@ +#include "Sky.h" +Sky::Sky(){ + score = 0; + level = 1; + ss_Score.str(""); + ss_Score << score; + string_Score = ss_Score.str(); + text_Score.setString("Score : " + string_Score); + font.loadFromFile("resource/fonts/myTTF.ttf"); + text_Score.setFont(font); + text_Score.setPosition(5, 5); + text_Score.setColor(sf::Color::Black); + text_Score.setCharacterSize(24); + + text_Level.setFont(font); + text_Level.setPosition((480 / 2) - 34 * 2 , 800 / 2); + text_Level.setColor(sf::Color::Black); + text_Level.setCharacterSize(34); + levelClock.restart(); + backgroundClock.restart(); + ifStart = false; +} +void Sky::add(sf::Sprite &sprite){ + if(((Plane*)(&sprite))->getType() == HERO){ + //std::cout << "add Plane\n"; + spriteVector.push_back(&sprite); + return; + } + if(((Plane*)(&sprite))->getType() == ENEMY_1){ + //std::cout << "add enemy\n"; + enemy_1Vector.push_back(&sprite); + return; + } + animationVector.push_back(&sprite); + //std::cout << "add other\n"; +} +void Sky::add(Bullet *bullet){ + if (bullet->getType() == HERO_BULLET){ + std::cout << "add hero bullet\n"; + herobulletVector.push_back(bullet); + return; + } + if (bullet->getType() == ENEMY_BULLET){ + std::cout << "add enemy bullet\n"; + enemybulletVector.push_back(bullet); + return; + } +} + +void Sky::draw(sf::RenderWindow &window){ + // move all + this->moveAll(); + // check + check(); + // draw the hero + for (int i = 0 ; i != spriteVector.size() ; i ++){ + window.draw(*spriteVector.at(i)); + } + // draw the hero bullet + for (int i = 0 ; i != herobulletVector.size() ; i ++){ + window.draw(*herobulletVector.at(i)); + } + // draw the enemy + for (int i = 0 ; i != enemy_1Vector.size() ; i ++){ + window.draw(*enemy_1Vector.at(i)); + } + // draw enemy bullet + for (int i = 0 ; i != enemybulletVector.size() ; i ++){ + window.draw(*enemybulletVector.at(i)); + } + // draw the score + window.draw(text_Score); + // draw the level + if (levelClock.getElapsedTime().asSeconds() < sf::seconds(1.5f).asSeconds()){ + window.draw(text_Level); + } + + +} +void Sky::moveAll(){ + // move hero bullets + int i = 0; + while (i < herobulletVector.size()){ + if(herobulletVector.at(i)->getPosition().y + herobulletVector.at(i)->getTexture()->getSize().y / 2 < 0){ + // erase the bullet + delete herobulletVector.at(i); + herobulletVector.erase(herobulletVector.begin() + i); + continue; + } + herobulletVector.at(i)->move(0, -5); + i ++; + } + // move enemybullet + i = 0; + while (i < enemybulletVector.size()){ + if(enemybulletVector.at(i)->getPosition().y - enemybulletVector.at(i)->getTexture()->getSize().y / 2 > 800){ + // erase the bullet + delete enemybulletVector.at(i); + enemybulletVector.erase(enemybulletVector.begin() + i); + continue; + } + enemybulletVector.at(i)->move(0 , 5); + i++; + } + // move enemy + i = 0; + while(i < enemy_1Vector.size()){ + if((enemy_1Vector.at(i)->getPosition().y - enemy_1Vector.at(i)->getTexture()->getSize().y / 2 > 800)||(((Plane*)enemy_1Vector.at(i))->ifCouldErase())){ + // erase the enemy + delete enemy_1Vector.at(i); + enemy_1Vector.erase(enemy_1Vector.begin()+i); + continue; + } + ((Plane*)enemy_1Vector.at(i))->moveThis(); + i ++; + } +} +void Sky::check(){ + int i = 0; + bool tmp = false; + while(i < herobulletVector.size()){ + for(int j = 0 ; j < enemy_1Vector.size() ; j ++){ + if (herobulletVector.at(i)->getGlobalBounds().intersects(enemy_1Vector.at(j)->getGlobalBounds()) && !((Plane*)enemy_1Vector.at(j))->ifkill()){ + // kill the enemy + ((Plane*)enemy_1Vector.at(j))->kill(this); + delete herobulletVector.at(i); + herobulletVector.erase(herobulletVector.begin() + i); + tmp = true; + score += 10; + ss_Score.str(""); + ss_Score << score; + string_Score = ss_Score.str(); + text_Score.setString("Score : " + string_Score); + if(level != (score / 100) + 1){ + // change the level + level = score / 100 + 1; + ss_Level.str(""); + ss_Level << level; + string_Level = ss_Level.str(); + text_Level.setString("Level " + string_Level); + levelClock.restart(); + } + break; + } + } + if (tmp){ + tmp = false; + continue; + } + i++; + } +} +int Sky::getLevel(){ + return level; +} +void Sky::guiChu(){ + if (ifStart){ + if (backgroundStartClock.getElapsedTime().asSeconds() > sf::seconds(0.5f).asSeconds()){ + ifStart = false; + backgroundClock.restart(); + return; + } + + } +} diff --git a/practices/cpp/level1/p11_Fighters/Sky.h b/practices/cpp/level1/p11_Fighters/Sky.h new file mode 100644 index 00000000..35e270ff --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Sky.h @@ -0,0 +1,84 @@ +#ifndef SKY_H +#define SKY_H +#include +#include +#include "Bullet.h" +#include +#include "Plane.h" +#include "LoadTexture.h" +#include +#include +// Sky Class +class Sky{ +public: + Sky(); + void add(sf::Sprite &sprite); + void add(Bullet *bullet); + void draw(sf::RenderWindow &window); + int getLevel(); +private: + void moveAll(); + void check(); + std::vector spriteVector; + std::vector herobulletVector; + std::vector enemybulletVector; + std::vector enemy_1Vector; + std::vector animationVector; + sf::Sprite* enemyNum[100]; + int score; + int level; + sf::Text text_Score; + std::string string_Score; + std::stringstream ss_Score; + sf::Font font; + sf::Clock levelClock; + sf::Text text_Level; + std::string string_Level; + std::stringstream ss_Level; + sf::Clock backgroundClock; + sf::Clock backgroundStartClock; + void guiChu(); + bool ifStart; +}; +// Firer class +class Firer{ +public: + Firer(Firer &firer); + Firer(Sky &sky , LoadTexture &loadtexture); + void fire(sf::Vector2f positon , int type); +private: + Bullet *bullet; + Sky *sky; + LoadTexture *loadtexture; +}; +// Plane class +#define HERO 1 +#define ENEMY_1 2 +class Plane:public sf::Sprite{ +public: + Plane(int type); + void setFirer(Firer &firer); + void fire(); + int getType(); + void kill(Sky *sky); + bool ifCouldErase(); + bool ifkill(); + void moveThis(); + void setTextureVector(std::vector *v); + void setTimePerTexture(float time); +private: + sf::Clock planeClock; + int type; + Firer *planeFirer; + sf::Sound bulletSound; + sf::SoundBuffer bulletSoundBuffer; + bool ifStop; + bool ifKilled; + int nowTexture; + std::vector *textureVector; + sf::Clock textureClock; + float timePerTexture; + sf::SoundBuffer downSoundbuffer; + sf::Sound downSound; +}; +#endif \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/main.cpp b/practices/cpp/level1/p11_Fighters/main.cpp new file mode 100644 index 00000000..2688587c --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/main.cpp @@ -0,0 +1,84 @@ +#include +#include +#include "Sky.h" +#include "LoadTexture.h" +#include "Plane.h" +#include "Controller.h" +#include "Bullet.h" +#include "Firer.h" +int main(void){ + // the score + // the clock to control the enemy + sf::Clock enemyClock; + enemyClock.restart(); + // the window + sf::RenderWindow window(sf::VideoMode(480 , 800) , "fighters"); + window.setFramerateLimit(60); + // background music + sf::Music backgroundMusic; + backgroundMusic.openFromFile("resource/sounds/background.ogg"); + backgroundMusic.setLoop(true); + //backgroundMusic.setVolume(20); + backgroundMusic.play(); + // background + sf::Sprite backgroundSprite; + sf::Texture backgroundTexture; + backgroundTexture.loadFromFile("resource/background.png"); + backgroundSprite.setTexture(backgroundTexture); + // sky + Sky sky; + sky.add(backgroundSprite); + // loadtexture + LoadTexture loadtexture; + loadtexture.initLoad(); + loadtexture.loadFromFile("resource/shoot.png"); + // firer + Firer firer(sky , loadtexture); + // hero + Plane hero(HERO); + hero.setTexture(*loadtexture.getTextureByName("hero2")); + // set hero position + hero.setOrigin(hero.getTexture()->getSize().x / 2 , hero.getTexture()->getSize().y / 2); + hero.setPosition(window.getSize().x / 2, window.getSize().y - hero.getTexture()->getSize().y / 2); + hero.setFirer(firer); + sky.add(hero); + // the controller + Controller controller; + // the enemy_1 pointer + Plane *enemy; + // the enemy_1 vector + std::vector enemy_1TextureVector; + enemy_1TextureVector.push_back(loadtexture.getTextureByName("enemy1_down1")); + enemy_1TextureVector.push_back(loadtexture.getTextureByName("enemy1_down2")); + enemy_1TextureVector.push_back(loadtexture.getTextureByName("enemy1_down3")); + enemy_1TextureVector.push_back(loadtexture.getTextureByName("enemy1_down4")); + // the loop + while (window.isOpen()){ + sf::Event event; + while (window.pollEvent(event)){ + if (event.type == sf::Event::Closed){ + window.close(); + } + } + // random add enemy + if((enemyClock.getElapsedTime().asSeconds() > sf::seconds(1.0f).asSeconds())) + { + enemy = new Plane(ENEMY_1); + enemy->setTexture(*loadtexture.getTextureByName("enemy1")); + enemy->setOrigin(enemy->getTexture()->getSize().x / 2, enemy->getTexture()->getSize().y / 2); + enemy->setFirer(firer); + enemy->setTimePerTexture(0.05f); + int a = 480- int(hero.getTexture()->getSize().x / 2), b = int(hero.getTexture()->getSize().x / 2); + enemy->setPosition((rand()%(b-a+1)+b), 0); + enemy->setTextureVector(&enemy_1TextureVector); + sky.add(*enemy); + enemy->fire(); + enemyClock.restart(); + } + window.clear(); + window.draw(backgroundSprite); + controller.control(hero); + sky.draw(window); + window.display(); + } +} diff --git a/prime.c b/prime.c new file mode 100644 index 00000000..745357e3 --- /dev/null +++ b/prime.c @@ -0,0 +1,31 @@ +#include +int pd(int n) +{ + if(n==1||n==2) + { + return 1; + } + for(int i=2;i<=n/2;i++) + { + if(n%i==0) + { + return 0; + } + } + return 0; + } + int main() + { + printf("Please Input a Number"); + int n; + scanf("%s",&n); + if(pd(n)==0) + { + print("Not Prime"); + } + else + { + print("Prime"); + } + return 0; + }