diff --git a/practices/c/level1/p01_runningLetter/p01_running_letter.c b/practices/c/level1/p01_runningLetter/p01_running_letter.c new file mode 100644 index 00000000..6bb4c403 --- /dev/null +++ b/practices/c/level1/p01_runningLetter/p01_running_letter.c @@ -0,0 +1,29 @@ +#include +#include +#define TIMES 50 +int main(void) +{ + int i,j; + + for(i=1;i0;i--) + { + for(j=1;j +int main(void) +{ + int num; + int i,j; + + printf("输入一个正整数:"); + while((scanf("%d",&num)==1)) + { + for(i=2,j=1;(i*i) +int main(void) +{ + float years=0; + float childhood,young,single; + + while(years<200) + { + childhood=(float)years/6; + young=(float)years/7; + single=(float)years/12; + if(years==(childhood+young+single+5+4)*2) + printf("儿子死时丢番图为%d岁.\n",(int)years-4); + years++; + } + return 0; +} diff --git a/practices/c/level1/p04_ narcissus/p04_ narcissus.c b/practices/c/level1/p04_ narcissus/p04_ narcissus.c new file mode 100644 index 00000000..32b21beb --- /dev/null +++ b/practices/c/level1/p04_ narcissus/p04_ narcissus.c @@ -0,0 +1,18 @@ +#include +int main(void) +{ + int a,b,c; + int num; + + for(a=1;a<=9;a++){ + for(b=0;b<=9;b++){ + for(c=0;c<=9;c++){ + num=a*100+b*10+c; + if(num==(a*a*a+b*b*b+c*c*c)){ + printf("%d\n",num); + } + } + } + } + return 0; +} diff --git a/practices/c/level1/p05_allPrimes/p05-allPrimes.c b/practices/c/level1/p05_allPrimes/p05-allPrimes.c new file mode 100644 index 00000000..bb336d17 --- /dev/null +++ b/practices/c/level1/p05_allPrimes/p05-allPrimes.c @@ -0,0 +1,24 @@ +#include +#include +int main(void) +{ + int num=1000; + int i,j,div; + int x,y; + + x=clock(); + for(i=2;i<=num;i++) + { + for(div=2,j=1;(div*div)i) + printf("%5d",i); + } + y=clock(); + printf("总的计算时间为:%d",y-x); + + return 0; + } diff --git a/practices/c/level1/p09_maze/Maze.h b/practices/c/level1/p09_maze/Maze.h new file mode 100644 index 00000000..6df31d30 --- /dev/null +++ b/practices/c/level1/p09_maze/Maze.h @@ -0,0 +1,179 @@ +#ifndef MZAE_H +#define MAZE_H +#include +#include +#include + +/*从文件中打开地图*/ +void open(char *filename); + +/*欢迎界面*/ +void hello(void); + +/*显示地图*/ +void show_map(void); + +/*实现移动*/ +void move(void); + +/*将光标移动到当前位置*/ +void gotoxy(int x, int y); + +/*隐藏光标*/ +void HideCursor(void); + +char map[31][31] = { 0 }; + +void open(char *filename) { + FILE *fp = fopen(filename, "r"); + if (fp == NULL) { + printf("File open error!"); + return; + } + else { + for (int i = 0;i < 31;i++) { + for (int j = 0;j < 31;j++) { + fscanf(fp, "%d", &map[i][j]); + } + } + } + return; +} +void hello(void) { + system("mode con cols=30 lines=15"); + system("cls"); + printf("*** A LITTLE MAZE ***\n\n\n\n"); + printf(" 1.play\n\n"); + printf(" 2.quit\n\n\n\n\n"); + printf("*** JUST FOR FUN ***\n\n"); + HideCursor(); + char option; + option = getch(); + if (option == '1') { + move(); + } + else { + system("cls"); + gotoxy(15, 7); + printf("Bye~"); + return; + } +} + +void show_map(void) { + int i, j; + for (i = 0;i < 31;i++) { + for (j = 0;j < 31;j++) { + if (map[i][j] == 0) printf(" "); + else if (map[i][j] == 1) printf("█"); + else printf("∞"); + } + printf(" \n"); + } +} + +void move(void) { + system("mode con cols=85 lines=33"); + system("cls"); + + int count = 0; + int x = 29, y = 29; + show_map(); + + char ch; + do { + ch = getch(); + if (ch == -32) { + switch (ch = getch()) { + case 72:ch = 'w';break; + case 80:ch = 's';break; + case 75:ch = 'a';break; + case 77:ch = 'd';break; + default: + printf("Oh!Wrong button!"); + break; + } + } + else { + system("cls"); + printf("Are you sure you want to quit?\n"); + printf(" 1.yes 2.no\n"); + if (getch() == '1') { + printf("Bye~"); + return; + } + else { + system("cls"); + show_map(); + } + } + if (ch == 'w'&&map[x - 1][y] == 0) { + map[x][y] = 0; + gotoxy(2 * y, x); + printf(" "); + map[x--][y] = 1; + gotoxy(2 * y, x); + printf("∞"); + count++; + } + else if (ch == 's'&&map[x + 1][y] == 0) { + map[x][y] = 0; + gotoxy(2 * y, x); + printf(" "); + map[x++][y] = 1; + gotoxy(2 * y, x); + printf("∞"); + count++; + } + else if (ch == 'a'&&map[x][y - 1] == 0) { + map[x][y] = 0; + gotoxy(2 * y, x); + printf(" "); + map[x][y--] = 1; + gotoxy(2 * y, x); + printf("∞"); + count++; + } + else if (ch == 'd'&&map[x][y + 1] == 0) { + map[x][y] = 0; + gotoxy(2 * y, x); + printf(" "); + map[x][y++] = 1; + gotoxy(2 * y, x); + printf("∞"); + count++; + } + if (x == 29 && y == 30) { + system("mode con cols=30 lines=15"); + system("cls"); + gotoxy(13, 6); + printf("You win!"); + return; + } + } while (1); +} + +void gotoxy(int x, int y) { + HANDLE hwnd; + COORD coord; + coord.X = x; + coord.Y = y; + hwnd = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleCursorPosition(hwnd, coord); + HideCursor(); +} + +void HideCursor(void)//度娘教的 +{ + CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; + SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); +} + +void refresh(int x, int y) { + for (;y < 31;y++) { + if (map[x][y] == 0)printf(" "); + else if (map[x][y] == 1)printf("%c", 219); + else printf("%c", 236); + } +} +#endif \ No newline at end of file diff --git a/practices/c/level1/p09_maze/main.c b/practices/c/level1/p09_maze/main.c new file mode 100644 index 00000000..e9cc0261 --- /dev/null +++ b/practices/c/level1/p09_maze/main.c @@ -0,0 +1,19 @@ +#include +/*todolist +1.显示游戏主界面 +2.显示地图 +3.显示玩家位置 +4.控制玩家移动 +上 +下 +左 +右 +5.更新地图 +6.退出:是或否 +6.显示玩家赢得游戏 +*/ +#include "maze.h" +int main(void) { + open("mazemap.txt"); + hello(); +} diff --git a/practices/c/level1/p09_maze/mazemap.txt b/practices/c/level1/p09_maze/mazemap.txt new file mode 100644 index 00000000..5b6b5d8f --- /dev/null +++ b/practices/c/level1/p09_maze/mazemap.txt @@ -0,0 +1,31 @@ +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 1 1 1 1 1 1 +1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 +1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 +1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 +1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 +1 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 +1 0 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 +1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 +1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 +1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 +1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 1 1 +1 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 +1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 1 1 0 1 +1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0 1 0 1 +1 0 1 0 1 1 1 1 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 +1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 +1 0 1 1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 0 1 1 1 0 1 +1 0 1 0 0 0 1 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 1 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 +1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 +1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 0 1 +1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 +1 0 1 1 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 0 1 +1 0 0 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 1 +1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 +1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 +1 0 1 1 1 0 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 +1 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 1 +1 0 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 +1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 2 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 1 1 1 1 1 1 diff --git a/practices/c/level1/p11_linkedList/List.c b/practices/c/level1/p11_linkedList/List.c new file mode 100644 index 00000000..51abd65a --- /dev/null +++ b/practices/c/level1/p11_linkedList/List.c @@ -0,0 +1,85 @@ +#include +#include "List.h" +static void CopyToNode(Item item, Node * pnode); + +void InitializeList(List * plist) { + *plist = NULL; +} + +bool ListIsEmpty(const List * plist) { + if (*plist == NULL) + return true; + else + return false; +} + +bool ListIsFull(const List * plist) { + Node * pt; + bool full; + + pt = (Node *)malloc(sizeof(Node)); + if (pt == NULL) + full = true; + else + full = false; + free(pt); + + return full; +} + +unsigned int ListItemCount(const List * plist) { + unsigned int count = 0; + Node * pnode; + pnode= *plist; + + while (pnode != NULL) { + ++count; + pnode = pnode->next; + } + + return count; +} + +bool AddItem(Item item, List *plist) { + Node * pnew; + Node * scan = *plist; + + pnew = (Node *)malloc(sizeof(Node)); + if (pnew == NULL) + return false; + + CopyToNode(item, pnew); + pnew->next = NULL; + if (scan == NULL) + *plist = pnew; + else { + while (scan->next != NULL) + scan = scan->next; + scan->next = pnew; + } + + return true; +} + +void Traverse(const List * plist,void(*pfun)(Item item)) { + Node *pnode = *plist; + + while (pnode != NULL) { + (*pfun)(pnode->item); + pnode = pnode->next; + } +} + +void EmptyTheList(List * plist) { + Node *psave; + + while (*plist != NULL) { + psave = (*plist)->next; + free(*plist); + *plist = psave; + } +} + +static void CopyToNode(Item item, Node * pnode) { + pnode->item = item; +} \ No newline at end of file diff --git a/practices/c/level1/p11_linkedList/README.md b/practices/c/level1/p11_linkedList/README.md index ed974fdd..00440371 100755 --- a/practices/c/level1/p11_linkedList/README.md +++ b/practices/c/level1/p11_linkedList/README.md @@ -6,4 +6,5 @@ 2. 閬嶅巻璇ラ摼琛紝渚濇鐜板疄鍚勮妭鐐圭殑value锛 3. 灏嗚閾捐〃鎵鏈夎妭鐐瑰弽搴忥紱 4. 鍦ㄨ閾捐〃涓煡鎵剧涓涓间负5鐨勮妭鐐癸紝濡傛灉鎵惧埌鍒欒繑鍥炶鑺傜偣鐨勫簭鍙凤紝鍚﹀垯杩斿洖锛1锛 -5. 鏌ユ壘涓嬩竴涓间负5鐨勮妭鐐癸紝杩斿洖鍊煎悓涓婏紱 \ No newline at end of file +5. 鏌ユ壘涓嬩竴涓间负5鐨勮妭鐐癸紝杩斿洖鍊煎悓涓婏紱 + diff --git a/practices/c/level1/p11_linkedList/list.h b/practices/c/level1/p11_linkedList/list.h new file mode 100644 index 00000000..8b4eb995 --- /dev/null +++ b/practices/c/level1/p11_linkedList/list.h @@ -0,0 +1,31 @@ +#ifndef LIST_H_ +#define LIST_H_ +#include + +typedef struct data { + int id; + int value; +}Item; + +typedef struct node { + Item item; + struct node * next; +}Node; + +typedef Node * List; + +void InitializeList(List * plist); + +bool ListIsEmpty(const List * plist); + +bool ListIsFull(const List * plist); + +unsigned int ListItemCount(const List * plist); + +bool AddItem(Item item, List * plist); + +void Traverse(const List * plist, void(*pfun)(Item item)); + +void EmptyTheList(List * plist); + +#endif \ No newline at end of file diff --git a/practices/c/level1/p11_linkedList/main.c b/practices/c/level1/p11_linkedList/main.c new file mode 100644 index 00000000..2bf32b21 --- /dev/null +++ b/practices/c/level1/p11_linkedList/main.c @@ -0,0 +1,97 @@ +#include +#include "List.h" +/*todolist +1. 在main函数中创建一个单向链表; +2. 遍历该链表,依次现实各节点的value; +3. 将该链表所有节点反序; +4. 在该链表中查找第一个值为5的节点,如果找到则返回该节点的序号,否则返回-1; +5. 查找下一个值为5的节点,返回值同上; +*/ +void showdata(Item item); +Node *ReverseOrder(List *plist);//将链表所有节点反序 +int FindValue(List * plist);//查找值 +Node *head; +int main() { + List data; + Item temp; + int i = 1; + + InitializeList(&data); + if (ListIsFull(&data)) { + printf("No memory available!\n"); + return 0; + } + puts("Enter the first digit:"); + while (scanf("%d", &temp.value) == 1) { + temp.id = i++; + if (AddItem(temp, &data) == false) { + printf("Problem allocating memory!\n"); + break; + } + if (ListIsFull(&data)) { + puts("The list is now full.\n"); + break; + } + puts("Enter the next value(empty 'q' to stop)"); + } + + Traverse(&data, showdata); + printf("\n"); + + Node *rehead = ReverseOrder(&data); + Traverse(&rehead, showdata); + + int x = FindValue(&rehead); + printf("%d\n", x); + + int y = FindValue(&head); + printf("%d", y); + + +} + +void showdata(Item item) { + printf("%d %d\n", item.id, item.value); +} + +Node *ReverseOrder(List *plist) { + Node * pre = *plist; + Node * current = pre->next; + Node * next = NULL; + + if (current) { + next = current->next; + (*plist)->next = NULL; + } + else + return *plist; + while (1) { + current->next = pre; + if (next) { + pre = current; + current = next; + next = next->next; + } + else + return current; + } +} + +int FindValue(List * plist) { + Node * pnode = *plist; + int k = 0; + + while (pnode != NULL) { + if (pnode->item.value == 5) { + head = pnode->next; + k = 1; + return (pnode->item).id; + } + pnode = pnode->next; + } + if (k == 0) + return -1; +} + + + diff --git a/practices/cpp/level1/p01_Queue/Queue.cpp b/practices/cpp/level1/p01_Queue/Queue.cpp new file mode 100644 index 00000000..f4ca341f --- /dev/null +++ b/practices/cpp/level1/p01_Queue/Queue.cpp @@ -0,0 +1,33 @@ +#include "Queue.h" +#include +using namespace std; +void Queue::disp(void) { + cout << "The queue is as follows:" << endl; + for (int i = head;i < tail;++i) { + cout << data[i] << endl; + } +} +void Queue::append(int num){ + data[tail] = num; + tail = (tail + 1) % 100; +} + +int Queue::pop(void) { + int temp = data[head]; + head = (head + 1) % 100; + return temp; +} + +bool Queue::isFull(void) { + if (head == (tail + 1) % 100) + return true; + else + return false; +} + +bool Queue::isEmpty(void) { + if (head == tail) + return true; + else + return false; +} \ No newline at end of file diff --git a/practices/cpp/level1/p01_Queue/Queue.h b/practices/cpp/level1/p01_Queue/Queue.h new file mode 100644 index 00000000..ed7f35cf --- /dev/null +++ b/practices/cpp/level1/p01_Queue/Queue.h @@ -0,0 +1,21 @@ +#ifndef QUEUE_H +#define QUEUE_H +class Queue { +public: + Queue() { + for (int i = 0;i < 100;++i) { + data[i] = 0; + } + } + void disp(void); + void append(int num); + int pop(void); + bool isEmpty(void); + bool isFull(void); +private: + int data[100]; + int head = 0; + int tail = 0; +protected: +}; +#endif diff --git a/practices/cpp/level1/p01_Queue/main.cpp b/practices/cpp/level1/p01_Queue/main.cpp new file mode 100644 index 00000000..a9113602 --- /dev/null +++ b/practices/cpp/level1/p01_Queue/main.cpp @@ -0,0 +1,65 @@ +/*todolist +1.显示队列 +2.入队 +3.出队 +4.判断队列是否已满 +*/ +#include "Queue.h" +#include +#include +using std::cin; +using std::cout; +using std::endl; +int main() { + Queue queue; + + cout << "Please enter the number to choose what you want to do(enter 'q' to exit)" << endl; + cout << "1.show the queue\n2.append\n3.pop\n4.isFull?" << endl; + + while (1) { + int option; + if (scanf("%d", &option)) { + switch (option) { + case 1: + if (!queue.isEmpty()) { + queue.disp(); + } + else { + cout << "The queue is empty!" << endl; + Sleep(1000); + } + break; + case 2: + if (!queue.isFull()) { + int num; + cout << "Enter the number to append:" << endl; + cin >> num; + queue.append(num); + } + break; + case 3: + if (!queue.isEmpty()) { + queue.pop(); + } + else { + cout << "The queue is empty!" << endl; + } + break; + case 4: + if (queue.isFull()) { + cout << "The queue is full!" << endl; + } + else { + cout << "The queue isn't full!" << endl; + } + break; + default: + cout << "Please enter the correct number 1~4 or 'q' to exit." << endl; + break; + } + } + else + return 0; + cout << "Please enter the option continue(enter 'q' to exit):" << endl; + } +} diff --git a/practices/cpp/level1/p02_Stack/Stack.cpp b/practices/cpp/level1/p02_Stack/Stack.cpp new file mode 100644 index 00000000..60ad7827 --- /dev/null +++ b/practices/cpp/level1/p02_Stack/Stack.cpp @@ -0,0 +1,18 @@ +#include "Stack.h" +bool Stack::isFull() { + return top == 99 ? true : false; +} + +void Stack::push(int a) { + top++; + data[top] = a; +} + +int Stack::pop(void) { + top--; + return data[top + 1]; +} + +bool Stack::isEmpty(void) { + return top == 0 ? true : false; +} \ No newline at end of file diff --git a/practices/cpp/level1/p02_Stack/Stack.h b/practices/cpp/level1/p02_Stack/Stack.h new file mode 100644 index 00000000..4fd20e27 --- /dev/null +++ b/practices/cpp/level1/p02_Stack/Stack.h @@ -0,0 +1,16 @@ +#ifndef STACK_H +#define STACK_H +#include +class Stack +{ +public: + bool isFull(); + void push(int data); + int pop(); + bool isEmpty(); + int data[100]; + int top = 0; +protected: +private: +}; +#endif \ No newline at end of file diff --git a/practices/cpp/level1/p02_Stack/main.cpp b/practices/cpp/level1/p02_Stack/main.cpp new file mode 100644 index 00000000..47b1a316 --- /dev/null +++ b/practices/cpp/level1/p02_Stack/main.cpp @@ -0,0 +1,47 @@ +//逆波兰算法 +#include +#include +#include "Stack.h" +using std::cin; +using std::cout; +using std::endl; +int main() { + int a, b; + Stack stack; + char s[100]; + bool full; + + while(scanf("%s",s)) + { + full = stack.isFull(); + if (full == true) { + cout << "栈已满!" << endl; + break; + } + + if (s[0] == '+') { + a = stack.pop(); + b = stack.pop(); + stack.push(a + b); + } + else if (s[0] == '-') { + b = stack.pop(); + a = stack.pop(); + stack.push(a - b); + } + else if (s[0] == '*') { + a = stack.pop(); + b = stack.pop(); + stack.push(a*b); + } + else { + stack.push(atoi(s)); + } + } + full = stack.isEmpty(); + if (full == true) { + cout << "栈为空" << endl; + } + cout << stack.pop() << endl; + system("pause"); +} diff --git a/practices/cpp/level1/p04_cppScoreManagement/Score.cpp b/practices/cpp/level1/p04_cppScoreManagement/Score.cpp new file mode 100644 index 00000000..00f8e653 --- /dev/null +++ b/practices/cpp/level1/p04_cppScoreManagement/Score.cpp @@ -0,0 +1,69 @@ +#include "Score.h" +#include +#include +using namespace std; +Score::Score(string subject) { + Subject = subject; + for (int i = 0;i < 40;++i) { + student[i].name = ""; + student[i].id = ""; + student[i].score = 0; + } +} +void Score::show() { + cout << "Subject:" << Subject << endl; + for (int i = 0;i < 40;++i) { + if ("" == student[i].name) continue; + cout << "Name:" << student[i].name << endl; + cout << "ID:" << student[i].id << endl; + cout << "Score:" << student[i].score << endl; + } + system("pause"); +} +void Score::add() { + int is = -1; + for (int i = 0;i < 40;++i) { + if ("" == student[i].name) { + is = i; + break; + } + } + if (-1 == is) { + cout << "FULL!" << endl; + system("pause"); + } + else { + cout << " Please enter a student's name:" << endl; + cin.get(); + getline(cin, student[is].name); + cout << " Please enter a student's ID:" << endl; + getline(cin, student[is].id); + } + +} +void Score::out() { + string in; + int find = 0; + cout << "Please enter the student's name to delete:" << endl; + cin >> in; + for (int i = 0;i < 40;++i) { + if (student[i].name == in) { + student[i].name = ""; + student[i].id = ""; + student[i].score = 0; + find = 1; + } + } + if (0 == find) { + cout << "No find the student!Please enter the correct name:" << endl; + } + system("pause"); +} +void Score::inputScore() { + for (int i = 0;i < 40;++i) { + if (student[i].name != "") { + cout << "Please enter the score of " << student[i].name << ":" << endl; + cin >> student[i].score; + } + } +} \ No newline at end of file diff --git a/practices/cpp/level1/p04_cppScoreManagement/Score.h b/practices/cpp/level1/p04_cppScoreManagement/Score.h new file mode 100644 index 00000000..a82a69ba --- /dev/null +++ b/practices/cpp/level1/p04_cppScoreManagement/Score.h @@ -0,0 +1,22 @@ +#ifndef SCORE_H +#define SCORE_H +#include +using std::string; +typedef struct student { + string name; + string id; + int score; +}stu; +class Score { +private: + string Subject; + stu student[40]; +public: + Score(string subject); + void show(); + void add(); + void out(); + void inputScore(); +protected: +}; +#endif \ No newline at end of file diff --git a/practices/cpp/level1/p04_cppScoreManagement/main.cpp b/practices/cpp/level1/p04_cppScoreManagement/main.cpp new file mode 100644 index 00000000..274502f3 --- /dev/null +++ b/practices/cpp/level1/p04_cppScoreManagement/main.cpp @@ -0,0 +1,73 @@ +/*todolist +1.show +2.add +3.delete +4.input the score +5.exit +*/ +#include +#include +#include +#include "Score.h" +using namespace std; +void showlist(void); +int main(void) { + string s1 = "chinese", s2 = "math", s3 = "English"; + Score menu[3] = { Score(s1),Score(s2),Score(s3) }; + showlist(); + int option; + while (1) { + + system("cls"); + showlist(); + cin >> option; + switch (option) { + case 1: + menu[0].show(); + break; + case 2: + menu[1].show(); + break; + case 3: + menu[2].show(); + break; + case 4: + menu[0].add(); + break; + case 5: + menu[1].add(); + break; + case 6: + menu[2].add(); + break; + case 7: + menu[0].out(); + break; + case 8: + menu[1].out(); + break; + case 9: + menu[2].out(); + break; + case 10: + menu[0].inputScore(); + break; + case 11: + menu[1].inputScore(); + break; + case 12: + menu[2].inputScore(); + break; + default: + cout << "exit" << endl; + break; + } + } +} +void showlist(void) { + cout << "Please enter the number to achieve the following functions:(enter 'q' to quit)" << endl; + cout << "Show the namelist:" << "1.chinese 2.math 3.English" << endl; + cout << "Add the student to:" << "4.chinese 5.math 6.English" << endl; + cout << "Delete the student of:" << "7.chinese 8.math 9.English" << endl; + cout << "Input the student's student of:" << "10.chinese 11.math 12.English" << endl; +} \ No newline at end of file diff --git a/practices/cpp/level1/p06_CircleAndPoint/Circle.cpp b/practices/cpp/level1/p06_CircleAndPoint/Circle.cpp new file mode 100644 index 00000000..0f3a94ad --- /dev/null +++ b/practices/cpp/level1/p06_CircleAndPoint/Circle.cpp @@ -0,0 +1,29 @@ +#include "Circle.h" +#include "Point.h" +using namespace std; + +Circle::Circle(Point p, double R){ + center = p; + radius = R; +} + +void Circle::resize(int a) { + radius += a; +} + +double Circle::area() { + return PI*radius*radius; +} + +void Circle::showCircle(void) { + cout << "Center of Circle:" << endl; + center.show(); + cout << "Radius:" << radius << endl; + cout.setf(ios_base::fixed, ios_base::floatfield); + cout.precision(2); + cout << "area:" << area()<< endl; +} + +void Circle::move(int OFFX, int OFFY) { + center.move(OFFX, OFFY); +} \ No newline at end of file diff --git a/practices/cpp/level1/p06_CircleAndPoint/Circle.h b/practices/cpp/level1/p06_CircleAndPoint/Circle.h new file mode 100644 index 00000000..fe89034a --- /dev/null +++ b/practices/cpp/level1/p06_CircleAndPoint/Circle.h @@ -0,0 +1,17 @@ +#ifndef CIRCLE_H +#define CIRCLE_H +#include "Point.h" +#define PI 3.14159 +class Circle { +private: + Point center; + double radius; +public: + Circle(Point p, double R); + void resize(int a); + double area(); + void Circle::showCircle(void); + void move(int OFFX, int OFFY); +protected: +}; +#endif \ No newline at end of file diff --git a/practices/cpp/level1/p06_CircleAndPoint/Point.cpp b/practices/cpp/level1/p06_CircleAndPoint/Point.cpp new file mode 100644 index 00000000..571c9086 --- /dev/null +++ b/practices/cpp/level1/p06_CircleAndPoint/Point.cpp @@ -0,0 +1,15 @@ +#include "Point.h" +using namespace std; + +Point::Point(int X = 0, int Y = 0) { + this->X = X, this->Y = Y; +} + +void Point::move(int OffX, int OffY) { + X += OffX; + Y += OffY; +} + +void Point::show(void) { + cout << "(" << X << "," << Y << ")" << endl; +} diff --git a/practices/cpp/level1/p06_CircleAndPoint/Point.h b/practices/cpp/level1/p06_CircleAndPoint/Point.h new file mode 100644 index 00000000..d18e87a8 --- /dev/null +++ b/practices/cpp/level1/p06_CircleAndPoint/Point.h @@ -0,0 +1,14 @@ +#ifndef POINT_H +#define POINT_H +#include +using namespace std; +class Point { +private: + int X,Y; +public: + Point(int X=0, int Y=0); + void move(int OffX,int OffY); + void show(void); +protected: +}; +#endif \ No newline at end of file diff --git a/practices/cpp/level1/p06_CircleAndPoint/main.cpp b/practices/cpp/level1/p06_CircleAndPoint/main.cpp new file mode 100644 index 00000000..6b74cca3 --- /dev/null +++ b/practices/cpp/level1/p06_CircleAndPoint/main.cpp @@ -0,0 +1,22 @@ +/* +todolist +1.定义圆心 +2.定义圆的半径 +3.移动圆心 +4.改变圆的半径 +5.显示圆 +*/ +#include "Point.h" +#include "Circle.h" +#include +using namespace std; +int main(int argc,char **argv) { + Point p(0, 0); + Circle c1(p, 5); + c1.showCircle(); + c1.move(-10, 20); + c1.showCircle(); + c1.resize(300); + c1.showCircle(); + return 0; +} \ No newline at end of file