因此,该程序应该为汽车创建一个链表,其中包含年份信息,安全评级和模型。但是,当输出安全等级时,例如,如果用户输入“5”,程序会将其输出为某个非常大的数字,而不是用户输入的数字。
示例:如果用户输入
Enter Year for Car: 1992
Enter Rating for Car (0.00 - 100.00): 25.5
Enter Model for Car: Chevy
节目输出是......
The Cars in the List Are As Follows:
Year:1992 Rating:79960111893652368676401906121179136.00 Model:Chevy
什么时候应该......
The Cars in the List Are As Follows:
Year:1992 Rating:25.50 Model:Chevy
我做错了什么?这是我的完整代码...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 400
struct car {
int year;
float safeRating;
char model[MAXSIZE];
struct car *next;
};
typedef struct car Car;
typedef struct car *CarPtr;
void menu();
void printList(CarPtr);
CarPtr makeCar(int, float, char *);
CarPtr removeCar(CarPtr, int);
CarPtr addCar(CarPtr sPtr, int infoA, float infoB, char *infoC);
void viewCar(CarPtr sPtr, int infoA, float infoB, char *infoC);
int main()
{
CarPtr startPtr;
int infoA, choice;
float infoB;
char infoC;
startPtr = NULL;
menu();
scanf("%d", &choice);
while (choice != 5){
switch (choice){
case 1: printf("\nEnter Year for Car: ");
scanf("%d", &infoA);
printf("\nEnter Rating for Car (0.00 - 100.00): ");
scanf("%f", &infoB);
printf("\nEnter Model for Car: ");
scanf("%s", &infoC);
startPtr = addCar(startPtr, infoA, infoB, &infoC);
printList(startPtr);
printf("\n");
break;
case 2: printf("\nEnter Car for deletion : ");
scanf("%d", &infoA);
startPtr = removeCar(startPtr, infoA);
printList(startPtr);
printf("\n");
break;
case 3: printf("\nEnter Car Number to View : ");
scanf("%d", &infoA);
viewCar(startPtr, infoA, infoB, &infoC);
printf("\n");
break;
case 4: printList(startPtr);
printf("\n");
break;
default: printf ("Invalid Option... Please Try Again \n");
break;
}
menu();
scanf("%d", &choice);
}
return 0;
}
void menu()
{
printf ("\t1: Insert Car into Ordered List\n");
printf ("\t2: Remove Car from List\n");
printf ("\t3: View Car from List\n");
printf ("\t4: Printing the List\n");
printf ("\t5: Exit\n");
printf ("\tEnter Choice: ");
}
CarPtr makeCar(int infoA, float infoB, char *infoC)
{
CarPtr np = (CarPtr) malloc(sizeof(Car));
np->year = infoA;
np->safeRating = infoB;
strcpy(np->model, infoC);
np->next = NULL;
return np;
}
void printList(CarPtr sPtr)
{
if(sPtr == NULL){
printf ("\nThere are no Cars to be Printed\n");
}
else {
printf("The Cars in the List Are As Follows Sorted by Year: \n");
while (sPtr != NULL) {
printf("Year:%d Rating:%.2f Model:%s\n", sPtr->year, sPtr->safeRating, sPtr->model);
sPtr = sPtr->next;
}
}
}
CarPtr addCar(CarPtr sPtr, int infoA, float infoB, char *infoC)
{
CarPtr newPtr, currPtr, prevPtr;
newPtr = makeCar(infoA, infoB, infoC);
prevPtr = NULL;
currPtr = sPtr;
while (currPtr != NULL && infoA > currPtr->year) {
prevPtr = currPtr;
currPtr = currPtr->next;
}
if (prevPtr == NULL) { // inserting at the start of the list
newPtr->next = sPtr;
sPtr = newPtr; // start of list has now changed
}
else {
newPtr->next = currPtr;
prevPtr->next = newPtr;
}
return sPtr;
}
CarPtr removeCar(CarPtr sPtr, int infoA)
{
CarPtr previousPtr, currentPtr, tempPtr;
previousPtr = NULL;
currentPtr = sPtr;
if(sPtr == NULL){
printf ("\nThe List is empty... No cars to be Removed\n");
return sPtr;
}
while (currentPtr != NULL && currentPtr->year != infoA) {
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
if(currentPtr == NULL){
printf("\nCar ( %d ) was not found \n", infoA);
}
else if (previousPtr == NULL){ // if node to be deleted is the first node
tempPtr = sPtr;
sPtr = sPtr->next; // start of list has been changed
printf("\nCar ( %d ) was deleted \n", tempPtr->year);
free(tempPtr);
}
else{
tempPtr = currentPtr;
previousPtr->next = currentPtr->next;
printf("\nCar ( %d ) was deleted \n", tempPtr->year);
free(tempPtr);
}
return sPtr;
}
void viewCar(CarPtr sPtr, int infoA, float infoB, char *infoC)
{
CarPtr previousPtr, currentPtr;
previousPtr = NULL;
currentPtr = sPtr;
int position = 0;
if(sPtr == NULL){
printf ("\nThe List is empty... No cars to View\n");
return;
}
while (currentPtr != NULL && currentPtr->year != infoA) {
previousPtr = currentPtr;
currentPtr = currentPtr->next;
position++;
}
if(currentPtr == NULL){
printf("\nCar ( %d ) was not found \n", infoA);
}
else{
printf("\nCar ( %d ) Rating: %.2f Model: %s was found at position : %d\n", currentPtr->year, currentPtr->safeRating, currentPtr->model, (position + 1));
}
}
答案 0 :(得分:2)
您没有为汽车模型字符串分配任何空间。您是scanf("%s", &infoC);
,但infoC
是一个char
,字符串没有空格。这会覆盖一些东西,可能会捣乱。然后,当你开车时,你会去strcpy
,这将复制,直到它达到NULL
字节。等等,等等。最好将其定义为char infoC[MAXSIZE]
,以匹配struct car
中的内容。