A154383 2022-12-16 22:43 采纳率: 60%
浏览 44
已结题

c语言字符串排序,相同的代码无法运行

问题遇到的现象和发生背景

使用链表,输入两个句子,并对其中的单词进行排序

遇到的现象和发生背景,请写出第一个错误信息

第一个句子可以完美排序,到了第二个句子就运行出错

用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
typedef struct LinkList {
    char word[M];
    struct LinkList* next;//下一节点
}LNode, * LinkList;

LinkList InitList()
{//初始化链表
    LinkList L = (LinkList)malloc(sizeof(LNode));// 为头结点分配空间
    if (!L) exit(OVERFLOW);
    L->next = NULL;
    return L;
}

status InsertList(LinkList L, char* e,int i)
{//在第i个位置之后插入节点
    LNode* p = L;
    int j = 0;
    while (p && j < i)
    {
        p = p->next;
        j++;
    }

    LinkList s = (LinkList)malloc(sizeof(LNode));
    strcpy(s->word, e);
    s->next = p->next;
    p->next = s;
    return OK;
}
void bubble_sort(LinkList L) 
{
    int i, j;
    LNode* p, * q;
    char temp[10];
    for (p = L->next, i = 1; p; i++, p = p->next)
    {
        for (q = L->next, j = 1; q; j++, q = q->next)
        {
            if (strcmp(p->word, q->word) < 0)
            {
                strcpy(temp,p->word);
                strcpy(p->word, q->word);
                strcpy(q->word, temp);
            }
        }
    }
}
void showlist(LinkList L)
{
    LNode* p = L->next;
    while (p)
    {
        printf("%s ", p->word);
        p = p->next;
    }
}


void main()
{
    LinkList l1 = InitList();
    LinkList l2 = InitList();
    char word[N][M];
    char c = ' ';
    int i = 0,j = 0;
    printf("请输入第一个句子: ");
    while (c != '.' && c != '!' && c != '?')
    {
        c = getchar();
        if (c != ' ' && c != '.' && c != '!' && c != '?')
            word[j][i++] = c;
        else
        {
            word[j++][i] = '\0';
            i = 0;
        }
    }
    for (i = 0; i < j; i++)
        InsertList(l1, word[i], 0);
    bubble_sort(l1);
    showlist(l1);
    i = 0; j = 0; c = ' ';
    printf("\n请输入第二个句子: \n");
    while (c != '.' && c != '!' && c != '?')
    {
        c = getchar();
        if (c != ' ' && c != '.' && c != '!' && c != '?')
            word[j][i++] = c;
        else
        {
            word[j++][i] = '\0';
            i = 0;
        }
    }
    for (i = 0; i < j; i++)
        InsertList(l2, word[i], 0);
    bubble_sort(l2);
    showlist(l2);


}
运行结果及详细报错内容

img

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”
  • 写回答

3条回答 默认 最新

  • X-道至简 2022-12-17 08:43
    关注

    你这个是因为读入数据不对造成的
    第一个句子读完后有一个回车需要处理一下,不然下面的读入字符都是错的了
    代码修改如下,在下面代码地方加一个 getchar(); 吸收回车符号

        bubble_sort(l1);
        showlist(l1);
        getchar();  /*加上这一句吧回车符号吸收了,在输入字符串和字符的时候要特别注意下*/
        i = 0; j = 0; c = ' ';
        printf("\n请输入第二个句子: \n");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月25日
  • 已采纳回答 12月17日
  • 赞助了问题酬金20元 12月17日
  • 创建了问题 12月16日

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch