Featured image of post 数据结构课设——一元多项式计算器

数据结构课设——一元多项式计算器

数据结构课程设计

源代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <locale.h>

#define MAX_TERMS 20  // 最大项数
#define MAX_LENTH_OF_LINE 1024  // 记事本一行最多字符数
#define MAX_POLYS 100 // 多项式最大个数
#define FILENAME "polysFile.txt"    // 需读取文件的名称
#define FILENAME_OUTPUT "polysRusult.txt"   // 需写入文件的名称

int SerialNumber;      // 全局变量,用于写入文件的序号

// 多项式项的结构体
typedef struct PolyNode
{
    int coef; // 系数
    int exp;  // 指数
    struct PolyNode* next;
} PolyNode;

PolyNode* createPoly();                                                 // 创建一个空的多项式
void insertTerm(PolyNode* poly, int coef, int exp);                     // 插入多项式项到链表中,按指数降序排列
void simplifyPoly(PolyNode *poly);                                      // 合并同类项
void freePoly(PolyNode *poly);                                          // 释放多项式链表的内存
void printPoly(PolyNode* poly, int flag);                               // 打印多项式
void printAllPoly(PolyNode* poly[],int polysNum);                       // 输出现有的全部多项式
void readPolyFromFile(const char* filename, PolyNode* poly, int line);  // 从文件中读取多项式
void writePolyToFile(PolyNode* poly, const char* filename);             // 将多项式写入文件
void inputPoly(PolyNode* poly);                                         // 输入一个新的多项式
void deletePoly(PolyNode* poly[],int polySum);                          // 删除一个已存在的多项式
void addItem(PolyNode* poly[],int polysNum);                            // 添加单项
PolyNode* addPoly(PolyNode* poly[],int polysNum);                       // 多项式加法
PolyNode* subtractPoly(PolyNode* poly[],int polysNum);                  // 多项式减法
PolyNode* multiplyPoly(PolyNode* poly[],int polysNum);                  // 多项式乘法
PolyNode* polyDerivative(PolyNode* poly[],int polysNum);                // 多项式求导
int evaluatePoly(PolyNode* poly[],int polysNum);                        // 计算多项式在x处的值
int Cchoice(int maxChoice);                                             // 菜单选择界面
int initSerialNumber();                                                 // 读取结果文件中的编号
int main();

// 创建一个空的多项式
PolyNode* createPoly()
{
    PolyNode* head = (PolyNode*)malloc(sizeof(PolyNode));
    head->next = NULL;
    return head;
}

// 插入多项式项到链表中,按指数降序排列
void insertTerm(PolyNode* poly, int coef, int exp)
{
    if(coef == 0) return;
    PolyNode* newTerm = (PolyNode*)malloc(sizeof(PolyNode));
    newTerm->coef = coef;
    newTerm->exp = exp;
    newTerm->next = NULL;

    PolyNode* current = poly;
    while (current->next != NULL && current->next->exp > exp)
    {
        current = current->next;
    }

    if (current->next != NULL && current->next->exp == exp)
    {
        // 如果指数相同,合并项
        current->next->coef += coef;
        free(newTerm);
    }
    else
    {
        newTerm->next = current->next;
        current->next = newTerm;
    }
}

// 合并多项式中的同类项
void simplifyPoly(PolyNode *poly) {
    PolyNode *current = poly->next;

    while (current != NULL) {
        PolyNode *nextTerm = current->next;
        PolyNode *prevTerm = current;

        if (prevTerm->coef == 0)
        {
            free(prevTerm);
            prevTerm = nextTerm;
            nextTerm = prevTerm->next;
        }

        while (nextTerm != NULL) {
            if (current->exp == nextTerm->exp) {
                // 合并同类项
                current->coef += nextTerm->coef;
                prevTerm->next = nextTerm->next;
                free(nextTerm);
                nextTerm = prevTerm->next;
            } else {
                prevTerm = nextTerm;
                nextTerm = nextTerm->next;
            }
        }
        current = current->next;
    }
}

// 释放多项式链表的内存
void freePoly(PolyNode *poly) {
    PolyNode *current = poly;

    while (current != NULL) {
        PolyNode* nextNode = current->next;
        free(current);
        current = nextNode;
    }
}

// 打印多项式,输出格式:系数x^指数,如果指数为0,则输出系数;如果指数为1,输出x
void printPoly(PolyNode* poly,int flag)
{
    PolyNode* current = poly->next;
    if (current == NULL)
    {
        printf("多项式不存在。\n");
        return;
    }

    while (current != NULL)
    {
        if (current->coef > 0 && current != poly->next)
        {
            printf("+");
        }

        // 系数为0,不存在的项
        if (current -> coef == 0)
        {
            current = current->next;
            continue;
        }
        // 处理x^0的情况
        if (current->exp == 0)
        {
            printf("%d", current->coef);
        }
        // 处理-1x^1的情况
        else if (current->exp == 1&&current->coef == -1)
        {
            printf("-x");
        }
        // 处理1x^1的情况
        else if (current->exp == 1&&current->coef == 1)
        {
            printf("x");
        }
        // 处理其他x^1
        else if (current->exp == 1)
        {
            printf("%dx", current->coef);
        }
        // 处理系数为1的情况
        else if (current->coef == 1)
        {
            printf("x^%d", current->exp);
        }
        // 处理系数为-1的情况
        else if (current->coef == -1)
        {
            printf("-x^%d", current->exp);
        }
        // 其他指数的情况
        else
        {
            printf("%dx^%d", current->coef, current->exp);
        }

        current = current->next;
    }
    if (flag == 1)
    printf("\n");
}

// 输出现有的全部多项式
void printAllPoly(PolyNode* poly[],int polysNum)
{
    int i;
    printf("当前已有的%d个多项式如下:\n", polysNum);
    for (i = 1; i <= polysNum; i++)
    {
        printf("%d:",i);
        printPoly(poly[i-1],1);
    }
}

// 从文件中读取多项式
void readPolyFromFile(const char* filename, PolyNode* poly, int line)
{
    FILE* file = fopen(filename, "r");
    if (file == NULL)
    {
        printf("无法打开文件 %s\n", filename);
        return;
    }
    // 从指定第line行(line从1开始)开始读取
    for (int i = 1; i < line; i++)
    {
        char line[MAX_LENTH_OF_LINE];
        fgets(line, MAX_LENTH_OF_LINE, file);
    }
    int coef = 0, exp = 0;
    char ch;
    while ((ch = fgetc(file)) != EOF)
    {
        // 检查是否遇到换行符,遇到换行符自动停止读取
        if (ch == '\n')
        {
            break;
        }

        //第一项系数为1的情况
        if (ch == 'x')
        {
            ch = fgetc(file);
            if (ch == '^')
            {
                fscanf(file, "%d", &exp);
            }
            else
            {
                ungetc(ch, file);
                exp = 1;
            }
            insertTerm(poly, 1, exp);
        }

        // 检查是否遇到数字或符号(+ 或 -),开始读取系数
        if (ch == '+' || ch == '-')
        {
            if (ch == '+') coef = 1;
            else coef = -1;
            ungetc(ch, file);
            if (!fscanf(file, "%d", &coef))
            {
                ch = fgetc(file);
                if (ch == 'x')
                {
                    ch = fgetc(file);
                    if (ch == '^')
                    {
                        fscanf(file, "%d", &exp); // 如果后面跟着^,则读取指数
                    }
                    else
                    {
                        ungetc(ch, file); // 如果没有^,说明指数是1
                        exp = 1;
                    }
                    insertTerm(poly, coef, exp);
                    continue;
                }
            }
        }

        if (isdigit(ch))
        {
            ungetc(ch, file);
            fscanf(file, "%d", &coef);
        }

        // 继续读取字符
        ch = fgetc(file);

        // 处理是否包含'x',并决定指数
        if (ch == 'x')
        {
            ch = fgetc(file);
            if (ch == '^')
            {
                fscanf(file, "%d", &exp); // 如果后面跟着^,则读取指数
            }
            else
            {
                ungetc(ch, file); // 如果没有^,说明指数是1
                exp = 1;
            }
        }
        else
        {
            ungetc(ch, file); // 如果没有x,说明是常数项,指数为0
            exp = 0;
        }

        // 将解析的项插入到多项式中
        insertTerm(poly, coef, exp);
    }

    fclose(file);
}

// 将多项式写入文件,格式:系数x^指数,如果指数为0,则输出系数;如果指数为1,输出x
void writePolyToFile(PolyNode* poly, const char* filename)
{
    FILE* file = fopen(filename, "a");
    if (file == NULL)
    {
        printf("无法打开文件 %s\n", filename);
        return;
    }

    PolyNode* current = poly->next;
    int isFirst = 1;
    while (current != NULL)
    {
        // 如果不是第一个项并且系数大于0,则需要添加 +
        if (!isFirst && current->coef > 0)
        {
            fprintf(file, "+");
        }

        // 系数为0,不写入
        if (current->coef == 0)
        {
            current = current->next;
            continue;
        }

        // 写入系数和指数
        if (current->exp == 0)
        {
            fprintf(file, "%d", current->coef); // 如果指数为0,直接输出系数
        }
        else if (current->exp == 1)
        {
            if (current->coef == 1)
                fprintf(file, "x");              // 如果指数、系数为1,输出 "x"
            fprintf(file, "%dx", current->coef); // 如果指数为1,输出 "coefx"
        }
        else if (current->coef == 1)
        {
            fprintf(file, "x^%d", current->exp); // 如果系数为1,输出x^exp
        }
        else if (current->coef == -1)
        {
            if (current->exp == 1)
                fprintf(file, "-x");                    // 如果系数为-1,指数为1,输出-x
            else fprintf(file, "-x^%d", current->exp); // 如果系数为-1,输出-x^exp
        }
        else
        {
            fprintf(file, "%dx^%d", current->coef, current->exp); // 其他情况输出 "coef x^exp"
        }

        current = current->next;
        isFirst = 0;
    }

    fclose(file);
}

// 多项式加法
PolyNode* addPoly(PolyNode* poly[],int polysNum)
{
    PolyNode* result = createPoly();
    int ch1,ch2;
    printAllPoly(poly,polysNum);
    printf("输入要相加的两个多项式(两个数字中间由空格隔开,输入-1取消):\n");
    scanf("%d %d",&ch1,&ch2);
    if (ch1 == -1||ch2 == -1) return NULL;
    PolyNode *p1 = poly[ch1-1]->next, *p2 = poly[ch2-1]->next;

    while (p1 != NULL && p2 != NULL)
    {
        if (p1->exp > p2->exp)
        {
            insertTerm(result, p1->coef, p1->exp);
            p1 = p1->next;
        }
        else if (p1->exp < p2->exp)
        {
            insertTerm(result, p2->coef, p2->exp);
            p2 = p2->next;
        }
        else
        {
            int sum = p1->coef + p2->coef;
            if (sum != 0)
            {
                insertTerm(result, sum, p1->exp);
            }
            p1 = p1->next;
            p2 = p2->next;
        }
    }

    // 插入剩余项
    while (p1 != NULL)
    {
        insertTerm(result, p1->coef, p1->exp);
        p1 = p1->next;
    }
    while (p2 != NULL)
    {
        insertTerm(result, p2->coef, p2->exp);
        p2 = p2->next;
    }

    printf("结果如下:");
    printf("(");
    printPoly(poly[ch1-1],0);
    printf(") + (");
    printPoly(poly[ch2-1],0);
    printf(") = ");
    printPoly(result,1);
        printf("是否将其写入结果文件(%s)中?y/n",FILENAME_OUTPUT);
    fflush(stdin);
    if (getchar() == 'y'||getchar() == 'Y')
    {
        FILE* file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, "%d.多项式加法:(", SerialNumber);
        fclose(file);
        writePolyToFile(poly[ch1-1],FILENAME_OUTPUT);
        file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, ") + (");
        fclose(file);
        writePolyToFile(poly[ch2-1],FILENAME_OUTPUT);
        file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, ") = ");
        fclose(file);
        writePolyToFile(result,FILENAME_OUTPUT);
        file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, "\n");
        fclose(file);
        SerialNumber++;
        printf("写入成功!\n");
        system("pause");
    }

    return result;
}

