写代码不掉头发. 2021-05-15 20:15 采纳率: 80%
浏览 85
已采纳

程序莫名终止,求解答

#include<stdio.h>
#include<stdlib.h>
struct student
{
	int num;
	float score;

	struct student* next;
};
void print(struct student* head);
struct student* creatsort();
int main()
{
	struct student* head;
	head = creatsort();
	print(head);
	return 0;
}
struct student* creatsort()
{
	struct student* head = NULL;
	struct student* pnew, * back, * front;
	int num;
	float score;
	printf("请输入成绩:\n");
	scanf_s("%d,%f", &num, &score);
	if (num < 0)
	{
		return head;
	}
	while (num >= 0)
	{
		pnew = (struct student*)malloc(sizeof(struct student));
		pnew->num = num;
		pnew->score = score;
		if (head == NULL)
		{
			head = pnew;
			pnew->next = NULL;
		}
		else
		{
			back = front = head;
			if (head->score < score)
			{
				pnew->next = head;
				head = pnew;
			}
			else
			{
				front = back = head;
				while (front != NULL && front->score > score)
				{
					back = front;
					front = front->next;
				}
				if (front == NULL && front->score > score)
				{
					pnew->next = back->next;
					front->next = pnew;
				}
				else
				{
					pnew->next = front->next;
					front->next = pnew;
				}
			}
		}
		printf("新排名如下:\n");
		print(head);
		printf("请输入新成绩:\n");
		scanf_s("%d,%f", &num, &score);
	}

	return head;
}
void print(struct student* head)
{
	struct student* p;
	p = head;
	if (p == NULL)
	{
		printf("Empty!\n");
		return;
	}
	while (p != NULL)
	{
		printf("%d,%.2f\n", p->num, p->score);
		p = p->next;
	}
}

如上图,程序在第三次输入后就不再输出,莫名奇妙的就终止了。

  • 写回答

4条回答 默认 最新

  • qzjhjxj 2021-05-15 22:31
    关注

    修改如下,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    struct student
    {
    	int    num;
    	float  score;
    	struct student* next;
    };
    
    void   print(struct student* head);
    struct student* creatsort();
    
    int main()
    {
    	struct student* head;
    	head = creatsort();
    	print(head);
        
    	return 0;
    }
    struct student* creatsort()
    {
    	struct student* head = NULL;
    	struct student* pnew, * back, * front;
    	int num;
    	float score;
    	printf("请输入成绩:\n");
    	scanf_s("%d,%f", &num, &score);
    	if (num < 0)
    	{
    		return head;
    	}
    	while (num >= 0)
    	{
    		pnew = (struct student*)malloc(sizeof(struct student));
    		pnew->num = num;
    		pnew->score = score;
                    pnew->next = NULL;  //移到这里
    		if (head == NULL)
    		{
                            printf("1\n");
    			head = pnew;
    			head->next = NULL; //pnew->next = NULL;
    		}
    		else
    		{
                            //back = front = head;
    			if (head->score < score)
    			{
    				pnew->next = head;
    				head = pnew;
    			}
    			else
    			{
                                    back  = head;
                                    front = head->next;
    				while (front != NULL && front->score > score)
    				{
    					back  = front;
    					front = front->next;
    				}
    				if (front == NULL)//if (front == NULL && front->score > score)
    				{
                                            printf("5\n");
    					pnew->next = back->next;
    					back->next = pnew;//front->next = pnew;
    				}
    				else
    				{
    					pnew->next = front; //pnew->next = front->next;
    					back->next = pnew;  //front->next = pnew;
    				}
    			}
    		}
    		printf("新排名如下:\n");
    		print(head);
    		printf("请输入新成绩:\n");
    		scanf_s("%d,%f", &num, &score);
    	}
    	return head;
    }
    void print(struct student* head)
    {
    	struct student* p;
    	p = head;
    	if (p == NULL)
    	{
    		printf("Empty!\n");
    		return;
    	}
    	while (p != NULL)
    	{
    		printf("%d,%.2f\n", p->num, p->score);
    		p = p->next;
    	}
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab