0726作业

1、

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void  fun(int *arr, int n, int *odd, int *even);
/*
编写一个函数fun,函数的功能是分别求出数组中所有奇数之和,以及所有偶数之和,
形参N给了数组中数据的个数;利用指针变量odd返回奇数之和,利用指针变量even返回偶数之和;
主函数打印结果
提示:fun(int *arr, int n, int *odd, int *even)
*/

int main()
{
	int num[10]={0};
	int *arr;
	arr=num;
	int odd=0,even=0;
	int *a,*b;
	a=&odd;
	b=&even;
	int i;
	srand((unsigned)time(NULL));
	for(i=0;i<10;i++)
	{
		num[i]=rand()%20+10;
		printf("%d ",num[i]);
	}
	printf("\n");
	fun(arr,10,a,b);
	printf("奇数之和为%d,偶数之和为%d",*a,*b);
	return 0;
} 

void  fun(int *arr, int n, int *odd, int *even)
{
	int i;
	for(i=0;i<10;i++)
	{
		if(*(arr+i)%2==0)
			*even+=*(arr+i);
		else
			*odd+=*(arr+i);
	}
}

结果

18 10 29 16 13 10 15 22 12 29
奇数之和为86,偶数之和为88
--------------------------------
Process exited after 0.04557 seconds with return value 0
请按任意键继续. . .

2、

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void print(int (*p)[5],int i);
void fb(int (*p)[5],int i);
/*
2.编写一个程序,声明一个3*5的数组并初始化,具体数值可随意,程序打印出
数值,然后数值翻一番,接着再次打印出新值,编写-一个函数来显示数组内容,
在编写另一个函数执行翻倍功能,数组名和数组行数作为参数由程序传递给函数
*/

int main()
{
	int a[3][5]={0};
	print(a,3);
	fb(a,3);
//	print(a,3);
//	srand((unsigned)time(NULL));
//	for(i=0;i<3;i++)
//	{
//		for(j=0;j<5;j++)
//		{
//			a[i][j]=rand()%20+10;
//			printf("%d ",a[i][j]);
//		}
//		printf("\n");
//	}
//	
	return 0;
}

void print(int (*p)[5],int i)
{
	srand((unsigned)time(NULL));
	int a,b;
	for(a=0;a<i;a++)
	{
		for(b=0;b<5;b++)
		{
			*(*(p+a)+b)=rand()%20+10;
			printf("%d ",*(*(p+a)+b));
		}
		printf("\n");
	}
	printf("\n\n");
}

void fb(int (*p)[5],int i)
{
	int a,b;
	for(a=0;a<i;a++)
	{
		for(b=0;b<5;b++)
		{
			*(*(p+a)+b)=*(*(p+a)+b)+*(*(p+a)+b);
			printf("%d ",*(*(p+a)+b));
		}
		printf("\n");
	}
}

结果

23 18 29 19 17
12 16 24 19 13
22 20 19 13 28


46 36 58 38 34
24 32 48 38 26
44 40 38 26 56

--------------------------------
Process exited after 0.05007 seconds with return value 0
请按任意键继续. . .

3、

#include <stdio.h>
/*
3.计算器程序修改,主调函数使用函数名做为实参,被调函数形参使用函数指针类型。
*/
int process(int (*fun)(int a,int b),int x,int y);
int jia(int a,int b);
int jian(int a,int b);
int cheng(int a,int b);
int chu(int a,int b);

int main()
{
	int a,b,c;
	printf("请输入算术式:\n");
	scanf("%d",&a);
	char ch;
	ch=getchar();
	scanf("%d",&b);
	switch(ch)
	{
		case '+':
			c=process(jia,a,b);
			break;
		case '-':
			c=process(jian,a,b);
			break;
		case '*':
			c=process(cheng,a,b);
			break;
		case '/':
			c=process(chu,a,b);
			break;
		default:
			break;
	}
	printf("=%d\n",c);
	return 0;
}

int process(int (*fun)(int a,int b),int x,int y)
{
	int rat;
	rat=fun(x,y);
	return rat;
}

int jia(int a,int b)
{
	int c;
	c=a+b;
	return c;
}
int jian(int a,int b)
{
	int c;
	c=a-b;
	return c;
}
int cheng(int a,int b)
{
	int c;
	c=a*b;
	return c;
}
int chu(int a,int b)
{
	int c;
	c=a/b;
	return c;
}

结果

请输入算术式:
5+8
=13

--------------------------------
Process exited after 2.514 seconds with return value 0
请按任意键继续. . .请输入算术式:
78-63
=15

--------------------------------
Process exited after 2.631 seconds with return value 0
请按任意键继续. . .

请输入算术式:
5*9
=45

--------------------------------
Process exited after 1.678 seconds with return value 0
请按任意键继续. . .
请输入算术式:
9/3
=3

--------------------------------
Process exited after 2.332 seconds with return value 0
请按任意键继续. . .

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值