关于#数据结构#的问题,请各位专家解答!

题目 1——应用实验 利用线性表实现一个通讯录管理,通信录的数据格式如下:

typedef struct
{ 
    int ID;         //编号 
    int classID;    //班级
    char name[32];  //姓名 
    char ch;        //性别 
    char phone[13]; //电话 
    char addr[32];  //地址
} StudentInfo;
要求: 
➢ 实现通讯录的建立、增加、删除、修改、查询等功能 
➢ 能够实现简单的菜单交互,即可以根据用户输入的命令,选择不同的操作。 
➢ 能够保存每次更新的数据 
➢ 能够进行分类,比如班级类、好友类
➢ 编写测试 main()函数测试线性表的正确性
```c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define WHITE_FRIEND 1
#define BLACK_FRIEND 2
typedef struct
{ 
    int ID;         //编号 
    int classID;    //班级
    char name[32];  //姓名 
    char ch;        //性别 
    char phone[13]; //电话 
    char addr[32];  //地址
} StudentInfo;

typedef struct
{
    int classID;         // 班级编号
    char classname[32];  // 班级名称
    char otherinfo[64];  // 其他信息
}ClassInfo;

typedef struct
{
    int firstID;   // 相互好友1
    int secondID;  // 相互好友2
    //int friendID; 
    int type;      // 1 好友; // 2 黑名单;
}RelationShip;

/**
 * @brief 读写文件示例
 * 
 * @param pCommand 
 **/
 void WriteFile(/*参数自定*/)
 {
     FILE *f;
     char DataBuffer[1024];
     f = fopen("StudentInfo.csv", "a+");
     if (f == NULL) return;
     int i;
    for(i = 100; i < 150; i++)
     {
        //memset(DataBuffer, 0x00, sizeof(DataBuffer));
        sprintf(DataBuffer, "%d,%d,%s,%c,%s,%s\n", i, i + 100, "姓名", 'F', "电话", "地址");
         //fwrite(DataBuffer, sizeof(char), strlen(DataBuffer), f);
         fputs(DataBuffer, f);
     }
     fclose(f);
 }
/**
 * @brief 读写文件示例
 * 
 * @param pCommand 
 **/
 void ReadFile(/*参数自定*/)
 {
     FILE *f;
    char DataBuffer[1024];
    f = fopen("StudentInfo.csv", "r+");
    if (f == NULL) return;
    int i;
    StudentInfo s;
    for (i = 100; i < 150; i++)
    {
        //fread(DataBuffer, sizeof(char), sizeof(DataBuffer), f);
        fgets(DataBuffer, 100, f);
        //printf("%s\n", DataBuffer);
        s.ID = atoi(strtok(DataBuffer, ","));
        // strtok 字符分割
        s.classID = atoi(strtok(NULL, ","));
        // strcpy 字符拷贝
        strcpy(s.name, strtok(NULL, ","));
        // 字符串数组的第一个字符
        s.ch = *(strtok(NULL, ","));
        strcpy(s.phone, strtok(NULL, ","));
        strcpy(s.addr, strtok(NULL, ","));
        printf("学生ID=%d\t班级ID=%d\t学生姓名=%s\t学生性别=%s\t学生电话=%s\t学生地址=%s\n",
                s.ID, s.classID, s.name, (s.ch == 'F')?"女":"男", s.phone, s.addr);
        //   C语言中的三项式(逻辑表达式)?"真返回值1":"假返回值2"
    }
    fclose(f);
 }

/**
 * @brief 
 * 
 * @param pCommand 
 **/
void DsiplayMenu(int &pCommand)
{
    printf("功能菜单:\n");
    printf("1:增加联系人\n");
    printf("2:删除联系人\n");
    printf("3:修改联系人\n");
    printf("4:查询:\n");
    printf("5:显示学生信息\n");
    printf("9: 退出\n");
    scanf("%d", &pCommand);
}
/**
 * @brief 
 * 
 * @param pCommand 
 **/
void SearchMenu(int &pCommand)
{
    printf("查询菜单:\n");
    printf("1:根据姓名查询一个学生\n");
    printf("2:根据班级查询所有学生\n");
    printf("3:根据电话查询一个学生\n");
    printf("4:查询学生的所有好友:\n");
    printf("9: 返回上一级菜单\n");
    scanf("%d", &pCommand);
}
/**
 * @brief  查询函数
 * 
 * @param pCommand 
 **/
void SearchFun()
{
    int command;
    while(1)
    {
        system("cls");
        SearchMenu(command);
        switch(command)
        {
            case 1:
                            
                break;
            case 2:
                            
                break;
            
        }
        if (command == 9) break;
    }
}
/**
 * @brief  编辑好友关系
 * 
 * @param pCommand 
 **/
void RelationMenu(int &pCommand)
{
    printf("学生关系菜单:\n");
    printf("1:显示好友信息:\n");
    printf("2:显示黑名单信息:\n");
    printf("3:增加好友:\n");
    printf("4:删除好友:\n");
    printf("5:增加黑名单\n");
    printf("6:删除黑名单:\n");
    printf("9: 返回上一级菜单\n");
    scanf("%d", &pCommand);
}
void DisplayStudentInfo()
{
    int command;
    int studentID;
    printf("输入学生ID:\n");
    scanf("%d", &studentID);
    // 显示学生信息
     
    // 显示学生好友,黑名单信息
    
     while(1)
     {
         system("cls");
         RelationMenu(command);
         switch(command)
         {
             case 1:
                             
                 break;
             case 2:
                             
                 break;
                 
         }
         if (command == 9) break;
     }
}
int main()
{
    int command;
    // 初始化学生信息数组
    // 初始化班级数组
    // 初始化学生关系数组
    WriteFile();
    ReadFile();
    while(1)
    {
        //system("cls");
        DsiplayMenu(command);
        switch (command) {
            case 1:
                
                break;
            case 2:
                
                break;
            case 4:
                SearchFun();                            
                break;
            case 5:
                DisplayStudentInfo();                            
                break;
        }
        if (command == 9) break;
    }
    return 0;
}

题目 2——应用实验
利用线性表实现一个一元多项式
Polynomial f(x) = a0 + a1x + a2x2 + a3x3 + … + anxn
提示:
Polynomial 的结点结构如下:
struct term {
float coef; //系数
int expn; //指数
}; 可以使用链表实现,也可以使用顺序表实现。
要求:
➢ 能够实现一元多项式的输入和输出
➢ 能够进行一元多项式相加
➢ 能够进行一元多项式相减
➢ 能够计算一元多项式在 x 处的值
➢ 能够计算一元多项式的导数
➢ 能够进行一元多项式相乘
➢ 编写测试 main()函数测试线性表的正确性

```c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct term 
{ 
    float coef; //系数 
    int expn; //指数
}PolynomialItem;

