大连理工大学C语言题目(四)

103 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1.编写函数实现函数f(x)的定积分,其中x为实数。 提示:函数声明可以设计为 :double calcus( double(*pf)(double), int low, int high, int n)

#include <stdio.h>
double calcus( double(*pf)(double), int low, int high, int n),one(double),two(double),three(double);
//pf为指向函数的指针,low为积分下限,high为积分上限,n控制积分精度。
void main(){
	int low,high,n;
	scanf("%d%d%d",&low,&high,&n);
	printf("%f",calcus(three,low,high,n));
} 

double one(double x){
	return x;
}

double two(double x){
	return x*x;
}

double three(double x){
	return x*x*x;
}

double calcus( double(*pf)(double), int low, int high, int n){
	double i,sum=0;
	for(i=low;i<=high;i+=1.0/n){
		sum+=(*pf)(i)/n;
	}
	return sum;
}

2.查找字符串中是否存在字符key,并从字符串中删除该字符,要求编写实现查找的函数和实现删除指定字符的函数,并在主函数中测试。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void select(char*a){
	int i,flag=0;
	for(i=0;i<strlen(a)-2;i++){
		if(a[i]=='k'&&a[i+1]=='e'&&a[i+2]=='y'){
			flag=1;
			printf("第%d到%d个元素是key\n",i,i+2);
		}
	}
	if (flag==0)
	printf("没找到");
}

void omit(char*a){
	char*b=(char*)malloc(strlen(a)+1);
	int i,cnt=0;
	for(i=0;i<strlen(a);i++){
		if(a[i]=='k'&&a[i+1]=='e'&&a[i+2]=='y'){
			i+=3;
			cnt++;
		}
		b[i-cnt*3]=a[i];
	}
	b[i-cnt*3]=0;
	printf("%s",b);
	free(b);
}

void main(){
	char*a="The key is I am kicking my keyboard.I think I've got a little crush on you.";
	select(a);
	omit(a);
}

3.编写函数实现在字符串中查找子串的功能,返回查找到的第一个子串的地址,并在主函数中进行测试。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int find(char*a){
	char*b=(char*)malloc(strlen(a)+1);
	scanf("%s",b);
	int i,j;
	for(i=0;i<strlen(a);i++){
		if(a[i]==b[0]){
			for(j=0;j<strlen(b);j++){
				if(a[i+j]==b[j])
				free(b);
				return i;
			}
		}
	}
}

void main (){
	char*a="The key is I am kicking my keyboard.I think I've got a little crush on you.";
	printf("a[%d]",find(a));
}