// 多项式减法
PolyNode* subtractPoly(PolyNode* poly[],int polysNum)
{
    PolyNode* result = createPoly();
    int ch1,ch2;
    printAllPoly(poly,polysNum);
    printf("输入要相减的两个多项式(两个数字中间由空格隔开,输入-1取消):\n");
    scanf("%d %d",&ch1,&ch2);
    if (ch1 == -1||ch2 == -1) return NULL;
    PolyNode *p1 = poly[ch1-1]->next, *p2 = poly[ch2-1]->next;
    while (p1 != NULL && p2 != NULL)
    {
        if (p1->exp > p2->exp)
        {
            insertTerm(result, p1->coef, p1->exp);
            p1 = p1->next;
        }
        else if (p1->exp < p2->exp)
        {
            insertTerm(result, -p2->coef, p2->exp);
            p2 = p2->next;
        }
        else
        {
            int diff = p1->coef - p2->coef;
            if (diff != 0)
            {
                insertTerm(result, diff, p1->exp);
            }
            p1 = p1->next;
            p2 = p2->next;
        }
    }

    // 插入剩余项
    while (p1 != NULL)
    {
        insertTerm(result, p1->coef, p1->exp);
        p1 = p1->next;
    }
    while (p2 != NULL)
    {
        insertTerm(result, -p2->coef, p2->exp);
        p2 = p2->next;
    }


    printf("结果如下:");
    printf("(");
    printPoly(poly[ch1-1],0);
    printf(") - (");
    printPoly(poly[ch2-1],0);
    printf(") = ");
    printPoly(result,1);
    printf("是否将其写入结果文件(%s)中?y/n",FILENAME_OUTPUT);
    fflush(stdin);
    if (getchar() == 'y'||getchar() == 'Y')
    {
        FILE* file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, "%d.多项式减法:(", SerialNumber);
        fclose(file);
        writePolyToFile(poly[ch1-1],FILENAME_OUTPUT);
        file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, ") - (");
        fclose(file);
        writePolyToFile(poly[ch2-1],FILENAME_OUTPUT);
        file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, ") = ");
        fclose(file);
        writePolyToFile(result,FILENAME_OUTPUT);
        file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, "\n");
        fclose(file);
        SerialNumber++;
        printf("写入成功!\n");
        system("pause");
    }

    return result;
}

// 多项式乘法
PolyNode* multiplyPoly(PolyNode* poly[], int polysNum)
{
    PolyNode* result = createPoly();
    int ch1,ch2;
    printAllPoly(poly,polysNum);
    printf("输入要相乘的两个多项式(两个数字中间由空格隔开,输入-1取消):\n");
    scanf("%d %d",&ch1,&ch2);
    if (ch1 == -1||ch2 == -1) return NULL;
    PolyNode *p1 = poly[ch1-1]->next, *p2 = poly[ch2-1]->next;
    PolyNode *tem = p2;
    while (p1 != NULL)
    {
        p2 = tem;
        while (p2 != NULL)
        {
            int coef = p1->coef * p2->coef;
            int exp = p1->exp + p2->exp;
            insertTerm(result, coef, exp);
            p2 = p2->next;
        }
        p1 = p1->next;
    }

    printf("结果如下:");
    printf("(");
    printPoly(poly[ch1-1],0);
    printf(") * (");
    printPoly(poly[ch2-1],0);
    printf(") = ");
    printPoly(result,1);
    printf("是否将其写入结果文件(%s)中?y/n",FILENAME_OUTPUT);
    fflush(stdin);
    if (getchar() == 'y'||getchar() == 'Y')
    {
        FILE* file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, "%d.多项式乘法:(", SerialNumber);
        fclose(file);
        writePolyToFile(poly[ch1-1],FILENAME_OUTPUT);
        file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, ") * (");
        fclose(file);
        writePolyToFile(poly[ch2-1],FILENAME_OUTPUT);
        file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, ") = ");
        fclose(file);
        writePolyToFile(result,FILENAME_OUTPUT);
        file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, "\n");
        fclose(file);
        SerialNumber++;
        printf("写入成功!\n");
        system("pause");
    }

    return result;
}