int InputData(PolynomialItem &pData)
{
    //pData.coef;
    // pData.expn;
    return 0;
}

// 建立多项式
int BuildPolynomialData(/*此处参数类型根据自己的设计 &pDataArr*/)
{
    int n;
    PolynomialItem item;
    printf("输入建立多项式的项数:");
    scanf("%d", &n);
    // 开始循环读取数据 while 或 for
    //
    //    InputData(item);
    //
    
    // 调用显示函数
    return 0;
}

// 显示多项式
int DisplayPolynomialData(/*此处参数类型根据自己的设计 pDataArr*/)
{

    return 0;
}

int PolynomialAdd(/*此处参数类型根据自己的设计 pDataArrIn1, pDataArrIn2, &pDataArrOut*/)
{
    // 两个输入一个输出
    // 调用显示函数
    return 0;
}

int PolynomialSub(/*此处参数类型根据自己的设计 pDataArrIn1, pDataArrIn2, &pDataArrOut*/)
{
    // 两个输入一个输出
    // 调用显示函数
    return 0;
}

int PolynomialValue(/*此处参数类型根据自己的设计 pDataArr*/)
{
    // 一个输入,获取一个x值
    int x_value;
    printf("输入x的值:");
    scanf("%d", &x_value);
    // 调用显示函数
    return 0;
}
int PolynomialDerivative(/*此处参数类型根据自己的设计 pDataArrIn, &pDataArrOut*/)
{
    // 一个输入一个输出
    // 调用显示函数
    return 0;
}

