您的位置:首页 > 房产 > 建筑 > 西安企业资本服务中心有限公司_电子工程网站大全_关键词营销推广_网页推广怎么做

西安企业资本服务中心有限公司_电子工程网站大全_关键词营销推广_网页推广怎么做

2025/4/18 20:49:03 来源:https://blog.csdn.net/qq_46987323/article/details/145533733  浏览:    关键词:西安企业资本服务中心有限公司_电子工程网站大全_关键词营销推广_网页推广怎么做
西安企业资本服务中心有限公司_电子工程网站大全_关键词营销推广_网页推广怎么做

目录

一、程序填空📝 --- 函数指针的操作

题目📃

分析🧐

二、程序修改🛠️ --- 单链表偶数结点值累加

题目📃

分析🧐        

三、程序设计💻 --- 判断回文

题目📃

分析🧐


前言
本文讲解:函数指针的操作、单链表偶数结点值累加、判断回文

🏠我的主页:我的主页
📚系列专栏:系列专栏

一、程序填空📝 --- 函数指针的操作

难度:⭐⭐

题目📃

在此程序中,函数 fun 的功能:
用函数指针指向要调用的函数,并进行调用。规定在【2】处使 f 指向函数 f1,在【3】处使 f 指向函数 f2。当调用正确时,程序输出:x1 = 5.000000, x2 = 3.000000, x1x1+x1x2=40.000000。

注意:部分源程序在文件 blank1.c 中。 不得增行或删行,也不得更改程序的结构!

代码如下: 
在1️⃣2️⃣3️⃣处填空

#include  <stdio.h>
double f1(double  x)
{  return  x*x;  }
double f2(double x, double y)
{  return  x*y;  }
double fun(double  a, double  b)
{
/**********found**********/1️⃣ (*f)();double  r1, r2;
/**********found**********/f = 2️⃣; r1 = f(a);
/**********found**********/f = 3️⃣ ; r2 = (*f)(a, b);return  r1 + r2;
}
void main()
{ double  x1=5, x2=3, r;r = fun(x1, x2);printf("\nx1=%f,  x2=%f,  x1*x1+x1*x2=%f\n",x1, x2, r);
}

分析🧐

这道题主要是理解指针函数的概念,在这个程序中先定义了函数指针,再通过这个函数指针指向对应的函数

  1. 填写  double
    由代码中可知,函数指针需要double类型的
  2. 填写  f1
    直接将函数名赋给函数指针即可
    函数指针就能够调用
  3. 填写  f2 
    同理也一样

解答代码如下:

#include  <stdio.h>
double f1(double  x)
{  return  x*x;  }
double f2(double x, double y)
{  return  x*y;  }
double fun(double  a, double  b)
{
/**********found**********/1️⃣double (*f)();double  r1, r2;
/**********found**********/f = 2️⃣f1; r1 = f(a);
/**********found**********/f = 3️⃣f2 ; r2 = (*f)(a, b);return  r1 + r2;
}
void main()
{ double  x1=5, x2=3, r;r = fun(x1, x2);printf("\nx1=%f,  x2=%f,  x1*x1+x1*x2=%f\n",x1, x2, r);
}

注:虽然程序里会有红线,但是不用管 

二、程序修改🛠️ --- 单链表偶数结点值累加

难度:⭐⭐

题目📃

在此程序中,建立一个带头结点的单向链表,并用随机函数为各结点赋值。函数 fun 的功能是将单向链表结点 (不包括头结点) 数据域为偶数的值累加起来,并且作为函数值返回。请改正函数 fun 中的错误,使它能得出正确的结果。

注意:部分源程序在文件 modi1.c 中。 不要改动 main 函数,不得增行或删行,也不得更改程序的结构!

