c语言程序设计论文_c语言程序设计重点
c语言程序设计论文由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c语言程序设计重点”。
C语言程序设计论文
摘要
该程序编译的是通讯录,可以实现一般主要功能有:有建立通讯录的个人信息功能,如记录该人姓名信息,地址信息,出生日期信息,所在城市信息,以及其城市的邮编信息等,删除个人信息,通讯录个人的信息列表,个人的信息查询,个人的信息保存,以及个人的信息提取的功能,最后可以直接退出通讯录。
当然实行不同的功能需要定义不同的功能函数,在记录个人信息定义不同的结构体,每一个成员的信息可以是一个基本类型或者是一个够造类型,对结构体变量的应用,其中包括赋值,输入,输出,运算等方式来实现结构变量的成员。
当然也可以执行该程序应用了函数的调用,自定义函数以及文件的存储与调用等。个人的信息保存在这个问题上,这信息可以动态的分配到内存空间,每一次分配一块空间用来存放一个人的个人信息的数据,定义两个域,一个是数据域,用来存放各种实际的数据,如个人姓名信息,个人地址信息,出生日期等等数据。在编辑程序,用到了基本的循环、选择、排序的算法,使用数组或数组元素的指针和指针变量,对结构体及其元素用指针或指针变量来指向。并且使用多种预处理功能,如宏定义、文件包含、条件编译等。来进行程序的修改、阅读、移植和调试,也有利于实现模块化程序设计。程序员在程序中用预处理命令来调用这些功能。
通讯录的个人信息数据的编辑、存储、列表、删除、查询、提取等实行运用,在程序中存储二百人的个人信息空间,日期不能超过两个字符。在运行后,程序自动给出提示选择,给出你所需要执行的功能,在选择后会给出相应的显示和下一步的提示,在编辑个人信息时必须有姓名,否者会返回上一选择菜单,有部分信息可以不填。在编辑完后,就可以实行其他功能,将给出其他选择,如将个人信息删除、将个人信息列表、保存个人信息、提取个人信息等等的功能。最后在执行完后就可直接根据提示选择退出。就实现了整个通讯录的流程。
关键词:通讯录、列表、函数、编译
C语言程序设计论文
目录
题目内容及其功能..........................2 算法原理..................................2 算法流程图................................8 源程序...................................14 运用结果及分析...........................20 设计心得.................................22 参考文献.................................23
C语言程序设计论文
题目内容及其功能
该通讯录主要功能有:有建立通讯录的个人信息,如姓名,地址,出生日期,城市,邮编等。也可以执行删除个人信息,通讯录信息列表,信息查询,信息保存,以及信息提取的功能,最后可以退出通讯录。该程序应用了函数的调用,自定义函数以及文件的存储与调用等,使用数组或数组元素的指针和指针变量,对结构体及其元素用指针或指针变量来指向。
算法原理
(1)通过选择来实现程序的功能: int main(void){ start=last=NULL;for(;;){
switch(menu_select())
{
case 1:enter();
break;
case 2:mldelete(&start,&last);
break;
case 3:list();
break;
case 4:search();
break;
case 5:save();
break;
case 6:load();
break;
case 7:exit(0);
} 输出运行结果并且给出选择: int menu_select(void){ char s[80];int c;printf(“******tongxunlu*******n”);printf(“*1.Enter a name
*n”);printf(“*2.delete a name
*n”);printf(“*3.list the file
*n”);printf(“*4.search
*n”);
printf(“*5.save the file
*n”);printf(“*6.load the file
*n”);printf(“*7.Quit
*n”);
C语言程序设计论文
printf(“**********************n”);do {
printf(“nplease enter your choice:”);
gets(s);
c=atoi(s);}while(c7);return c;}
(2)输入个人信息:名字,地址,日期,邮政编码 void enter(void){ struct addre *info;for(;;){
info=(struct addre *)malloc(sizeof(struct addre));
if(!info)
{
printf(“nout of memory”);
return;
}
inputs(“please enter name:”,info->name,30);
if(!info->name[0])break;/*stop entering*/
inputs(“please enter street:”,info->street,40);
inputs(“please enter city:”,info->city,20);
inputs(“please enter state:”,info->state,3);
inputs(“please enter zip:”,info->zip,10);
dls_store(info,&start,&last);}/*entry loop*/ } 如果超过给定内存,将给出提示,输入太长: void inputs(char *prompt,char *s,int count){ char p[225];do {
printf(prompt);
fgets(p,254,stdin);
if(strlen(p)>count)
printf(“nToo Longn”);} while(strlen(p)>count);p[strlen(p)-1]=0;/* remove newline character */ strcpy(s,p);}(3)制作列表,对输入的个人信息进行存储与排列 void dls_store(C语言程序设计论文
struct addre *i,/*new element*/ struct addre **start,/*first element in list*/ struct addre **last/*last element in list*/){ struct addre *old,*p;if(*last==NULL){/*first element in list*/
i->next=NULL;
i->prior=NULL;
*last=i;
*start=i;
return;} p=*start;/*start at top of list*/ old=NULL;while(p){
if(strcmp(p->name,i->name)
old=p;
p=p->next;
}
else{
if(p->prior)
{
p->prior->next=i;
i->next=p;
i->prior=p->prior;
p->prior=i;
return;
}
i->next=p;/*new first element*/
i->prior=NULL;
p->prior=i;
*start=i;
return;
} } old->next=i;/*put on end*/ i->next=NULL;i->prior=old;*last=i;}(4)将通讯录中的信息,先进行查找,然后删除个人信息 void mldelete(struct addre **start,struct addre **last)
C语言程序设计论文
{ struct addre *info;char s[80];
inputs(“Enter name:”,s,30);info=find(s);if(info){
if(*start==info){
*start=info->next;
if(*start)(*start)->prior=NULL;
else *last=NULL;
}
else{
info->prior->next=info->next;
if(info!=*last)
info->next->prior=info->prior;
else
*last=info->prior;
}
free(info);/*return memory to system*/ } }(5)从其内存中查找某个名字,并进行输出,如果没有就输出没有发现名字 struct addre *find(char *name){ struct addre *info;info=start;while(info){
if(!strcmp(name,info->name))return info;
info = info->next;/* get next addre */ } printf(“Name not found.n”);return NULL;/*not found*/ }(6)将通讯录中所有个人信息分别列出 void list(void){ struct addre *info;info=start;while(info){
display(info);
info = info->next;/* get next addre */
C语言程序设计论文
} printf(“nn”);}(7)分别输出个人信息
void display(struct addre *info){ printf(“%sn”,info->name);printf(“%sn”,info->street);printf(“%sn”,info->city);printf(“%sn”,info->state);printf(“%sn”,info->zip);printf(“nn”);}(8)根据其个人信息的存储地址来查找个人名字信息 void search(void){ char name[40];struct addre *info;printf(“Enter name to find:”);gets(name);info = find(name);if(!info)
printf(“Not Foundn”);else display(info);}(9)保存输入的个人信息 void save(void){ struct addre *info;FILE *fp;fp = fopen(“mlist”,“wb”);if(!fp){
printf(“Cannot open file.n”);
exit(1);} printf(“nSaving Filen”);info = start;while(info){
fwrite(info,sizeof(struct addre),1,fp);
info = info->next;/* get next addre */ } fclose(fp);
C语言程序设计论文
}(10)查找并提取已输入的个人信息 void load(){ struct addre *info;FILE *fp;fp=fopen(“mlist”,“rb”);if(!fp){
printf(“Cannot open file.n”);
exit(1);} 释放内存空间:
while(start){
info=start->next;
free(info);
start=info;fclose(fp);}
C语言程序设计论文
算法流程图
图1-1制作列表流程图
C语言程序设计论文
图1-2删除个人信息流程图
C语言程序设计论文
图1-3个人信息-名字的查找流程图
C语言程序设计论文
图1-4查找到个人信息流程图
C语言程序设计论文
图1-5保存个人信息流程图
C语言程序设计论文
图1-6提取下载个人信息流程图
C语言程序设计论文
源程序
#include“stdio.h” #include“stdlib.h” #include“string.h” struct addre{ char name[30];char street[40];char city[20];char state[3];char zip[11];struct addre *next;struct addre *prior;};struct addre *start;struct addre *last;struct addre *find(char *);
void enter(void),search(void),save(void);void load(void),list(void);void mldelete(struct addre **,struct addre **);void dls_store(struct addre *i,struct addre **start,struct addre **last);void inputs(char *,char*,int),display(struct addre *);int menu_select(void);
int main(void){ start=last=NULL;for(;;){
switch(menu_select())
{
case 1:enter();
break;
case 2:mldelete(&start,&last);
break;
case 3:list();
break;
case 4:search();
break;
case 5:save();
break;
case 6:load();
break;
C语言程序设计论文
case 7:exit(0);
} }return 0;}
int menu_select(void){ char s[80];int c;printf(“******tongxunlu*******n”);printf(“*1.Enter a name
*n”);printf(“*2.delete a name
*n”);printf(“*3.list the file
*n”);printf(“*4.search
*n”);
printf(“*5.save the file
*n”);printf(“*6.load the file
*n”);printf(“*7.Quit
*n”);
printf(“**********************n”);do {
printf(“nplease enter your choice:”);
gets(s);
c=atoi(s);}while(c7);return c;}
/*Enter names and addre.*/ void enter(void){ struct addre *info;for(;;){
info=(struct addre *)malloc(sizeof(struct addre));
if(!info)
{
printf(“nout of memory”);
return;
}
inputs(“please enter name:”,info->name,30);
if(!info->name[0])break;/*stop entering*/
inputs(“please enter street:”,info->street,40);
inputs(“please enter city:”,info->city,20);
inputs(“please enter state:”,info->state,3);
inputs(“please enter zip:”,info->zip,10);
dls_store(info,&start,&last);
C语言程序设计论文
}/*entry loop*/ } /*******************************************/ void inputs(char *prompt,char *s,int count){ char p[225];do {
printf(prompt);
fgets(p,254,stdin);
if(strlen(p)>count)
printf(“nToo Longn”);} while(strlen(p)>count);p[strlen(p)-1]=0;/* remove newline character */ strcpy(s,p);} /*Create a doubly linked list in sorted order */ void dls_store(struct addre *i,/*new element*/ struct addre **start,/*first element in list*/ struct addre **last/*last element in list*/){ struct addre *old,*p;if(*last==NULL){/*first element in list*/
i->next=NULL;
i->prior=NULL;
*last=i;
*start=i;
return;} p=*start;/*start at top of list*/ old=NULL;while(p){
if(strcmp(p->name,i->name)
old=p;
p=p->next;
}
else{
if(p->prior)
{
p->prior->next=i;
i->next=p;
i->prior=p->prior;
C语言程序设计论文
p->prior=i;
return;
}
i->next=p;/*new first element*/
i->prior=NULL;
p->prior=i;
*start=i;
return;
} } old->next=i;/*put on end*/ i->next=NULL;i->prior=old;*last=i;} /*Remove an element from the list*/ void mldelete(struct addre **start,struct addre **last){ struct addre *info;char s[80];
inputs(“Enter name:”,s,30);info=find(s);if(info){
if(*start==info){
*start=info->next;
if(*start)(*start)->prior=NULL;
else *last=NULL;
}
else{
info->prior->next=info->next;
if(info!=*last)
info->next->prior=info->prior;
else
*last=info->prior;
}
free(info);/*return memory to system*/ } }
/*Find an addre.*/
struct addre *find(char *name){
C语言程序设计论文
struct addre *info;info=start;while(info){
if(!strcmp(name,info->name))return info;
info = info->next;/* get next addre */ } printf(“Name not found.n”);return NULL;/*not found*/ } /* Display the entire list */ void list(void){ struct addre *info;info=start;while(info){
display(info);
info = info->next;/* get next addre */ } printf(“nn”);} /* this function actually prints the fields in each addre.*/ void display(struct addre *info){ printf(“%sn”,info->name);printf(“%sn”,info->street);printf(“%sn”,info->city);printf(“%sn”,info->state);printf(“%sn”,info->zip);printf(“nn”);} /* Look for a name in the list */ void search(void){ char name[40];struct addre *info;printf(“Enter name to find:”);gets(name);info = find(name);if(!info)
printf(“Not Foundn”);else display(info);}
C语言程序设计论文
/* Save the file to disk */ void save(void){ struct addre *info;FILE *fp;fp = fopen(“mlist”,“wb”);if(!fp){
printf(“Cannot open file.n”);
exit(1);} printf(“nSaving Filen”);info = start;while(info){
fwrite(info,sizeof(struct addre),1,fp);
info = info->next;/* get next addre */ } fclose(fp);} /*
Load the addre file.*/ void load(){ struct addre *info;FILE *fp;fp=fopen(“mlist”,“rb”);if(!fp){
printf(“Cannot open file.n”);
exit(1);} /* free any previously allocated memory */ while(start){
info=start->next;
free(info);
start=info;} /*
reset top and bottom pointers
*/ start=last=NULL;printf(“nLoading Filen”);while(!feof(fp)){
info=(struct addre *)malloc(sizeof(struct addre));
C语言程序设计论文
} if(!info){
printf(“Out of Memory”);
return;} if(1!=fread(info,sizeof(struct addre),1,fp))break;dls_store(info,&start,&last);}
fclose(fp);运用结果及分析
图1-1 运行结果并且给出选择
图1-2输入个人信息
C语言程序设计论文
图1-3删除个人信息
1-4列出个人信息
C语言程序设计论文 1-5查找个人信息
图1-6保存个人信息
C语言程序设计论文
设计心得
通过进行C语言程序设计,我更加懂得C语言设计不仅仅需要逻辑思维地紧密,更加需要细心,通过两个星期的折腾,总算把课程设计给完成了,这是一个坚苦而又漫长的过程。读了那么多年的书,课程设计可是第一次。看着劳动成果,很欣慰!
刚开始,可以说是没有头绪,于是就去图书馆找资料,找到了一些关于画图方面的,可是这点小进展远远不够,这只是一个小小的开始。下一步是上网查,找到了些与我们题目相似的,那时我很高兴,完成了这个程序。
虽然对着电脑做程序,有点累有点热,可是当看到劳动成果时,真是别有一番滋味在心头啊!世上无难事,只怕有心人,的确如此。
做完这个课程设计,我的自信一下子提高了,我也会写程序了;尽管对于有些人这种程序会很简单,可对我们C语言初学者来说,已经很不容易了。这次体验为以后的学习计算机的我们增强了信心。享受劳动成果的滋味实在很美妙啊!
程序设计过程有如解决一实际问题,从解决实际问题的角度,我们可以这样来看:首先要了解这个问题的基本要求,即输入、输出、完成从输入到输出的要求是什么;其次,从问题的要害入手,从前到后的解决问题的每个方面,即从输入开始入手,着重考虑如何从输入导出输出,在这个过程中,可确定所需的变量、数组、函数,然后确定处理过程--算法。可得最后结论。
一个多礼拜的挣扎,绞尽脑汁终于带着麻木的手指和大脑脱出那堆“泥潭”。现在的心情是如鱼得水,干涩的眼睛前方是万里晴空。终于,所有的努力即将化成了一个个铅字,打印机“滋滋”的声音仿佛已经宛如天籁般悦耳地在耳畔响起。此时此刻,真想把一切抛上天空。高呼:“出来了!我的成果即将出来了!”,心里也在默默的感慨着:“有志者,事竟成,破釜沉舟,百二秦关终归楚。苦心人,天不负,卧薪尝胆,三千越甲可吞吴。”
当然这次程序设计也让我看到了自己基础的薄弱。古人有诗云:“路漫漫其修远兮,吾将上下而求索”。从今天起我将不断继续求索,学习之路也好,人生之路也罢。
总之我受益匪浅。
C语言程序设计论文
参考文献
《C程序设计》„„谭浩强 著„„清华大学出版社„2005.7 《C语言程序设计》„„(美)郝伯特.希尔特 著 „„电子工业出版社