gpt4 book ai didi

c - 学生平均数的链表程序

转载 作者:行者123 更新时间:2023-11-30 21:25:34 25 4
gpt4 key购买 nike

我是 C 编程新手,尤其是链表。有人可以帮我修改我的代码吗?当我编译程序时没有错误,但当我尝试运行程序时,其输出是错误的。不知道是存储错误还是打印错误。

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

/* self-referential structure */
struct listNode {
char name[80];
char subject[80]; /* each listNode contains a character */
float unit;
float grade;
struct listNode *nextPtr; /* pointer to next node */
}; /* end structure listNode */

typedef struct listNode ListNode; /* synonym for struct listNode */
typedef ListNode *ListNodePtr; /* synonym for ListNode* */

/* prototypes */
void insert(ListNodePtr *sPtr, char SUB[80], float grade, float unit, char NAME[80]);
int isEmpty(ListNodePtr sPtr);
void printList(ListNodePtr currentPtr, ListNodePtr *sPtr);
void instructions(void);

int main(void)
{
ListNodePtr startPtr = NULL; /* initially there are no nodes */
int choice; /* user's choice */
float grades, units;
char subn[80], stdn[80];
int i, j, std = 0, sub = 0;

instructions(); /* display the menu */
printf("? ");
scanf("%d", &choice);

/* loop while user does not choose 3 */
while (choice != 3) {
switch (choice) {
case 1: /* input data */
printf("Number of Students: (at least 5 students)\n");
scanf("%d", &std);

for (i = 0; i < std; i++) {
printf("\nStudent name: ");
scanf("%s", &stdn);
printf("How many subjects?\n");
scanf("%d", &sub);
for (j = 0; j < sub; j++) {
printf("\nSub­ject: ");
scanf("%s", &subn);
printf("Units­: ");
scanf("%f", &units);
printf("Final­ Grade: ");
scanf("%f", &grades);
insert(&startPtr, subn, grades, units, stdn);
}
}
break;

case 2:
printList(startPtr, &startPtr);
break;

default:
printf("Invalid choice.\n\n");
instructions();
break;
} /* end switch */

printf("\n\n? ");
scanf("%d", &choice);
} /* end while */

printf("End of run.\n");
return 0; /* indicates successful termination */
} /* end main */

/* display program instructions to user */
void instructions(void)
{
printf("Enter your choice:\n"
" 1 to Input Student Data.\n"
" 2 to Display Student Data.\n"
" 3 to end.\n");
} /* end function instructions */

/* for series */
void insert(ListNodePtr *sPtr, char SUB[80], float grade, float unit, char NAME[80])
{
ListNodePtr newPtr; /* pointer to new node */
ListNodePtr previousPtr; /* pointer to previous node in list */
ListNodePtr currentPtr; /* pointer to current node in list */

newPtr = malloc(sizeof(ListNode)); /* create node */

if (newPtr != NULL) { /* is space available */
strcpy(newPtr->na­me, NAME);
strcpy(newPtr->subject, SUB); /* place value in node */
newPtr->grade = grade;
newPtr->unit = unit;
newPtr->nextPtr = NULL; /* node does not link to another node */

previousPtr = NULL;
currentPtr = *sPtr;

/* insert new node at beginning of list */
if (previousPtr == NULL) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
} /* end if */
else { /* insert new node between previousPtr and currentPtr */
previousPtr->nex­tPtr = newPtr;
newPtr->nextPtr = currentPtr;
} /* end else */
} /* end if */
else {
printf("Student data not inserted. No memory available.\n");
} /* end else */
} /* end function insert */

int isEmpty(ListNodePtr sPtr)
{
return sPtr == NULL;
} /* end function isEmpty */

/* Print the list */
void printList(ListNodePtr currentPtr, ListNodePtr *sPtr)
{
float total = 0.00, total2 = 0.00;
/* if list is empty */
if (currentPtr == NULL) {
printf("List is empty.\n\n");
} /* end if */
else {
printf("\n");
printf("%-20s\t%s\­t%s\n", "Subject", "Units", "Final Grade");
/* while not the end of the list */
while (currentPtr != NULL) {
printf("Student­ Name: %s\n", currentPtr->name);
printf("%-20s\t%.2f\t%.2f\n­", currentPtr->subject, ­ currentPtr->unit, currentPtr->grade);
total += currentPtr->unit;
total2 += currentPtr->unit*cur­rentPtr->grade;
currentPtr = currentPtr->nextPtr;
} /* end while */

printf("\nWeight:­ %.2f", total2 / total);
printf("\n///////­/////////////////////­/////////////////////­///");
} /* end else */
} /* end function printList */

最佳答案

标题

嗨,我没有仔细查看代码。但是,快速浏览后我发现了以下基本问题

nextPtr 应该是一个指针,您正在创建一个实例并复制下一个结构的内容。这效率不高。我将声明如下

struct listNode *nextPtr;/指向下一个节点的指针 */

我将再次更改以下typedef ListNode ListNodePtr;typedef ListNode ListNodePtr;

至typedef ListNode *ListNodePtr;

尝试用这个重构你的代码,看看效果如何。如果没有帮助,我晚上可以详细看看。

关于c - 学生平均数的链表程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29048162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com