int PolynomialMult(/*此处参数类型根据自己的设计 pDataArrIn1, pDataArrIn2, &pDataArrOut*/)
{
    // 两个输入一个输出
    // 调用显示函数
    return 0;
}
void DsiplayMenu(int &pCommand)
{
    printf("多项式菜单:\n");
    printf("1:多项式加法\n");
    printf("2:多项式减法\n");
    printf("3:多项式求值x\n");
    printf("4:多项式求导:\n");
    printf("5:多项式乘法\n");
    printf("9: 退出\n");
    scanf("%d", &pCommand);
}

int main()
{
    int command;
    // 建立两个多项式
    while(1)
    {
        system("cls");
        DsiplayMenu(command);
        switch (command) {
            case 1: // 多项式加法
                
                break;
            case 2: // 多项式减法
                
                break;
            case 3: // 多项式求值
                            
                break;
            case 4: // 多项式导数
                            
                break;
            case 5: // 多项式乘法
                                        
                break;
        }
        if (command == 9) break;
    }
    return 0;
}

题目 3——应用实验
用链表实现大整数加减法操作: 32 位机器直接操作的数据最大为 32 个 bit,若超过 32bit,则需要单独设计算法。在这 里,可以用链表每个结点存储大整数的每一位的十进制数字,则可以进行大整数的算数运算, 该实验仅实现加减法操作。
要求:
1.随机产生 2 个 1~50 位的数字串,并存储到 2 个链表中。
2.进行加法操作,结果存储到新的链表中。
3.打印运算结果。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>

int getRandomValue(/*此处参数类型根据自己的设计 &pBigNumer*/)
{
    int numbercnt, reVal, i;
    char numbercontent[50], tmp[10];
    srand((unsigned int)time(NULL));
    numbercnt = rand() % 10 + 40;
    printf("numbercnt = %d\n", numbercnt);
    numbercontent[0] = '\0';
    for (i = 0; i < numbercnt; i++)
    {
        reVal = rand() % 10;
        /*****************/
        // 此处加入链表生成算法
        /*****************/
        itoa(reVal, tmp, 10);    
        strcat(numbercontent, tmp);
    }
    if (numbercontent[0] == '0') numbercontent[0] = 1;
    printf("生成的数字是: %s", numbercontent);
    return 0;
}

int BigNumAdd(/*此处参数类型根据自己的设计 pBigNumerIn1, pBigNumerIn2, &pBigNumerOut*/)
{
    // 两个输入,一个输出
    // 计算并合并链表
    // 注意由于进位的原因,输出的链表节点数量可能大于,输入的链表节点数量,所以合并时可能会malloc一个节点,存储进位
    return 0;
}
int main()
{
    getRandomValue(/* 添加链表的参数*/);
    // BigNumAdd()
    // 显示结果
    return 0;
}

基于最新版ChatGPT4的回答,望采纳!!!有其他问题也可以询问我哦、”(最新版更智能,功能更加强大)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 1000

typedef struct {
    int ID;
    int classID;
    char name[32];
    char ch;
    char phone[14];
    char addr[32];
} Studentinfo;

typedef struct {
    Studentinfo data[MAX_SIZE];
    int length;
} SeqList;

void InitList(SeqList *L) {
    L->length = 0;
}

void Insert(SeqList *L, int i, Studentinfo x) {
    if (L->length == MAX_SIZE) {
        printf("List is full\n");
        return;
    }
    if (i < 1 || i > L->length + 1) {
        printf("Invalid position\n");
        return;
    }
    for (int j = L->length; j >= i; j--) {
        L->data[j] = L->data[j - 1];
    }
    L->data[i - 1] = x;
    L->length++;
}

void Delete(SeqList *L, int i) {
    if (i < 1 || i > L->length) {
        printf("Invalid position\n");
        return;
    }
    for (int j = i; j < L->length; j++) {
        L->data[j - 1] = L->data[j];
    }
    L->length--;
}

void Update(SeqList *L, int i, Studentinfo x) {
    if (i < 1 || i > L->length) {
        printf("Invalid position\n");
        return;
    }
    L->data[i - 1] = x;
}

int Search(SeqList *L, int ID) {
    for (int i = 0; i < L->length; i++) {
        if (L->data[i].ID == ID) {
            return i + 1;
        }
    }
    return 0;
}

