/***头文件(.h)****/
#include "stdio.h" /*I/O函数*/ 
#include "stdlib.h" /*其它说明*/ 
#include "string.h" /*字符串函数*/ 
#include "conio.h" /*屏幕操作函数*/ 
#include "mem.h" /*内存操作函数*/ 
#include "ctype.h" /*字符操作函数*/ 
#include "alloc.h" /*动态地址分配函数*/ 
#define N 10
typedef struct nw /*定义数据结构*/
{ char word;      /*生词*/
  char acceptation;  /*词义*/
  char aspirate;     /*音标*/
  int  no[N];       /*序号*/
  char note;         /*注释(包括词性,例句等)*/
  struct nw  *next;
}NEWWORD;
/*以下是函数原型*/ 
NEWWORD *create(); /*2创建链表*/
NEWWORD *input();  /*输入*/
NEWWORD *delete(NEWWORD *h); /*3删除记录*/
void print(NEWWORD *h); /*4显示所有记录*/
void search(NEWWORD *h); /*5查找*/
void save(NEWWORD *h); /*6保存*/
NEWWORD *load(); /*7读入记录*/
NEWWORD *insert(NEWWORD *h); /*8插入记录*/
void append(); /*9追加记录*/
NEWWORD *sort(NEWWORD *h); /*11排序*/
int menu_select(); /*12菜单函数*/
/******主函数开始*******/ 
main() 
{ 
 int i;
 NEWWORD *head; /*链表定义头指针*/
 head=init(); /*初始化链表*/
 clrscr(); /*清屏*/
 {
  switch(menu_select()) /*调用主菜单函数,返回值整数作开关语句的条件*/
  { /*值不同,执行的函数不同,break 不能省略*/
   case 0:head=init();break; /*执行初始化*/
   case 1:head=create();break; /*创建链表*/
   case 2:head=input();break;  /*输入*/
   case 3:head=delete(head);break; /*删除记录*/
   case 4:print(head);break; /*显示全部记录*/
   case 5:search(head);break; /*查找记录*/
   case 6:save(head);break; /*保存文件*/
   case 7:head=load(); break; /*读文件*/
   case 8:head=insert(head); break; /*插入记录*/
   case 9:head=sort(head);break; /*排序*/
   case 10:append();break; /*追加记录*/
   case 11:exit(0); /*如菜单返回值为0程序结束*/
  }
 }
} 
/*菜单函数,返回值为整数12*/
menu_select() 
{ 
 char *menu[]={"***************MENU***************", /*定义菜单字符串数组*/
 "________________________自由软件开发者_高伟______"
 " 0. init list", /*初始化*/
 " 1. Enter list", /*输入记录*/
 " 2. Delete a record from list", /*从表中删除记录*/
 " 3. print list ", /*显示单链表中所有记录*/
 " 4. Search record on word", /*按照生词查找记录*/
 " 5. Save the file", /*将单链表中记录保存到文件中*/
 " 6. Load the file", /*从文件中读入记录*/
 " 7. insert record to list ", /*插入记录到表中*/
 " 8. copy the file to new file", /*复制文件*/
 " 9. sort to make new file", /*排序*/
 " 10. append record to file", /*追加记录到文件中*/
 " 11. Quit"}; /*退出*/
char s[3]; /*以字符形式保存选择号*/ 
int c,i; /*定义整形变量*/ 
gotoxy(1,25); /*移动光标*/ 
printf("press any key enter menu......\n"); /*压任一键进入主菜单*/ 
getch(); /*输入任一键*/ 
clrscr(); /*清屏幕*/ 
gotoxy(1,1); /*移动光标*/ 
textcolor(YELLOW); /*设置文本显示颜色为黄色*/ 
textbackground(BLUE); /*设置背景颜色为蓝色*/ 
gotoxy(10,2); /*移动光标*/ 
putch(0xc9); /*输出左上角边框┏*/ 
for(i=1;i<44;i++) 
putch(0xcd); /*输出上边框水平线*/ 
putch(0xbb); /*输出右上角边框 ┓*/ 
  for(i=3;i<20;i++)
  {
   gotoxy(10,i);putch(0xba); /*输出左垂直线*/
   gotoxy(54,i);putch(0xba); /*输出右垂直线*/
  }
 gotoxy(10,20);putch(0xc8); /*输出左上角边框┗*/
 for(i=1;i<44;i++)
 putch(0xcd); /*输出下边框水平线*/
 putch(0xbc); /*输出右下角边框┛*/
 window(11,3,53,19); /* 制作显示菜单的窗口,大小根据菜单条数设计*/
 clrscr(); /*清屏*/
 for(i=0;i<16;i++) /*输出主菜单数组*/
 {
 gotoxy(10,i+1);
 cprintf("%s",menu[i]);
 }
 textbackground(BLACK); /*设置背景颜色为黑色*/
 window(1,1,80,25); /*恢复原窗口大小*/
 gotoxy(10,21); /*移动光标*/
 do{
 printf("\n Enter you choice(0~13):"); /*在菜单窗口外显示提示信息*/
 scanf("%s",s); /*输入选择项*/
 c=atoi(s); /*将输入的字符串转化为整形数*/
 }while(c<0||c>13); /*选择项不在0~13之间重输*/
return c; /*返回选择项,主程序根据该数调用相应的函数*/
}
{ 
return NULL; 
}
/*创建链表2*/
NEWWORD *create()
{ 
int i;
NEWWORD *h=NULL,*info; /* NEWWORD指向结构体的指针*/
for(;;) 
{ 
info=(NEWWORD *)malloc(sizeof(NEWWORD)); /*申请空间*/
if(!info) /*如果指针info为空*/ 
{ 
printf("\nout of memory"); /*输出内存溢出*/ 
return NULL; /*返回空指针*/ 
} 
inputs("enter no:",info->no,11); /*输入序号并校验*/
if(info->no[0]=='@') break; /*如果序号首字符为@则结束输入*/
inputs("enter word:",info->word,15); /*输入词,并进行校验*/
printf("please input %s newword \n", NEWWORD); /*提示开始输入生词*/
for(i=0;i<N;i++) /*N个生词循环N次*/
{ 
 do{
 printf("no%d:",i+1); /*提示输入第几生词*/
 scanf("%d",&info->word); /*输入生词*/
 if(info->word!=NULL) /*确保生词不为空*/
 printf("repeat input\n"); /*出错提示信息*/
 }while(info->word=NULL);
} 
info->no=0; /*未排序前此值为0*/
info->next=h; /*将头结点做为新输入结点的后继结点*/ 
h=info; /*新输入结点为新的头结点*/ 
} 
return(h); /*返回头指针*/ 
}
/*输入*/
NEWWORD *input()
{
 NEWWORD *p1,p2,head;                             /* NEWWORD指向结构体的指针*/
 head=p2=(NEWWORD*)malloc(sizeof(NEWWORD));       /*申请空间*/
 clrscr();                                        /*清屏*/
 printf("please input newword:\n");
 for(;;)                                         /*永循环*/
 {
  p1=(NEWWORD*)malloc(sizeof(NEWWORD));         /*p1指向新申请的空间*/
  printf("please input no;\n");
  scanf("%d",&p1->no);
  if(p1->no==0)                                /*序号为0跳出*/
   {
    p2->next=NULL;
    break;
   }
 printf("|%s|%-10s|%-15s|%10s|%d|\n", p1->word,p1->acceptation,p1->aspirate,p1->note,p1->no);
 p2->next=p1;
 p2=p1;
 }
 clrscr();  /*清屏*/
 printf("\n输入完成请保存:");
 getch();
 return(head);
}
/*删除记录3*/
NEWWORD *delete(newword *h)
{ 
NEWWORD *p,*q; /*p为查找到要删除的结点指针,q为其前驱指针*/
char s[11]; /*存放序号*/
clrscr(); /*清屏*/ 
printf("please deleted no\n"); /*显示提示信息*/ 
scanf("%s",s); /*输入要删除记录的序号*/
q=p=h; /*给q和p赋初值头指针*/ 
while(strcmp(p->no,s)&&p!=NULL) /*当记录的序号不是要找的,或指针不为空时*/
{ 
 q=p; /*将p指针值赋给q作为p的前驱指针*/
 p=p->next; /*将p指针指向下一条记录*/
} 
if(p==NULL) /*如果p为空,说明链表中没有该结点*/ 
printf("\nlist no %s NEWWORD\n",s);
else /*p不为空,显示找到的记录信息*/ 
{ 
  printf("*****************************have found***************************\n");
  printf("word| acceptation| aspirate| note | no|\n");
  printf("|---|------------|---------|------|---|\n");
  printf("|%-10s|%-15s|%-10s|%-20s| %3d |\n", p->word,p->accptation,p->aspirate,p->note,p->no);
  printf("********************************end*******************************\n");
 getch(); /*压任一键后,开始删除*/
 if(p==h) /*如果p==h,说明被删结点是头结点*/
 h=p->next; /*修改头指针指向下一条记录*/
 else
 q->next=p->next; /*不是头指针,将p的后继结点作为q的后继结点*/
 free(p); /*释放p所指结点空间*/
 printf("\n have deleted no %s NEWWORD\n",word);
 printf("Don't forget save\n");/*提示删除后不要忘记保存文件*/
} 
return(h); /*返回头指针*/ 
} 
/*输出链表中结点信息4*/
void print(NEWWORD *h)
{ 
 int i=0; /* 统计记录条数*/
 NEWWORD *p; /*移动指针*/
 clrscr(); /*清屏*/
 p=h; /*初值为头指针*/
 printf("\n\n\n************NEWWORD*************\n");
 printf("word| acceptation| aspirate| note | no|\n");
 printf("|---|------------|---------|------|---|\n");
 while(p!=NULL)
{ 
 i++;
 printf("|%d|%s|%-10s|%-15s|%10s\n", i, p->word,p->acceptation,p->aspirate,p->note,p->no);
 p=p->next;
} 
printf("**********************************end*********************************\n"); 
} 
/*查找记录5*/
void search(NEWWORD *h)
{ 
  NEWWORD *p; /* 移动指针*/
  char s[15]; /*存放词的字符数组*/
  clrscr(); /*清屏幕*/
  printf("please enter newword for search\n");
  scanf("%s",s); /*输入词*/
  p=h; /*将头指针赋给p*/
  while(strcmp(p->word,s)&&p!=NULL) /*当记录的词不是要找的,或指针不为空时*/
  p=p->next; /*移动指针,指向下一结点*/
 if(p==NULL) /*如果指针为空*/
 printf("\nlist no %s student\n",s); /*显示没有该词*/
 else /*显示找到的记录信息*/
 {
 printf("\n\n*****************************havefound***************************\n");
 printf("word| acceptation| aspirate| note | no|\n");
 printf("|---|------------|---------|------|---|\n");
 printf("|%-10s|%-15s|%-10s|%-20s| %3d |\n", p->word,p->accptation,p->aspirate,p->note,p->no);
 printf("********************************end*******************************\n");
 }
}   
/*保存数据到文件6*/
void save(NEWWORD *h)
{ 
 FILE *fp; /*定义指向文件的指针*/
 NEWWORD *p; /* 定义移动指针*/
 char outfile[10]; /*保存输出文件名*/
 printf("Enter outfile word,for example c:\\f1\\te.txt:\n"); /*提示文件名格式信息*/
 scanf("%s",outfile);
 if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,如没有则建立*/
{ 
 printf("can not open file\n");
 exit(1);
} 
 printf("\nSaving file......\n"); /*打开文件,提示正在保存*/
 p=h; /*移动指针从头指针开始*/
 while(p!=NULL) /*如p不为空*/
{
 fwrite(p,sizeof(NEWWORD),1,fp);/*写入一条记录*/
 p=p->next; /*指针后移*/
} 
 fclose(fp); /*关闭文件*/
 printf("-----save success!!-----\n"); /*显示保存成功*/
} 
/* 从文件读数据7*/
NEWWORD *load()
{ 
 NEWWORD *p,*q,*h=NULL; /*定义记录指针变量*/
 FILE *fp; /* 定义指向文件的指针*/
 char infile[10]; /*保存文件名*/
 printf("Enter infile name,for example c:\\f1\\te.txt:\n"); scanf("%s",infile); /*输入文件名*/
 if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为读方式*/
{
 printf("can not open file\n"); /*如不能打开,则结束程序*/
 exit(1);
} 
 printf("\n -----Loading file!-----\n");
 p=(NEWWORD *)malloc(sizeof(NEWWORD)); /*申请空间*/
if(!p) 
{ 
 printf("out of memory!\n"); /*如没有申请到,则内存溢出*/
 return h; /*返回空头指针*/
} 
 h=p; /*申请到空间,将其作为头指针*/
 while(!feof(fp)) /*循环读数据直到文件尾结束*/
{ 
 if(1!=fread(p,sizeof(NEWWORD),1,fp))
 break; /*如果没读到数据,跳出循环*/
 p->next=(NEWWORD *)malloc(sizeof(NEWWORD)); /*为下一个结点申请空间*/
 if(!p->next)
{ 
 printf("out of memory!\n"); /*如没有申请到,则内存溢出*/
 return h;
} 
 q=p; /*保存当前结点的指针,作为下一结点的前驱*/
 p=p->next; /*指针后移,新读入数据链到当前表尾*/
} 
 q->next=NULL; /*最后一个结点的后继指针为空*/
 fclose(fp); /*关闭文件*/
 printf("---You have success read data from file!!!---\n");
 return h; /*返回头指针*/
} 
/*插入记录8*/
NEWWORD *h)
{ 
NEWWORD *p,*q,*info; /*p指向插入位置,q是其前驱,info指新插入记录*/
char s[11]; /*保存插入点位置的序号*/
int nw,i;
printf("please enter location before the no\n"); 
scanf("%s",s); /*输入插入点序号*/
printf("\nplease new record\n"); /*提示输入记录信息*/ 
info=(NEWWORD *)malloc(sizeof(NEWWORD)); /*申请空间*/
if(!info) 
{ 
 printf("\nout of memory"); /*如没有申请到,内存溢出*/
 return NULL; /*返回空指针*/
} 
inputs("enter no:",info->no,11); /*输入序号*/
inputs("enter word:",info->word,15); /*输入生词*/
inputs("enter aspirate",info->aspirate,12);  /*输入音标*/
printf("please input %s acceptation \n",acceptation); /*提示输入词义*/
printf("please input %s note",note);   /*提示输入注释*/
while(strcmp(p->no,s)&&p!=NULL) /*查找插入位置*/ 
{ 
 q=p; /*保存指针p,作为下一个p的前驱*/
 p=p->next; /*将指针p后移*/
} 
if(p==NULL) /*如果p指针为空,说明没有指定结点*/ 
if(p==h) /*同时p等于h,说明链表为空*/ 
h=info; /*新记录则为头结点*/ 
else 
q->next=info; /*p为空,但p不等于h,将新结点插在表尾*/ 
else 
if(p==h) /*p不为空,则找到了指定结点*/ 
{ 
 info->next=p; /*如果p等于h,则新结点插入在第一个结点之前*/
 h=info; /*新结点为新的头结点*/
} 
else 
{ 
 info->next=p; /*不是头结点,则是中间某个位置,新结点的后继为p*/
 q->next=info; /*新结点作为q的后继结点*/
} 
printf("\n ----have inserted %s NEWWORD----\n",info->word); printf("---Don't forget save---\n"); /*提示存盘*/
return(h); /*返回头指针*/ 
}
/*追加记录到文件9*/
void append() 
{ 
 FILE *fp; /*定义指向文件的指针*/
 NEWWORD *info; /*新记录指针*/
 int nw,i;
 char infile[10]; /*保存文件名*/
 printf("\nplease new record\n");
 info=(NEWWORD *)malloc(sizeof(NEWWORD)); /*申请空间*/
 if(!info)
{ 
 printf("\nout of memory"); /*没有申请到,内存溢出本函数结束*/
 return ;
} 
 inputs("enter no:",info->no,11); /*输入序号*/
 inputs("enter word:",info->word,15); /*输入生词*/
 inputs("enter aspirate",info->aspirate,12);  /*输入音标*/
 printf("please input %s acceptation \n",acceptation); /*提示输入词义*/
 printf("please input %s note",note);   /*提示输入注释*/
}
/*排序*/
NEWWORD *sort()
{
}

