我很难为包含在LIST中搜索char元素的函数编写代码。当我运行程序时,它总是显示结果的空白(如案例4所示)并且它只有在我搜索int元素时才有效。 (案例2和案例3尚未完成。)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define MAX_LENGTH 80
#define LOOPS 2
struct delivery
{
int num;
char name[20];
char code[8];
char info[80];
int day;
int month;
int year;
int amount;
};
typedef struct delivery BODY;
struct List{
BODY body;
struct List*pNext;
};
typedef struct List LIST;
int enterBody(BODY*ps);
void printBody(BODY s);
void printList(LIST*pFirst);
LIST*insertBegin(LIST*pFirst, BODY newBody);
int mean_Subject(LIST *pFirst);
void searchGroup(LIST*pFirst, char gr);
int main()
{
FILE*pOut = NULL, *pIn = NULL;
char Fname[] = "List_bin.dat";
BODY newbody;
LIST*pFirst = NULL, *p;
int res, i, mode;
int sfn;
char gr;
int newgr, fn;
BODY delivery;
char*menu[] = { "MENU:",
"1 - Enter info for new delivery ",
"2 - Print info for deliveries for an entered month ",
"3 - Total delivery amount for an entered product ",
"4 - Show providers for an entered product ",
};
do
{
system("cls");
for (i = 0; i < 5; i++)
printf("\n%s\n", menu[i]);
do
{
fflush(stdin);
printf("\nChoose operation (1-4): ");
res = scanf("%d", &mode);
} while (res != 1);
switch (mode)
{
case 1:
for (i = 0; i < LOOPS; i++)
{
res = enterBody(&delivery);
if (res != 1)
{
printf("Error in initialization %d\n", res);
break;
}
p = insertBegin(pFirst, delivery);
if (p == NULL)
{
printf("Not enough memory! ");
break;
}
pFirst = p;
}
system("pause");
break;
case 2:
if (pFirst != NULL)
{
printList(pFirst);
}
else printf("\nEmpty list!\n");
system("pause");
break;
case 3:
if (pFirst != NULL)
printf("Total delivered amount: %d\n", mean_Subject(pFirst));
else
printf("\nEmpty list!\n");
system("pause");
break;
case 4:
printf("\nEnter name of product: ");
scanf("%s", &gr);
if (pFirst != NULL)
searchGroup(pFirst, gr);
else
printf("Empty list\n");
system("pause");
break;
default:
printf("\nIncorrect operation!\n");
system("pause");
}
} while (mode != 11);
system("pause");
return 0;
}
int enterBody(BODY*ps)
{
if (ps == NULL) return 0;
memset(ps, 0, sizeof(BODY));
fflush(stdin);
printf("\nEnter delivery number: ");
scanf("%d", &(ps->num));
fflush(stdin);
printf("\nEnter name of product: ");
gets(ps->name);
printf("\nEnter provider code: ");
gets(ps->code);
printf("\nEnter provider's company, address and phone number: ");
gets(ps->info);
printf("\nEnter date: ");
scanf("%d", &(ps->day));
fflush(stdin);
printf("\nEnter month: ");
scanf("%d", &(ps->month));
fflush(stdin);
printf("\nEnter year: ");
scanf("%d", &(ps->year));
fflush(stdin);
printf("\nEnter amount delivered: ");
scanf("%d", &(ps->amount));
fflush(stdin);
return 1;
}
void printBody(BODY s)
{
printf("\nNumber: %d\tName: %s\tCode: %s\tInfo: %s\nDate: %d-%d-%d\tAmount: %d",
s.num, s.name, s.code, s.info, s.day, s.month, s.year, s.amount);;
}
void printList(LIST*pFirst)
{
LIST*p = NULL;
if (pFirst == NULL) printf("Empty list.\n");
else
{
p = pFirst;
while (p != NULL)
{
printBody(p->body);
p = p->pNext;
}
printf("\n");
}
}
LIST*insertBegin(LIST*pFirst, BODY newBody)
{
LIST*p;
p = (LIST*)malloc(sizeof(LIST));
if (p == NULL)
{
printf("Not enough memory\n");
return NULL;
}
else
{
p->body = newBody;
p->pNext = pFirst;
pFirst = p;
return p;
}
}
int mean_Subject(LIST *pFirst)
{
LIST *p;
int mean = 0;
int sum = 0;
int count = 0;
if (pFirst == NULL)
printf("Empty list\n");
else
{
p = pFirst;
while (p != NULL)
{
sum += p->body.amount;
count++;
p = p->pNext;
}
}
return sum;
}
void searchGroup(LIST*pFirst, char gr)
{
LIST*p;
p = pFirst;
while (p != NULL)
{
if (p->body.name == gr)
printf("&s", p->body.info);
p = p->pNext;
}
}