void PrintList(SeqList *L) {
    printf("ID\tclassID\tname\tch\tphone\taddr\n");
    for (int i = 0; i < L->length; i++) {
        printf("%d\t%d\t%s\t%c\t%s\t%s\n",
            L->data[i].ID, L->data[i].classID, L->data[i].name, L->data[i].ch,
            L->data[i].phone, L->data[i].addr);
    }
}

void SaveList(SeqList *L, char *filename) {
    FILE *fp = fopen(filename, "wb");
    if (fp == NULL) {
        printf("Cannot open file %s\n", filename);
        return;
    }
    fwrite(&(L->length), sizeof(int), 1, fp);
    fwrite(L->data, sizeof(Studentinfo), L->length, fp);
    fclose(fp);
}

void LoadList(SeqList *L, char *filename) {
    FILE *fp = fopen(filename, "rb");
    if (fp == NULL) {
        printf("Cannot open file %s\n", filename);
        return;
    }
    fread(&(L->length), sizeof(int), 1, fp);
    fread(L->data, sizeof(Studentinfo), L->length, fp);
    fclose(fp);
}

void Menu() {
    printf("\nMenu:\n");
    printf("1. Add a student\n");
    printf("2. Delete a student\n");
    printf("3. Update a student\n");
    printf("4. Search a student\n");
    printf("5. Print the list\n");
    printf("6. Save the list to file\n");
    printf("7. Load the list from file\n");
    printf("0. Exit\n");
}

