diff --git a/level1/p01_runningLetter/runningLetter.c b/level1/p01_runningLetter/runningLetter.c new file mode 100644 index 00000000..d9d8b178 --- /dev/null +++ b/level1/p01_runningLetter/runningLetter.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +//constants +enum{ N = 1000 , SLEEP_TIME = 50}; +char space[N]; +int i; +//func +int GetWidth(); +int abs(int x); +//entertainment +void DrawLine(int width); +void ChangeColor(int cnt); + +int main(int argc, char const *argv[]) +{ + printf("Please enter several letters within a line:\n"); + //Init + char str[N]; scanf("%s",str); + int len = strlen(str); + int width = GetWidth(); + int pos; + //Play + int cnt = 0; + while (1) { + /*calculate pos*/ + pos = width - abs(width - ( cnt++ % (2 * width) ) ); + /*prevent crossing the border*/ + if (pos + len > width) continue; + + ChangeColor(cnt); + system("cls"); + DrawLine(width); + /*Output*/ + for (i = 0; i < width; ++ i) + space[i] = i < pos || i >= pos + len ? ' ' : str[i - pos]; + space[i] = '\0'; + printf("%s",space); + + DrawLine(width); + /*Sleep a while, let the user watch.*/ + Sleep(SLEEP_TIME); + } + return 0; +} +int GetWidth() +{ + CONSOLE_SCREEN_BUFFER_INFO scr; + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); + GetConsoleScreenBufferInfo(hOut, &scr); + return scr.dwMaximumWindowSize.X; +} +int abs(int x) +{ + return x >= 0 ? x : -x; +} +void DrawLine(int width) +{ + for (i = 0; i < width; ++ i) space[i] = '*'; space[i] = '\0'; + printf("%s\n",space); +} +void ChangeColor(int cnt) { + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),cnt % 16); + // SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),(cnt % 16) * 16 + 16 - (cnt % 16)); +} \ No newline at end of file diff --git a/level1/p02_isPrime/isPrime.c b/level1/p02_isPrime/isPrime.c new file mode 100644 index 00000000..2bd5a726 --- /dev/null +++ b/level1/p02_isPrime/isPrime.c @@ -0,0 +1,17 @@ +#include +int main(){ + int n,i; + scanf("%d",&n); + if (n <= 1) { + printf("error"); + return 0; + } + for (i = 2; i <= n / i; ++ i) { + if (n % i == 0) { + printf("Not prime."); + return 0; + } + } + printf("Is prime."); + return 0; +} \ No newline at end of file diff --git a/level1/p03_Diophantus/Diophantus.c b/level1/p03_Diophantus/Diophantus.c new file mode 100644 index 00000000..3d1aeaae --- /dev/null +++ b/level1/p03_Diophantus/Diophantus.c @@ -0,0 +1,12 @@ +#include +int main(){ + double t; + for (t = 5; ; ++ t) { + if (t / 6 + t / 12 + t / 7 + 5 + t / 2 + 4 == t) { + printf("%g\n",t - 4); + break; + } + } + system("pause"); + return 0; +} \ No newline at end of file diff --git a/level1/p04_ narcissus/narcissus.c b/level1/p04_ narcissus/narcissus.c new file mode 100644 index 00000000..ace53d27 --- /dev/null +++ b/level1/p04_ narcissus/narcissus.c @@ -0,0 +1,17 @@ +#include +int is_narcissus(int x){ + int a = x / 100; + int c = x % 10; + int b = (x - a * 100 - c) / 10; + if (x == a * a * a + b * b * b + c * c * c) + return 1; + else return 0; +} +int main(){ + int i; + for (i = 100; i <= 999; ++ i) { + if (is_narcissus(i)) printf("%d\n",i); + } + system("pause"); + return 0; +} \ No newline at end of file diff --git a/level1/p05_allPrimes/allPrimes.c b/level1/p05_allPrimes/allPrimes.c new file mode 100644 index 00000000..295ccc85 --- /dev/null +++ b/level1/p05_allPrimes/allPrimes.c @@ -0,0 +1,32 @@ +#include +#include +enum {N = 1001}; +int primes[N], cnt; +int sieved[N]; + +void get_primes(int n); +int main(int argc, char const *argv[]) +{ + clock_t timer = clock(); + get_primes(1000); + int i; + for (i = 0; i < cnt; ++ i) { + printf("%d\t",primes[i]); + } + printf("\nTime:%dms\n",clock() - timer); + system("pause"); + return 0; +} +void get_primes(int n) +{ + int i,j; + for (i = 2; i <= n; i ++ ) + { + if (!sieved[i]) primes[cnt ++ ] = i; + for (j = 0; primes[j] <= n / i; j ++ ) + { + sieved[primes[j] * i] = 1; + if (i % primes[j] == 0) break; + } + } +} \ No newline at end of file diff --git a/level1/p06_Goldbach/Goldback.c b/level1/p06_Goldbach/Goldback.c new file mode 100644 index 00000000..e92d2c4c --- /dev/null +++ b/level1/p06_Goldbach/Goldback.c @@ -0,0 +1,33 @@ +#include +enum {N = 100}; +int sieved[N + 1]; +int primes[N + 1],cnt; +int i,j,k,l; +void init_primes(); +int main(){ + init_primes(); + for (i = 6; i <= N; ++ i){ + for (j = 0; j < cnt; ++ j) + for (k = 0; k < cnt; ++ k) + for (l = 0; l < cnt; ++ l) + if (primes[j] + primes[k] + primes[l] == i) { + printf("%d == %d + %d + %d\n",i,primes[j],primes[k],primes[l]); + goto flag; + } + printf("It is wrong within 100!"); + flag: + ; + } + printf("It is verified that Goldbach conjecture is right within 100.\n"); + system("pause"); + return 0; +} +void init_primes(){ + for (i = 2; i <= N; ++ i) { + if (!sieved[i]) { + primes[cnt ++] = i; + for (j = i + i; j <= N; j += i) + sieved[j] = 1; + } + } +} \ No newline at end of file diff --git a/level1/p07_encrypt_decrypt/encrypt_decrypt.c b/level1/p07_encrypt_decrypt/encrypt_decrypt.c new file mode 100644 index 00000000..f56a281b --- /dev/null +++ b/level1/p07_encrypt_decrypt/encrypt_decrypt.c @@ -0,0 +1,74 @@ +#include +#include +char book[] = "18381048095"; +typedef struct string { + char * str; + int length; + int capacity; +} string; +void get_str(string * str); +void init_str(string * str); +void update(string * str); +void encrypt(string * str); +void decrypt(string * str); +void showStr(string * str); +int main(int argc, char const *argv[]) +{ + printf("Please enter the letters:\n"); + string * str = (string *)malloc(sizeof(string)); + get_str(str); + char ch; + printf("Enter 'e' to encrypt it, 'd' to decrypt it and 'q' to quit.\n"); + while (ch = getchar()) { + if (ch == 'q') break; + else if (ch == 'e') encrypt(str); + else if (ch == 'd') decrypt(str); + else if (ch == '\n') continue; + else { + printf("Invalid input!\n"); + continue; + } + showStr(str); + } + return 0; +} +void showStr(string * str) { + int i; + for (i = 0; i < str->length; ++ i){ + putchar(str->str[i]); + } + putchar('\n'); +} +void encrypt(string * str) { + int i; + for (i = 0; i < str -> length; ++ i) str -> str[i] += book[i % 11] - '0'; +} +void decrypt(string * str) { + int i; + for (i = 0; i < str -> length; ++ i) str -> str[i] -= book[i % 11] - '0'; +} +void get_str(string * str) { + init_str(str); + char ch; + while (ch = getchar()) { + if (ch == '\n') break; + update(str); + str->str[str->length++] = ch; + } +} +void init_str(string * str) { + str -> length = 0; + str -> capacity = 10; + str -> str = (char*)malloc(sizeof(char) * str -> capacity); +} +void update(string * str) { + if (str -> length > str -> capacity - 3) { + str -> capacity += str -> capacity / 2; + + char * p = (char*)malloc(sizeof(char) * str -> capacity); + int i; + for (i = 0; i < str -> length; ++ i) + p[i] = str -> str[i]; + str -> str = p; + } +} \ No newline at end of file diff --git a/level1/p08_hanoi/hanoi.c b/level1/p08_hanoi/hanoi.c new file mode 100644 index 00000000..490e0bcb --- /dev/null +++ b/level1/p08_hanoi/hanoi.c @@ -0,0 +1,15 @@ +#include +void hanoi(int n,char origin,char medium,char dest) { + if (n == 1) { + printf("%c -> %c\n", origin, dest); + return; + } + hanoi(n-1,origin,dest,medium); + printf("%c -> %c\n", origin, dest); + hanoi(n-1,medium,origin,dest); +} +int main() { + int n = 3; + hanoi(n,'A','B','C'); + return 0; +} \ No newline at end of file diff --git a/level1/p09_maze/maze.c b/level1/p09_maze/maze.c new file mode 100644 index 00000000..7aa09a83 --- /dev/null +++ b/level1/p09_maze/maze.c @@ -0,0 +1,159 @@ +#include +#include +#include +#include +#include +#define CHECK(r,c) r >= 0 && r < Height && c >= 0 && c < Width +int c_r, c_c, ch, i, j,quitflag; +enum {Width = 50, Height = 25}; +char out[(Width+1) * (Height+1)]; +int dir[4][2] = { + {0,1}, + {0,-1}, + {1,0}, + {-1,0} +}; +int ** map = NULL; +int * set = NULL; +void init(); +void victory(); +void showMap(int ** map); +void dig(); +int find(int x); +void interact(); +int main() { + while (1) { + init(); + dig(); + system("cls"); + showMap(map); + printf("Use WASD or arrows to move to '$'.\n"); + while (c_r != Height - 1 || c_c != Width - 1) { + interact(); + } + victory(); + if (quitflag) { + break; + } + } + printf("\nThanks for playing."); + return 0; +} +void init() { + srand((unsigned)time(0)); + c_r = c_c = 0; + if (!set) { + set = (int *)malloc(Height * Width * sizeof(int)); + } + for (i = 0; i < Height * Width; ++ i) { + set[i] = i; + } + if (!map) { + map = (int **)malloc(Height * sizeof(int *)); + } + + for (i = 0; i < Height; ++ i) { + map[i] = (int *)malloc(Width * sizeof(int)); + } + for (i = 0; i < Height; ++ i) + for (j = 0; j < Width; ++ j) + map[i][j] = 1; +} +void dig() { + while (1) { + int p1 = find(0), p2 = find(Height * Width - 1); + if (p1 == p2) { + break; + } + int r = rand() % Height, c = rand() % Width; + int cnt = 0; + for (i = 0; i < 4; ++ i) { + int nr = r + dir[i][0], nc = c + dir[i][1]; + if (CHECK(nr,nc) && !map[nr][nc]) { + cnt ++; + } + } + if (cnt <= 3 && map[r][c]) { + map[r][c] = 0; + int i; + for (i = 0; i < 4; ++ i) { + int nr = r + dir[i][0], nc = c + dir[i][1]; + if (CHECK(nr,nc) && !map[nr][nc]) { + int p_1 = find(r * Width + c); + int p_2 = find(nr * Width + nc); + set[p_2] = p_1; + } + } + } + } +} +void showMap(int ** map) { + int cnt = 0; + int i,j; + for (i = 0; i < Height; ++ i) { + + for (j = 0; j < Width; ++ j) + { + if (i == c_r && j == c_c) { + out[cnt++] = '*'; + } + else if (i == Height - 1 && j == Width - 1) { + out[cnt++] = '$'; + } + else if (map[i][j]) { + out[cnt++] = 'O'; + } + else { + out[cnt++] = ' '; + } + } + out[cnt++] = '\n'; + } + out[cnt] = '\0'; + printf("%s",out); +} +int find(int x) { + if (set[x] != x) set[x] = find(set[x]); + return set[x]; +} +void interact() { + if (_kbhit()) { + ch = _getch(); + int n_r = c_r, n_c = c_c; + if (ch == 75 || ch == 97) { + n_c -= 1; + } + if (ch == 72 || ch == 119) { + n_r -= 1; + } + if (ch == 77 || ch == 100) { + n_c += 1; + } + if (ch == 80 || ch == 115) { + n_r += 1; + } + if (CHECK(n_r,n_c) && !map[n_r][n_c]) { + c_r = n_r, c_c = n_c; + } + system("cls"); + showMap(map); + } +} +void victory() { + quitflag = 0; + system("cls"); + printf("You win!\nEnter q to quit and r to restart."); + while (1) { + if (_kbhit()) { + ch = _getch(); + if (ch == 113) { + quitflag = 1; + break; + } + else if (ch == 114) + break; + system("cls"); + printf("You win!\nEnter q to quit and r to restart."); + } + } +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/maps.txt b/level1/p10_pushBoxes/maps.txt new file mode 100644 index 00000000..c63faa44 --- /dev/null +++ b/level1/p10_pushBoxes/maps.txt @@ -0,0 +1,52 @@ +OOOOOOOOOOOOOOO +O O $ O O +O O O +O O OO +OO O###O O +O$$O$# * #$O$$O +O O###O OO +OO O O O +O O O +O O $ O O +OOOOOOOOOOOOOOO +& +OOOOOOO +O $ O +O # O +O$#*#$O +O # O +O $ O +OOOOOOO +& +OOOOOOOOO +O $O$ O +O O +O O# O +O #*# O +O O#O O +O O +O $O$ O +OOOOOOOOO +& +OOOOOOOOOOO +O $ # $ O +O O # O +O O#O O +O$O * O$O +O O#O O +O # O O +O $ # $ O +OOOOOOOOOOO +& +OOOOOOOOOOOOO +O$ O $O +O O O O O O O +O O +O ### O +O$O$O#*#O$O$O +O ### O +O O +O O O O O +O$ O $O +OOOOOOOOOOOOO +& \ No newline at end of file diff --git a/level1/p10_pushBoxes/pushBoxes.c b/level1/p10_pushBoxes/pushBoxes.c new file mode 100644 index 00000000..2a481eaa --- /dev/null +++ b/level1/p10_pushBoxes/pushBoxes.c @@ -0,0 +1,232 @@ +#include +#include +#include +#include +#define CHECK +enum {MapNum = 10,MaxNum = 100, WinNum = 4}; +enum {Space, Wall, Box, Dest, BoxOverDest, P, LineBreak, BoxInDest, PInDest}; +int *** maps = NULL; +char ch; +int Steps = 0, quitflag = 0; +int i, j; +int cur_map = 0; +struct point +{ + int row; + int col; +}player, mapInfo[MapNum]; +void Move(int n_r,int n_c); +int NotOut(int rr, int cc); +int char2int(char c); +char int2char(int t); +void Update(); +void GetPlayerPosition(); +void initMaps(); +void showMap(); +void restart(); +void interact(); +int victory(); +int main(int argc, char const *argv[]) +{ + FILE * score_file = fopen("score.txt","w"); + system("cls"); + initMaps(); + GetPlayerPosition(); + Update(); + while(1) { + interact(); + if (quitflag) break; + if (victory()) { + fprintf(score_file,"The level %d takes totally %d steps, the score is %d\n",cur_map,Steps,(1000 + 10000 * cur_map) / Steps); + Steps = 0; + cur_map ++; + if (cur_map == WinNum) { + printf("You win!"); + break; + } + system("cls"); + GetPlayerPosition(); + Update(); + } + } + fclose(score_file); + return 0; +} + +void initMaps() +{ + maps = (int ***)malloc(sizeof(int **)*MapNum); + for (i = 0; i < MapNum; ++ i) { + maps[i] = (int**)malloc(sizeof(int*)*MaxNum); + for (j = 0; j < MaxNum; ++ j) { + maps[i][j] = (int*)malloc(sizeof(int)*MaxNum); + } + } + + FILE * maps_file = fopen("maps.txt","r"); + int mapcnt = 0,rowcnt = 0, colcnt = 0; + while (~(ch = fgetc(maps_file))) { + if (ch == '&'){ + mapInfo[mapcnt].row = rowcnt; + mapcnt++; + rowcnt = 0; + colcnt = 0; + fgetc(maps_file); + continue; + } + else if (ch == '\n') { + mapInfo[mapcnt].col = colcnt; + maps[mapcnt][rowcnt][colcnt++] = char2int(ch); + rowcnt ++; + colcnt = 0; + continue; + } + maps[mapcnt][rowcnt][colcnt++] =char2int(ch); + }; + fclose(maps_file); +} +void GetPlayerPosition() +{ + for (i = 0; i < mapInfo[cur_map].row; ++ i) + for (j = 0; j < mapInfo[cur_map].col; ++ j) { + if (maps[cur_map][i][j] == P) player.row = i, player.col = j; + } +} +void Update() +{ + char show[1000]; + int cnt = 0; + for (i = 0; i < mapInfo[cur_map].row; ++ i) + for (j = 0; j < mapInfo[cur_map].col + 1; ++ j) { + show[cnt++] = int2char(maps[cur_map][i][j]); + } + show[cnt] = '\0'; + printf("%s",show); + printf("Please move the '#'(box) to the '$'(destination), note that you can move the box in the '$' and you can alse walk on the '$'"); + printf("\nYou can use WASD to move.\nYou can hit R to restart or Q to quit.\n"); + printf("\nNow you have walk %d steps.\n",Steps); +} +void interact() { + if (_kbhit()) { + + ch = _getch(); + int n_r = player.row, n_c = player.col; + if (ch == 114) { + restart(); + return; + } + else if (ch == 113) { + Steps ++; + quitflag = 1; + return; + } + if (ch == 97) { + Steps ++; + n_c -= 1; + } + else if (ch == 119) { + Steps ++; + n_r -= 1; + } + else if (ch == 100) { + Steps ++; + n_c += 1; + } + else if (ch == 115) { + Steps ++; + n_r += 1; + } + Move(n_r,n_c); + system("cls"); + Update(); + } +} +int NotOut(int rr, int cc) { + return rr >= 0 && rr < mapInfo[cur_map].row && cc >= 0 && cc < mapInfo[cur_map].col; +} +// void swap(char * a, char * b) { +// char t = *a; +// *a = *b; +// *b = t; +// } +int char2int(char c) { + if (c == '*') return P; + else if (c == ' ') return Space; + else if (c == 'O') return Wall; + else if (c == '#') return Box; + else if (c == '$') return Dest; + else if (c == '\n') return LineBreak; +} +char int2char(int t) { + if (t == P || t == PInDest) return '*'; + else if (t == Space) return ' '; + else if (t == Wall) return 'O'; + else if (t == Box || t == BoxInDest) return '#'; + else if (t == Dest) return '$'; + else if (t == LineBreak) return '\n'; +} +int victory() { + for (i = 0; i < mapInfo[cur_map].row; ++ i) { + for (j = 0; j < mapInfo[cur_map].col; ++ j) { + if (maps[cur_map][i][j] == Box) return 0; + } + } + return 1; +} +void restart() { + Steps = 0; + free(maps); + maps = NULL; + initMaps(); + GetPlayerPosition(); + system("cls"); + Update(); +} +void Move(int n_r,int n_c) { + if (!NotOut(n_r,n_c)) return; + //前方空地 + if (maps[cur_map][n_r][n_c] == Space) { + maps[cur_map][n_r][n_c] = P; + maps[cur_map][player.row][player.col] = maps[cur_map][player.row][player.col] == PInDest ? Dest : Space; + player.row = n_r, player.col = n_c; + } + //前方归位点 + if (maps[cur_map][n_r][n_c] == Dest) { + maps[cur_map][n_r][n_c] = PInDest; + maps[cur_map][player.row][player.col] = maps[cur_map][player.row][player.col] == PInDest ? Dest : Space; + player.row = n_r, player.col = n_c; + } + //前方归位箱 + if (maps[cur_map][n_r][n_c] == BoxInDest) { + pushBox(BoxInDest,n_r,n_c); + } + //前方箱子 + if (maps[cur_map][n_r][n_c] == Box) { + pushBox(Box,n_r,n_c); + } +} +void pushBox(int BoxNowState, int n_r, int n_c) { + int b_n_r = n_r + n_r - player.row; + int b_n_c = n_c + n_c - player.col; + if (!NotOut(b_n_r,b_n_c)) return; + //箱子前方空地 + if (maps[cur_map][b_n_r][b_n_c] == Space) { + if (BoxNowState == BoxInDest) + maps[cur_map][n_r][n_c] = PInDest; + else if (BoxNowState == Box) + maps[cur_map][n_r][n_c] = P; + maps[cur_map][b_n_r][b_n_c] = Box; + maps[cur_map][player.row][player.col] = maps[cur_map][player.row][player.col] == PInDest ? Dest : Space; + player.row = n_r, player.col = n_c; + } + //箱子前方归位点 + else if (maps[cur_map][b_n_r][b_n_c] == Dest) { + if (BoxNowState == BoxInDest) + maps[cur_map][n_r][n_c] = PInDest; + else if (BoxNowState == Box) + maps[cur_map][n_r][n_c] = P; + maps[cur_map][b_n_r][b_n_c] = BoxInDest; + maps[cur_map][player.row][player.col] = maps[cur_map][player.row][player.col] == PInDest ? Dest : Space; + player.row = n_r, player.col = n_c; + } +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/pushBoxes.rar b/level1/p10_pushBoxes/pushBoxes.rar new file mode 100644 index 00000000..3683785d Binary files /dev/null and b/level1/p10_pushBoxes/pushBoxes.rar differ diff --git a/level1/p10_pushBoxes/score.txt b/level1/p10_pushBoxes/score.txt new file mode 100644 index 00000000..e69de29b diff --git a/level1/p11_linkedList/linkedList.c b/level1/p11_linkedList/linkedList.c new file mode 100644 index 00000000..d6c4f51e --- /dev/null +++ b/level1/p11_linkedList/linkedList.c @@ -0,0 +1,88 @@ +#include +#include +typedef struct node{ + struct node * next; + int val; +} * node; +node head,point5; +int num; +char ch; +int cnt = 0,endflag = 0; +void reverse(node n); +void show(); +void add(int x); +void init(); +int main() { + init(); + printf("Please enter:\n\tq to quit,\n\tc to create\n\tn to get next order of 5\n\tr to reverse\n"); + while (ch = getchar()) { + putchar('\n'); + if (ch == 'q') break;//退出 + else if (ch == 'n') printf("%d",next5());//下一个5 + else if (ch == 'c') {//创建 + printf("Please enter several numbers:\n"); + init(); + do { + scanf("%d",&num); + add(num); + } + while (getchar() != '\n'); + printf("We create:\n"); + show(); + } + else if (ch == 'r') { + reverse(head->next); + show(); + } + } + // show(); +} +void init() { + point5 = NULL; + endflag = 0; + cnt = 0; + head = (node)malloc(sizeof(*head)); + head->next = NULL; + head->val = 0; +} +void add(int x) { + node p = head; + while (p->next != NULL) p = p->next; + node n_p = (node)malloc(sizeof(*n_p)); + n_p->next = NULL; + n_p->val = x; + p->next = n_p; +} +void show() { + putchar('\n'); + node p = head->next; + while (p != NULL) { + printf("%d\t",p->val); + p = p->next; + } + putchar('\n'); +} +void reverse(node n) { + if (!n) return; + //如果递归到n是最后一个节点 + if (!n->next) { + head -> next = n; + return; + } + node next = n -> next; + if (n == head -> next) n -> next = NULL; //构造尾节点 + reverse(next); + next -> next = n; +} +int next5() { + if (endflag) return -1; + if (!point5) point5 = head; + point5 = point5 -> next; + cnt += 1; + while (point5 && point5 -> val != 5) cnt ++, point5 = point5 -> next; + if (!point5) { + endflag = 1; + return -1; + } + else return cnt; +} \ No newline at end of file diff --git a/level1/p12_warehouse/data.txt b/level1/p12_warehouse/data.txt new file mode 100644 index 00000000..99769b3b --- /dev/null +++ b/level1/p12_warehouse/data.txt @@ -0,0 +1,9 @@ +PLP-12 +12 +12 +EQS-12 +23 +12 +RFD-42 +12 +13 diff --git a/level1/p12_warehouse/warehouse.c b/level1/p12_warehouse/warehouse.c new file mode 100644 index 00000000..05d3c296 --- /dev/null +++ b/level1/p12_warehouse/warehouse.c @@ -0,0 +1,97 @@ +#include +#include +#include +#define MAX_SIZE 100 +char filename[] = "data.txt",tempfile_name[] = "temp.txt"; +char put[MAX_SIZE],get[MAX_SIZE]; +int i; +void CheckIn(); +FILE * OpenData(char * filename); +void Log(FILE * data_file); +char * ReadLine(FILE * data_file); +void ListWare(); +void CheckOut(); + +int main(int argc, char const *argv[]) +{ + + printf("Please hit\n\t1 to list wares\n\t2 to check in\n\t3 to check out\n\t4 to quit.\n"); + int ch; + while (1) { + if (_kbhit()) { + ch = _getch(); + if (ch == 49) { + ListWare(); + } + else if (ch == 50) { + CheckIn(); + } + else if (ch == 51) { + CheckOut(); + } + else if (ch == 52) { + break; + } + } + } + printf("Thanks for using"); + return 0; +} +void CheckOut() { + FILE * data_file = OpenData(filename); + FILE * temp_file = OpenData(tempfile_name); + char model_number[MAX_SIZE]; + printf("Please enter the model number you want to check out:\n"); + fgets(model_number,MAX_SIZE,stdin); + printf("Having successfully checked out %s",model_number); + while (ReadLine(data_file)) { + fseek(temp_file,0L,SEEK_END); + if (strcmp(model_number,get)) { + fputs(get,temp_file); + } + else { + ReadLine(data_file);ReadLine(data_file); + } + } + fclose(temp_file); + fclose(data_file); + remove(filename); + rename(tempfile_name,filename); +} +FILE * OpenData(char * filename) { + FILE * data_file = fopen(filename,"r+"); + if (NULL == data_file) { + data_file = fopen(filename,"w+"); + } + return data_file; +} +void ListWare() { + FILE * data_file = OpenData(filename); + printf("We already have following wares:\n"); + fseek(data_file,0L,SEEK_SET); + for (i = 0;; ++ i) { + if (!ReadLine(data_file))break; + if (!(i % 3)) + printf("\t%s",get); + } + fclose(data_file); +} +void Log(FILE * data_file) { + fgets(put,MAX_SIZE,stdin); + fseek(data_file,0L,SEEK_END); + fputs(put,data_file); +} +char * ReadLine(FILE * data_file) { + return fgets(get,MAX_SIZE,data_file); +} +void CheckIn() { + FILE * data_file = OpenData(filename); + printf("Please enter the model number:\n"); + Log(data_file); + printf("Please enter the quantity:\n"); + Log(data_file); + printf("Please enter the weight(XXkg):\n"); + Log(data_file); + printf("OK, the ware is successfully checked in!\n"); + fclose(data_file); +} \ No newline at end of file