// 计算多项式在x处的值
int evaluatePoly(PolyNode* poly[], int polysNum)
{

    printAllPoly(poly,polysNum);
    printf("输入要计算的多项式:(范围1~%d,输入0或-1取消)",polysNum);
    fflush(stdin);
    int ch;
    scanf("%d",&ch);
    if (ch == 0||ch == -1) return 0;

    int result = 0;
    PolyNode* current = poly[ch-1]->next;
    fflush(stdin);
    int x;
    printf("输入需要计算的x的值:");
    scanf("%d",&x);

    while (current != NULL)
    {
        int termValue = current->coef;
        for (int i = 0; i < current->exp; i++)
        {
            termValue *= x;
        }
        result += termValue;
        current = current->next;
    }

    printf("结果如下:\n");
    printf("当x = %d 时,",x);
    printPoly(poly[ch-1],0);
    printf("  =  %d",result);
    printf("是否将其写入结果文件(%s)中?y/n",FILENAME_OUTPUT);
    fflush(stdin);
    if (getchar() == 'y'||getchar() == 'Y')
    {
        FILE* file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, "%d.多项式求值: 当x = %d 时,", SerialNumber,x);
        fclose(file);
        writePolyToFile(poly[ch-1],FILENAME_OUTPUT);
        file = fopen(FILENAME_OUTPUT, "a");
        fprintf(file, "  =  %d\n",result);
        fclose(file);
        SerialNumber++;
        printf("写入成功!\n");
        system("pause");
    }

    return result;
}

// 输入一个新的多项式
void inputPoly(PolyNode* poly)
{
    fflush(stdin);  //清空输入缓存区
    int coef=0, exp;
    char input[100];
            printf("输入的多项式格式为:系数+(空格)+指数,或者系数x^指数(支持书面写法,例如: x ->系数,指数均为1),每次输入其中一项,输入0 0结束\n");
            while (1)
            {
                fgets(input, sizeof(input), stdin); // 获取用户输入
                // 去除输入字符串末尾的换行符
                input[strcspn(input, "\n")] = 0;

                // 如果输入是 "0 0",结束输入
                if (strcmp(input, "0 0") == 0)
                {
                    printf("你输入的多项式为:");
                    printPoly(poly,1);
                    system("pause");
                    break;
                }

                // 尝试解析 "系数x^指数" 格式
                if (strchr(input, 'x') != NULL)
                {
                    int coef, exp;
                    if (sscanf(input, "%dx^%d", &coef, &exp) == 2)
                    {
                        insertTerm(poly, coef, exp);
                    }
                    else if (sscanf(input, "%dx", coef) == 1)
                    {
                        insertTerm(poly, coef, 1);
                    }
                    else if (sscanf(input, "%d", coef) == 1)
                    {
                        insertTerm(poly, coef, 0);
                    }
                    else if (sscanf(input, "x^%d", exp) == 1)
                    {
                        insertTerm(poly, 1, exp);
                    }
                    else if (sscanf(input, "-x^%d", exp) == 1)
                    {
                        insertTerm(poly, -1, exp);
                    }
                    else if (input[0] == 'x')
                    {
                        insertTerm(poly, 1, 1);
                    }
                    else
                    {
                        printf("输入格式错误!请重新输入。\n");
                    }
                }
                // 尝试解析 "系数 指数" 格式
                else if (sscanf(input, "%d %d", &coef, &exp) == 2)
                {
                    if (coef == 0 && exp == 0)
                    {
                        break;
                    }
                    insertTerm(poly, coef, exp);
                }
                else
                {
                    printf("输入格式错误!请重新输入。\n");
                }
            }
}