int main() {
SeqList L;
InitList(&L);
int choice;
do {
Menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter ID: ");
scanf("%d", &(L.data[L.length].ID));
printf("Enter classID: ");
scanf("%d", &(L.data[L.length].classID));
printf("Enter name: ");
scanf("%s", L.data[L.length].name);
printf("Enter gender (M/F): ");
scanf(" %c", &(L.data[L.length].ch));
printf("Enter phone number: ");
scanf("%s", L.data[L.length].phone);
printf("Enter address: ");
scanf("%s", L.data[L.length].addr);
L.length++;
printf("Student added.\n");
break;
case 2:
printf("Enter the ID of the student to delete: ");
int id_to_delete;
scanf("%d", &id_to_delete);
int pos = Search(&L, id_to_delete);
if (pos == 0) {
printf("Student not found.\n");
} else {
Delete(&L, pos);
printf("Student deleted.\n");
}
break;
case 3:
printf("Enter the ID of the student to update: ");
int id_to_update;
scanf("%d", &id_to_update);
pos = Search(&L, id_to_update);
if (pos == 0) {
printf("Student not found.\n");
} else {
printf("Enter the new information:\n");
printf("Enter ID: ");
scanf("%d", &(L.data[pos - 1].ID));
printf("Enter classID: ");
scanf("%d", &(L.data[pos - 1].classID));
printf("Enter name: ");
scanf("%s", L.data[pos - 1].name);
printf("Enter gender (M/F): ");
scanf(" %c", &(L.data[pos - 1].ch));
printf("Enter phone number: ");
scanf("%s", L.data[pos - 1].phone);
printf("Enter address: ");
scanf("%s", L.data[pos - 1].addr);
printf("Student updated.\n");
}
break;
case 4:
printf("Enter the ID of the student to search: ");
int id_to_search;
scanf("%d", &id_to_search);
pos = Search(&L, id_to_search);
if (pos == 0) {
printf("Student not found.\n");
} else {
printf("ID\tclassID\tname\tch\tphone\taddr\n");
printf("%d\t%d\t%s\t%c\t%s\t%s\n",
L.data[pos - 1].ID, L.data[pos - 1].classID,
L.data[pos - 1].name, L.data[pos - 1].ch,
L.data[pos - 1].phone, L.data[pos - 1].addr);
}
break;
case 5:
PrintList(&L);
break;
case 6:
printf("Enter the filename to save: ");
char filename[32];
scanf("%s", filename);
SaveList(&L, filename);
printf("List saved.\n");
break;
case 7:
printf("Enter the filename to load: ");
scanf("%s", filename);
LoadList(&L, filename);
printf("List loaded.\n");
break;
case 0:
printf("Goodbye.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 0);
return 0;
}

以下是一个简单的测试main()函数,用于测试上述代码的正确性:

int main() {
    SeqList L;
    InitList(&L);
    Studentinfo s;
    s.ID = 1;
    s.classID = 1;
    strcpy(s.name, "Alice");
    s.ch = 'F';
    strcpy(s.phone, "12345678901");
    strcpy(s.addr, "No.1, Main Street");
    Insert(&L, 1, s);
    s.ID = 2;
    s.classID = 1;
    strcpy(s.name, "Bob");
    s.ch = 'M';
    strcpy(s.phone, "12345678902");
    strcpy(s.addr, "No.2, Main Street");
    Insert(&L, 2, s);
    s.ID = 3;
    s.classID = 2;
    strcpy(s.name, "Charlie");
    s.ch = 'M';
    strcpy(s.phone, "12345678903");
    strcpy(s.addr, "No.3, Main Street");
    Insert(&L, 3, s);
    PrintList(&L);
    Delete(&L, 2);
    PrintList(&L);
    s.ID = 3;
    s.classID = 3;
    strcpy(s.name, "David");
    s.ch = 'M';
    strcpy(s.phone, "12345678904");
    strcpy(s.addr, "No.4, Main Street");
    Update(&L, 1, s);
    PrintList(&L);
    int pos = Search(&L, 1);
    printf("Position of student with ID 1: %d\n", pos);
    pos = Search(&L, 2);
    printf("Position of student with ID 2: %d\n", pos);
    pos = Search(&L, 3);
    printf("Position of student with ID 3: %d\n", pos);
    char filename[32] = "data.bin";
    SaveList(&L, filename);
    InitList(&L);
    LoadList(&L, filename);
    PrintList(&L);
    return 0;
}

以下是一个简单的测试main()函数,用于测试上述代码的正确性:

scssCopy code
int main() {
    SeqList L;
    InitList(&L);
    Studentinfo s;
    s.ID = 1;
    s.classID = 1;
    strcpy(s.name, "Alice");
    s.ch = 'F';
    strcpy(s.phone, "12345678901");
    strcpy(s.addr, "No.1, Main Street");
    Insert(&L, 1, s);
    s.ID = 2;
    s.classID = 1;
    strcpy(s.name, "Bob");
    s.ch = 'M';
    strcpy(s.phone, "12345678902");
    strcpy(s.addr, "No.2, Main Street");
    Insert(&L, 2, s);
    s.ID = 3;
    s.classID = 2;
    strcpy(s.name, "Charlie");
    s.ch = 'M';
    strcpy(s.phone, "12345678903");
    strcpy(s.addr, "No.3, Main Street");
    Insert(&L, 3, s);
    PrintList(&L);
    Delete(&L, 2);
    PrintList(&L);
    s.ID = 3;
    s.classID = 3;
    strcpy(s.name, "David");
    s.ch = 'M';
    strcpy(s.phone, "12345678904");
    strcpy(s.addr, "No.4, Main Street");
    Update(&L, 1, s);
    PrintList(&L);
    int pos = Search(&L, 1);
    printf("Position of student with ID 1: %d\n", pos);
    pos = Search(&L, 2);
    printf("Position of student with ID 2: %d\n", pos);
    pos = Search(&L, 3);
    printf("Position of student with ID 3: %d\n", pos);
    char filename[32] = "data.bin";
    SaveList(&L, filename);
    InitList(&L);
    LoadList(&L, filename);
    PrintList(&L);
    return 0;
}

这个测试程序首先创建了一个空的顺序表L,然后向其中插入了三个学生信息,再打印整个顺序表,接着删除了其中一个学生信息,再次打印整个顺序表,然后修改了另一个学生信息,再次打印整个顺序表,接着通过搜索函数查找三个学生信息,并打印其位置,最后将顺序表保存到文件data.bin中,清空顺序表L,从文件data.bin中加载顺序表,并再次打印整个顺序表。如果输出结果符合预期,则说明这个线性表实现了通讯录管理的功能,并且保存和加载操作也正常工作。

第二个

#include <stdio.h>
#include <stdlib.h>

typedef struct term {
    float coef;
    int expn;
    struct term *next;
} Term, *Polynomial;

void InitPolynomial(Polynomial *P) {
    *P = (Polynomial) malloc(sizeof(Term));
    (*P)->next = NULL;
}

void InsertTerm(Polynomial P, float coef, int expn) {
    Polynomial new_node = (Polynomial) malloc(sizeof(Term));
    new_node->coef = coef;
    new_node->expn = expn;
    new_node->next = NULL;
    while (P->next != NULL && P->next->expn > expn) {
        P = P->next;
    }
    if (P->next != NULL && P->next->expn == expn) {
        P->next->coef += coef;
        if (P->next->coef == 0) {
            Polynomial tmp = P->next;
            P->next = tmp->next;
            free(tmp);
        }
    } else {
        new_node->next = P->next;
        P->next = new_node;
    }
}

void PrintPolynomial(Polynomial P) {
    if (P->next == NULL) {
        printf("0\n");
    } else {
        while (P->next != NULL) {
            if (P->next->coef > 0 && P != NULL) {
                printf("+");
            }
            printf("%.2fx^%d", P->next->coef, P->next->expn);
            P = P->next;
        }
        printf("\n");
    }
}

Polynomial Add(Polynomial P1, Polynomial P2) {
    Polynomial P, r, s;
    InitPolynomial(&P);
    r = P;
    P1 = P1->next;
    P2 = P2->next;
    while (P1 != NULL && P2 != NULL) {
        if (P1->expn > P2->expn) {
            s = (Polynomial) malloc(sizeof(Term));
            s->coef = P1->coef;
            s->expn = P1->expn;
            s->next = NULL;
            r->next = s;
            r = s;
            P1 = P1->next;
        } else if (P1->expn < P2->expn) {
            s = (Polynomial) malloc(sizeof(Term));
            s->coef = P2->coef;
            s->expn = P2->expn;
            s->next = NULL;
            r->next = s;
            r = s;
            P2 = P2->next;
        } else {
            float sum = P1->coef + P2->coef;
            if (sum != 0) {
                s = (Polynomial) malloc(sizeof(Term));
                s->coef = sum;
                s->expn = P1->expn;
                s->next = NULL;
                r->next = s;
                r = s;
            }
            P1 = P1->next;
            P2 = P2->next;
        }
    }
    if (P1 != NULL) {
        r->next = P1;
    }
    if (P2 != NULL) {
        r->next = P2;
    }
    return P;
}

Polynomial Subtract(Polynomial P1, Polynomial Polynomial Subtract(Polynomial P1, Polynomial P2) {
Polynomial P, r, s;
InitPolynomial(&P);
r = P;
P1 = P1->next;
P2 = P2->next;
while (P1 != NULL && P2 != NULL) {
if (P1->expn > P2->expn) {
s = (Polynomial) malloc(sizeof(Term));
s->coef = P1->coef;
s->expn = P1->expn;
s->next = NULL;
r->next = s;
r = s;
P1 = P1->next;
} else if (P1->expn < P2->expn) {
s = (Polynomial) malloc(sizeof(Term));
s->coef = -P2->coef;
s->expn = P2->expn;
s->next = NULL;
r->next = s;
r = s;
P2 = P2->next;
} else {
float diff = P1->coef - P2->coef;
if (diff != 0) {
s = (Polynomial) malloc(sizeof(Term));
s->coef = diff;
s->expn = P1->expn;
s->next = NULL;
r->next = s;
r = s;
}
P1 = P1->next;
P2 = P2->next;
}
}
if (P1 != NULL) {
r->next = P1;
}
if (P2 != NULL) {
while (P2 != NULL) {
s = (Polynomial) malloc(sizeof(Term));
s->coef = -P2->coef;
s->expn = P2->expn;
s->next = NULL;
r->next = s;
r = s;
P2 = P2->next;
}
}
return P;
}

float Evaluate(Polynomial P, float x) {
float sum = 0;
P = P->next;
while (P != NULL) {
sum += P->coef * pow(x, P->expn);
P = P->next;
}
return sum;
}

Polynomial Derivative(Polynomial P) {
Polynomial Q, r, s;
InitPolynomial(&Q);
r = Q;
P = P->next;
while (P != NULL) {
if (P->expn > 0) {
s = (Polynomial) malloc(sizeof(Term));
s->coef = P->coef * P->expn;
s->expn = P->expn - 1;
s->next = NULL;
r->next = s;
r = s;
}
P = P->next;
}
return Q;
}

Polynomial Multiply(Polynomial P1, Polynomial P2) {
Polynomial P, r, s;
InitPolynomial(&P);
r = P;
P1 = P1->next;
P2 = P2->next;
while (P1 != NULL) {
while (P2 != NULL) {
float coef = P1->coef * P2->coef;
int expn = P1->expn + P2->expn;
s = (Polynomial) malloc(sizeof(Term));
s->coef = coef;
s->expn = expn;
s->next = NULL;
r->next = s;
r = s;
P2 = P2->next;
}
P1 = P1->next;
P2 = P->next;
}
return P;
}

int main() {
Polynomial P1, P2, P3, P4, P5;
InitPolynomial(&P1);
InitPolynomial(&P2);
InsertTerm(P1, 2, 3);
InsertTerm(P1, 1, 2);
InsertTerm(P1, 4, 1);
InsertTerm(P1, 3, 0);
InsertTerm(P2, -1, 2);
InsertTerm(P2, 2, 1);
InsertTerm(P2, 1, 0);
printf("P1: ");
PrintPolynomial(P1);
printf("P2: ");
PrintPolynomial(P2);
P3 = Add(P1, P2);
printf("P1 + P2: ");
PrintPolynomial(P3);
P4 = Subtract(P1, P2);
printf("P1 - P2: ");
PrintPolynomial(P4);
float x = 2;
printf("P1(%.2f) = %.2f\n", x, Evaluate(P1, x));
printf("P2(%.2f) = %.2f\n", x, Evaluate(P2, x));
P5 = Derivative(P1);
printf("dP1/dx: ");
PrintPolynomial(P5);
P5 = Multiply(P1, P2);
printf("P1 * P2: ");
PrintPolynomial(P5);
return 0;
}

这个测试程序创建了两个多项式P1P2,并分别向其插入了四个项,然后打印出这两个多项式。接着,使用Add()函数计算P1P2的和,并打印出结果。使用Subtract()函数计算P1P2的差,并打印出结果。使用Evaluate()函数计算P1P2在$x=2$处的值,并打印出结果。使用Derivative()函数计算P1的导数,并打印出结果。使用Multiply()函数计算P1P2的乘积,并打印出结果。如果输出结果符合预期,则说明这个线性表实现了一元多项式的功能。

感觉1可以写,手打,不是chatgpt,不过需要时间

该回答引用ChatGPT
代码:

struct Contact {
    string name;
    string phone;
    string email;
};

然后,定义一个线性表类ContactList,其中包括如下方法:

  • void addContact(Contact c):向通讯录中添加一个联系人。
  • void removeContact(int i):从通讯录中移除第i个联系人。
  • void editContact(int i, Contact c):编辑第i个联系人的信息。
  • Contact getContact(int i):获取第i个联系人的信息。
  • int getSize():获取通讯录中联系人的数量。
#include <iostream>
#include <vector>
using namespace std;

struct Contact {
    string name;
    string phone;
    string email;
};

class ContactList {
public:
    void addContact(Contact c) {
        contacts.push_back(c);
    }

    void removeContact(int i) {
        contacts.erase(contacts.begin() + i);
    }

    void editContact(int i, Contact c) {
        contacts[i] = c;
    }

    Contact getContact(int i) {
        return contacts[i];
    }

    int getSize() {
        return contacts.size();
    }
private:
    vector<Contact> contacts;
};

int main() {
    ContactList contactList;

    Contact c1 = {"Alice", "123456789", "alice@example.com"};
    Contact c2 = {"Bob", "987654321", "bob@example.com"};

    contactList.addContact(c1);
    contactList.addContact(c2);
    cout << "Contact list size: " << contactList.getSize() << endl;

    Contact c3 = {"Charlie", "456789123", "charlie@example.com"};
    contactList.addContact(c3);
    cout << "Contact list size: " << contactList.getSize() << endl;

    Contact c4 = {"David", "789123456", "david@example.com"};
    contactList.editContact(1, c4);
    cout << "Contact at index 1: " << contactList.getContact(1).name << endl;

    contactList.removeContact(0);
    cout << "Contact list size: " << contactList.getSize() << endl;

    return 0;
}