- 论坛徽章:
- 0
|
首先想问下大家:你们在编译程序的时候,用过Dev-C++个吗?小弟在用这个的同时,还用VC60编译器。如下是我写的一个小程序,望大家指教:
#include <stdio.h>;
#include <stdlib.h>;
#include <malloc.h>;
#include <string.h>;
typedef struct list_t {
int id;
char name[41];
int age;
struct list_t *next;
}LIST;
LIST *add_node(int id, char name[], int age, LIST *p);
LIST *remove_node(int id, LIST *p);
void display_node(LIST *head);
int save_list(LIST *head);
LIST *load_list(void);
void free_node(LIST *head);
int main(void)
{
int a, id, age;
char ch, s[50], con = 'Y', name[41];
LIST *head = NULL;
if((head = (LIST *)malloc(sizeof(LIST))) == NULL) {
printf("Insertion is not success!"
exit(1);
}
head->;next = NULL;
do {
printf("A(Add) R(Remove) D(Display) S(Save) L(Load) Q(QUIT)\n"
printf("lease change:"
scanf("%s", &ch);
gets(s);
switch(ch) {
case 'A':
do {
while(1) {
printf("lease input the \"id\":"
scanf("%d", &id);
gets(s);
if(id < 1 || id >; 9999 ) {
printf("error!\n"
continue;
}
break;
}
while(1) {
printf("lease input the \"name\"(less than 40 letters):"
scanf("%s", &name);
gets(s);
a = strlen(name);
if(a >; 40) {
printf("error!\n"
continue;
}
break;
}
while(1) {
printf("lease input the \"age\"(1 - 200):"
scanf("%d", &age);
gets(s);
if(age < 1 || age >; 200) {
printf("error!\n"
continue;
}
break;
}
head = add_node(id, name, age, head);
printf("Continue?(Y/N):"
scanf("%s", &con);
gets(s);
while((con != 'Y') && (con != 'N')) {
printf("Input error!\n");
printf("Continue?(Y/N):");
scanf("%s", &con);
gets(s);
if((con == 'N') || (con == 'Y')) {
break;
}
}
}while(con == 'Y');
break;
case 'R':
if(head->;next == NULL) {
printf("The list is empty!\n");
break;
}
printf("lease input the \"id\":");
scanf("%d", &id);
gets(s);
head = remove_node(id, head);
break;
case 'D':
display_node(head);
break;
case 'S':
save_list(head);
printf("File Save OK!\n");
break;
case 'L':
free_node(head->;next);
head = load_list();
break;
case 'Q':
printf("The end!\n");
free_node(head->;next);
exit(0);
default:
printf("Input error! Again!\n");
}
} while(ch != '\0');
return 0;
}
LIST *add_node(int id, char name[], int age, LIST *p)
{
LIST *head = p, *pr = NULL, *new = NULL;
while(p != NULL && p ->; id < id) {
pr = p;
p = p ->; next;
}
if((new = (LIST *)malloc(sizeof(LIST))) == NULL) {
printf("Insertion is not success!");
exit(1);
} else {
new->;id = id;
strcpy(new->;name, name);
new->;age = age;
new->;next = pr->;next;
pr->;next = new;
}
return (head);
}
LIST *remove_node(int id, LIST *p)
{
LIST *head = p;
LIST *p1 = NULL;
LIST *p2 = NULL;
p1 = head;
while(id != p1->;id && p1->;next != NULL) {
p2 = p1;
p1 = p1->;next;
}
if(id == p1->;id) {
if(p1 == head) {
head = p1->;next;
printf("delete OK!\n");
} else {
p2->;next = p1->;next;
free(p1);
printf("delete OK!\n");
}
} else {
printf("%d not in list !\n", id);
}
return (head);
}
void display_node(LIST *head)
{
LIST *p;
if(head->;next == NULL) {
printf("The list is empty!\n");
} else {
printf("Output the id :\n");
p = head->;next;
while(p != NULL) {
printf("id: %d\n", p->;id);
printf("name: %s\n", p->;name);
printf("age: %d\n\n", p->;age);
p = p->;next;
}
}
}
int save_list(LIST *head)
{
LIST *p;
FILE *fp;
if((fp = fopen("a.txt", "w")) == NULL) {
return(1);
} else {
if(head->;next == NULL) {
fprintf(fp, "The list is empty! Please add data first!\n");
} else {
p = head->;next;
fprintf(fp, "ID\tName\tAge\n");
while(p != NULL) {
fprintf(fp, "%d\t%s\t%d\n", p->;id, p->;name, p->;age);
p = p->;next;
}
}
}
fclose(fp);
return 0;
}
LIST *load_list(void)
{
int a = 12, id, age, i;
char strid[6], name[41] = "", strage[4];
FILE *fp;
LIST *head = NULL;
if((fp = fopen("a.txt", "r")) == NULL) {
printf("Cannot open file!\n");
exit(0);
}
fseek(fp, a, 0);
for(i = 1; ;i ++) {
if(fscanf(fp, "%s", strid) == EOF) {
break;
} else {
id = atoi(strid);
if(id == 0) {
printf("The list is empty! Please add data first!\n");
break;
}
fseek(fp, a = (a + strlen(strid) + 1), 0);
fscanf(fp, "%s", name);
fseek(fp, a = (a + strlen(name) + 1), 0);
fscanf(fp, "%s", strage);
age = atoi(strage);
fseek(fp, a = (a + strlen(strage) + 3), 0);
head = add_node(id, name, age, head);
}
}
fclose(fp);
return(head);
}
void free_node(LIST *head)
{
LIST *nextdata = NULL;
while(head != NULL) {
nextdata = head;
free(head);
head = nextdata;
}
}
有这样的问题:(1)我用Dev-C++编译的时候,在做add_node函数调用的时候说“指定的内存不能‘read’”是什么问题?而在VC60下编译,就没这样的问题。(2)我的load_node函数为什么不能执行?请大家给予指教。 谢谢 。 |
|