// 多项式求导
PolyNode* polyDerivative(PolyNode* poly[],int polysNum)
{
    PolyNode* result = createPoly();
    int ch;
    printAllPoly(poly,polysNum);
    printf("输入要求导的多项式(输入0或-1取消):\n");
    fflush(stdin);
    scanf("%d",&ch);
    if (ch == -1||ch == 0) return NULL;
    PolyNode *p1 = poly[ch-1]->next;
    while (p1 != NULL)
    {
        if (p1->exp == 0)   // 系数为0,求导无结果
        {
            p1 = p1->next;
            continue;
        }
        // 系数不为0的情况
        int coef = p1->coef * p1->exp;
        int exp = p1->exp - 1;
        insertTerm(result, coef, exp);
        p1 = p1->next;
    }

    printf("结果如下:");
    printPoly(poly[ch-1],0);
    printf("  ->  ");
    printPoly(result,1);
    printf("是否将其写入结果文件(polysRusult.txt)中?y/n");
    fflush(stdin);
    if (getchar() == 'y'||getchar() == 'Y')
    {
        FILE* file = fopen("polysRusult.txt", "a");
        fprintf(file, "%d.多项式求导:", SerialNumber);
        fclose(file);
        writePolyToFile(poly[ch-1],FILENAME_OUTPUT);
        file = fopen("polysRusult.txt", "a");
        fprintf(file, " -> ");
        fclose(file);
        writePolyToFile(result,FILENAME_OUTPUT);
        file = fopen("polysRusult.txt", "a");
        fprintf(file, "\n");
        fclose(file);
        SerialNumber++;
        printf("写入成功!\n");
        system("pause");
    }

    return result;
}

// 菜单选择界面
int Cchoice(int maxChoice)
{
    int input;
    printf("请输入你的选择:范围(1~%d),输入0返回",maxChoice);
    fflush(stdin);
    scanf("%d",&input);
    while (input < 0 || input > maxChoice)
    {
        fflush(stdin);
        printf("输入的选项有误!请重新输入:范围(1~%d),输入0返回/n",maxChoice);
        scanf("%d",&input);
    }
    return input;
}

// 删除一个已存在的多项式
void deletePoly(PolyNode* poly[],int polySum)
{
    fflush(stdin);
    printAllPoly(poly, polySum);
    int choice=Cchoice(polySum);
    for (int i = choice-1; i < polySum-1; i++)
    {
        poly[i] = poly[i+1];
    }
}

// 读取结果文件的编号
int initSerialNumber()
{
    int result = 0;
    char line[MAX_LENTH_OF_LINE];
    FILE* file = fopen(FILENAME_OUTPUT, "r");
    if (file == NULL)
        return 0;
    while (fgets(line, sizeof(line), file) != NULL)
    {
        int number;
        if (sscanf(line, "%d", &number) == 1)
            result = number;
    }
    fclose(file);
    return result;
}

// 添加单项
void addItem(PolyNode* poly[],int polysNum)
{
    int coef,exp;
    printAllPoly(poly,polysNum);
    int ch = Cchoice(polysNum);
    if (ch == 0) return;
    char input[100];
    printf("输入的多项式格式为:系数+(空格)+指数,或者系数x^指数(支持书面写法,例如: x ->系数,指数均为1),每次输入其中一项,输入0 0结束\n");
    fflush(stdin);
            while (1)
            {
                fgets(input, sizeof(input), stdin); // 获取用户输入
                // 去除输入字符串末尾的换行符
                input[strcspn(input, "\n")] = 0;

                // 如果输入是 "0 0",结束输入
                if (strcmp(input, "0 0") == 0)
                {
                    printf("修改后的多项式为:");
                    printPoly(poly[ch-1],1);
                    system("pause");
                    break;
                }

                // 尝试解析 "系数x^指数" 格式
                if (strchr(input, 'x') != NULL)
                {
                    int coef, exp;
                    if (sscanf(input, "%dx^%d", &coef, &exp) == 2)
                    {
                        insertTerm(poly[ch-1], coef, exp);
                    }
                    else if (sscanf(input, "%dx", coef) == 1)
                    {
                        insertTerm(poly[ch-1], coef, 1);
                    }
                    else if (sscanf(input, "%d", coef) == 1)
                    {
                        insertTerm(poly[ch-1], coef, 0);
                    }
                    else if (sscanf(input, "x^%d", exp) == 1)
                    {
                        insertTerm(poly[ch-1], 1, exp);
                    }
                    else if (sscanf(input, "-x^%d", exp) == 1)
                    {
                        insertTerm(poly[ch-1], -1, exp);
                    }
                    else if (input[0] == 'x')
                    {
                        insertTerm(poly[ch-1], 1, 1);
                    }
                    else
                    {
                        printf("输入格式错误!请重新输入。\n");
                    }
                }
                // 尝试解析 "系数 指数" 格式
                else if (sscanf(input, "%d %d", &coef, &exp) == 2)
                {
                    if (coef == 0 && exp == 0)
                    {
                        break;
                    }
                    insertTerm(poly[ch-1], coef, exp);
                }
                else
                {
                    printf("输入格式错误!请重新输入。\n");
                }
            }
}