代码如下:
在代码中找出2个错误并修改

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct aa
{ int data;struct aa *next;
} NODE;
int fun (NODE *h)
{ int sum=0;NODE *p;p=h->next;
/*************found**************/while(p -> next){ if(p->data%2==0)sum+=p->data;
/*************found**************/p=h->next;}return sum;
}
NODE *creatlink(int n)
{ NODE *h,*p,*s;int i;h=p=(NODE*)malloc(sizeof(NODE));for(i=1;i<n;i++){s=(NODE*)malloc(sizeof(NODE));s->data=rand()%16;p->next=s;p=p->next;}p->next=NULL;return h;
}
void outlink(NODE *h)
{ NODE  *p;p=h->next;printf("\n\n The LIST :\n\n HEAD");while(p){ printf("->%d",p->data); p=p->next;}printf("\n");
}
void main()
{ NODE *head; int sum;system("CLS");head=creatlink(10);outlink(head);sum=fun(head);printf("\nSUM=%d",sum); 
}

分析🧐        

这道题虽然是一道链表题,但还是比较容易理解的,这里只要看fun函数即可

  1. 第13行改成
    while(p != NULL)
    脑子里要有画面,想象一下
    由11行可以知道,p指向的,是当前要判断的
  2. 第17行改成
    p = p -> next;
    这里要注意就是当前指针走就可以了,原始的指针不动
    动了就没有意义了

解答代码如下:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct aa
{ int data;struct aa *next;
} NODE;
int fun (NODE *h)
{ int sum=0;NODE *p;p=h->next;
/*************found**************/1️⃣while(p  != NULL){ if(p->data%2==0)sum+=p->data;
/*************found**************///每次只要移动p就可以了//画图就很清楚了2️⃣ p=p->next;}return sum;
}
NODE *creatlink(int n)
{ NODE *h,*p,*s;int i;h=p=(NODE*)malloc(sizeof(NODE));for(i=1;i<n;i++){s=(NODE*)malloc(sizeof(NODE));s->data=rand()%16;p->next=s;p=p->next;}p->next=NULL;return h;
}
void outlink(NODE *h)
{ NODE  *p;p=h->next;printf("\n\n The LIST :\n\n HEAD");while(p){ printf("->%d",p->data); p=p->next;}printf("\n");
}
void main()
{ NODE *head; int sum;system("CLS");head=creatlink(10);outlink(head);sum=fun(head);printf("\nSUM=%d",sum); 
}

三、程序设计💻 --- 判断回文

难度:⭐⭐⭐

题目📃

在此程序中,编写函数 fun,该函数的功能是:判断字符串是否为回文,若是,则函数返回 1,主函数中输出 "YES",否则返回 0,主函数中输出 "NO"。回文是指顺读和倒读都一样的字符串。

例如,字符串 LEVEL 是回文,而字符串 123312 就不是回文。

注意:部分源程序在文件 prog1.c 中。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入你编写的若干语句。

代码如下:
在fun函数中编写 

#include <stdio.h>
#include <string.h>
#define  N  80
int fun(char *str)
{}main()
{ char  s[N] ;void NONO ();printf("Enter a string: ") ; gets(s) ;printf("\n\n") ; puts(s) ;if(fun(s)) printf("  YES\n") ;else       printf("  NO\n") ;
}

分析🧐

这道题就是编写判断是否为回文的程序

  1. 先定义两个指针,i, j
  2. i 指向最前面
  3. j 指向最后面
  4. 依次进行判断即可

解答代码如下:

#include <stdio.h>
#include <string.h>
#define  N  80
int fun(char *str)
{//先求得字符串总长度int len = strlen(str);int i, j;//i从0开始//j从len - 1开始,依次进行对比for(i = 0, j = len - 1; i < j; i++, j--){if(str[i] != str[j])return 0;}return 1;
}main()
{ char  s[N] ;printf("Enter a string: ") ; gets(s) ;printf("\n\n") ; puts(s) ;if(fun(s)) printf("  YES\n") ;else       printf("  NO\n") ;
}

希望本文能够帮助到你😊

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com