从条件列表中打印数据

时间:2016-05-24 09:47:48

标签: c list

我需要创建一个仅打印特定月份和年份数据的函数(请参阅case 2void printList(LIST*pFirst, int pm, int py)),但似乎我做错了,因为整个程序崩溃了:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define MAX_LENGTH 80
#define LOOPS 1
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, int pm, int py);
LIST*insertBegin(LIST*pFirst, BODY newBody);

int main()
{
    FILE*pOut = NULL, *pIn = NULL;
    char Fname[] = "List_bin.dat";
    BODY newbody;
    LIST*pFirst = NULL, *p;
    int res, i, mode, pm, py;
    char sfn[20], gr[20];
    int newgr, fn;
    BODY delivery;
    char*menu[] = { "MENU:",
        "1 - Enter info for new delivery ",
        "2 - Print info for deliveries for an entered month ",
    };
    do
    {
        system("cls");
        for (i = 0; i < 3; i++)
            printf("\n%s\n", menu[i]);
        do
        {
            fflush(stdin);
            printf("\nChoose operation (1-2): ");
            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:
            printf("\nEnter month: ");
            scanf("%19s", pm);
            printf("\nEnter year: ");
            scanf("%19s", py);
            if (pFirst != NULL)
            {
                printList(pFirst, pm, py);
            }
            else printf("\nEmpty 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, int pm, int py)
{
    LIST*p = NULL;

        if(strcmp(p->body.month, pm) == 0)
        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;
    }
}

1 个答案:

答案 0 :(得分:0)

对于案例2,您只需要进行2次小的更改即可使程序崩溃。您正在扫描错误的数字类型,您应该使用%d和指向int的指针。打印列表时,您需要从头开始。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define MAX_LENGTH 80
#define LOOPS 1
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, int pm, int py);
LIST*insertBegin(LIST*pFirst, BODY newBody);

int main()
{
    FILE*pOut = NULL, *pIn = NULL;
    char Fname[] = "List_bin.dat";
    BODY newbody;
    LIST*pFirst = NULL, *p;
    int res, i, mode, pm, py;
    char sfn[20], gr[20];
    int newgr, fn;
    BODY delivery;
    char*menu[] = { "MENU:",
                    "1 - Enter info for new delivery ",
                    "2 - Print info for deliveries for an entered month ",
    };
    do
    {
        system("cls");
        for (i = 0; i < 3; i++)
            printf("\n%s\n", menu[i]);
        do
        {
            fflush(stdin);
            printf("\nChoose operation (1-2): ");
            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:
                printf("\nEnter month: ");
                scanf("%d", &pm);
                printf("\nEnter year: ");
                scanf("%d", &py);
                if (pFirst != NULL)
                {
                    printList(pFirst, pm, py);
                }
                else printf("\nEmpty 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, int pm, int py)
{
    LIST*p = NULL;

    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;
    }
}

测试

MENU:

1 - Enter info for new delivery 

2 - Print info for deliveries for an entered month 

Choose operation (1-2): 1

Enter delivery number: 2

Enter name of product: 
Enter provider code: 2

Enter provider's company, address and phone number: 2

Enter date: 2

Enter month: 2

Enter year: 2

Enter amount delivered: 2
sh: 1: pause: not found
sh: 1: cls: not found

MENU:

1 - Enter info for new delivery 

2 - Print info for deliveries for an entered month 

Choose operation (1-2): 2

Enter month: 2

Enter year: 2

Number: 2   Name:   Code: 2 Info: 2
Date: 2-2-2 Amount: 2