永发信息网

C语言中实现简单的功能用有参宏比用函数有什么好处?

答案:6  悬赏:70  手机版
解决时间 2021-12-03 05:51
  • 提问者网友:箛茗
  • 2021-12-03 01:52
C语言中实现简单的功能用有参宏比用函数有什么好处?
最佳答案
  • 五星知识达人网友:醉吻情书
  • 2021-12-03 03:03
1、宏与函数的区别主要在于是否返回值,一般函数都有返回值,宏是不可以的。宏和函数都不必须要求参数,参数都是可有可无的。
2、用函数,因为要有函数调用,增加了执行时的开销,效率不如宏,但是可代码重用。用宏虽然没有函数调用,但是因为在使用这个宏的地方,代码会被展开编译,增加了程序文件的大小。总起来说,参数和宏是一个用空间换时间还是用时间换空间的抉择。
全部回答
  • 1楼网友:行路难
  • 2021-12-03 06:36
宏只能写简单的.不能写复杂的,因为宏没有返回值.
  • 2楼网友:过活
  • 2021-12-03 06:09

空间换时间的思想
  • 3楼网友:洒脱疯子
  • 2021-12-03 04:48
借一下地方
#include
#include
#include
#include
struct student
{
char name[16];
char tel[18];
char sexual[7];
char age[3];
char birthday[15];
char dorm[20];
struct student *next;
};
struct student *head;
int n=0;
FILE *fp;

void savetofile(struct student *head,FILE *fp);
int load(FILE *fp);
void addfromfiletofile();
struct student *creat();
struct student *insert(struct student *head);
void print(struct student *head);

void del();

struct student *delbyname(struct student *head);
struct student *delbytel(struct student *head);

void find();

void findbyname(struct student *head);
void findbytel(struct student *head);
void findbydorm(struct student *head);


int load(FILE *fp)
{ struct student *p1,*p2;
head=(struct student *)malloc(sizeof(struct student));
if(fread(head,sizeof(struct student),1,fp)!=1) {
free(head);
head=NULL;
return(0);
}
p2=head;n++;
while(!feof(fp)){
p1=(struct student *) malloc(sizeof(struct student));
fread(p1,sizeof(struct student),1,fp);
p2->next=p1;
p2=p1;
n++;
}
p2->next=NULL;
return(n);
}

void savetofile(struct student *p,FILE *fp)
{
while(p!=NULL)
{fwrite(p,sizeof(struct student),1,fp);
p=p->next;
}
}

void addfromfiletofile()
{
FILE *fromf,*tof;
char ch,from[20],to[20];
printf("Enter the file name you want to add from:\n");
scanf("%s",from);
printf("Enter the file name you want to add to :\n");
scanf("%s",to);
if((fromf=fopen(from,"r"))==NULL)
{
printf("cannot open fromfile\n");
exit(0);
}
if((tof=fopen(to,"a"))==NULL)
{
printf("cannot open tofile\n");
exit(0);
}
while(!feof(fromf)) fputc(fgetc(fromf),tof);
fclose(fromf);
fclose(tof);
}

struct student *creat()
{
struct student *h,*p1,*p2;
n=0;
printf("input name tel sexual age birthday dorm. end with 0\n");
p1=p2=(struct student *)malloc(sizeof(struct student));
scanf("%s",&p1->name);
h=NULL;
while(strcmp(p1->name,"0")!=0)
{
scanf("%s%s%s%s%s",&p1->tel,&p1->sexual,&p1->age,&p1->birthday,&p1->dorm);
n++;
if(n==1) h=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
scanf("%s",&p1->name);
}
p2->next=NULL;
return(h);
}

struct student *insert(struct student *head)
{
struct student *p0,*p1,*one;
one=(struct student *)malloc(sizeof(struct student));
printf("ple input name tel sexual age birthday dorm\n");
scanf("%s%s%s%s%s%s",&one->name,&one->tel,&one->sexual,&one->age,&one->birthday,&one->dorm);
p1=head;
p0=one;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{
while(p1->next!=NULL)
p1=p1->next;
p1->next=p0;
p0->next=NULL;
}
n++;
return(head);
}

void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
{ printf("name tel sexual age birthday dorm\n");
do
{
printf("%-10s%-15s%-5s%-5s%-10s%-10s\n",p->name,p->tel,p->sexual,p->age,p->birthday,p->dorm);
p=p->next;
}while(p!=NULL);
}
else
printf("the list is empty!\n");
}

void del()
{
struct student *delbyname(struct student *head);
struct student *delbytel(struct student *head);
int m;
printf("1: delete by name press 1\n");
printf("2: delete by tel press 2\n");
scanf("%d",&m);
if(m==1)
head=delbyname(head);
else if(m==2)
head=delbytel(head);
else
del();
}

