NEFU锐格实验四[指针与函数]

推荐:NEFU大一下C语言锐格实验与作业参考程序目录

NEFU锐格实验四[指针与函数]

知识点

题目知识点
5880指针写个函数算平均值,总和
5881指针写个函数实现单词统计
5882写个返回指针值的函数
5883指针malloc库函数实现字符串数组内存动态开辟
5884貌似是不属于这个章节的玩意

题目

5880

数据太少,题目也没要求,懒得写函数了hh,函数传个数组头然后搬运进去就可以了(不会吧不会吧,不会有人改成函数都不会吧)

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define N 5

int main()
{
	int n;
	double sc[N],total[N];//sc存个人的三门,total存每门课总分
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=1;i<=n;i++)
		{
			for(int i=0;i<3;i++)
				scanf("%lf",&sc[i]);
			double *p=sc;double sum=0;//sum存个人总分
			for(int i=0;i<3;i++)
			{
				sum+=*(p+i);
				total[i]+=*(p+i);	
			}
			printf("%.2lf %.2lf\n",sum,sum/3.0);
		}
		double *p=total;//这里*p已经不是sc了
		for(int i=0;i<3;i++)
			printf("%.2lf %.2lf\n",*(p+i),*(p+i)/n);
	}
	return 0;
} 

5881

喜闻乐见的字符串+指针,大底是令很多人头疼的组合

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define N 1005

void solve(char *p)
{
	int cnt=0;
	bool flag=0;
	while(*p!='\0')
	{
		if(*p>='a'&&*p<='z'||*p>='A'&&*p<='Z')
		{
			printf("%c",*p);
			flag=1;
		}
		else if(flag)//没有想象中的末尾直接接'\0'所以这样就可以了
		{
			printf(" ");
			cnt++;
			flag=0;
		}
		++p;
	}
	if(cnt)printf("\n分出了%d个单词\n",cnt);//如果没有的话就不输出
} 
int main()
{
	int n;
	char str[N];
	char *p=str;//其实直接写str用到下面也ok,走个流程 
	while(scanf("%d ",&n)!=EOF)
	{
		for(int i=1;i<=n;i++)
		{
			gets(p);
			solve(p);
		}		
	}
	return 0;
} 

5882

略有难度,不过把字符串当成二维数组就完事了,不会的建议复习一下指针代替二维数组下标

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define N 105

char* solve(char str[][N],int n)
{
	char *res;
	char (*p)[N]=&str[0];//行指针
	int max=0;
	for(int i=0;i<n;i++)
	{
		int cnt=0;
		for(int j=0;*(*(p+i)+j)!='\0';j++)
		{
			if(*(*(p+i)+j)=='*')cnt++;
		}
		if(cnt>max)
		{
			res=*(p+i);
			max=cnt;
		}
	}
	return res;
}
int main()
{
	int n;
	char str[N][N];
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
			scanf("%s",&str[i]);
		char *p=solve(str,n);
		printf("%s\n",p);
	}
	return 0;
}

5883

看到动态开辟内存时我是懵逼的,想到了C++里的vector。这里就查些资料现学现卖吧,等老师进度到这了再瞅瞅,貌似可以借助malloc函数?
附上:链接
不过可以学一下已给出程序片段里指针传字符串数组的方式

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort1(char **pp,int n)
{
	char tmp[100];
	for(int i=0;i<n;i++)
		for(int j=i+1;j<n;j++)
			if(strcmp(pp[i],pp[j])>0)
			{
				strcpy(tmp,pp[i]);
				strcpy(pp[i],pp[j]);
				strcpy(pp[j],tmp);
			}
    //补充完成程序
}
void output1(char *p[],int n)
{
	for(int i=0;i<n;i++)
		printf("%s\n",p[i]);
 //补充完成程序   
}
int main()
{
    void sort1(char **pp,int n);
    void output1(char *p[],int n);
    char str[50],*cp[20];
    int n,l,i;
    while(scanf("%d\n",&n)!=EOF)
    {
          for(i=0;i<n;i++)
          {
              gets(str);
             //start
             int len=strlen(str);
             cp[i]=(char*)malloc(len*sizeof(char)+1);
             strcpy(cp[i],str);
              //在此写动态开辟并存储程序
             //end
           }
           sort1(cp,n);
           output1(cp,n);
    }
    return 0;
}

5884

不是很懂为啥把这玩意放这个章节,这玩意也有指针做法??(想象力贫困)
数学题目

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
double get_fun(int id,double x)
{
    if(id==1)return x*x*x-x*x-1;
	else if(id==2)return x*x*x*x-3*x+1;
	else return x-exp(-x);//exp求e的幂次
}

double get_der(int id,double x)
{
	if(id==1)return 3*x*x-2*x;
	else if(id==2)return 4*x*x*x-3;
	else return 1+exp(-x);
}
int main()
{
	int id;
	double st,eps,ne;//st为初始的,ne为下一次,eps为精度
	while(scanf("%d",&id)!=EOF)
	{
		scanf("%lf%lf",&st,&eps);
		for(int i=1;i<=100000;i++)
		{
			double f1=get_fun(id,st);//计算原函数 
			double f2=get_der(id,st);//计算导数 
			ne=st-f1/f2;
			if(fabs(ne-st)<eps)break;
			st=ne;
		}
		printf("%d %.2e %.5lf\n",id,eps,ne);
	}
	return 0;
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值