首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

恕小弟我眼拙:这个代码如何都看不出哪里出错了

2013-12-11 
恕我眼拙:这个代码怎么都看不出哪里出错了我做题时,按照题目要求写了代码,但打印信息时发现竟然少了一条,

恕我眼拙:这个代码怎么都看不出哪里出错了
    我做题时,按照题目要求写了代码,但打印信息时发现竟然少了一条,但代码中又看不出哪里出了问题。麻烦达人帮忙看看。题目要求如下:

    The Colossus Airlines fleet consists of one plane with a seating capacity of 12. It makes one flight daily. Write a seating reservation program with the following features:
    The program uses an array of 12 structures. Each structure should hold a seat identification number, a marker that indicates whether the seat is assigned, the last name of the seat holder, and the first name of the seat holder.
    The program displays the following menu:

    To choose a function, enter its letter label:
    a) Show number of empty seats
    b) Show list of empty seats
    c) Show alphabetical list of seats
    d) Assign a customer to a seat assignment
    e) Delete a seat assignment
    f) Quit

    The program successfully executes the promises of its menu. Choices d) and e) require additional input, and each should enable the user to abort an entry.
    After executing a particular function, the program shows the menu again, except for choice f).
    Data is saved in a file between runs. When the program is restarted, it first loads in the data, if any, from the file.

未完成的代码如下:

/* ex 8, ch14 */

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

#define LEN 81
#define MAX 21
#define NUM 12
#define SIZE   7
#define ASSIGNED  true
#define UNASSIGNED   false
#define SYMBOL      '*'

typedef struct seatinfo {
char frsnm[LEN];
char lstnm[LEN];
char seatnum[MAX];
bool state;
}INFO;

void menu(void);
int getchoice(void);
int charnum(char **, int);
void printchar(char, int);
void printstr(char **, int);
void eatline(void);
void processchoice(char);
void Exit(FILE *);
void show_number_empty_seats(INFO *, int, FILE *);
void show_empty_seats(INFO *, int, FILE *);
void show_aphabet_list_seats(INFO *, int, FILE *);
void assign_a_seat(INFO *, int, FILE *);
void delete_seat_assignment(INFO *, int, FILE *);

int main(void)
{
    int choice;

    while(1)
    {
        menu();
    choice = getchoice();
    processchoice(choice);
    }

getch();
return 0;
}

void menu(void)
{
char *strings[SIZE] = {
"\nTo choose a function, enter its letter label:",
"a) Show number of empty seats",
"b) Show list of empty seats",
"c) Show alphabetical list of seats",
"d) Assign a customer to a seat assignment",
"e) Delete a seat assignment",
"f) Quit"
};
int maxchar;

maxchar = charnum(strings, SIZE);
printchar(SYMBOL, maxchar);
printstr(strings, SIZE);
printchar(SYMBOL, maxchar);
printf("\nYour choice: ");
}

int charnum(char ** p, int n)
{
int i, max;

max = strlen(p[0]);

for(i = 1; i < n; i++)
if(strlen(p[i]) > max)
max = strlen(p[i]);

return max;
}

void printchar(char sym, int n)
{
int i;

for(i = 0; i < n; i++)
putchar(sym);
}

void printstr(char ** p, int n)
{
int i;

for(i = 0; i < n; i++)
puts(p[i]);
}

int getchoice(void)
{
int ch;
char *str = "abcdef";

ch = getchar();
ch = tolower(ch);
eatline();
while(strchr(str, ch) == NULL)
{
printf("\nInvalid input! Make sure enter a letter among "%s". Input again.", str);


ch = getchar();
eatline();
}
return ch;
}

void eatline(void)
{
while(getchar() != '\n')
continue;
}

void processchoice(char ch)
{
INFO package[NUM] = {
{.seatnum = "CA3225322A", .state = UNASSIGNED},
{.seatnum = "CA2344425B", .state = UNASSIGNED},
{.seatnum = "CA3265927C", .state = UNASSIGNED},
{.seatnum = "CA3225322D", .state = UNASSIGNED},
{.seatnum = "CA4255427A", .state = UNASSIGNED},
{.seatnum = "CA6265425B", .state = UNASSIGNED},
{.seatnum = "CA6235528C", .state = UNASSIGNED},
{.seatnum = "CA5326382D", .state = UNASSIGNED},
{.seatnum = "CA5229323A", .state = UNASSIGNED},
{.seatnum = "CA7225322B", .state = UNASSIGNED},
{.seatnum = "CA7324321C", .state = UNASSIGNED},
{.seatnum = "CA1326322D", .state = UNASSIGNED},
};
INFO * pp = &package[0];
FILE * data;
int index;

if((data = fopen("info.dat", "rb+")) == NULL)    
{
fprintf(stderr, "Error opening file.");
exit(EXIT_FAILURE);
}
if(fread(&pp[0], sizeof(INFO), 1, data) == 0)  //对事先建立的空文档进行试探性读取
        for(index = 0; index < NUM; index++)           //如果是空文档,就进行首次写入,内容为struct的
if(fwrite(&pp[index], sizeof (INFO), 1, data) == 0) //初始化资料
            {
                printf("\nA write error has occurred.");   //我估计就是这个写操作以及接下来的读操作
                exit(EXIT_FAILURE);                        //有问题,但做了诸般测试,还是测不出来
    }
for(index = 0; index < NUM; index++)
if(fread(&pp[index], sizeof (INFO), 1, data) == 0)
{
            printf("\nA read error has occurred.");
            exit(EXIT_FAILURE);
}
switch(ch)
{
case 'a': show_number_empty_seats(pp, NUM, data); break;
case 'b': show_empty_seats(pp, NUM, data); break;
case 'c': show_aphabet_list_seats(pp, NUM, data); break;
case 'd': assign_a_seat(pp, NUM, data); break;
case 'e': delete_seat_assignment(pp, NUM, data); break;
case 'f': Exit(data);
default: break;
}
fclose(data);
}

void Exit(FILE * fd)
{
printf("\nBye!");
fclose(fd);
exit(EXIT_SUCCESS);
}

void show_number_empty_seats(INFO * pt, int n, FILE * fd)
{
int index, empty = 0;

    rewind(fd);
for(index = 0; index < n; index++)
if(pt[index].state == UNASSIGNED)
empty++;
if(empty != 0)
printf("Number of empty seats: %d\n", empty);
else
puts("\nWe're soryy to inform you that no empty seats are left!");
}

void show_empty_seats(INFO * pt, int n, FILE * fd)
{

int index;

    rewind(fd);
for(index = 0; index < n; index++)
        if(pt[index].state == UNASSIGNED)
            printf("%2d-Empty seats: %s\n", index+1, pt[index].seatnum);
}

void show_aphabet_list_seats(INFO * pt, int n, FILE * fd)
{
    int i,j;
INFO temp;

    rewind(fd);
for(i = 0; i < n-1; i++)
for(j = i+1; j < n; j++)
if(strcmp(pt[i].seatnum, pt[j].seatnum) > 0)
{
temp = pt[i];
pt[i] = pt[j];
pt[j] = temp;
}
    for(i = 0; i < n; i++)
printf("%2d-%s\n", i+1, pt[i].seatnum);
}
void assign_a_seat(INFO * pt, int n, FILE * fd)
{
    ;    //这里还没来得及写
}
void delete_seat_assignment(INFO * pt, int n, FILE * fd)
{
    ;   //同上
}

分享到:
[解决办法]
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试是程序员必须掌握的技能之一。

热点排行