struct student *delbyname(struct student *head)
{
struct student *p1,*p2;
char name[16];
printf("input the name:");
scanf("%s",name);
if(head==NULL) {printf("\nlist null!\n");goto end;}
p1=head;
while(strcmp(name,p1->name)!=0&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(strcmp(name,p1->name)==0)
{
if(p1==head)head=p1->next;
else p2->next=p1->next;
free(p1);
printf("have delete:%s\n",name);
n--;
}
else
printf("%s not been found! \n",name);
end:
return(head);
}

struct student *delbytel(struct student *head)
{
struct student *p1,*p2;
char tel[18];
printf("input the tel:");
scanf("%s",tel);
if(head==NULL) {printf("\nlist null!\n");goto end;}
p1=head;
while(strcmp(tel,p1->tel)!=0&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(strcmp(tel,p1->tel)==0)
{
if(p1==head)head=p1->next;
else p2->next=p1->next;
free(p1);
printf("have delete:%s\n",tel);
n--;
}
else
printf("%s not been found! \n",tel);
end:
return(head);
}

void find()
{
void findbyname(struct student *head);
void findbytel(struct student *head);
void findbydorm(struct student *head);
int m;
printf("1:find by name,press 1\n");
printf("2:find by tel, press 2\n");
printf("3:find by dorm,press 3\n");
scanf("%d",&m);
switch(m){
case 1:findbyname(head);break;
case 2:findbytel(head);break;
case 3:findbydorm(head);break;
default: find();
}
}

void findbyname(struct student *head)
{
struct student *p=head;
char name[16];
printf("input name:\n");
scanf("%s",&name);
while(strcmp(p->name,name)!=0&&p->next!=NULL)
p=p->next;
if(strcmp(p->name,name)==0)
printf("%-10s%-15s%-5s%-5s%-10s%-10s\n",p->name,p->tel,p->sexual,p->age,p->birthday,p->dorm);
else
printf("no such person\n");
}

void findbytel(struct student *head)
{
struct student *p=head;
char tel[18];
printf("input tel:\n");
scanf("%s",tel);
while(strcmp(p->tel,tel)!=0&&p->next!=NULL)
p=p->next;
if(strcmp(p->tel,tel)==0)
printf("%-10s%-15s%-5s%-5s%-10s%-10s\n",p->name,p->tel,p->sexual,p->age,p->birthday,p->dorm);
else
printf("no such person\n");
}

void findbydorm(struct student *head)
{
struct student *p=head;
char dorm[20];
printf("input dorm:\n");
scanf("%s",dorm);
while(strcmp(p->dorm,dorm)!=0&&p->next!=NULL)
p=p->next;
if(strcmp(p->dorm,dorm)==0)
printf("%-10s%-15s%-5s%-5s%-10s%-10s\n",p->name,p->tel,p->sexual,p->age,p->birthday,p->dorm);
else
printf("no such person\n");
}

void main()
{
int m;
printf(" ***************************************************\n\n\n");
printf(" ********* Welcome to use july's ***************\n\n\n");
printf(" ************** addressbook **********************\n\n\n");
while(1)
{ printf(" ********************************************\n");
printf(" 0 creat, press 0\n");
printf(" 1 insert, press 1\n");
printf(" 2 delete, press 2\n");
printf(" 3 print, press 3\n");
printf(" 4 find press 4\n");
printf(" 5 load file, press 5\n");
printf(" 6 save, press 6\n");
printf(" 7 add from file to file, press 7\n");
printf(" 8 show me how many people press 8\n");
printf(" 9 exit,press 9\n\n");
printf(" ******************************\n");
scanf("%d",&m);
if(m>=0&&m<=9)
{
switch(m)
{
case 0: head=creat();
break;
case 1: head=insert(head);
break;
case 2: del();
break;
case 3: print(head);
break;
case 4: find();
break;
case 5:if((fp=fopen("record.txt","rb"))==NULL)
{printf("canot open this file\n");exit(0);}
n=load(fp);
fclose(fp);
break;
case 6:if((fp=fopen("record.txt","wb"))==NULL)
{printf("canot open this file\n");exit(0);}
savetofile(head,fp);
fclose(fp);
break;
case 7:addfromfiletofile();
break;
case 8:printf("there are %d people in the address book.\n",n);
break;
case 9:exit(0);
}
printf("\n\nHave finished,select again!\n");
}
else
printf("\n\nError,select again!\n");
}
}
  • 4楼网友:污到你湿
  • 2021-12-03 03:46
效率上宏和内联函数一样.
对于很短的函数,宏书写要简单一点(不需要写函数声明,通常声明和实现在不同地方)

个人认为,对于长一点的功能宏没有什么优势,并且很难调试,你试想一下错误在宏定义内部就知道了.
  • 5楼网友:北城痞子
  • 2021-12-03 03:28
用函数,因为要有函数调用,增加了执行时的开销,效率不如宏,但是可代码重用。用宏虽然没有函数调用,但是因为在使用这个宏的地方,代码会被展开编译,增加了程序文件的大小。总起来说,参数和宏是一个用空间换时间还是用时间换空间的抉择。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