Recommended Answers

All 2 Replies

where is what wrong? Do you get compile errors ? If yes, then post some of them. Sorry, but I don't have my crystle ball with me today :)

Some error I found. By any means not the only ones. I did not pay attention to the logic of your program. I am sure that there's need to considere that part too, id est free any of the dynamic memory that you call after been used.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "mem.h"
#include "ctype.h"
#include "alloc.h"

/* Should be */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <mem.h>
#include <ctype.h>
#include <alloc.h>
/* if you don't have those header files in the current directory as I suspect */



NEWWORD *head; /* head is a pointer to a structure of type NEWWORD */
head=init(); /* You are trying to point to something not of type NEWWORD */


{ 
return NULL; /* Outside any function, does nothing but compiler error */
}



printf("please input %s newword \n", NEWWORD); /* %s is format for string, you are trying to pass it NEWWORD
which is the definer for a type of structure value */



if(info->word!=NULL) /* info->word points to a char value element in a structure of type NEWWORD; NULL is  a null pointer define as ((void *)0); ASCII (NUL) '\0' is not the same as NULL which is the value that would be in the char if the comparison were "false" */



while(info->word=NULL) /* the use of = is for assigment not comparison (==) */
info->no=0; /* info->no is an array of integers; wrong assigment */


NEWWORD *p1,p2,head;           
head=p2=(NEWWORD*)malloc(sizeof(NEWWORD)); /* p2 and head are not pointers to structures of type NEWWORD, dynamic memory needs pointers to point to it */


NEWWORD *delete(newword *h) /* newword is not the same that NEWWORD */


while(strcmp(p->no,s)&&p!=NULL) /* the strcmp function is prototyped as int strcmp( const char *str1, const char *str2 ); the first argument should be an array of chars, you are passing an array of ints */


while(strcmp(p->word,s)&&p!=NULL) /* same than above but with a char instead of an array of chars */

printf("|%-10s|%-15s|%-10s|%-20s| %3d |\n", p->word,p->accptation,p->aspirate,p->note,p->no); /* mispelling structure element acceptation; futher more p->no needs indexing since it's an array of integers */

printf("\n have deleted no %s NEWWORD\n",word); /* word is not declared as a string */

NEWWORD *h) /* not a proper function definition */


/*These statements are outside a function */
 inputs("enter no:",info->no,11);
 inputs("enter word:",info->word,15);
 inputs("enter aspirate",info->aspirate,12); 
 printf("please input %s acceptation \n",acceptation); /* wrong variable type, moreover not a string */ 
 printf("please input %s note",note); /* same that above */
/* end of statements block */

NEWWORD *sort() /* at least it should return a pointer to a structure of type NEWWORD */
{
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.