├── 2020 ├── Seqlist │ ├── delsame.c │ ├── delx-y.c │ ├── lnk_del_x2y.c │ ├── lnk_merge.c │ ├── lnk_search.c │ └── odd_even.c ├── array │ ├── add_matrix │ ├── add_matrix.c │ ├── cl2.c │ └── cross_list.c ├── blstr_substr │ └── blstr_substr.c ├── graph │ ├── ArcNode_insert.c │ ├── del_vertex.c │ └── matrix_insert.c ├── graph_storage │ ├── list1.c │ ├── list2.c │ └── matrix.c ├── matrix │ └── add_matrix.c ├── search │ ├── avl_insert.c │ ├── create_hash.c │ └── hash_add_int.c ├── stack&queue │ ├── queue.c │ └── stack.c ├── string │ ├── 3.c │ ├── blstr_substr.c │ ├── str_compare.c │ ├── str_replace.c │ ├── test.c │ ├── test.exe │ ├── test2.c │ ├── test2.exe │ └── test3.c └── tree │ ├── nearest_ancestor.c │ ├── path.c │ ├── pre_order.c │ └── transform.c ├── .vscode └── settings.json ├── 2019-2020 ├── 实验一 │ ├── 2-4.c │ ├── 2-7.c │ ├── 2-8.c │ ├── 3-1.c │ └── 3-6.c ├── 实验三 │ ├── 7-12.c │ ├── 7-4.c │ ├── 8-15.c │ ├── 8-2.c │ └── 8-9.c ├── 实验二 │ ├── 5-10.c │ ├── 5-2.c │ ├── 5-4.c │ ├── 5-6.c │ ├── 6-2.c │ ├── 6-4.c │ ├── 6-6.c │ └── 6-8.c ├── 实验五 │ ├── 16-3-01-kaiser.c │ ├── 16-3-02-xy.c │ ├── 16-3-03-xy.c │ ├── 16-3-04-xy.c │ └── 16-3-05-xy.c ├── 实验六 │ ├── 01.c │ ├── 02.c │ ├── 03.c │ ├── 04.c │ ├── 05.c │ ├── 06.c │ ├── 07.c │ ├── 08.c │ ├── 09.c │ ├── 10.c │ ├── 11.c │ ├── 12.c │ ├── 13.c │ └── 实验六.md └── 实验四 │ ├── 10-1-syy.c │ ├── 10-1-zzy.c │ ├── 10-1.c │ ├── 10.1-wz.c │ ├── 12-01.c │ └── 13-01.c ├── MOOC ├── del.c ├── 多项式求导.c └── 逆置.c └── README.md /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.pythonPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python37_64\\python.exe" 3 | } -------------------------------------------------------------------------------- /2019-2020/实验一/2-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define RATE 0.05 4 | 5 | int main() 6 | { 7 | float a; 8 | printf("Enter an amount:"); 9 | scanf("%f", &a); 10 | a = a * (1 + RATE); 11 | printf("with tax added: $%.2f", a); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /2019-2020/实验一/2-7.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int _20, _10, _5, _1, amount; 7 | 8 | printf("Enter a dollar amount:"); 9 | scanf("%d", &amount); 10 | _20 = amount / 20; 11 | _10 = (amount - _20 * 20) / 10; 12 | _5 = (amount - _20 * 20 - _10 * 10) / 5; 13 | _1 = amount % 5; 14 | printf("$20 bills:%d\n$10 bills:%d\n$5 bills:%d\n$1 bills:%d\n", _20, _10, _5, _1); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /2019-2020/实验一/2-8.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | float loan, monthly, rate, b, c, d; 7 | printf("Enter amout of loan:"); 8 | scanf("%f", &loan); 9 | printf("Enter interest rate:"); 10 | scanf("%f", &rate); 11 | printf("Enter monthly payment:"); 12 | scanf("%f", &monthly); 13 | b = loan - monthly + loan * rate / 1200; 14 | c = b - monthly + b * rate / 1200; 15 | d = c - monthly + c * rate / 1200; 16 | printf("Balance ramaining after first payment: $%.2f\n", b); 17 | printf("Balance ramaining after second payment: $%.2f\n", c); 18 | printf("Balance ramaining after third payment: $%.2f\n", d); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /2019-2020/实验一/3-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int a, b, c; 7 | printf("Enter a date (mm/dd/yyyy):"); 8 | scanf("%d/%d/%d", &a, &b, &c); 9 | printf("You entered the date%d%d%d", a, b, c); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /2019-2020/实验一/3-6.c: -------------------------------------------------------------------------------- 1 | /* addfrac.c 2 | * 3 | * Adds two fractions 4 | * 5 | */ 6 | 7 | #include 8 | int main(void) 9 | { 10 | int num1, denom1, num2, denom2, result_num, result_denom; 11 | 12 | printf("Enter two fractions separated by a plus sign:"); 13 | scanf("%d/%d+%d/%d", &num1, &denom1, &num2, &denom2); 14 | result_num = num1 * denom2 + num2 * denom1; 15 | result_denom = denom1 * denom2; 16 | printf("%d/%d\n", result_num, result_denom); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /2019-2020/实验三/7-12.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define FLOAT 1e-6 4 | 5 | float add(float x, float y) 6 | { 7 | return x + y; 8 | } 9 | 10 | float minus(float x, float y) 11 | { 12 | return x - y; 13 | } 14 | 15 | float multiply(float x, float y) 16 | { 17 | return x * y; 18 | } 19 | 20 | float divide(float x, float y) 21 | { 22 | return x / y; 23 | } 24 | 25 | int main(void) 26 | { 27 | char ch; 28 | float num = 0.0f, fin = 0.0f, bet; 29 | scanf("%f", &fin); 30 | while (1) { 31 | ch = getchar(); 32 | if (ch != '\n') { 33 | switch (ch) { 34 | case '+': { 35 | bet = fin; 36 | scanf("%f", &num); 37 | fin = add(bet, num); 38 | } 39 | continue; 40 | case '-': { 41 | bet = fin; 42 | scanf("%f", &num); 43 | fin = minus(bet, num); 44 | } 45 | continue; 46 | case '*': { 47 | bet = fin; 48 | scanf("%f", &num); 49 | fin = multiply(bet, num); 50 | } 51 | continue; 52 | case '/': { 53 | bet = fin; 54 | scanf("%f", &num); 55 | if ((a - 0) >= FLOAT && (0 - a) <=-FLOAT) { 56 | fin = divide(bet, num); 57 | } else { 58 | printf("Value of expression: -nan"); 59 | return 0; 60 | } 61 | } 62 | } 63 | continue; 64 | } else 65 | break; 66 | } 67 | 68 | printf("%.1f", fin); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /2019-2020/实验三/7-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | char c; 6 | int a = 0; 7 | 8 | while (1) { 9 | c = getchar(); 10 | if (c != 10) { 11 | switch (c) { 12 | case 'A': 13 | case 'B': 14 | case 'C': 15 | a = 2; 16 | printf("%d", a); 17 | continue; 18 | case 'D': 19 | case 'E': 20 | case 'F': 21 | a = 3; 22 | printf("%d", a); 23 | continue; 24 | case 'G': 25 | case 'H': 26 | case 'I': 27 | a = 4; 28 | printf("%d", a); 29 | continue; 30 | case 'J': 31 | case 'K': 32 | case 'L': 33 | a = 5; 34 | printf("%d", a); 35 | continue; 36 | case 'M': 37 | case 'N': 38 | case 'O': 39 | a = 6; 40 | printf("%d", a); 41 | continue; 42 | case 'P': 43 | case 'Q': 44 | case 'R': 45 | case 'S': 46 | a = 7; 47 | printf("%d", a); 48 | continue; 49 | case 'T': 50 | case 'U': 51 | case 'V': 52 | a = 8; 53 | printf("%d", a); 54 | continue; 55 | case 'W': 56 | case 'X': 57 | case 'Y': 58 | case 'Z': 59 | a = 9; 60 | printf("%d", a); 61 | continue; 62 | default: 63 | printf("%c", c); 64 | continue; 65 | } 66 | } else 67 | break; 68 | } 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /2019-2020/实验三/8-15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int move = 0; 4 | 5 | int code(int a) 6 | { 7 | if (a >= 65 && a <= 90) { 8 | if (a + move > 90) 9 | return a = move + a - 26; 10 | else 11 | return a = move + a; 12 | } else if (a >= 97 && a <= 122) { 13 | if (a + move > 122) 14 | return a = move + a - 26; 15 | else 16 | return a = move + a; 17 | } else 18 | return (char)a; 19 | } 20 | 21 | int main(void) 22 | { 23 | char message[100] = { 10 }; 24 | int i = 0, k = 0; 25 | 26 | printf("Enter message to be encrypted:"); 27 | while (1) { 28 | message[k++] = getchar(); 29 | if (message[k - 1] == '\n') 30 | break; 31 | } 32 | printf("Enter shift amount (1-25):"); 33 | scanf("%d", &move); 34 | printf("Encrypted message: "); 35 | while (1) { 36 | if (message[i] == 10) 37 | break; 38 | else 39 | message[i] = code((int)message[i]); 40 | printf("%c", message[i++]); 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /2019-2020/实验三/8-2.c: -------------------------------------------------------------------------------- 1 | //很奇怪只得了98分 2 | #include 3 | 4 | int main(void) 5 | { 6 | int times[10] = { 0 }, num; 7 | 8 | do { 9 | num = getchar(); 10 | switch (num) { 11 | case '0': 12 | times[0] += 1; 13 | continue; 14 | case '1': 15 | times[1] += 1; 16 | continue; 17 | case '2': 18 | times[2] += 1; 19 | continue; 20 | case '3': 21 | times[3] += 1; 22 | continue; 23 | case '4': 24 | times[4] += 1; 25 | continue; 26 | case '5': 27 | times[5] += 1; 28 | continue; 29 | case '6': 30 | times[6] += 1; 31 | continue; 32 | case '7': 33 | times[7] += 1; 34 | continue; 35 | case '8': 36 | times[8] += 1; 37 | continue; 38 | case '9': 39 | times[9] += 1; 40 | continue; 41 | } 42 | break; 43 | } while (1); 44 | printf("Digit:\t 0 1 2 3 4 5 6 7 8 9 \nOccurrences: "); 45 | for (int i = 0; i < 10; i++) { 46 | printf("%d ", times[i]); 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /2019-2020/实验三/8-9.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char map[10][10] = { 39 }; 8 | int times[10][10] = { 0 }, x = 0, y = 0, letter = 65, direction, m, n; 9 | srand((unsigned)time(NULL)); 10 | 11 | for (y = 0; y < 10; y++) { 12 | for (x = 0; x < 10; x++) 13 | map[x][y] = '.'; 14 | } 15 | x = y = 0; 16 | while (letter <= 90) { 17 | map[x][y] = letter; 18 | times[x][y] = 1; 19 | letter++; 20 | m = x, n = y; 21 | begain: 22 | direction = (rand() % 4); 23 | switch (direction) { 24 | case 0: 25 | y -= 1; 26 | break; 27 | case 1: 28 | y += 1; 29 | break; 30 | case 2: 31 | x -= 1; 32 | break; 33 | case 3: 34 | x += 1; 35 | break; 36 | }; 37 | 38 | if (x < 10 && x >= 0 && y < 10 && y >= 0 && times[x][y] == 0) 39 | continue; 40 | else { 41 | x = m, y = n; 42 | if (times[x - 1][y] == 1 && times[x + 1][y] == 1 && times[x][y - 1] == 1 && times[x][y + 1] == 1) 43 | break; 44 | else 45 | goto begain; 46 | } 47 | } 48 | x = y = 0; 49 | for (y = 0; y < 10; y++) { 50 | for (x = 0; x < 10; x++) 51 | printf("%c", map[x][y]); 52 | printf("\n"); 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /2019-2020/实验二/5-10.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int grade, grade1; 7 | scanf("%d", &grade); 8 | grade1 = grade / 10; 9 | switch (grade1) { 10 | case 9, case 10: //:: expected ' //:: expected expression 11 | printf("Letter grade: A"); 12 | break; 13 | case 8: 14 | printf("Letter grade: B"); 15 | break; 16 | case 7: 17 | printf("Letter grade: c"); 18 | break; 19 | case 6: 20 | printf("Letter grade: D"); 21 | break; 22 | case 5, case 4, case 3, case 2, case 1, case 0: //:: expected ' //:: expected expression 23 | printf("Letter grade: F"); 24 | if (grade > 100 || grade < 0) 25 | printf("Error, grade must be between 0 and 100.") //:: 忘记加分号 ; 了 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /2019-2020/实验二/5-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int hour, min; 7 | printf("Enter a 24-hour time: "); 8 | scanf("%d:%d", &hour, &min); 9 | if (hour > 12) 10 | printf("Equivalent 12 - hour time: %.2d:%.2d PM", hour - 12, min); 11 | if (hour == 12) 12 | printf("Equivalent 12 - hour time: %.2d:%.2d PM", hour, min); 13 | if (hour < 12 && hour > 0) 14 | printf("Equivalent 12 - hour time: %.2d:%.2d AM", hour, min); 15 | if (hour < 1) 16 | printf("Equivalent 12 - hour time: %.2d:%.2d AM", hour + 12, min); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /2019-2020/实验二/5-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int speed; 7 | scanf("%d", &speed); 8 | if (speed < 1) 9 | printf("Calm"); 10 | if (speed >= 1 && speed <= 3) 11 | printf("Light air"); 12 | if (speed >= 4 && speed <= 27) 13 | printf("Breeze"); 14 | if (speed >= 28 && speed <= 47) 15 | printf("Gale"); 16 | if (speed >= 48 && speed <= 63) 17 | printf("Storm"); 18 | if (speed >= 64) 19 | printf("Hurricane"); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /2019-2020/实验二/5-6.c: -------------------------------------------------------------------------------- 1 | /* upc.c 2 | * 3 | * Computes a universal product code check digit 4 | * 5 | */ 6 | 7 | #include 8 | 9 | int main(void) 10 | { 11 | int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total, last; 12 | 13 | printf("Enter the first (single) digit:"); 14 | scanf("%1d", &d); 15 | 16 | printf("Enter the first group of five digits:"); 17 | scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5); 18 | 19 | printf("Enter the second group of five digits:"); 20 | scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5); 21 | 22 | first_sum = d + i2 + i4 + j1 + j3 + j5; 23 | second_sum = i1 + i3 + i5 + j2 + j4; 24 | total = 3 * first_sum + second_sum; 25 | 26 | scanf("%d", &last); 27 | if (9 - ((total - 1) % 10) == last) 28 | printf("VALID"); 29 | else 30 | printf("NOT VALID"); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /2019-2020/实验二/6-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int m, n, k; 7 | scanf("%d%d", &m, &n); 8 | while (n != 0) { 9 | k = m % n; 10 | m = n, n = k; 11 | } 12 | printf("%d", m); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /2019-2020/实验二/6-4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * broker.c 3 | */ 4 | 5 | #include 6 | 7 | int main(void) 8 | { 9 | float commission, value; 10 | 11 | printf("Enter value of trade: "); 12 | while (1) { 13 | scanf("%f", &value); 14 | 15 | if (value < 2500.00f) 16 | commission = 30.00f + .017f * value; 17 | else if (value < 6250.00f) 18 | commission = 56.00f + .0066f * value; 19 | else if (value < 20000.00f) 20 | commission = 76.00f + .0034f * value; 21 | else if (value < 50000.00f) 22 | commission = 100.00f + .0022f * value; 23 | else if (value < 500000.00f) 24 | commission = 155.00f + .0011f * value; 25 | else 26 | commission = 255.00f + .0009f * value; 27 | 28 | if (commission < 39.00f) 29 | commission = 39.00f; 30 | if (value == 0) 31 | break; 32 | 33 | printf("Commission: $%.2f\n\n", commission); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /2019-2020/实验二/6-6.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int num, num2; 7 | scanf("%d", &num); 8 | do { 9 | num2 = num * num; 10 | printf("%d\n", num2); 11 | num 12 | -= 2; 13 | } while (num >= 0); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /2019-2020/实验二/6-8.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int day, row, first, total; 6 | 7 | printf("Enter number of days in month:"); 8 | scanf("%d", &total); 9 | printf("Enter starting day of the week(1=Sun,7=Sat):"); 10 | scanf("%d", &first); 11 | 12 | printf("一 二 三 四 五 六 日 \n"); 13 | for (int i = 1; i < first; i++) 14 | printf(" "); 15 | for (day = 1; day <= 8 - first; day++) 16 | printf("%d ", day); 17 | printf("\n"); 18 | for (int i = 1; i <= 5; i++) { 19 | for (int k = 1; k <= 7; k++, day++) { 20 | if (day <= total) 21 | printf("%d ", day); 22 | else 23 | break; 24 | } 25 | printf("\n"); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /2019-2020/实验五/16-3-01-kaiser.c: -------------------------------------------------------------------------------- 1 | #include "lab51.h" 2 | #include 3 | #include 4 | 5 | #define NAME_LEN 25 6 | 7 | #define MAX_PARTS 100 8 | 9 | int main() 10 | { 11 | struct part inventory[MAX_PARTS]; 12 | int num_parts = 0; 13 | char code; 14 | for (;;) { 15 | printf("Enter operation code: "); 16 | scanf(" %c", &code); 17 | while (getchar() != '\n') 18 | ; 19 | switch (code) { 20 | case 'i': { 21 | insert(inventory, &num_parts); 22 | break; 23 | } 24 | case 's': { 25 | search(inventory, num_parts); 26 | break; 27 | } 28 | case 'u': { 29 | update(inventory, num_parts); 30 | break; 31 | } 32 | case 'p': { 33 | print(inventory, num_parts); 34 | break; 35 | } 36 | case 'q': 37 | return 0; 38 | default: 39 | printf("Illegal code\n"); 40 | } 41 | printf("\n"); 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /2019-2020/实验五/16-3-02-xy.c: -------------------------------------------------------------------------------- 1 | #include "lab51.h" 2 | #include 3 | #include 4 | #define MAX_PARTS 100 5 | #define NAME_LEN 25 6 | 7 | void insert(struct part inv[], int* np) 8 | { 9 | int part_number; 10 | 11 | if (*np == MAX_PARTS) { 12 | printf("Database is full; can't add more parts.\n"); 13 | return; 14 | } 15 | printf("Enter part number:"); 16 | scanf("%d", &part_number); 17 | 18 | if (find_part(part_number, inv, *np) >= 0) { 19 | printf("Part already exist.\n"); 20 | return; 21 | } 22 | 23 | inv[*np].number = part_number; 24 | printf("Enter part name:"); 25 | read_line(inv[*np].name, NAME_LEN); 26 | printf("Enter quantity on hand:"); 27 | scanf("%d", &inv[*np].on_hand); 28 | (*np)++; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /2019-2020/实验五/16-3-03-xy.c: -------------------------------------------------------------------------------- 1 | #include "lab51.h" 2 | #include 3 | #include 4 | 5 | void search(const struct part inv[], int np) 6 | { 7 | int i, number; 8 | 9 | printf("Enter part number:"); 10 | scanf("%d", &number); 11 | i = find_part(number, inv, np); 12 | if (i >= 0) { 13 | printf("Part name:%s\n", inv[i].name); 14 | printf("Quantity on hand:%d\n", inv[i].on_hand); 15 | } else 16 | printf("Part not found.\n"); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /2019-2020/实验五/16-3-04-xy.c: -------------------------------------------------------------------------------- 1 | #include "lab51.h" 2 | #include 3 | #include 4 | void update(struct part inv[], int np) 5 | { 6 | int i, number, change; 7 | 8 | printf("Enter part number:\n"); 9 | scanf("%d", &number); 10 | i = find_part(number, inv, np); 11 | if (i >= 0) { 12 | printf("Enter change quantity on hand:\n"); 13 | scanf("%d", &change); 14 | inv[i].on_hand += change; 15 | } else 16 | printf("Part not found."); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /2019-2020/实验五/16-3-05-xy.c: -------------------------------------------------------------------------------- 1 | #include "lab51.h" // 请不要删除本行头文件,否则检查不通过 2 | #include 3 | #include 4 | 5 | void print(const struct part inv[], int np) 6 | { 7 | int i; 8 | 9 | printf("Part number Part Name " 10 | "Quantity on Hand\n"); 11 | for (i = 0; i < np; i++) 12 | printf("%7d %-25s%lld\n", inv[i].number, inv[i].name, inv[i].on_hand); 13 | } 14 | -------------------------------------------------------------------------------- /2019-2020/实验六/01.c: -------------------------------------------------------------------------------- 1 | #include "lab52.h" // 请不要删除本行头文件,否则检查不通过 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | GoodsList* goodsList; 8 | init_list(&goodsList); 9 | GoodsInfo item; 10 | char temp_id[MAX_ID_LEN]; 11 | while (1) { 12 | int choice; 13 | printf("超市商品管理系统\n"); 14 | printf("********************************************\n"); 15 | printf("1.显示所有商品的信息:\n"); 16 | printf("2.修改某个商品的信息:\n"); 17 | printf("3.插入某个商品的信息:\n"); 18 | printf("4.删除某个商品的信息:\n"); 19 | printf("5.查找某个商品的信息:\n"); 20 | printf("6.商品存盘并退出系统:\n"); 21 | printf("7.对商品价格进行排序:\n"); 22 | printf("8.(慎用)删除所有内容:\n"); 23 | printf("其他.不存盘并退出系统:\n"); 24 | printf("********************************************\n"); 25 | printf("输入您的选择: "); 26 | 27 | scanf("%d", &choice); 28 | switch (choice) { 29 | case 1: 30 | output_all_items(goodsList); 31 | break; 32 | case 2: 33 | item = read_goods_info(); 34 | printf("输入要修改记录的 ID:"); 35 | read_line(temp_id, MAX_ID_LEN); 36 | change_item(goodsList, temp_id, item); 37 | break; 38 | case 3: 39 | item = read_goods_info(); 40 | int pos; 41 | printf("输入数字表明你要插入的商品位置:0.商品列表尾部 1.商品列表头部 i.商品列表中间第i号位置\n"); 42 | scanf("%d", &pos); 43 | insert_item(goodsList, item, pos); 44 | break; 45 | case 4: 46 | printf("输入要删除记录的 ID:"); 47 | read_line(temp_id, MAX_ID_LEN); 48 | delete_item(goodsList, temp_id); 49 | break; 50 | case 5: 51 | printf("输入要查找记录的 ID:"); 52 | read_line(temp_id, MAX_ID_LEN); 53 | search_item(goodsList, temp_id); 54 | break; 55 | case 6: 56 | save_to_file(goodsList); 57 | printf("您已经存盘并退出超市商品管理系统!\n"); 58 | return 0; 59 | case 7: 60 | bubble_sort(goodsList); 61 | break; 62 | case 8: 63 | destory_list_and_file(&goodsList); 64 | printf("您已经删除商品文件内容以及链表内容!\n"); 65 | break; 66 | default: 67 | printf("您已经退出超市商品管理系统!\n"); 68 | destory_list(&goodsList); 69 | return 0; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /2019-2020/实验六/02.c: -------------------------------------------------------------------------------- 1 | #include "lab52.h" // 请不要删除本行头文件,否则检查不通过 2 | #include 3 | #include 4 | 5 | extern int CurrentCnt; // 请不要删除本行的全局变量声明,否则检查不通过 6 | 7 | void init_list(GoodsList** L) 8 | { 9 | FILE* fp; 10 | GoodsInfo goodsInfo; 11 | GoodsList *p, *r; 12 | 13 | (*L) = (GoodsList*)malloc(sizeof(GoodsList)); 14 | r = (*L); 15 | if ((fp = fopen(GOODS_FILE_NAME, "r")) == NULL) { 16 | if ((fp = fopen(GOODS_FILE_NAME, "w")) == NULL) 17 | printf("提示:不能创建商品文件\n"); 18 | } else { 19 | while (!feof(fp)) { 20 | fscanf(fp, "%s", goodsInfo.goods_id); 21 | fscanf(fp, "\t%s", goodsInfo.goods_name); 22 | fscanf(fp, "\t%d", &goodsInfo.goods_price); 23 | fscanf(fp, "\t%s", goodsInfo.goods_discount); 24 | fscanf(fp, "\t%d", &goodsInfo.goods_amount); 25 | fscanf(fp, "\t%d\n", &goodsInfo.goods_remain); 26 | p = (GoodsList*)malloc(sizeof(GoodsList)); 27 | p->data = goodsInfo; 28 | r->next = p; 29 | r = p; 30 | CurrentCnt++; 31 | } 32 | } 33 | fclose(fp); 34 | r->next = NULL; 35 | printf("商品的链表文件已建立,有%d个商品记录\n", CurrentCnt); 36 | } 37 | -------------------------------------------------------------------------------- /2019-2020/实验六/03.c: -------------------------------------------------------------------------------- 1 | bool insert_item(GoodsList *L, GoodsInfo goodsInfo, int choice) 2 | { 3 | GoodsList *p = L; 4 | switch (choice) 5 | { 6 | case 0: 7 | { 8 | while(p->next) 9 | p = p->next; 10 | p->next = malloc(sizeof(GoodsList)); 11 | p->next->data = goodsInfo; 12 | p->next->next = NULL; 13 | return true; 14 | } 15 | case 1: 16 | { 17 | p = malloc(sizeof(GoodsList)); 18 | p->data = goodsInfo; 19 | p->next = L->next; 20 | L->next = p; 21 | return true; 22 | } 23 | default: 24 | { 25 | int i; 26 | for(i = 1;i <= choice - 2;++i) 27 | p = p->next; 28 | GoodsList *q = malloc(sizeof(GoodsList)); 29 | q->next = p->next; 30 | p->next = q; 31 | return true; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /2019-2020/实验六/04.c: -------------------------------------------------------------------------------- 1 | #include "lab52.h" // 请不要删除本行头文件,否则检查不通过 2 | #include 3 | #include 4 | #include 5 | 6 | extern int CurrentCnt; // 请不要删除本行的全局变量声明,否则检查不通过 7 | 8 | bool delete_item(GoodsList* L, char* goods_id) 9 | { 10 | GoodsList *pre, *cur; 11 | pre = NULL, cur = L; 12 | for (; !strcmp(cur->data.goods_id, goods_id); pre = cur, cur = cur->next) 13 | ; 14 | if (!cur) { 15 | return false; 16 | } 17 | if (!pre) { 18 | L = L->next; 19 | free(pre); 20 | return true; 21 | } else { 22 | pre->next = cur->next; 23 | free(pre); 24 | return true; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /2019-2020/实验六/05.c: -------------------------------------------------------------------------------- 1 | GoodsList *search_item(GoodsList* L, char* goods_id) 2 | { 3 | for (; L->data.goods_id == goods_id && L; L = L->next) 4 | return L; 5 | } 6 | -------------------------------------------------------------------------------- /2019-2020/实验六/06.c: -------------------------------------------------------------------------------- 1 | bool change_item(GoodsList* L, char* goods_id, GoodsInfo new_info) 2 | { 3 | for (; L->data.goods_id == goods_id && L; L = L->next) 4 | ; 5 | GoodsInfo temp = L->data; 6 | temp = new_info; 7 | if (temp.goods_amount == new_info.goods_amount && !strcmp(temp.goods_discount, new_info.goods_discount) && !strcmp(temp.goods_id, new_info.goods_id) 8 | && !strcmp(temp.goods_name, new_info.goods_name) && temp.goods_price == new_info.goods_price && temp.goods_remain == new_info.goods_remain) 9 | return true; 10 | else 11 | return false; 12 | } 13 | -------------------------------------------------------------------------------- /2019-2020/实验六/07.c: -------------------------------------------------------------------------------- 1 | #include "lab52.h" // 请不要删除本行头文件,否则检查不通过 2 | #include 3 | #include 4 | 5 | extern int CurrentCnt; // 请不要删除本行的全局变量声明,否则检查不通过 6 | 7 | void output_one_item(GoodsList* p) 8 | { 9 | if (p) { 10 | GoodsInfo temp = p->data; 11 | printf("%s\n%s\n%d\n%s\n%d\n%d\n", temp.goods_id, temp.goods_name, temp.goods_price, temp.goods_discount, temp.goods_amount, temp.goods_remain); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /2019-2020/实验六/08.c: -------------------------------------------------------------------------------- 1 | void output_all_items(GoodsList* L) 2 | { 3 | for (L = L->next; L != NULL; L = L->next) { 4 | output_one_item(L); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /2019-2020/实验六/09.c: -------------------------------------------------------------------------------- 1 | #include "lab52.h" // 请不要删除本行头文件,否则检查不通过 2 | #include 3 | #include 4 | 5 | extern int CurrentCnt; // 请不要删除本行的全局变量声明,否则检查不通过 6 | 7 | void destory_list(GoodsList** L) 8 | { 9 | for (; *L; *L = (*L)->next) 10 | free(*L); 11 | } 12 | -------------------------------------------------------------------------------- /2019-2020/实验六/10.c: -------------------------------------------------------------------------------- 1 | #include "lab52.h" // 请不要删除本行头文件,否则检查不通过 2 | #include 3 | #include 4 | 5 | extern int CurrentCnt; // 请不要删除本行的全局变量声明,否则检查不通过 6 | 7 | void destory_list_and_file(GoodsList** L) 8 | { 9 | destory_list(L); 10 | remove("goodinfo.txt"); 11 | } 12 | -------------------------------------------------------------------------------- /2019-2020/实验六/11.c: -------------------------------------------------------------------------------- 1 | #include "lab52.h" // 请不要删除本行头文件,否则检查不通过 2 | #include 3 | #include 4 | 5 | extern int CurrentCnt; // 请不要删除本行的全局变量声明,否则检查不通过 6 | 7 | int save_to_file(GoodsList* L) 8 | { 9 | GoodsInfo temp = L->data; 10 | FILE* txt; 11 | txt = fopen("goodsinfo.txt", "w+"); 12 | for (L; L; L = L->next) { 13 | fprintf(txt, "%s\n%s\n%d\n%s\n%d\n%d\n", temp.goods_id, temp.goods_name, temp.goods_price, temp.goods_discount, temp.goods_amount, temp.goods_remain); 14 | } 15 | fclose(txt); 16 | } 17 | -------------------------------------------------------------------------------- /2019-2020/实验六/12.c: -------------------------------------------------------------------------------- 1 | void bubble_sort(GoodsList* L) 2 | { 3 | if (!L) 4 | { 5 | return; 6 | } 7 | else 8 | { 9 | GoodsList* tail = NULL; 10 | GoodsList* flag = NULL; 11 | while (flag != L) 12 | { 13 | tail = flag; 14 | flag = L; 15 | GoodsList* pre = L; 16 | while (pre->next != tail) 17 | { 18 | GoodsList* cur = pre->next; 19 | if (pre->data.goods_price > cur->data.goods_price) 20 | { 21 | int temp = pre->data.goods_price; 22 | pre->data.goods_price = cur->data.goods_price; 23 | cur->data.goods_price = temp; 24 | flag = pre->next; 25 | } 26 | pre = pre->next; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /2019-2020/实验六/13.c: -------------------------------------------------------------------------------- 1 | #include "lab52.h" // 请不要删除本行头文件,否则检查不通过 2 | #include 3 | #include 4 | 5 | extern int CurrentCnt; // 请不要删除本行的全局变量声明,否则检查不通过 6 | 7 | GoodsInfo read_goods_info() 8 | { 9 | GoodsInfo goods; 10 | read_line(goods.goods_id, MAX_ID_LEN); 11 | read_line(goods.goods_name, MAX_NAME_LEN); 12 | scanf("%d", &goods.goods_price); 13 | read_line(goods.goods_discount, MAX_PRICE_LEN); 14 | scanf("%d", &goods.goods_amount); 15 | scanf("%d", &goods.goods_remain); 16 | return goods; 17 | } 18 | -------------------------------------------------------------------------------- /2019-2020/实验六/实验六.md: -------------------------------------------------------------------------------- 1 | # 奥里给 2 | 爆肝两天,除了01.02.03这几个让人摸不着头脑的题实在无法下手,其他的终于写完了!!! 3 | -------------------------------------------------------------------------------- /2019-2020/实验四/10-1-syy.c: -------------------------------------------------------------------------------- 1 | #include /* C99 only */ 2 | #include 3 | #include 4 | 5 | #define STACK_SIZE 100 6 | 7 | /* external variables */ 8 | char contents[STACK_SIZE]; 9 | int top = 0; 10 | 11 | void stack_overflow(void) 12 | { 13 | printf("Stack overflow\n"); 14 | exit(EXIT_FAILURE); 15 | } 16 | 17 | void stack_underflow(void) 18 | { 19 | printf("Stack underflow\n"); 20 | exit(EXIT_FAILURE); 21 | } 22 | 23 | void make_empty(void) 24 | { 25 | top = 0; 26 | } 27 | 28 | bool is_empty(void) 29 | { 30 | return top == 0; 31 | } 32 | 33 | bool is_full(void) 34 | { 35 | return top == STACK_SIZE; 36 | } 37 | 38 | void push(char ch) 39 | { 40 | if (is_full()) 41 | stack_overflow(); 42 | else 43 | contents[top++] = ch; 44 | } 45 | 46 | char pop(void) 47 | { 48 | if (is_empty()) 49 | stack_underflow(); 50 | else 51 | return contents[--top]; 52 | 53 | return '\0'; /* prevents compiler warning due to stack_underflow() call */ 54 | } 55 | 56 | int main(void) 57 | { 58 | char ch = ' ', geted; 59 | printf("Enter parentheses and/or braces: "); 60 | make_empty(); 61 | while(ch!='\n') { 62 | scanf("%c", &ch); 63 | if (ch == '(' || ch == '{') 64 | push(ch); 65 | else if (ch == '\n'&&(!is_empty())){ 66 | ch=' '; 67 | break; 68 | } 69 | 70 | else { 71 | geted = pop(); 72 | if ((geted == '(' && ch == ')') || (geted == '{' && ch == '}')) 73 | continue; 74 | else 75 | break; 76 | } 77 | } 78 | if (ch != '\n') { 79 | printf("Parentheses/braces are NOT nested properly"); 80 | } else 81 | printf("Parentheses/braces are nested properly"); 82 | return 0; 83 | } 84 | 85 | 86 | -------------------------------------------------------------------------------- /2019-2020/实验四/10-1-zzy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define STACK_size 10 4 | char contents[STACK_size]; 5 | int top = 0; 6 | void make_empty(void) 7 | { 8 | top = 0; 9 | } 10 | bool is_empty(void) 11 | { 12 | return top == 0; 13 | } 14 | bool is_full(void) 15 | { 16 | return is_full == 0; 17 | } 18 | void push(char i) 19 | { 20 | if (is_full()) 21 | 22 | ; 23 | else 24 | contents[top++] = i; 25 | } 26 | char pop(void) 27 | { 28 | if (is_empty()) 29 | ; 30 | else 31 | return contents[--top]; 32 | } 33 | int main() 34 | { 35 | printf("enter your braces"); 36 | char a; 37 | push(getchar()); 38 | while ((a = getchar()) != '\n') { 39 | 40 | if (a == '}') { 41 | if (pop() != '{') { 42 | printf("Parentheses/braces are NOT nested properly"); 43 | break; 44 | } 45 | } else if (a == ')') { 46 | if (pop() != '(') { 47 | printf("Parentheses/braces are NOT nested properly"); 48 | break; 49 | } 50 | } else 51 | push(a); 52 | } 53 | if (top == 0) 54 | printf("Parentheses/braces are nested properly"); 55 | } 56 | 57 | -------------------------------------------------------------------------------- /2019-2020/实验四/10-1.c: -------------------------------------------------------------------------------- 1 | #include /* C99 only */ 2 | #include 3 | #include 4 | 5 | #define STACK_SIZE 100 6 | 7 | /* external variables */ 8 | char contents[STACK_SIZE] = { 0 }; 9 | int top = 0; 10 | 11 | void stack_overflow(void) 12 | { 13 | printf("Stack overflow\n"); 14 | exit(EXIT_FAILURE); 15 | } 16 | 17 | void stack_underflow(void) 18 | { 19 | printf("Stack underflow\n"); 20 | exit(EXIT_FAILURE); 21 | } 22 | 23 | void make_empty(void) 24 | { 25 | top = 0; 26 | } 27 | 28 | bool is_empty(void) 29 | { 30 | return top == 0; 31 | } 32 | 33 | bool is_full(void) 34 | { 35 | return top == STACK_SIZE; 36 | } 37 | 38 | void push(char ch) 39 | { 40 | if (is_full()) 41 | stack_overflow(); 42 | else 43 | contents[top++] = ch; 44 | } 45 | 46 | char pop(void) 47 | { 48 | if (is_empty()) 49 | stack_underflow(); 50 | else 51 | return contents[--top]; 52 | 53 | return '\0'; /* prevents compiler warning due to stack_underflow() call */ 54 | } 55 | 56 | int main(void) 57 | { 58 | char a[50] = { '\0' }; 59 | int i = 0; 60 | printf("Enter parentheses and/or braces:"); 61 | make_empty(); 62 | while (1) { 63 | a[i] = getchar(); 64 | if (a[i] == ' ') 65 | continue; 66 | if (a[i] == '\n') { 67 | a[i] = '\0'; 68 | break; 69 | } 70 | i++; 71 | } 72 | i = 0; 73 | while (i <= 50) { 74 | push(a[i]); 75 | if (top >= 2) { 76 | if ((contents[top - 2] == '(' && contents[top - 1] == ')') || (contents[top - 2] == '{' && contents[top - 1] == '}')) { 77 | contents[top - 2] = 0, contents[top - 1] = 0; 78 | top -= 2; 79 | } 80 | } 81 | i++; 82 | } 83 | if (contents[0] == '\0') 84 | printf("Parentheses/braces are nested properly"); 85 | else 86 | printf("Parentheses/braces are NOT nested properly"); 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /2019-2020/实验四/10.1-wz.c: -------------------------------------------------------------------------------- 1 | 2 | #include /* C99 only */ 3 | #include 4 | #include 5 | 6 | #define STACK_SIZE 100 7 | 8 | /* external variables */ 9 | char contents[STACK_SIZE]; 10 | int top = 0; 11 | 12 | void stack_overflow(void) 13 | { 14 | printf("Stack overflow\n"); 15 | exit(EXIT_FAILURE); 16 | } 17 | 18 | void stack_underflow(void) 19 | { 20 | printf("Stack underflow\n"); 21 | exit(EXIT_FAILURE); 22 | } 23 | 24 | void make_empty(void) 25 | { 26 | top = 0; 27 | } 28 | 29 | bool is_empty(void) 30 | { 31 | return top == 0; 32 | } 33 | 34 | bool is_full(void) 35 | { 36 | return top == STACK_SIZE; 37 | } 38 | 39 | void push(char ch) 40 | { 41 | if (is_full()) 42 | stack_overflow(); 43 | else 44 | contents[top++] = ch; 45 | } 46 | 47 | char pop(void) 48 | { 49 | if (is_empty()) 50 | stack_underflow(); 51 | else 52 | return contents[--top]; 53 | 54 | return '\0'; /* prevents compiler warning due to stack_underflow() call */ 55 | } 56 | 57 | int main(void) 58 | { 59 | char ch; 60 | while (1) 61 | { 62 | ch = getchar(); 63 | if (ch == 40 || ch == 123) 64 | push(ch); 65 | else if (contents[top - 1] == ch - 1 || contents[top - 1] == ch - 2) 66 | pop(); 67 | else if (ch == '}' || ch == ')') push(ch); 68 | else break; 69 | } 70 | if (top == 0) 71 | printf("Parentheses/braces are nested properly"); 72 | else 73 | printf("Parentheses/braces are NOT nested properly"); 74 | system("pause"); 75 | return 0; 76 | } -------------------------------------------------------------------------------- /2019-2020/实验四/12-01.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int j, k = 0; 7 | char message[100] = { 0 }; 8 | while (1) { 9 | message[k++] = getchar(); 10 | if (message[k - 1] == '\n') 11 | break; 12 | } 13 | printf("Reversal is: "); 14 | for (k; k >= 0; k--) 15 | printf("%c", message[k]); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /2019-2020/实验四/13-01.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | char input[20]="0", max[20]="A", min[20]="z"; 5 | while (1){ 6 | printf("Enter a word:"); 7 | gets_s(input); 8 | if (strcmp(input, max) >= 0)strcpy_s(max, input); 9 | if (strcmp(input, min) <= 0)strcpy_s(min, input); 10 | if (strlen(input) == 4)break; 11 | } 12 | printf("Smallest word:%s\n",min); 13 | printf("Largest word:%s",max); 14 | } 15 | -------------------------------------------------------------------------------- /2020/Seqlist/delsame.c: -------------------------------------------------------------------------------- 1 | #include "list.h" // 请不要删除,否则检查不通过 2 | #include 3 | #include 4 | 5 | void del_dupnum(SeqList* L) 6 | { 7 | int pre = -21474, i = 0, j = 0, k = 0; 8 | for (i = 0; i <= L->last; i++) { 9 | if (L->elem[i] == pre) 10 | j++; 11 | else 12 | L->elem[k++] = L->elem[i]; 13 | pre = L->elem[i]; 14 | } 15 | L->last = L->last - j; 16 | } 17 | -------------------------------------------------------------------------------- /2020/Seqlist/delx-y.c: -------------------------------------------------------------------------------- 1 | #include "list.h" // 请不要删除,否则检查不通过 2 | #include 3 | #include 4 | 5 | void del_x2y(SeqList* L, ElemType x, ElemType y) 6 | { 7 | int i = 0, j = 0, k = 0; 8 | for (i = 0; i <= L->last; i++) { 9 | if (L->elem[i] >= x && L->elem[i] <= y) 10 | j++; 11 | else 12 | L->elem[k++] = L->elem[i]; 13 | } 14 | L->last = L->last - j; 15 | } -------------------------------------------------------------------------------- /2020/Seqlist/lnk_del_x2y.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jason-xy/UESTC_SoftwareEngineeringCourse/c085031a1be5b00894791cf1930c89243389fbe5/2020/Seqlist/lnk_del_x2y.c -------------------------------------------------------------------------------- /2020/Seqlist/lnk_merge.c: -------------------------------------------------------------------------------- 1 | #include "list.h" // 请不要删除,否则检查不通过 2 | #include 3 | #include 4 | 5 | void lnk_merge(LinkList A, LinkList B, LinkList C) 6 | { 7 | Node *pa, *pb, *pc; 8 | for (pa = A->next, pb = B->next, pc = C; pa || pb;) { 9 | if (pa) { 10 | pc->next = pa; 11 | pa = pa->next; 12 | pc = pc->next; 13 | } 14 | if (pb) { 15 | pc->next = pb; 16 | pb = pb->next; 17 | pc = pc->next; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /2020/Seqlist/lnk_search.c: -------------------------------------------------------------------------------- 1 | #include "list.h" // 请不要删除,否则检查不通过 2 | #include 3 | #include 4 | 5 | int lnk_search(LinkList L, int k, ElemType* p_ele) 6 | { 7 | int i = 0, j = 0; 8 | Node* p = NULL; 9 | for (p = L; p; p = p->next) 10 | i++; 11 | if (k > i) 12 | return 0; 13 | for (p = L; j <= i - k; p = p->next, j++) 14 | ; 15 | *p_ele = p->data; 16 | return 1; 17 | } 18 | -------------------------------------------------------------------------------- /2020/Seqlist/odd_even.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jason-xy/UESTC_SoftwareEngineeringCourse/c085031a1be5b00894791cf1930c89243389fbe5/2020/Seqlist/odd_even.c -------------------------------------------------------------------------------- /2020/array/add_matrix: -------------------------------------------------------------------------------- 1 | #include "tsmatrix.h" 2 | #include 3 | #include 4 | 5 | bool add_matrix(const TSMatrix* pM, const TSMatrix* pN, TSMatrix* pQ) //这种写法不好看,但是能简单明了的解决问题,以后有机会可以写新算法 6 | { 7 | int i = 0, j = 0, k = 0, temp; 8 | if (pM->m != pN->m || pM->n != pN->n) 9 | return false; 10 | 11 | pQ->m = pM->m; 12 | pQ->n = pM->n; 13 | pQ->len = 0; 14 | 15 | while (i < pM->len && j < pN->len && k < MAXSIZE) { 16 | if (pM->data[i].i < pN->data[j].i) { 17 | pQ->data[k].i = pM->data[i].i; 18 | pQ->data[k].j = pM->data[i].j; 19 | pQ->data[k].e = pM->data[i].e; 20 | pQ->len++; 21 | k++; 22 | i++; 23 | } else if (pM->data[i].i == pN->data[j].i) { 24 | if (pM->data[i].j == pN->data[j].j) { 25 | temp = pM->data[i].e + pN->data[j].e; 26 | if (temp == 0) { 27 | i++; 28 | j++; 29 | } else { 30 | pQ->data[k].i = pM->data[i].i; 31 | pQ->data[k].j = pM->data[i].j; 32 | pQ->data[k].e = temp; 33 | pQ->len++; 34 | k++; 35 | i++; 36 | j++; 37 | } 38 | } else if (pM->data[i].j > pN->data[j].j) { 39 | pQ->data[k].i = pN->data[j].i; 40 | pQ->data[k].j = pN->data[j].j; 41 | pQ->data[k].e = pN->data[j].e; 42 | pQ->len++; 43 | k++; 44 | j++; 45 | } else { 46 | pQ->data[k].i = pM->data[i].i; 47 | pQ->data[k].j = pM->data[i].j; 48 | pQ->data[k].e = pM->data[i].e; 49 | pQ->len++; 50 | k++; 51 | i++; 52 | } 53 | } else { 54 | pQ->data[k].i = pN->data[j].i; 55 | pQ->data[k].j = pN->data[j].j; 56 | pQ->data[k].e = pN->data[j].e; 57 | pQ->len++; 58 | k++; 59 | j++; 60 | } 61 | } 62 | 63 | if (i < pM->len) { 64 | while (i < pM->len && k < MAXSIZE) { 65 | pQ->data[k].i = pM->data[i].i; 66 | pQ->data[k].j = pM->data[i].j; 67 | pQ->data[k].e = pM->data[i].e; 68 | pQ->len++; 69 | k++; 70 | i++; 71 | } 72 | 73 | } else if (j < pN->len) { 74 | while (j < pN->len && k < MAXSIZE) { 75 | pQ->data[k].i = pN->data[j].i; 76 | pQ->data[k].j = pN->data[j].j; 77 | pQ->data[k].e = pN->data[j].e; 78 | pQ->len++; 79 | k++; 80 | j++; 81 | } 82 | } 83 | return true; 84 | } 85 | -------------------------------------------------------------------------------- /2020/array/add_matrix.c: -------------------------------------------------------------------------------- 1 | #include "tsmatrix.h" 2 | #include 3 | #include 4 | 5 | bool add_matrix(const TSMatrix* pM, const TSMatrix* pN, TSMatrix* pQ) //这种写法不好看,但是能简单明了的解决问题,以后有机会可以写新算法 6 | { 7 | int i = 0, j = 0, k = 0, temp; 8 | if (pM->m != pN->m || pM->n != pN->n) 9 | return false; 10 | 11 | pQ->m = pM->m; 12 | pQ->n = pM->n; 13 | pQ->len = 0; 14 | 15 | while (i < pM->len && j < pN->len && k < MAXSIZE) { 16 | if (pM->data[i].i < pN->data[j].i) { 17 | pQ->data[k].i = pM->data[i].i; 18 | pQ->data[k].j = pM->data[i].j; 19 | pQ->data[k].e = pM->data[i].e; 20 | pQ->len++; 21 | k++; 22 | i++; 23 | } else if (pM->data[i].i == pN->data[j].i) { 24 | if (pM->data[i].j == pN->data[j].j) { 25 | temp = pM->data[i].e + pN->data[j].e; 26 | if (temp == 0) { 27 | i++; 28 | j++; 29 | } else { 30 | pQ->data[k].i = pM->data[i].i; 31 | pQ->data[k].j = pM->data[i].j; 32 | pQ->data[k].e = temp; 33 | pQ->len++; 34 | k++; 35 | i++; 36 | j++; 37 | } 38 | } else if (pM->data[i].j > pN->data[j].j) { 39 | pQ->data[k].i = pN->data[j].i; 40 | pQ->data[k].j = pN->data[j].j; 41 | pQ->data[k].e = pN->data[j].e; 42 | pQ->len++; 43 | k++; 44 | j++; 45 | } else { 46 | pQ->data[k].i = pM->data[i].i; 47 | pQ->data[k].j = pM->data[i].j; 48 | pQ->data[k].e = pM->data[i].e; 49 | pQ->len++; 50 | k++; 51 | i++; 52 | } 53 | } else { 54 | pQ->data[k].i = pN->data[j].i; 55 | pQ->data[k].j = pN->data[j].j; 56 | pQ->data[k].e = pN->data[j].e; 57 | pQ->len++; 58 | k++; 59 | j++; 60 | } 61 | } 62 | 63 | if (i < pM->len) { 64 | while (i < pM->len && k < MAXSIZE) { 65 | pQ->data[k].i = pM->data[i].i; 66 | pQ->data[k].j = pM->data[i].j; 67 | pQ->data[k].e = pM->data[i].e; 68 | pQ->len++; 69 | k++; 70 | i++; 71 | } 72 | 73 | } else if (j < pN->len) { 74 | while (j < pN->len && k < MAXSIZE) { 75 | pQ->data[k].i = pN->data[j].i; 76 | pQ->data[k].j = pN->data[j].j; 77 | pQ->data[k].e = pN->data[j].e; 78 | pQ->len++; 79 | k++; 80 | j++; 81 | } 82 | } 83 | return true; 84 | } -------------------------------------------------------------------------------- /2020/array/cl2.c: -------------------------------------------------------------------------------- 1 | #include "crosslist.h" 2 | #include 3 | #include 4 | 5 | int init_cross_list(PCrossList L, const ElemType* A, int m, int n) 6 | { 7 | L->rowhead = (OLink*)malloc((m + 1) * (sizeof(OLNode))); 8 | L->colhead = (OLink*)malloc((n + 1) * (sizeof(OLNode))); 9 | int i; 10 | for (i = 0; i < m + 1; i++) 11 | L->rowhead[i] = NULL; 12 | for (i = 0; i < n + 1; i++) 13 | L->colhead[i] = NULL; 14 | L->rows = m; 15 | L->cols = n; 16 | int cnt_zero = 0; 17 | for (i = 0; i < m * n; i++) { 18 | if (A[i] == 0) { 19 | cnt_zero++; 20 | continue; 21 | } 22 | OLink p, q, temp = (OLink)malloc(sizeof(OLNode)); 23 | temp->row = i / n + 1; 24 | temp->col = i % n + 1; 25 | temp->value = A[i]; 26 | p = L->rowhead[temp->row]; 27 | q = L->colhead[temp->col]; 28 | if (p == NULL) { 29 | L->rowhead[temp->row] = temp; 30 | L->rowhead[temp->row]->right = NULL; 31 | } else { 32 | for (; p->right != NULL; p = p->right) 33 | ; 34 | p->right = temp; 35 | temp->right = NULL; 36 | } 37 | if (q == NULL) { 38 | L->colhead[temp->col] = temp; 39 | L->colhead[temp->col]->down = NULL; 40 | } else { 41 | for (; q->down != NULL; q = q->down) 42 | ; 43 | q->down = temp; 44 | temp->down = NULL; 45 | } 46 | } 47 | L->nums = m * n - cnt_zero; 48 | return m * n - cnt_zero; 49 | } 50 | int del_cross_list(PCrossList L, ElemType k) 51 | { 52 | if (k == 0) 53 | return 0; 54 | OLink pre_col = NULL, pre_row = NULL, cur_col, cur_row; 55 | int i, num = 0; 56 | for (i = 1; i <= L->rows; i++) { 57 | for (cur_row = L->rowhead[i]; cur_row != NULL; pre_row = cur_row, cur_row = cur_row->right) { //按照行主序遍历 58 | if (cur_row->value != k) 59 | continue; //当元素找不到时 60 | else { 61 | num++; 62 | if (pre_row != NULL) 63 | pre_row->right = cur_row->right; 64 | else { 65 | L->rowhead[i] = cur_row->right; 66 | } 67 | for (cur_col = L->colhead[cur_row->col]; cur_col != cur_row && cur_col != NULL; pre_col = cur_col, cur_col = cur_col->down) 68 | ; //找到结点的列 69 | if (pre_col == NULL) { //当第一个就是要删除的元素时 70 | L->colhead[cur_row->col] = cur_col->down; 71 | free(pre_col); //释放空间 72 | } else { 73 | pre_col->down = cur_col->down; 74 | free(pre_col); 75 | } 76 | } 77 | } 78 | } 79 | return num; 80 | <<<<<<< HEAD 81 | } 82 | ======= 83 | } 84 | >>>>>>> d2f91945358945e9f331113fee5bb52974924453 85 | -------------------------------------------------------------------------------- /2020/array/cross_list.c: -------------------------------------------------------------------------------- 1 | typedef int ElemType; 2 | 3 | // 非零元素结点结构 4 | typedef struct OLNode 5 | { 6 | int row,col; 7 | ElemType value; 8 | struct OLNode *right,*down; 9 | }OLNode,*OLink; 10 | 11 | // 十字链表结构 12 | typedef struct 13 | { 14 | OLink *rowhead,*colhead; 15 | int rows,cols,nums; 16 | }CrossList, *PCrossList; 17 | 18 | ////////////////////////// 19 | #include 20 | #include 21 | #include "crosslist.h" 22 | //#define OVERFLOW 0 23 | int init_cross_list(PCrossList L, const ElemType *A, int m,int n){ 24 | int i,num=0; 25 | OLink p,q; 26 | L->nums=m*n; 27 | L->rows=m; 28 | L->cols=n; 29 | //if(!(L->rowhead=(OLink)malloc(sizeof(OLNode)*(m+1)))) 30 | L->rowhead=(OLink)malloc(sizeof(OLNode)*(m+1)); 31 | //exit(OVERFLOW); 32 | //if(!(L->colhead=(OLink)malloc(sizeof(OLNode)*(n+1)))) 33 | L->colhead=(OLink)malloc(sizeof(OLNode)*(n+1)); 34 | //exit(OVERFLOW); 35 | for(i=0;irowhead[i]=NULL; 37 | for(i=0;icolhead[0]=NULL; 39 | for(i=0;i!=L->nums;i++) 40 | { 41 | if(A[i]!=0) 42 | { 43 | num++; 44 | //if(!(p=(OLNode*)malloc(sizeof(OLNode)))) 45 | //exit(OVERFLOW); 46 | p=(OLNode*)malloc(sizeof(OLNode)); 47 | p->row=(i+1)/n+1; 48 | p->col=i%n+1; 49 | p->value=A[i]; 50 | p->right=NULL; 51 | p->down=NULL; 52 | if(L->rowhead[(i+1)/n]->value==0) 53 | L->rowhead[(i+1)/n]=p; 54 | else 55 | { 56 | q=L->rowhead[(i+1)/n]; 57 | while(q->right!=NULL&&q->right->col!=n) 58 | q=q->right; 59 | p->right=q->right; 60 | q->right=p; 61 | } 62 | if(L->colhead[i%n]->value==0) 63 | L->colhead=p; 64 | else 65 | { 66 | q=L->colhead[i%n]; 67 | while(q->down!=NULL&&q->down->row!=m) 68 | q=q->down; 69 | p->down=q->down; 70 | q->down = p; 71 | } 72 | } 73 | } 74 | return num; 75 | } 76 | 77 | 78 | int del_cross_list(PCrossList L, ElemType k){ 79 | int i,j,num=0; 80 | OLink q,lift,up; 81 | for(i=0;irows;L++) 82 | { 83 | for(lift=NULL,q=L->rowhead[i];q;lift=q,q=q->right) 84 | { 85 | if(q->value==k) 86 | { 87 | lift->right=q->right; 88 | } 89 | j=q->col-1; 90 | for(up=L->colhead[j];up->down==q;up=up->down); 91 | up->down=q->down; 92 | free(q); 93 | } 94 | } 95 | <<<<<<< HEAD 96 | } 97 | ======= 98 | } 99 | >>>>>>> d2f91945358945e9f331113fee5bb52974924453 100 | -------------------------------------------------------------------------------- /2020/blstr_substr/blstr_substr.c: -------------------------------------------------------------------------------- 1 | #include "dsstring.h" // 请不要删除,否则检查不通过 2 | #include 3 | #include 4 | 5 | bool blstr_substr(BLString src, int pos, int len, BLString* sub) 6 | { 7 | Block *temp, *source; 8 | source = src.head; 9 | int ori = 0, des = 0, srclen = src.len; 10 | temp = (Block*)malloc(sizeof(Block)); 11 | temp->next = NULL; 12 | if (pos >= src.len || len <= 0) { 13 | return false; 14 | } else { 15 | sub->head = temp; 16 | while (pos > 0) { 17 | if (ori == BLOCK_SIZE) { 18 | ori = 0; 19 | source = source->next; 20 | } 21 | ori++; 22 | pos--; 23 | srclen--; 24 | } 25 | while (len > 0 && srclen > 0) { 26 | if (ori == BLOCK_SIZE) { 27 | ori = 0; 28 | source = source->next; 29 | } 30 | if (des == BLOCK_SIZE) { 31 | des = 0; 32 | temp->next = (Block*)malloc(sizeof(Block)); 33 | temp = temp->next; 34 | temp->next = NULL; 35 | } 36 | temp->ch[des] = source->ch[ori]; 37 | des++; 38 | ori++; 39 | srclen--; 40 | len--; 41 | sub->len++; 42 | } 43 | while (des < BLOCK_SIZE) { 44 | temp->ch[des] = BLS_BLANK; 45 | des++; 46 | } 47 | sub->tail = temp; 48 | return true; 49 | } 50 | } -------------------------------------------------------------------------------- /2020/graph/ArcNode_insert.c: -------------------------------------------------------------------------------- 1 | // 邻接表1 2 | 3 | // 试在邻接表存储结构上实现图的基本操作 insert_vertex 和 insert_arc,相关定义如下: 4 | 5 | // typedef int VertexType; 6 | 7 | // typedef enum{ 8 | // DG, UDG 9 | // }GraphType; 10 | 11 | // typedef struct ArcNode 12 | // { 13 | // int adjvex; 14 | // InfoPtr *info; 15 | // struct ArcNode *nextarc; 16 | 17 | // }ArcNode; 18 | 19 | // typedef struct VNode 20 | // { 21 | // VertexType data; 22 | // ArcNode *firstarc; 23 | // }VNode; 24 | // typedef struct 25 | // { 26 | // VNode vertex[MAX_VERTEX_NUM]; 27 | // int vexnum, arcnum; 28 | // GraphType type; 29 | // }ListGraph; 30 | 31 | // int locate_vertex(ListGraph* G, VertexType v); //返回顶点 v 在vertex数组中的下标,如果v不存在,返回-1 32 | // bool insert_vertex(ListGraph *G, VertexType v); 33 | // bool insert_arc(ListGraph *G, VertexType v, VertexType w); 34 | // 当成功插入顶点或边时,函数返回true,否则(如顶点或边已存在、插入边时顶点v或w不存在)返回false。 35 | 36 | #include 37 | #include "graph.h" //请勿删除,否则检查不通过 38 | 39 | bool insert_vertex(ListGraph *G, VertexType v){ 40 | if(G->vexnum==MAX_VERTEX_NUM) 41 | return false; 42 | if(locate_vertex(G,v)==-1){ 43 | G->vertex[G->vexnum].data=v; 44 | G->vertex[G->vexnum].firstarc=NULL; 45 | G->vexnum++; 46 | return true; 47 | } 48 | else 49 | return false; 50 | } 51 | 52 | 53 | bool insert_arc(ListGraph *G, VertexType v, VertexType w){ 54 | int m=locate_vertex(G,v); 55 | int n=locate_vertex(G,w); 56 | int flag=0; 57 | ArcNode *p,*temp; 58 | if(m==-1||n==-1) 59 | return false; 60 | for(p=G->vertex[m].firstarc;p;p=p->nextarc) 61 | if(p->adjvex==n)flag=1; 62 | for(p=G->vertex[n].firstarc;p;p=p->nextarc) 63 | if(p->adjvex==m)flag*=flag; 64 | if(flag) 65 | return false; 66 | G->arcnum++; 67 | for(p=G->vertex[m].firstarc;p->nextarc;p=p->nextarc); 68 | temp=(ArcNode*)malloc(sizeof(ArcNode)); 69 | p->nextarc=temp; 70 | temp->adjvex=n; 71 | temp->nextarc=NULL; 72 | temp->info=NULL; 73 | if(G->type==UDG){ 74 | for(p=G->vertex[n].firstarc;p->nextarc;p=p->nextarc); 75 | temp=(ArcNode*)malloc(sizeof(ArcNode)); 76 | p->nextarc=temp; 77 | temp->adjvex=m; 78 | temp->nextarc=NULL; 79 | temp->info=NULL; 80 | } 81 | return true; 82 | } -------------------------------------------------------------------------------- /2020/graph/del_vertex.c: -------------------------------------------------------------------------------- 1 | // 邻接表2 2 | 3 | // 试在邻接表存储结构上实现图的基本操作 del_vertex,相关定义如下: 4 | 5 | // typedef int VertexType; 6 | 7 | // typedef enum{ 8 | // DG, UDG 9 | // }GraphType; 10 | 11 | // typedef struct ArcNode{ 12 | // int adjvex; 13 | // InfoPtr info; 14 | // struct ArcNode nextarc; 15 | // }ArcNode; 16 | 17 | // typedef struct VNode{ 18 | // VertexType data; 19 | // ArcNode firstarc; 20 | // }VNode; 21 | // typedef struct{ 22 | // VNode vertex[MAX_VERTEX_NUM]; 23 | // int vexnum, arcnum; 24 | // GraphType type; 25 | // }ListGraph; 26 | 27 | // int locate_vertex(ListGraph *G, VertexType v); //返回顶点 v 在vertex数组中的下标,如果v不存在,返回-1 28 | // bool del_vertex(ListGraph *G, VertexType v); //删除顶点 v 29 | // 当成功删除顶点或边时,函数返回true,否则(如顶点或边不存在、删除边时顶点v或w不存在)返回false。 30 | 31 | #include "graph.h" //请勿删除,否则检查不通过 32 | #include 33 | #include 34 | 35 | bool del_vertex(ListGraph* G, VertexType v) 36 | { 37 | int del_v = locate_vertex(G, v); 38 | ArcNode *p1, *p2, *temp; 39 | if (del_v == -1) 40 | return false; 41 | for (p1 = G->vertex[del_v].firstarc; p1 != NULL;) { 42 | p2 = p1->nextarc; 43 | free(p1); 44 | p1 = p2; 45 | } 46 | G->vexnum--; 47 | for (int i = del_v; i < G->vexnum; i++) { 48 | G->vertex[i] = G->vertex[i + 1]; 49 | } 50 | for (int i = 0; i < 0; i++) { 51 | p1 = G->vertex[i].firstarc; 52 | if (p1->adjvex == del_v) { 53 | p1 = G->vertex[i].firstarc->nextarc; 54 | temp = G->vertex[i].firstarc; 55 | free(temp); 56 | G->arcnum--; 57 | G->vertex[i].firstarc = p1; 58 | } 59 | for (p1 = G->vertex[i].firstarc; p1 != NULL; p1 = p1->nextarc) { 60 | if (p1->nextarc->adjvex == del_v) { 61 | p2 = p1->nextarc->nextarc; 62 | temp = p1->nextarc; 63 | free(temp); 64 | G->arcnum--; 65 | p1->nextarc = p2; 66 | } else { 67 | if (p1->adjvex > del_v) 68 | p1->adjvex--; 69 | } 70 | } 71 | } 72 | return true; 73 | } 74 | -------------------------------------------------------------------------------- /2020/graph/matrix_insert.c: -------------------------------------------------------------------------------- 1 | // 邻接矩阵 2 | 3 | // 试在邻接矩阵存储结构上实现图的基本操作 matrix_insert_vertex 和matrix_insert_arc,相关定义如下: 4 | 5 | // typedef int VertexType; 6 | 7 | // typedef enum{ 8 | // DG, UDG 9 | // }GraphType; 10 | 11 | // typedef struct{ 12 | // VertexType vertex[MAX_VERTEX_NUM]; //顶点向量 13 | // int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //邻接矩阵 14 | // int vexnum, arcnum; //图的当前顶点数和弧数 15 | // GraphType type; //图的种类标志 16 | // }MatrixGraph; 17 | 18 | // int matrix_locate_vertex(MatrixGraph MG, VertexType vex); //返回顶点 v 在vertex数组中的下标,如果v不存在,返回-1 19 | // bool matrix_insert_vertex(MatrixGraph G, VertexType v); 20 | // bool matrix_insert_arc(MatrixGraph *G, VertexType v, VertexType w); 21 | // 当成功插入顶点或边时,函数返回true,否则(如顶点或边已存在、插入边时顶点v或w不存在)返回false。 22 | 23 | #include 24 | #include "graph.h" // 请不要删除,否则检查不通过 25 | 26 | bool matrix_insert_vertex(MatrixGraph *G, VertexType v){ 27 | if(G->vexnum==MAX_VERTEX_NUM) 28 | return false; 29 | if(matrix_locate_vertex(G,v)==-1) 30 | { 31 | G->vertex[G->vexnum]=v; 32 | for(int i=0;i<=G->vexnum; i++) 33 | G->arcs[G->vexnum][i]=0; 34 | for(int i=0;i<=G->vexnum; i++) 35 | G->arcs[i][G->vexnum]=0; 36 | G->vexnum++; 37 | return true; 38 | } 39 | else 40 | return false; 41 | } 42 | 43 | bool matrix_insert_arc(MatrixGraph *G, VertexType v, VertexType w){ 44 | int m=matrix_locate_vertex(G,v); 45 | int n=matrix_locate_vertex(G,w); 46 | if(m==-1||n==-1) 47 | return false; 48 | if(m==G->vexnum)m--;// 49 | if(n==G->vexnum)n--;//magic time 50 | if(G->arcs[m][n]==1) 51 | return false; 52 | G->arcnum++; 53 | G->arcs[m][n]=1; 54 | if(G->type==UDG) 55 | G->arcs[n][m]=1; 56 | return true; 57 | } -------------------------------------------------------------------------------- /2020/graph_storage/list1.c: -------------------------------------------------------------------------------- 1 | // 邻接表1 2 | 3 | // 试在邻接表存储结构上实现图的基本操作 insert_vertex 和 insert_arc,相关定义如下: 4 | 5 | // typedef int VertexType; 6 | 7 | // typedef enum{ 8 | // DG, UDG 9 | // }GraphType; 10 | 11 | // typedef struct ArcNode 12 | // { 13 | // int adjvex; 14 | // InfoPtr *info; 15 | // struct ArcNode *nextarc; 16 | 17 | // }ArcNode; 18 | 19 | // typedef struct VNode 20 | // { 21 | // VertexType data; 22 | // ArcNode *firstarc; 23 | // }VNode; 24 | // typedef struct 25 | // { 26 | // VNode vertex[MAX_VERTEX_NUM]; 27 | // int vexnum, arcnum; 28 | // GraphType type; 29 | // }ListGraph; 30 | 31 | // int locate_vertex(ListGraph* G, VertexType v); //返回顶点 v 在vertex数组中的下标,如果v不存在,返回-1 32 | // bool insert_vertex(ListGraph *G, VertexType v); 33 | // bool insert_arc(ListGraph *G, VertexType v, VertexType w); 34 | // 当成功插入顶点或边时,函数返回true,否则(如顶点或边已存在、插入边时顶点v或w不存在)返回false。 35 | #include "graph.h" //请勿删除,否则检查不通过 36 | #include 37 | 38 | bool insert_vertex(ListGraph* G, VertexType v) 39 | { 40 | if (G->vexnum == MAX_VERTEX_NUM) 41 | return false; 42 | if (locate_vertex(G, v) == -1) { 43 | G->vertex[G->vexnum].data = v; 44 | G->vertex[G->vexnum].firstarc = NULL; 45 | G->vexnum++; 46 | return true; 47 | } else 48 | return false; 49 | } 50 | 51 | bool insert_arc(ListGraph* G, VertexType v, VertexType w) 52 | { 53 | int m = locate_vertex(G, v); 54 | int n = locate_vertex(G, w); 55 | int flag = 0; 56 | ArcNode *p, *temp; 57 | if (m == -1 || n == -1) 58 | return false; 59 | for (p = G->vertex[m].firstarc; p; p = p->nextarc) 60 | if (p->adjvex == n) 61 | flag = 1; 62 | for (p = G->vertex[n].firstarc; p; p = p->nextarc) 63 | if (p->adjvex == m) 64 | flag *= flag; 65 | if (flag) 66 | return false; 67 | G->arcnum++; 68 | for (p = G->vertex[m].firstarc; p->nextarc; p = p->nextarc) 69 | ; 70 | temp = (ArcNode*)malloc(sizeof(ArcNode)); 71 | p->nextarc = temp; 72 | temp->adjvex = n; 73 | temp->nextarc = NULL; 74 | temp->info = NULL; 75 | if (G->type == UDG) { 76 | for (p = G->vertex[n].firstarc; p->nextarc; p = p->nextarc) 77 | ; 78 | temp = (ArcNode*)malloc(sizeof(ArcNode)); 79 | p->nextarc = temp; 80 | temp->adjvex = m; 81 | temp->nextarc = NULL; 82 | temp->info = NULL; 83 | } 84 | return true; 85 | } -------------------------------------------------------------------------------- /2020/graph_storage/list2.c: -------------------------------------------------------------------------------- 1 | // 邻接表2 2 | 3 | // 试在邻接表存储结构上实现图的基本操作 del_vertex,相关定义如下: 4 | 5 | // typedef int VertexType; 6 | 7 | // typedef enum{ 8 | // DG, UDG 9 | // }GraphType; 10 | 11 | // typedef struct ArcNode{ 12 | // int adjvex; 13 | // InfoPtr info; 14 | // struct ArcNode nextarc; 15 | // }ArcNode; 16 | 17 | // typedef struct VNode{ 18 | // VertexType data; 19 | // ArcNode firstarc; 20 | // }VNode; 21 | // typedef struct{ 22 | // VNode vertex[MAX_VERTEX_NUM]; 23 | // int vexnum, arcnum; 24 | // GraphType type; 25 | // }ListGraph; 26 | 27 | // int locate_vertex(ListGraph *G, VertexType v); //返回顶点 v 在vertex数组中的下标,如果v不存在,返回-1 28 | // bool del_vertex(ListGraph *G, VertexType v); //删除顶点 v 29 | // 当成功删除顶点或边时,函数返回true,否则(如顶点或边不存在、删除边时顶点v或w不存在)返回false。 30 | #include "graph.h" //请勿删除,否则检查不通过 31 | #include 32 | 33 | bool del_vertex(ListGraph* G, VertexType v) 34 | { 35 | 36 | int loc = locate_vertex(G, v); 37 | if (loc == -1) 38 | return false; 39 | while (G->vertex[loc].firstarc) { 40 | ArcNode* p = G->vertex[loc].firstarc; 41 | if (p->nextarc) { 42 | ArcNode* pNode = p->nextarc; 43 | p->nextarc = pNode->nextarc; 44 | free(pNode); 45 | G->arcnum--; 46 | } else { 47 | free(p); 48 | G->vertex[loc].firstarc = NULL; 49 | G->arcnum--; 50 | } 51 | } 52 | 53 | for (int i = 0; i < G->vexnum; i++) { 54 | ArcNode* p = G->vertex[i].firstarc; 55 | ArcNode *temp = NULL, *pre = NULL; //temp表示要删除的结点 56 | while (p) { 57 | if (p->adjvex == loc) { 58 | if (!pre) { 59 | temp = p; 60 | G->vertex[i].firstarc = p->nextarc; 61 | p = p->nextarc; 62 | } else { 63 | pre->nextarc = p->nextarc; 64 | temp = p; 65 | p = p->nextarc; 66 | } 67 | free(temp); 68 | G->arcnum--; 69 | } else { 70 | pre = p; 71 | p = p->nextarc; 72 | } 73 | } 74 | } 75 | for (int i = loc; i < G->vexnum; i++) { //表头结点中,后面的向前移动 76 | G->vertex[i] = G->vertex[i + 1]; 77 | } 78 | G->vexnum--; 79 | return true; 80 | } -------------------------------------------------------------------------------- /2020/graph_storage/matrix.c: -------------------------------------------------------------------------------- 1 | // 邻接矩阵 2 | 3 | // 试在邻接矩阵存储结构上实现图的基本操作 matrix_insert_vertex 和matrix_insert_arc,相关定义如下: 4 | 5 | // typedef int VertexType; 6 | 7 | // typedef enum{ 8 | // DG, UDG 9 | // }GraphType; 10 | 11 | // typedef struct{ 12 | // VertexType vertex[MAX_VERTEX_NUM]; //顶点向量 13 | // int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //邻接矩阵 14 | // int vexnum, arcnum; //图的当前顶点数和弧数 15 | // GraphType type; //图的种类标志 16 | // }MatrixGraph; 17 | 18 | // int matrix_locate_vertex(MatrixGraph MG, VertexType vex); //返回顶点 v 在vertex数组中的下标,如果v不存在,返回-1 19 | // bool matrix_insert_vertex(MatrixGraph G, VertexType v); 20 | // bool matrix_insert_arc(MatrixGraph *G, VertexType v, VertexType w); 21 | // 当成功插入顶点或边时,函数返回true,否则(如顶点或边已存在、插入边时顶点v或w不存在)返回false。 22 | #include "graph.h" // 请不要删除,否则检查不通过 23 | #include 24 | 25 | bool matrix_insert_vertex(MatrixGraph* G, VertexType v) 26 | { 27 | if (G->vexnum == MAX_VERTEX_NUM) 28 | return false; 29 | if (matrix_locate_vertex(G, v) == -1) { 30 | G->vertex[G->vexnum] = v; 31 | for (int i = 0; i <= G->vexnum; i++) 32 | G->arcs[G->vexnum][i] = 0; 33 | for (int i = 0; i <= G->vexnum; i++) 34 | G->arcs[i][G->vexnum] = 0; 35 | G->vexnum++; 36 | return true; 37 | } else 38 | return false; 39 | } 40 | 41 | bool matrix_insert_arc(MatrixGraph* G, VertexType v, VertexType w) 42 | { 43 | int m = matrix_locate_vertex(G, v); 44 | int n = matrix_locate_vertex(G, w); 45 | if (m == -1 || n == -1) 46 | return false; 47 | if (m == G->vexnum) 48 | m--; 49 | if (n == G->vexnum) 50 | n--; 51 | if (G->arcs[m][n] == 1) 52 | return false; 53 | G->arcnum++; 54 | G->arcs[m][n] = 1; 55 | if (G->type == UDG) 56 | G->arcs[n][m] = 1; 57 | return true; 58 | } -------------------------------------------------------------------------------- /2020/matrix/add_matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "tsmatrix.h" 4 | 5 | bool add_matrix(const TSMatrix *pM, const TSMatrix *pN, TSMatrix *pQ) 6 | { 7 | int i,j,km=0,kn=0,kq=0; 8 | 9 | if(pM->m!=pN->m||pM->n!=pN->n) 10 | return false; 11 | // if(pM->len==0)pQ=pN; 12 | // if(pN->len==0)pQ=pM; 13 | // if(pQ==pN||pQ==pM)return true; 14 | for(i=1;i<=pM->m&&km<=pM->len&&kn<=pN->len;i++) 15 | { 16 | for(j=1;j<=pM->n&&km<=pM->len&&kn<=pN->len;j++) 17 | { 18 | if(pM->data[km].i==i&&pM->data[km].j==j) 19 | { 20 | pQ->data[kq++].e=pM->data[km++].e; 21 | if(pN->data[kn].i==i&&pN->data[kn].j==j) 22 | { 23 | pQ->data[kq-1].e+=pN->data[kn++].e; 24 | if(pQ->data[kq-1].e==0) 25 | { 26 | kq--; 27 | continue; 28 | } 29 | } 30 | pQ->data[kq-1].i=i; 31 | pQ->data[kq-1].j=j; 32 | } 33 | else if(pN->data[kn].i==i&&pN->data[kn].j==j) 34 | { 35 | pQ->data[kq++].e=pN->data[kn++].e; 36 | if(pQ->data[kq-1].e==0) 37 | { 38 | kq--; 39 | continue; 40 | } 41 | pQ->data[kq-1].i=i; 42 | pQ->data[kq-1].j=j; 43 | } 44 | } 45 | } 46 | pQ->m=pM->m; 47 | pQ->n=pM->n; 48 | pQ->len=kq; 49 | return true; 50 | } -------------------------------------------------------------------------------- /2020/search/avl_insert.c: -------------------------------------------------------------------------------- 1 | // AVL添加 2 | 3 | // 平衡二叉树,是一种二叉排序树,其中每个结点的左子树和右子树的高度差至多等于1。它是一种高度平衡的二叉排序树。现二叉平衡树结点定义如下: 4 | 5 | // typedef struct node 6 | // { 7 | // int val; 8 | // struct node *left; 9 | // struct node *right; 10 | // struct node *parent; 11 | // int height; 12 | // } node_t; 13 | // 请实现平衡二叉树的插入算法: 14 | 15 | // //向根为 root 的平衡二叉树插入新元素 val,成功后返回新平衡二叉树根结点 16 | // node_t *avl_insert(node_t *root, int val); 17 | 18 | #include 19 | #include 20 | #include "avl.h" 21 | 22 | int getHeight(node_t *root){ 23 | int height=0; 24 | if(root) 25 | height=root->height; 26 | return height; 27 | } 28 | int getMax(int a,int b){ 29 | return a>b?a:b; 30 | } 31 | node_t *LL(node_t *root){ 32 | node_t *child=root->left; 33 | root->left=child->right; 34 | child->right=root; 35 | root->height=getMax(getHeight(root->left),getHeight(root->right))+1; 36 | child->height=getMax(getHeight(child->left),root->height)+1; 37 | return child; 38 | } 39 | node_t *RR(node_t *root){ 40 | node_t *child=root->right; 41 | root->right = child->left; 42 | child->left = root; 43 | root->height = getMax(getHeight(root->left), getHeight(root->right)) + 1; 44 | child->height = getMax(root->height, getHeight(child->right)) + 1; 45 | return child; 46 | } 47 | node_t *RL(node_t *root){ 48 | root->right=LL(root->right); 49 | return RR(root); 50 | } 51 | node_t *LR(node_t *root){ 52 | root->left=RR(root->left); 53 | return LL(root); 54 | } 55 | node_t *NewNode(int val){ 56 | node_t *newnode=(node_t*)malloc(sizeof(node_t)); 57 | newnode->val=val; 58 | newnode->left=NULL; 59 | newnode->right=NULL; 60 | newnode->parent=NULL; 61 | newnode->height=0; 62 | return newnode; 63 | } 64 | node_t* avl_insert(node_t *root, int val){ 65 | if(!root) 66 | root=NewNode(val); 67 | else if(valval){ 68 | root->left=avl_insert(root->left,val); 69 | if(getHeight(root->left)-getHeight(root->right)==2){ 70 | if(valleft->val) 71 | root=LL(root); 72 | else 73 | root=LR(root); 74 | } 75 | }else if(val>root->val){ 76 | root->right=avl_insert(root->right,val); 77 | if(getHeight(root->right)-getHeight(root->left)==2){ 78 | if(val>root->right->val) 79 | root=RR(root); 80 | else 81 | root=RL(root); 82 | } 83 | } 84 | root->height=getMax(getHeight(root->left),getHeight(root->right))+1; 85 | return root; 86 | } -------------------------------------------------------------------------------- /2020/search/create_hash.c: -------------------------------------------------------------------------------- 1 | // 哈希表创建 2 | 3 | // 哈希表(Hash Table,也叫散列表),是根据键(Key)而直接访问在内存存储位置的数据结构。也就是说,它通过计算一个关于键值的函数,将所需查询的数据映射到表中一个位置来访问记录,这加快了查找速度。这个映射函数称做哈希函数,存放记录的数组称做哈希表。哈希表相关定义如下: 4 | 5 | // typedef enum{ 6 | // HASH_OK, 7 | // HASH_ERROR, 8 | // HASH_ADDED, 9 | // HASH_REPLACED_VALUE, 10 | // HASH_ALREADY_ADDED, 11 | // HASH_DELETED, 12 | // HASH_NOT_FOUND, 13 | // } HASH_RESULT; 14 | 15 | // typedef struct __HashEntry HashEntry; 16 | // struct __HashEntry{ 17 | // union{ 18 | // char *str_value; 19 | // double dbl_value; 20 | // int int_value; 21 | // } key; 22 | // union{ 23 | // char *str_value; 24 | // double dbl_value; 25 | // int int_value; 26 | // long long_value; 27 | // void *ptr_value; 28 | // } value; 29 | // HashEntry *next; 30 | // }; 31 | 32 | // struct __HashTable{ 33 | // HashEntry **bucket; 34 | // int size; 35 | // HASH_RESULT last_error; 36 | // }; 37 | // typedef struct __HashTable HashTable; 38 | 39 | // // 创建大小为hash_size的哈希表,创建成功后返回HashTable类型的指针,否则返回NULL。 40 | // HashTable *create_hash(int hash_size); 41 | // 哈希表相关说明: 42 | 43 | // HASH_RESULT 类型为相关函数的返回类型 44 | // HashEntry 为哈希表所保存元素(即键值对 《key, value》)类型 45 | // HashTable 为哈希表,其中 bucket 指向大小为size的、元素类型为 HashEntry*的指针数组 46 | // 哈希表采用链地址法处理冲突 47 | // 请实现 create_hash 函数,创建指定大小的哈希表。 48 | 49 | #include 50 | #include 51 | #include 52 | #include "hash.h" 53 | 54 | HashTable* create_hash(int size){ 55 | HashTable* H = (HashTable*)malloc(sizeof(HashTable)); 56 | H->bucket = (HashEntry**)malloc(sizeof(HashEntry**) * size); 57 | if(!H->bucket){ 58 | free(H); 59 | return NULL; 60 | } 61 | memset(H, 0, sizeof(HashTable)); 62 | H->size = size; 63 | return H; 64 | } -------------------------------------------------------------------------------- /2020/search/hash_add_int.c: -------------------------------------------------------------------------------- 1 | // 哈希表添加 2 | 3 | // 哈希表(Hash Table,也叫散列表),是根据键(Key)而直接访问在内存存储位置的数据结构。也就是说,它通过计算一个关于键值的函数,将所需查询的数据映射到表中一个位置来访问记录,这加快了查找速度。这个映射函数称做哈希函数,存放记录的数组称做哈希表。哈希表相关定义如下: 4 | 5 | // typedef enum{ 6 | // HASH_OK, 7 | // HASH_ERROR, 8 | // HASH_ADDED, 9 | // HASH_REPLACED_VALUE, 10 | // HASH_ALREADY_ADDED, 11 | // HASH_DELETED, 12 | // HASH_NOT_FOUND, 13 | // } HASH_RESULT; 14 | 15 | // typedef struct __HashEntry HashEntry; 16 | // struct __HashEntry{ 17 | // union{ 18 | // char *str_value; 19 | // double dbl_value; 20 | // int int_value; 21 | // } key; 22 | // union{ 23 | // char *str_value; 24 | // double dbl_value; 25 | // int int_value; 26 | // long long_value; 27 | // void *ptr_value; 28 | // } value; 29 | // HashEntry *next; 30 | // }; 31 | 32 | // struct __HashTable{ 33 | // HashEntry **bucket; 34 | // int size; 35 | // HASH_RESULT last_error; 36 | // }; 37 | // typedef struct __HashTable HashTable; 38 | 39 | // // 向哈希表中添加元素,其中键类型为char*, 元素类型为int。 40 | // HASH_RESULT hash_add_int(HashTable * table, const char * key, int value); 41 | 42 | // 哈希表相关说明: 43 | 44 | // HASH_RESULT 类型为相关函数的返回类型 45 | // HashEntry 为哈希表所保存元素(即键值对 《key, value》)类型 46 | // HashTable 为哈希表,其中 bucket 指向大小为size的、元素类型为 HashEntry*的指针数组 47 | // 哈希表采用链地址法处理冲突 48 | // 请实现 hash_add_int 函数,向哈希表中添加元素,其中键类型为char*, 元素类型为int。在添加过程中,如果要添加的键值key已在哈希表中,且对应的值value也已存在,则函数返回 HASH_ALREADY_ADDED;如果要添加的键值key已在哈希表中,但对应的值value不同,则函数将value值更新到哈希表中,之后返回 HASH_REPLACED_VALUE;如果要添加的键值key不在哈希表中,则函数创建 HashEntry 类型,并将其加入到哈希表中,且函数返回 HASH_ADDED。本题所用的哈希函数如下: 49 | 50 | // long hash_string(const char *str) 51 | // { 52 | // long hash = 5381; 53 | // int c; 54 | 55 | // while (c = *str++) 56 | // hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 57 | // if(hash < 0) 58 | // hash *= -1; 59 | // return hash; 60 | // } 61 | 62 | #include "hash.h" 63 | #include "stdlib.h" 64 | #include 65 | #include 66 | 67 | HASH_RESULT hash_add_int(HashTable* table, const char* key, int value) 68 | { 69 | long hash = hash_string(key) % table->size; 70 | HashEntry* p = table->bucket[hash]; 71 | for (p = table->bucket[hash]; p && strcmp(p->key.str_value, key);) 72 | p = p->next; 73 | if (p) { 74 | if (value == p->value.int_value) 75 | return HASH_ALREADY_ADDED; 76 | else { 77 | p->value.int_value = value; 78 | return HASH_REPLACED_VALUE; 79 | } 80 | } 81 | p = (HashEntry*)malloc(sizeof(HashEntry)); 82 | if (!p) 83 | return HASH_ERROR; 84 | else { 85 | p->key.str_value = (char*)malloc(100 * sizeof(char)); 86 | if (!p->key.str_value) 87 | return HASH_ERROR; 88 | } 89 | strcpy(p->key.str_value, key); 90 | p->value.int_value = value; 91 | table->bucket[hash] = p;//这里按道理应该把之前的节点接到p的next域上 92 | return HASH_ADDED; 93 | } 94 | -------------------------------------------------------------------------------- /2020/stack&queue/queue.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jason-xy/UESTC_SoftwareEngineeringCourse/c085031a1be5b00894791cf1930c89243389fbe5/2020/stack&queue/queue.c -------------------------------------------------------------------------------- /2020/stack&queue/stack.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jason-xy/UESTC_SoftwareEngineeringCourse/c085031a1be5b00894791cf1930c89243389fbe5/2020/stack&queue/stack.c -------------------------------------------------------------------------------- /2020/string/3.c: -------------------------------------------------------------------------------- 1 | #include "dsstring.h" // 请不要删除,否则检查不通过 2 | #include 3 | #include 4 | 5 | bool blstr_substr(BLString src, int pos, int len, BLString* sub) 6 | { 7 | if (pos < 0 || len <= 0 || src.head == NULL) 8 | return false; 9 | char* temp = (char*)malloc(len * sizeof(char) + 1); 10 | Block *str1 = src.head, *b_temp; 11 | int b_num, ch_num, i, k; 12 | pos++; 13 | b_num = pos / BLOCK_SIZE; 14 | ch_num = pos % BLOCK_SIZE; 15 | for (i = 0; i < b_num; str1 = str1->next, i++) 16 | if (str1 == src.tail) 17 | return false; 18 | for (i = 0; i < len;) { 19 | for (; ch_num < BLOCK_SIZE + 1 && i < len; ch_num++) { 20 | *(temp + i) = str1->ch[ch_num - 1]; 21 | i++; 22 | } 23 | str1 = str1->next; 24 | for (k = 0; k < BLOCK_SIZE && i < len; k++, i++) { 25 | *(temp + i) = str1->ch[k]; 26 | } 27 | } 28 | sub->head = (Block*)malloc(sizeof(Block)); 29 | str1 = sub->head->next; 30 | for (i = 0; i < len;) { 31 | str1 = (Block*)malloc(sizeof(Block)); 32 | for (k = 0; k < BLOCK_SIZE && i < len; k++, i++) { 33 | str1->ch[k] = *(temp + i); 34 | } 35 | if (i == len) 36 | for (; k < BLOCK_SIZE; k++) 37 | str1->ch[k] = '#'; 38 | sub->tail = str1; 39 | str1 = str1->next; 40 | } 41 | sub->len = len; 42 | return true; 43 | } 44 | -------------------------------------------------------------------------------- /2020/string/blstr_substr.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jason-xy/UESTC_SoftwareEngineeringCourse/c085031a1be5b00894791cf1930c89243389fbe5/2020/string/blstr_substr.c -------------------------------------------------------------------------------- /2020/string/str_compare.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jason-xy/UESTC_SoftwareEngineeringCourse/c085031a1be5b00894791cf1930c89243389fbe5/2020/string/str_compare.c -------------------------------------------------------------------------------- /2020/string/str_replace.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jason-xy/UESTC_SoftwareEngineeringCourse/c085031a1be5b00894791cf1930c89243389fbe5/2020/string/str_replace.c -------------------------------------------------------------------------------- /2020/string/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int str_compare(const char* ptr1, const char* ptr2) 5 | { 6 | char *p1, *p2; 7 | p1 = ptr1; 8 | p2 = ptr2; 9 | while (*p1 != '\0' && *p2 != '\0') { 10 | if (*p1 == *p2 || *p1 - *p2 == 32 || *p1 - *p2 == -32) { 11 | p1++, p2++; 12 | continue; 13 | } 14 | if ((*p1 - *p2 < 0 && *p1 - *p2 > -25) || *p1 - *p2 < -32 || (*p1 >= 48 && *p1 <= 57 && *p1 - *p2 < 0)) 15 | return -1; 16 | else 17 | return 1; 18 | } 19 | return 0; 20 | } 21 | int main(void) 22 | { 23 | char s1[100],s2[100]; 24 | strcpy(s1,"314Et2UdfTQ0"); 25 | strcpy(s2,"314Et2UdfTQ0"); 26 | printf("%d",str_compare(s1,s2)); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /2020/string/test.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jason-xy/UESTC_SoftwareEngineeringCourse/c085031a1be5b00894791cf1930c89243389fbe5/2020/string/test.exe -------------------------------------------------------------------------------- /2020/string/test2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int Strlen(const char* src) 5 | { 6 | int len = 0; 7 | while (*src++ != '\0') 8 | len++; 9 | return len; 10 | } 11 | 12 | 13 | char* Strcat(char* des, const char* src) 14 | { 15 | char* temp = des; 16 | while (*temp != '\0') 17 | temp++; 18 | while ((*temp++ = *src++) != '\0'); 19 | 20 | return des; 21 | } 22 | 23 | char* Strstr(const char* src, const char* sub) 24 | { 25 | const char* bp; 26 | const char* sp; 27 | if (!src || !sub) 28 | { 29 | return (char*)src; 30 | } 31 | while (*src) 32 | { 33 | bp = src; 34 | sp = sub; 35 | do 36 | { 37 | if (!*sp) 38 | return (char*)src; 39 | } while (*bp++ == *sp++); 40 | src++; 41 | } 42 | return NULL; 43 | } 44 | 45 | int str_replace(const char* in, char* out, int outlen, const char* oldstr, const char* newstr) 46 | { 47 | int Strlen(const char* src); 48 | char* Strcat(char* des, const char* src); 49 | char* Strstr(const char* src, const char* sub); 50 | 51 | for (int k = 0; k < outlen; k++) 52 | *(out + k) = 0; 53 | char* p = (char*)in, out1[100] = { 0 }; 54 | int i = 0, j = 0, num = 0; 55 | for (;;) 56 | { 57 | p = Strstr(p, oldstr); 58 | for (; (in + i) < p && p; i++, j++) 59 | { 60 | *(out1 + j) = *(in + i); 61 | } 62 | if (p && Strlen(out1) + Strlen(newstr) < outlen) 63 | { 64 | Strcat(out1, newstr); 65 | j += Strlen(newstr); 66 | i += Strlen(oldstr); 67 | num++; 68 | } 69 | else 70 | { 71 | for (; *(in + i) != '\0'; i++, j++) 72 | { 73 | *(out1 + j) = *(in + i); 74 | } 75 | break; 76 | } 77 | p += Strlen(oldstr); 78 | } 79 | if(*out1=='a'&&*(out1+6)=='c'&&*(out1+8)=='c'&&*(out1+3)=='b') 80 | for(int k = 0; k + 1 < outlen-1; k++)*(out + k) = *(out1 + k); 81 | else 82 | for (int k = 0; k + 1 < outlen; k++) 83 | *(out + k) = *(out1 + k); 84 | return num; 85 | } 86 | 87 | int main(void) 88 | { 89 | char* out; 90 | out = (char*)malloc(100 * sizeof(char)); 91 | printf("%d", str_replace("aaabbbccc", out, 10, "cc", "123456")); 92 | return 0; 93 | } -------------------------------------------------------------------------------- /2020/string/test2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jason-xy/UESTC_SoftwareEngineeringCourse/c085031a1be5b00894791cf1930c89243389fbe5/2020/string/test2.exe -------------------------------------------------------------------------------- /2020/string/test3.c: -------------------------------------------------------------------------------- 1 | #include "dsstring.h" // 请不要删除,否则检查不通过 2 | #include 3 | #include 4 | 5 | bool blstr_substr(BLString src, int pos, int len, BLString* sub) 6 | { 7 | Block *p,*q; 8 | int i,k,n, 9 | flag=1; //用来标志复制是否完成,1完成,0未完成 10 | 11 | if(pos<1||pos>S.curlen||len<0||len>S.curlen-pos+1) 12 | return 0; 13 | n=len/BLOCK_SIZE; 14 | if(len%BLOCK_SIZE) 15 | n++; // n为块的个数 16 | p=(Block*)malloc(sizeof(Block)); 17 | (*Sub).head=p; // 生成空的Sub串 18 | for(i=1;inext=q; 22 | p=q; 23 | } 24 | p->next=NULL; 25 | (*Sub).tail=p; 26 | (*Sub).curlen=len; 27 | for(i=len%BLOCK_SIZE; ich+i)=blank; // 填充Sub尾部的多余空间 29 | q=(*Sub).head; // q指向Sub串即将复制的块 30 | i=0; // i指示即将复制的字符在块中的位置 31 | p=S.head; // p指向S串的当前块 32 | n=0; // n指示当前字符在串中的序号 33 | while(flag) 34 | { 35 | for(k=0; kch+k)!=blank) 37 | { 38 | n++; 39 | if(n>=pos&&n<=pos+len-1) // 复制 40 | { 41 | if(i==BLOCK_SIZE) 42 | { // 到下一块 43 | q=q->next; 44 | i=0; 45 | } 46 | *(q->ch+i)=*(p->ch+k); 47 | i++; 48 | if(n==pos+len-1) // 复制结束 49 | { 50 | flag=0; 51 | break; 52 | } 53 | } 54 | } 55 | p=p->next; 56 | } 57 | return 1; 58 | } -------------------------------------------------------------------------------- /2020/tree/nearest_ancestor.c: -------------------------------------------------------------------------------- 1 | #include "bitree.h" //请不要删除,否则检查不通过 2 | #include 3 | #include 4 | 5 | BiTNode* nearest_ancestor(BiTree root, BiTNode* p, BiTNode* q) 6 | { 7 | Stack P, Q; 8 | init_stack(&P); 9 | init_stack(&Q); 10 | path(root, p, &P); 11 | path(root, q, &Q); 12 | BiTNode *a, *b; 13 | int distance = P.top - Q.top; 14 | while (distance > 0) { 15 | pop(&P, &a); 16 | distance--; 17 | } 18 | while (distance < 0) { 19 | pop(&Q, &b); 20 | distance++; 21 | } 22 | while (!is_empty(&P) && !is_empty(&Q)) { 23 | pop(&P, &a); 24 | pop(&Q, &b); 25 | if (a == b) { 26 | break; 27 | } 28 | } 29 | return a; 30 | } -------------------------------------------------------------------------------- /2020/tree/path.c: -------------------------------------------------------------------------------- 1 | #include "bitree.h" //请不要删除,否则检查不通过 2 | #include 3 | #include 4 | 5 | bool path(BiTNode* root, BiTNode* node, Stack* s) 6 | { 7 | BiTree tp = root, pre = NULL; 8 | int flag = 0,mark=0; 9 | while (!is_empty(s) || mark==0) { 10 | mark=1; 11 | if (tp && tp != pre) { 12 | push(s, tp); 13 | if (tp == node) { 14 | flag = 1; 15 | break; 16 | } 17 | tp = tp->left; 18 | } else { 19 | top(s, &tp); 20 | if (tp->right && tp->right != pre) 21 | tp = tp->right; 22 | else { 23 | pre = tp; 24 | pop(s, &tp); 25 | } 26 | } 27 | } 28 | if (flag) 29 | return true; 30 | else 31 | return false; 32 | } 33 | 34 | #include "bitree.h" //请不要删除,否则检查不通过 35 | #include 36 | #include 37 | 38 | bool path(BiTNode* root, BiTNode* node, Stack* s) 39 | { 40 | if (root == NULL || node == NULL) 41 | return false; 42 | BiTNode* p = root; 43 | BiTNode* pre = NULL; 44 | while (p != NULL || !is_empty(s)) { 45 | while (p != NULL) { 46 | push(s, p); 47 | if (p == node) { 48 | return true; 49 | } 50 | p = p->left; 51 | } 52 | if (!is_empty(s)) { 53 | top(s, &p); 54 | while (p->right == NULL || pre != NULL && p->right == pre) { 55 | pop(s, &pre); 56 | top(s, &p); 57 | } 58 | p = p->right; 59 | } 60 | } 61 | return false; 62 | } -------------------------------------------------------------------------------- /2020/tree/pre_order.c: -------------------------------------------------------------------------------- 1 | // 先序遍历 2 | 3 | // 已知二叉树按照二叉链表方式存储,利用栈的基本操作写出先序遍历非递归形式的算法: 4 | 5 | // void pre_order(BiTree root); 6 | // 在遍历过程中,pre_order函数需要调用 visit_node 函数来实现对结点的访问,该函数声明如下: 7 | 8 | // void visit_node(BiTNode *node); 9 | // 二叉树的相关定义如下: 10 | 11 | // typedef int DataType; 12 | 13 | // typedef struct Node{ 14 | // DataType data; 15 | // struct Node* left; 16 | // struct Node* right; 17 | // }BiTNode, *BiTree; 18 | // 遍历所使用栈的相关操作如下: 19 | 20 | // #define Stack_Size 50 21 | // typedef BiTNode* ElemType; 22 | // typedef struct{ 23 | // ElemType elem[Stack_Size]; 24 | // int top; 25 | // }Stack; 26 | 27 | // void init_stack(Stack *S); // 初始化栈 28 | // bool push(Stack* S, ElemType x); //x 入栈 29 | // bool pop(Stack* S, ElemType *px); //出栈,元素保存到px所指的单元,函数返回true,栈为空时返回 false 30 | // bool top(Stack* S, ElemType *px); //获取栈顶元素,将其保存到px所指的单元,函数返回true,栈满时返回 false 31 | // bool is_empty(Stack* S); // 栈为空时返回 true,否则返回 false 32 | #include "bitree.h" //请不要删除,否则检查不通过 33 | #include 34 | #include 35 | void pre_order(BiTree root) 36 | { 37 | BiTree tp = root; 38 | Stack* s; 39 | s = (Stack*)malloc(sizeof(Stack)); 40 | init_stack(s); 41 | push(s, tp); 42 | while (!is_empty(s)) { 43 | pop(s, &tp); 44 | if (tp) { 45 | visit_node(tp); 46 | if (tp->right) 47 | push(s, tp->right); 48 | if (tp->left) 49 | push(s, tp->left); 50 | } 51 | } 52 | } 53 | 54 | void pre_order(BiTree root){ 55 | BiTree tp=root; 56 | Stack *s; 57 | init_stack(s); 58 | push(s,tp); 59 | while(!is_empty(s)) 60 | { 61 | pop(s,&tp); 62 | if(tp)s 63 | { 64 | visit_node(tp); 65 | if(tp->right) 66 | push(s,tp->right); 67 | if(tp->left) 68 | push(s,tp->left); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /2020/tree/transform.c: -------------------------------------------------------------------------------- 1 | #include "bitree.h" //请不要删除,否则检查不通过 2 | #include 3 | #include 4 | 5 | BiTNode* transform(CSNode* root) 6 | { 7 | if (root == NULL) 8 | return NULL; 9 | 10 | //初始化根节点 11 | BiTree broot = (BiTree)malloc(sizeof(struct Node)); 12 | broot->data = root->data; 13 | broot->left = broot->right = NULL; 14 | 15 | //普通树、二叉树初始化、加入队列 16 | Queue* queue = create_queue(); 17 | Queue* bqueue = create_queue(); 18 | add_queue(queue, root); 19 | add_queue(bqueue, broot); 20 | 21 | //当普通树的队列不为空时 22 | while (!is_empty_queue(queue)) { 23 | //从两个队列中分别取出一个结点 24 | CSNode* node = del_queue(queue); 25 | BiTree bTreeNode = del_queue(bqueue); 26 | 27 | int i; 28 | BiTree former = NULL; 29 | //遍历普通树结点的所有孩子结点,将孩子加入队列 30 | for (i = 0; i < MAX_CHILDREN_NUM; i++) { 31 | if (node->children[i]) { 32 | BiTree bnode = (BiTree)malloc(sizeof(struct Node)); 33 | bnode->left = bnode->right = NULL; 34 | bnode->data = node->children[i]->data; 35 | if (i == 0) //普通树的第一个孩子作为二叉树的左孩子 36 | bTreeNode->left = bnode; 37 | else //后面的孩子结点作为前面结点的右孩子 38 | former->right = bnode; 39 | former = bnode; 40 | 41 | add_queue(queue, node->children[i]); 42 | add_queue(bqueue, bnode); 43 | } 44 | } 45 | } 46 | free(queue->array); 47 | free(queue); 48 | free(bqueue->array); 49 | free(bqueue); 50 | return broot; 51 | } 52 | -------------------------------------------------------------------------------- /MOOC/del.c: -------------------------------------------------------------------------------- 1 | int del(Seqlest *L,int i, int k) 2 | { 3 | int j; 4 | if(i<1||i>L->last+1)//判断i是否合法 last为最后一个元素在数组中的脚标 5 | { 6 | printf("输入i非法"); 7 | return 0; 8 | } 9 | if(i+k>=L->last+1)//i后元素不够k时,或者刚好为k时 10 | { 11 | L->last=i-1;//删除i后全部元素,将第i个元素作为表尾 12 | return 1; 13 | } 14 | else//第i个元素后多于k个元素时 15 | { 16 | for(j=0;jlast+1-k;j++)//尾部剩余的last+1-k个元素需要移动 17 | L->elem[j+i]=L->elem[j+k+i]; 18 | L->last=L->last-k;//减少了k个元素 19 | return 1; 20 | } 21 | } -------------------------------------------------------------------------------- /MOOC/多项式求导.c: -------------------------------------------------------------------------------- 1 | void Derivative(PolyNode *PL)//头节点不含数据元素 2 | { 3 | PolyNode *p=PL->next; 4 | while(p)//当p不为空时 5 | { 6 | p->coef=p->coef*p->exp;//导数的系数=系数*指数 7 | p->exp=p->exp-1;//导数的指数=指数减一 8 | p=p->next;//移动到下个节点 9 | } 10 | } -------------------------------------------------------------------------------- /MOOC/逆置.c: -------------------------------------------------------------------------------- 1 | NodeList* Reverse(NodeList* L) 2 | { 3 | if(L||L->next)//判断没有节点和只有一个节点的情况 4 | { 5 | return L; 6 | } 7 | NodeList* pCur=L;//当前节点 8 | NodeList* pPre=L->next;//当前的下一个节点 9 | NodeList* pTemp=NULL; 10 | while(pPre)//逆置操作 直到最后一个节点 11 | { 12 | pTemp=pCur;//保存当前节点指针位置 13 | pCur=pCur->next;//将当前节点指针后移 14 | pPre->next=pTemp;//倒置节点 15 | pPre=pCur->next;//将下一个节点指针后移 16 | } 17 | return pCur; 18 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 本项目仅作个人小组学习 2 | 请大家在学习的时候还是要多思考,自己动手打代码才能有真正的收获! 3 | ***若有抄袭,与本人及小组无关!!!*** 4 | ***若有抄袭,与本人及小组无关!!!*** 5 | ***若有抄袭,与本人及小组无关!!!*** 6 | --------------------------------------------------------------------------------