C语言程序设计《程序设计基础实验》实验4 张顺利

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

//1.编写一个函数swap,用来实现交换两个内存变量a和b的值。要求用指针变量编写程序。
void swap(int *p_1,int *p_2);
int main1(int argc, char *argv[]) {
	int a,b;
	scanf("%d %d",&a,&b);
	printf("before,a=%d,b=%d\n",a,b);
	swap(&a,&b);
	return 0;
}
void swap(int *p_1,int *p_2)
{
	int *temp;
	temp=p_1;
	p_1=p_2;
	p_2=temp;
	printf("later,a=%d,b=%d\n",*p_1,*p_2);
	
}
//2.将数组中的数进行排序,要求用指针变量编写程序.
int main2()
{
	int a[5],*p_1,*p_2,*temp;
	int i,j,m,n;
	printf("please input 5 numbers:\n");
	for(m=0;m<5;m++)
	{
		scanf("%d",&a[m]);
	}
	for(i=0;i<4;i++)
	{
		for(j=1;j<5-i;j++)
		{
			p_1=&a[j-1];
			p_2=&a[j];
			if(a[j-1]>a[j])
			{
				temp=*p_2;
				*p_2=*p_1;
				*p_1=temp;
			}
		}
	}
	for(n=0;n<5;n++)
	 printf("\n%5d",a[n]);
	return 0;
}
//3.编写一函数sort,完成对n个字符串的降序排序,
//然后在main函数中调用sort对"beijing","shenzhen","nanjing","dalian","shanghai"和"qingdao"
//这六个字符串排序,要求用指针数组表示这六个字符串。
int main3()
{
	char city[6][10]={"beijing","shenzhen","nanjing","dalian","shanghai","qingdao"};//存储城市名 
	char *ptstr[6];//指针数组
	int i;
	for(i=0;i<6;i++)
	{
		ptstr[i]=city[i];//令指针指向城市名 
	} 
    char *temp;
	int m,n;
	for(m=1;m<6;m++)
	{
		for(n=1;n<6-m;n++)
		{
			if(strcmp(ptstr[n-1],ptstr[n])>0)
			{
				temp=ptstr[n-1];
				ptstr[n-1]=ptstr[n];
				ptstr[n]=temp;
			}
		}
	}
	for(i=0;i<6;i++)
	 puts(ptstr[i]);
	return 0;
}
//4.编写函数实现矩阵(4行4列)的转置。要求函数的实参为数组名,形参为指针形式。
int zhuanzhi(int *p_a);
int main()
{
	int a[4][4];
	int i,j,temp;
	for(i=0;i<4;i++)
	{
		printf("please input numbers of line %d:\n",i+1);
		for(j=0;j<4;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	//输入部分结束
	printf("您输入的矩阵为:\n");
	int count=0;
	for(i=0;i<4;i++)
	 for(j=0;j<4;j++)
	 {
	 	count++;
	 	printf("%d",a[i][j]);
	 	if(count%4==0)
	 	 printf("\n");
	 } 
	 //显示矩阵结束 
	/*for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
		{
			temp=a[i][j];
			a[i][j]=a[j][i];
			a[j][i]=temp;
		}
	}
	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
		{
			printf("%d ",a[i][j]);
			count++;
			if(count%4==0)
			 printf("\n");
		}
	}*/
	int b[4][4];
	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
		 b[i][j]=zhuanzhi(a[i]);
	}
	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
		{
			printf("%d ",b[i][j]);
			count++;
			if(count%4==0)
			 printf("\n");
		}
	}
}
int zhuanzhi(int *p_a)
{
	int i,j,temp;
	for(i=0;i<4;i++)
	{
		return *(p_a+i);
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值