整理资料时发现了大一时C程序设计的大作业

源代码在线查看: function.c

软件大小: 145 K
上传用户: Numb_pqc
关键词: 发现 C程序设计
下载地址: 免注册下载 普通下载 VIP

相关代码

				/*** function.c***/
				/*添加联系人*/
				#include "E:\CONTACT.c"
				void ContactAdd()
				{
				   FILE *fp;
				   Contact mycontact;
				   fp=fopen("contact.txt","rb");/*读方式打开文件*/
				   if(fp==NULL)
				      fp=fopen("contact.txt","wb"); /*写方式打开文件*/
				   else/*如果有联系人数据库就添加*/
				   {
				      fclose(fp);
				      fp=fopen("contact.txt","ab");/*追加方式打开文件*/
				   }
				   ClrScr();/*清屏*/
				   printf("Intput new contact name: ");/*逐个输入新联系人的资料*/
				   gets(mycontact.name);
				   gotoxy(2,4);
				   printf("Input the phone munber: ");
				   gets(mycontact.phone);
				   strcpy(mycontact.group,"");
				   mycontact.yes=1;/*表示联系人未分组*/
				   mycontact.next=NULL;
				   fwrite(&mycontact,sizeof(Contact),1,fp);/*块写*/
				   fclose(fp); /*关闭文件*/
				   ClrScr();
				}
				/*联系人查询*/
				void ContactFind()
				{
				   FILE *fp;
				   Contact mycontact;
				   char contactname[20]; /*联系人*/
				   int flag=0,i=0;
				   ClrScr();
				   gotoxy(2,3);
				   printf("Input the name: ");
				   gets(contactname); /*输入要查询的联系人*/
				   fp=fopen("contact.txt","rb");
				   if(fp==NULL)/*没找到文件*/
				   {
				      ClrScr();
				      gotoxy(2,3);
				      printf("Error!");
				      fclose(fp);
				      return;
				   }
				   else
				      while(!feof(fp))/*查询联系人*/
				      {
				     fread(&mycontact,sizeof(Contact),1,fp);
				     if(strcmp(mycontact.name,contactname)==0)/*如果查找到*/
				     {
				        ClrScr();
				        gotoxy(2,3);
				        printf("%s's information as follow:",mycontact.name);
				        gotoxy(2,4);
				        printf("phone number:%s",mycontact.phone);
				        gotoxy(2,5);
				        printf("group:%s",mycontact.group);
				        i++;
				        flag=1; /*查找数据标志*/
				     }
				      }
				   if(flag==0)
				   {
				      ClrScr();
				      gotoxy(2,3);
				      printf("Can't found!");
				   }
				   fclose(fp);
				}
				/*删除联系人*/
				void ContactDel()
				{
				   FILE *fp;
				   Contact *head,*p,*q;
				   char contactname[20];
				   fp=fopen("contact.txt","rb");/*读方式打开文件*/
				   if(fp==NULL)/*没找到文件*/
				   {
				      gotoxy(2,3);
				      printf("Error!");
				      fclose(fp);
				      return;
				   }
				   head=p=q=(Contact*)malloc(sizeof(Contact));/*将文件内容放入链表*/
				   fread(p,sizeof(Contact),1,fp);   /*读第一条记录*/
				   while(!feof(fp))
				   {
				      q=p;
				      p=(Contact*)malloc(sizeof(Contact));
				      fread(p,sizeof(Contact),1,fp); /*读记录*/
				      q->next=p;/*新记录插入到链表的尾*/
				   }
				   p->next=NULL;/*最后一个结点的后继为空*/
				   fclose(fp);
				   ClrScr();
				   printf("Input the name: ");
				   gets(contactname); /*输入联系人*/
				   p=head;
				   while(p!=NULL)/*按名字查找要删除的联系人*/
				   {
				      if(strcmp(p->name,contactname)==0)/*找到要删除的联系人*/
				      {
				     if(p==head) /*如果是第一个结点*/
				        head=head->next;
				     else
				        q->next=p->next;/*不是第一个结点*/
				     break;
				      }
				      q=p;  /*指针后移*/
				      p=p->next;
				   }
				   fp=fopen("contact.txt","wb");/*删除后从链表的头开始重新保存*/
				   while(head!=NULL)
				   {
				      fwrite(head,sizeof(Contact),1,fp);/*块写*/
				      head=head->next;
				   }
				   fclose(fp);
				   ClrScr();
				}
				 /*分组*/
				void ContactGroup()
				{
				   FILE *fp;
				   Contact mycontact;
				   Group  mygroup;
				   char contactname[20],groupname[20];
				   int flag=0,mflag=0;
				   ClrScr();
				   gotoxy(2,3);
				   printf("Input the group name: ");
				   gets(groupname); /*输入联系人*/
				   ClrScr();
				   if((fp=fopen("group.txt","rb"))==NULL)/*没找到文件*/
				   {
				      gotoxy(3,3);
				      printf("Cannot open file!\n");
				      return;
				   }
				   else
				      while(!feof(fp))/*查询联系人资料*/
				      {
				     fread(&mygroup,sizeof(Group),1,fp);
				     if(strcmp(mygroup.name,groupname)==0)
				     {
				        mflag=1; /*存在该分组*/
				        break;
				     }
				      }
				   fclose(fp);
				   if(mflag==0)
				   {
				      gotoxy(2,4);
				      printf("Can't find the group");/*没有该分组*/
				      return;
				   }
				   gotoxy(3,5);
				   printf("Input the contact name: ");
				   gets(contactname); /*输入联系人*/
				   ClrScr();
				   if((fp=fopen("contact.txt","rb+"))==NULL)/*没找到文件*/
				   {
				      gotoxy(2,3);
				      printf("Can't open file!\n");
				      return;
				   }
				   while(!feof(fp))/*查询联系人资料*/
				   {
				      fread(&mycontact,sizeof(Contact),1,fp);
				      if(strcmp(mycontact.name,contactname)==0)
				      {
				     if(mycontact.yes==0)/*已经分组的判断*/
				     {
				        gotoxy(2,3);
				        printf("This contact has groupped!\n");
				        break;
				     }
				     else
				     {
				        mycontact.yes=0; /*作分组标志*/
				        strcpy(mycontact.group,groupname);/*登记分组*/
				        fseek(fp,-1L*sizeof(Contact),1);/*从当前位置前移一条记录指针*/
				        fwrite(&mycontact,sizeof(Contact),1,fp);/*写记录*/
				        gotoxy(2,3);
				        printf("Group success!");
				        flag=1;/*分组标志*/
				        break;
				     }
				      }
				   }
				   if(flag!=1)
				   {
				      ClrScr();
				      gotoxy(2,3);
				      printf("Group fail!\n");/*分组*/
				   }
				   fclose(fp);
				}
				/*取消分组*/
				void ContactGroupNo()
				{
				   FILE *fp;
				   Contact mycontact;
				   char contactname[20];
				   int flag=0;
				   ClrScr();
				   if((fp=fopen("contact.txt","rb+"))==NULL)/*没找到文件*/
				   {
				      gotoxy(2,3);
				      printf("Can't open the file!\n");
				      return;
				   }
				   printf("Input the contact name: ");
				   gets(contactname); /*输入联系人名*/
				   ClrScr();
				   while(!feof(fp))/*查询联系人资料*/
				   {
				      fread(&mycontact,sizeof(Contact),1,fp);
				      if(strcmp(mycontact.name,contactname)==0)
				      {
				     if(mycontact.yes==0)/*已经分组的判断*/
				     {
				        mycontact.yes=1;/*未分组标志*/
				        strcpy(mycontact.group,"");
				        fseek(fp,-1L*sizeof(Contact),1);
				        fwrite(&mycontact,sizeof(Contact),1,fp);
				        gotoxy(2,3);
				        printf("Group delete success!");
				        flag=1;
				        break;
				     }
				      }
				   }
				   if(flag!=1)
				      printf("Group delete fail!\n");
				   fclose(fp);
				}
				/*清除屏幕*/
				void ClrScr()
				{
				   int i,j;
				   puttext(2,3,78,23,c);/*刚开始已经用gettext把蓝色的一块地方保存下来,现在再还原*/
				   gotoxy(2,3);
				}
				/*增加分组*/
				void GroupAdd()
				{
				   FILE *fp;
				   Group mygroup;
				   fp=fopen("group.txt","rb");
				   if(fp==NULL)/*如果分组数据库没就新建*/
				      fp=fopen("group.txt","wb");
				   else/*如果有分组数据库就添加*/
				   {
				      fclose(fp);
				      fp=fopen("group.txt","ab");/*追加方式打开*/
				   }
				   ClrScr();
				   gotoxy(2,3);
				   printf("Intput new group name: ");/*逐个输入分组资料*/
				   gets(mygroup.name);
				   gotoxy(2,4);
				   printf("Input group memo: ");
				   gets(mygroup.memo);
				      fwrite(&mygroup,sizeof(Group),1,fp);/*写入记录*/
				   fclose(fp);
				   ClrScr();
				}
				/*查询分组*/
				void GroupFind()
				{
				   FILE *fp;
				   Group mygroup;
				   char groupname[20];
				   int flag=0;
				   ClrScr();
				   gotoxy(2,3);
				   printf("Input the group name: ");/*输入要查询的分组名*/
				   gets(groupname);
				   fp=fopen("group.txt","rb");
				   if(fp==NULL)/*没找到文件*/
				   {
				      ClrScr();
				      gotoxy(2,3);
				      printf("Error!");
				      fclose(fp);
				      return;
				   }
				   while(!feof(fp))/*查询分组资料*/
				   {
				      fread(&mygroup,sizeof(Group),1,fp);
				      if(strcmp(mygroup.name,groupname)==0)/*比较*/
				      {
				     ClrScr();
				     gotoxy(2,3);
				     printf("%s's information as follow:",mygroup.name);
				     gotoxy(2,4);
				     printf("memo: %s",mygroup.memo);
				     flag=1;
				     break;
				      }
				   }
				   if(flag==0)
				   {
				      ClrScr();
				      gotoxy(2,3);
				      printf("Can't found this group!");
				   }
				   fclose(fp);
				}
				/*删除分组*/
				void GroupDel()
				{
				   FILE *fp;
				   Group *head,*p,*q;
				   char groupname[20];
				   fp=fopen("group.txt","rb");
				   if(fp==NULL)/*没找到文件*/
				   {
				      gotoxy(2,3);
				      printf("Error!");
				      fclose(fp);
				      return;
				   }
				   head=p=q=(Group*)malloc(sizeof(Group));/*将文件内容放入链表*/
				   fread(p,sizeof(Group),1,fp);
				   while(!feof(fp))
				   {
				      q=p;
				      p=(Group*)malloc(sizeof(Group));
				      fread(p,sizeof(Group),1,fp);/*读记录*/
				      q->next=p;/*链入到链表的尾部*/
				   }
				   p->next=NULL;
				   fclose(fp);
				   ClrScr();
				   printf("Input the group name: ");/*输入分组名*/
				   gets(groupname);
				   p=head;
				   while(p!=NULL)/*按名字查找要删除的联系人*/
				   {
				      if(strcmp(p->name,groupname)==0)/*查找分组*/
				      {
				     if(p==head)
				        head=head->next;/*如果是第一条记录*/
				     else
				        q->next=p->next;/*不是第一条记录*/
				     break;
				      }
				      q=p;  /*没找到,则指针后移继续找*/
				      p=p->next;
				   }
				   fp=fopen("group.txt","wb");/*删除后重新保存*/
				   while(head!=NULL)
				   {
				      fwrite(head,sizeof(Group),1,fp);/*块写*/
				      head=head->next;
				   }
				   fclose(fp);
				   ClrScr();
				}
				/*查询分组包含的联系人*/
				void GroupList()
				{
				   FILE *fp;
				   Contact mycontact;
				   Group mygroup;
				   char contactname[20],groupname[20];
				   int flag=0,mflag=0,i=0;
				   ClrScr();
				   gotoxy(2,3);
				   printf("Input the group name: ");/*输入分组名*/
				   gets(groupname);
				   if((fp=fopen("group.txt","rb"))==NULL)/*没找到文件*/
				   {
				      gotoxy(3,3);
				      printf("Can't open the file!\n");
				      return;
				   }
				   else
				      while(!feof(fp))/*查询分组是否存在*/
				      {
				     fread(&mygroup,sizeof(Group),1,fp);
				     if(strcmp(mygroup.name,groupname)==0)
				     {
				        mflag=1;
				        break;
				     }
				      }
				   fclose(fp);
				   if(mflag==0)
				   {
				      gotoxy(2,4);
				      printf("No the group!");
				      return;
				   }
				   ClrScr();
				   if((fp=fopen("contact.txt","rb+"))==NULL)/*没找到文件*/
				   {
				      gotoxy(2,3);
				      printf("Cannot open file!\n");
				      return;
				   }
				   while(!feof(fp))/*查询联系人资料*/
				   {
				      fread(&mycontact,sizeof(Contact),1,fp);
				      gotoxy(2,3);
				      printf("This group %s contact follow:",groupname);
				      gotoxy(2,4);
				      printf("********************************************");
				      if(strcmp(mycontact.group,groupname)==0)
				      {
				     gotoxy(2,5+i*4);
				     printf("Contact name:%s",mycontact.name);
				     gotoxy(2,6+i*4);
				     printf("phone:%s",mycontact.phone);
				     printf("\n");
				     i++;
				     flag=1;
				      }
				   }
				   if(flag!=1)
				   {
				      ClrScr();
				      gotoxy(2,3);
				      printf("no contact!\n");
				   }
				   else
				   {
				      gotoxy(2,i*4+6);
				      printf("****** total is %d *********",i);/*显示总数*/
				   }
				   fclose(fp);
				}
				/*帮助文件*/
				void Help()
				{
				   ClrScr();
				   gotoxy(10,6);
				   printf("MEMU:");/*输出字符串*/
				   gotoxy(12,8);
				   printf("ALT+C     Contact management system");
				   gotoxy(12,9);
				   printf("ALT+G     Group management system");
				   gotoxy(12,10);
				   printf("ALT+H     Help and copyright infomation");
				   getch();
				   ClrScr();
				}
				/***版本信息***/
				void About(void)
				{
				   Init();
				   Copy();
				   DrawSnow();/*雪花飘动*/
				   Close();
				   exit(0);
				}
				void Init(void)
				{
				   int gd=DETECT,gm;
				   initgraph(&gd,&gm,"C:\\TC");
				   cleardevice();
				}
				void Close(void)/*图形关闭*/
				{
				   getch();
				   closegraph();
				}
				void Copy(void)/*保存区域*/
				{
				   setcolor(0);
				   setfillstyle(SOLID_FILL,15);
				   fillellipse(200,200,4,4);
				   size=imagesize(196,196,204,204);/*定义保存图象区域大小*/
				   save1=malloc(size);/*申请空间*/
				   save2=malloc(size);
				   getimage(196,196,204,204,save1);/*保存雪花*/
				   getimage(96,96,104,104,save2); /*保存背景黑色*/
				}
				void Pr(void)/*在雪中输出文字*/
				{
				   settextstyle(0,0,1);
				   if(change>=20&&change=50&&change				     change>=100&&change				      setcolor(YELLOW);
				   else
				      setcolor(BLACK);
				   outtextxy(250,250,"anykey to continue");
				   setcolor(change/10);/*变换颜色显示标题*/
				   settextstyle(0,0,3);
				   outtextxy(100,200,"Finished By Li Hang");/*标题*/
				}
				/*显示雪花的具体过程*/
				void DrawSnow(void)
				{
				   int i;
				   int sx[62];
				   randomize();
				   for(i=0;i				      sx[i]=(i+2)*10;
				   cleardevice();
				   while(!kbhit())
				   {
				      Pr();
				      if(snownum!=100)/*生成新的雪花*/
				      {
				     snow[snownum].speed=2+random(5);/*速度随机定,但不小于2*/
				     i=random(62);
				     snow[snownum].x=sx[i];/*随机取x坐标*/
				     snow[snownum].y=10-random(100);
				      }
				      for(i=0;i				     putimage(snow[i].x,snow[i].y,save2,COPY_PUT);
				      Pr(); /*显示标题*/
				      if(snownum!=100)
				     snownum++;
				      setfillstyle(SOLID_FILL,15);/*画雪*/
				      for(i=0;i				      {
				     snow[i].y+=snow[i].speed;
				     putimage(snow[i].x,snow[i].y,save1,COPY_PUT);
				     if(snow[i].y>500)
				        snow[i].y=10-random(200);
				      }
				      change++;
				      if(change==140)/*颜色变化*/
				     change=10;
				   }
				}
							

相关资源