int main()
{
    here:
    int i;  // i代表已存在的多项式的个数
    PolyNode* poly[MAX_POLYS];
    char inputFile[20] = FILENAME;
    char outputFile[20] = FILENAME_OUTPUT;
    // 如果文件不存在则建立文件
    FILE *file;
    if (!(file = fopen(inputFile, "r")))
        file = fopen(inputFile, "w");
    if (!(file = fopen(outputFile, "r")))
        file = fopen(outputFile, "w");
    fclose(file);
    SerialNumber = initSerialNumber() + 1;
    // 在文本中读取多项式
    for (i = 1; i <= MAX_POLYS; i++)
    {
        poly[i-1] = createPoly();
        readPolyFromFile(inputFile,poly[i-1],i);
        if (poly[i-1]->next == NULL)
        {
            free(poly[i-1]);
            i--;
            break;
        }
    }
    while (1)
    {
        system("cls");
        printf("--------------------------------------------\n");
        printf("               一元多项式计算器                \n");
        printf("--------------------------------------------\n");
        printf("已存在%d个多项式\n",i);
        printf("1.查看已存在的多项式\n");
        printf("2.输入一个新的多项式\n");
        printf("3.删除一个多项式\n");
        printf("4.多项式的加法\n");
        printf("5.多项式的减法\n");
        printf("6.多项式的乘法\n");
        printf("7.多项式求导\n");
        printf("8.求多项式在x处的值\n");
        printf("9.在已有的多项式中添加单项\n");
        printf("0.退出系统\n");
        printf("*:重启系统\n");
        printf("--------------------------------------------\n");
        printf("请输入你的选择:\n");
        char choice;
        fflush(stdin);  //清除输入缓存区
        scanf("%c",&choice);
        switch (choice)
        {
            case '1':
                system("cls");
                printAllPoly(poly,i);
                system("pause");
                break;
            case '2':
                system("cls");
                poly[i]=createPoly();
                inputPoly(poly[i]);
                i++;
                break;
            case '3':
                system("cls");
                deletePoly(poly,i);
                i--;
                break;
            case '4':
                system("cls");
                poly[i] = createPoly();
                if (poly[i] = addPoly(poly,i))    i++;
                break;
            case '5':
                system("cls");
                poly[i]=createPoly();
                if (poly[i] = subtractPoly(poly,i))    i++;
                break;
            case '6':
                system("cls");
                poly[i]=createPoly();
                if (poly[i] = multiplyPoly(poly,i))    i++;
                break;
            case '7':
                system("cls");
                poly[i]=createPoly();
                if (poly[i] = polyDerivative(poly,i))    i++;
                break;
            case '8':
                system("cls");
                evaluatePoly(poly,i);
                break;
            case '9':
                system("cls");
                addItem(poly,i);
                break;
            case '0':
                exit(0);
            case '*':
                system("cls");
                goto here;
            default:
                printf("输入的选项不存在,请重新输入!\n");
                system("pause");
        }
    }
    return 0;

}

程序说明书

课程设计说明书.doc

答辩PPT(较简陋)

程序设计基础课程设计.pptx

使用 Hugo 构建
主题 StackJimmy 设计
math: 数据结构课设——一元多项式计算器