呵呵,终于通过运行了:) 说来惭愧,我学习C语言已经有两年了(断断续续,同时在看C++,Java),自己感觉学得还可以,所以认为这个题目应该很简单。结果昨天花了一晚上也没有搞定(因为很长时间没有使用C了,所以在指针理解上出了问题,一个链表总是不能正确连接,看来要真正掌握C语言的指针确实要花功夫。),今天早上再看了看才把程序做好: 我的程序由三个文件组成:list.h, list.c, main.c(在VC, Dev-C++中建立一个工程然后把文件加进去就可以运行了)
还有一个问题是在结构体中的学号(id),经过保存,读取这个过程之后结果会变不正确。(估计是个X86计算机的体系结构有关系,不知哪位大虾能解释一下。)
下面是源程序:
/* list.c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include \"list.h\"Student *head = NULL; /* the head of the list */
void createList(void) { /* The first node doesn't contain useful data */ head = (Student *)malloc(sizeof(Student));
if(head) head->next = NULL; else { printf(\"Memory Access Failed!\n\"); exit(1); } }
void addNode(void) { Student *ptr, *pre; Student *temp = (Student *)malloc(sizeof(Student));
if(temp) { printf(\"\nStudent ID: \"); scanf(\"%d\", &temp->id); printf(\"Name: \"); scanf(\"%s\", temp->name); printf(\"Math. Score: \"); scanf(\"%d\", &temp->math); printf(\"Computer Score: \"); scanf(\"%d\", &temp->computer); temp->next = NULL; } else { printf(\"Memory Access Failed!\n\"); exit(1); }
pre = head; ptr = head->next; while(ptr) { /* find the right place to insert */ if(temp->id > ptr->id) { pre = ptr; ptr = ptr->next; } else break; }
if(ptr == NULL) /* insert at last */ pre->next = temp; else { /*insert in the middle */ pre->next = temp; temp->next = ptr; } }
void saveList(void) { FILE *fp; Student *ptr = head->next;
fp = fopen(\"score\", \"wb\"); if(fp == NULL) { printf(\"Can not open file!\n\"); exit(1); }
while(ptr) { if(fwrite(ptr, sizeof(Student), 1, fp) == 1) ptr = ptr->next; else { printf(\"File write error!\n\"); exit(1); } } fclose(fp); }
void readList(void) { FILE *fp; Student *ptr, *pre;
fp = fopen(\"score\", \"rb\"); if(fp == NULL) { printf(\"Can not open file!\n\"); exit(1); }
head = (Student *)malloc(sizeof(Student)); if(head == NULL) { printf(\"Memory Access failed!\n\"); exit(1); } else head->next = NULL;
pre = head; ptr = (Student *)malloc(sizeof(Student)); if(ptr == NULL) { printf(\"Memory Access Failed\n\"); exit(1); } else ptr->next = NULL;
while(fread(ptr, sizeof(Student), 1, fp) == 1) { pre->next = ptr; pre = ptr; ptr = (Student *)malloc(sizeof(Student)); if(ptr == NULL) { printf(\"Memory Access Failed!\n\"); exit(1); } else ptr->next = NULL; }
fclose(fp); }
void displayList(void) { Student *ptr = head->next;
printf(\"\n=================================\n\"); printf(\"==== The Students' Message ====\n\"); printf(\"=================================\n\"); while(ptr) { printf(\"\nStudnet ID: %d\n\", ptr->id); printf(\"Name: %s\n\", ptr->name); printf(\"Math. Score: %d\n\", ptr->math); printf(\"Computer Score: %d\n\", ptr->computer); ptr = ptr->next; } }
void clearList(void) { Student *pre = head; Student *ptr;
while(pre) { ptr = pre->next; free(pre); pre = ptr; } }
int isEmpty(void) { return (head == NULL) ? 1 : 0; }
[此贴子已经被作者于2004-07-25 12:13:13编辑过]
还有两个文件:
/* list.h *//* This file contains the basic data structures and the fuction declarations that will be used in other files. */
/* The struct represents a student */ typedef struct node { int id; char name[20]; int math; int computer; struct node *next; } Student;
/* create the list */ void createList(void);
/* add a node to the list */ void addNode(void);
/* save the data to hard disk */ void saveList(void);
/* clear the list */ void clearList(void);
/* read the data from disk */ void readList(void);
/* display the list */ void displayList(void);
/* whether the list is empty */ int isEmpty(void);
/* main.c */ #include <stdio.h> #include \"list.h\"
int menu(void) { int choice;
printf(\"\n-----------MENU----------\n\"); printf(\"1 - Add a student's data\n\"); printf(\"2 - Save the data\n\"); printf(\"3 - Display the data\n\"); printf(\"4 - Quit\n\"); printf(\"Enter your choice: \"); scanf(\"%d\", &choice);
return choice; }
int main(void) { int choice;
createList(); choice = menu();
while(choice != 4) { switch(choice) { case 1: addNode(); break; case 2: saveList(); clearList(); break; case 3: if(isEmpty()) { readList(); displayList(); } else displayList(); break; case 4: break; default: printf(\"Wrong input!\n\"); break; } choice = menu(); }
if(!isEmpty()) clearList();
return 0; }
[此贴子已经被作者于2004-07-25 12:15:33编辑过]
帮我看看这个程序有没有错误的地方 谢谢各位 !!!
#include <stdio.h>
main()
{ FILE *fp;
struct st
{ int number;
char name[20];
float math;
float computer;
float total;
} student;
int i,j;
if((fp=fopen("score","wb+"))==NULL)
{ printf("file open error\n");
exit(0);
}
for(i=0;i<30;i++)
{ scanf("%d,%20s,%f,%f",
&student.number,student.name,&student.math,&student.computer);
student.total=student.math+student.computer;
j=student.number-1;
fseek(fp,(long)j*sizeof(struct st),SEEK_SET);
if(fwrite(&student,sizeof(student),1,fp)!=1) printf("write file error\n");
}
fclose(fp);
}
[此贴子已经被作者于2004-07-26 17:36:19编辑过]