第9章实验 指针
- 题目9.3:
利用例9.6程序中的函数Swap(),从键盘输入10个整数,用函数编程实现计算其最大值和最小值,并互换他们所在数组中的位置。
1、解题思路:
主函数中,定义数组,使用指针定义最大值,最小值及其位置,并对指针赋初值,在循环中输入10个数,在外层函数原型声明输出函数,调用此函数,输出初始数组数。外层函数原型声明,用指针指最大值,最小值及其位置,主函数调用,外层定义最大值,最小值的位置起始为0,循环中进行比较if(a[j]>=a[m]) m=j;if(a[j]<=a[p]) p=j;用指针指出最大值,最小值及其位置带出来。输出最大值,最小值。交换最大值与最小值的位置,外层函数声明交换函数,定义temp,交换后,用指针带出来,输出交换后的数组。
2、源代码:
#include <stdio.h>
#include <stdlib.h>
int n=10;
void Swap(int *a,int *b);
void printfnum(int a[],int n);
void Findnum(int a[],int n,int *pMax,int *pMin,int *pMaxpos,int *pMinpos);
int main()
{
int i,c,d;
int a[n],Max,Min,Maxpos,Minpos,*pMax,*pMin,*pMaxpos,*pMinpos;
pMax=&Max;
pMin=&Min;
pMaxpos=&Maxpos;
pMinpos=&Minpos;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printfnum(a,n);
Findnum(a,n,pMax,pMin,pMaxpos,pMinpos);
printf("Max = %d",*pMax);
printf("Min = %d",*pMin);
printf("\n");
c=&a[*pMaxpos];
d=&a[*pMinpos];
Swap(c,d);
printfnum(a,n);
return 0;
}
void Swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void printfnum(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%5d",a[i]);
}
printf("\n");
}
void Findnum(int a[],int n,int *pMax,int *pMin,int *pMaxpos,int *pMinpos)
{
int i,j,m=0,p=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>=a[m]) m=j;
if(a[j]<=a[p]) p=j;
}
}
*pMax=a[m];
*pMin=a[p];
*pMaxpos=m;
*pMinpos=p;
}
3、程序运行效果截图:
- 题目2:
按如下函数原型用函编程解决如下的日期转换问题(要求考虑闰年问题)
(1)输入某年某月某日,计算并输出它是这一年的第几天:
/*函数功能:对给定的某年某月某日,计算它是这一年的第几天。
函数参数:整型变量year, month,day,分别代表年月日
函数返回值:这一 年的第几天*/
- 解题思路:
分别定义闰年和平年,主函数中输入年月日,判断是否为闰年,调用外层函数,在闰年,一维数组,循环中累加,加上输入的数,指针带出。在平年,类似方法,调出。
2、源代码:
#include <stdio.h>
#include <stdlib.h>
void DayofYear(int Mouth,int Day,int *Days);
void DayofYear1(int Mouth,int Day,int *Days);
int main()
{
int YearDay,*Days=&YearDay;
int Year,Mouth,Day;
printf("Year:");
scanf("%d",&Year);
printf("Mouth:");
scanf("%d",&Mouth);
printf("Day:");
scanf("%d",&Day);
if((Year % 4 == 0)&&(Year % 100 != 0)||(Year % 400 == 0))
{
printf("Year is leap year");
DayofYear(Mouth,Day,Days);
printf("第 %d 天",*Days);
}
else
{
printf("Year is not leap year");
DayofYear1(Mouth,Day,Days);
printf("第 %d 天",*Days);
}
return 0;
}
void DayofYear(int Mouth,int Day,int *Days)
{
int sum=0,i;
int a[]={31,29,31,30,31,30,31,31,30,31,30,31};
for(i=0;i<Mouth-1;i++)
{
sum=sum+a[i];
}
sum=Day+sum;
*Days=sum;
}
void DayofYear1(int Mouth,int Day,int *Days)
{
int sum=0,j;
int b[]={31,28,31,30,31,30,31,31,30,31,30,31};
for(j=0;j<Mouth-1;j++)
{
sum=sum+b[j];
}
sum=Day+sum;
*Days=sum;
}
3、程序运行效果截图:
- 题目3:
2)输入某一年的第几天,计算并输出它是这一年的第几月第几日。
函数功能:对给定的某一年的第儿天,计算它是这年的第几月第儿日
函数人口参数:整型变量year,存储年
整型变量yearDay,存储这年的第几天
函数出口参数:整型指针pMonth,指向存储这一年第几月的整型变量
整型指针pDay,指向存储第几日的整型变量
函数返回值:无*/
- 解题思路:
主函数中输入年份和天数,判断闰年,平年。调用外层函数,输出月,日。外层函数For循环,判断句,直到yearday小于数组中数值,跳出循环。用指针带出月,日。
2、源代码:
#include <stdio.h>
#include <stdlib.h>
void DayofYear(int *MouthDay,int *pMouth,int YearDay);
void DayofYear1(int *MouthDay,int *pMouth,int YearDay);
int main()
{
int Mouth,*pMouth=&Mouth;
int Year,YearDay;
int Days,*MouthDay=&Days;
printf("Year:");
scanf("%d",&Year);
printf("YearDay:");
scanf("%d",&YearDay);
if((Year % 4 == 0)&&(Year % 100 != 0)||(Year % 400 == 0))
{
printf("Year is leap year");
DayofYear(MouthDay,pMouth,YearDay);
printf("第 %d 月 第 %d 天",*pMouth,*MouthDay);
}
else
{
printf("Year is not leap year");
DayofYear1(MouthDay,pMouth,YearDay);
printf("第 %d 月 第 %d 天",*pMouth,*MouthDay);
}
return 0;
}
void DayofYear(int *MouthDay,int *pMouth,int YearDay)
{
int i;
int a[]={31,29,31,30,31,30,31,31,30,31,30,31};
for(i=0;YearDay>a[i];i++)
{
YearDay=YearDay-a[i];
}
*pMouth=i;
*MouthDay=YearDay;
}
void DayofYear1(int *MouthDay,int *pMouth,int YearDay)
{
int j;
int b[]={31,28,31,30,31,30,31,31,30,31,30,31};
for(j=0;YearDay>b[j];j++)
{
YearDay=YearDay-b[j];
}
*pMouth=j;
*MouthDay=YearDay;
}
3、程序运行效果截图:
- 题目4:
(3)输入如下菜单 ,用switch语句实现根据用户输入的选择执行相应的操作:
1.year/ month/ day -> yearDay
2.yearDay -> year/ month/ day
3. Exit
Please enter your choice:
1、解题思路:
Switch选择想要选项。
- 源代码:
#include <stdio.h>
#include <stdlib.h>
void DayofYear(int Mouth,int Day,int *pDays);
void DayofYear1(int Mouth,int Day,int *pDays);
void DayofYear2(int *MouthDay,int *pMouth,int YearDay);
void DayofYear3(int *MouthDay,int *pMouth,int YearDay);
int main()
{
int num;
int YearDay,*pDays=&YearDay;
int Year,Mouth,Day;
int kMouth,*pMouth=&kMouth;
int Days,*MouthDay=&Days;
printf("Enter 1 to Yearday:\n");
printf("Enter 2 to Mouth and:\n");
printf("Exist enter your choice:\n");
scanf("%d",&num);
switch(num)
{
for(;num !=1 && num != 2;)
{
case 3:
printf("Please enter your choice:");
break;
case 1:
printf("Year:");
scanf("%d",&Year);
printf("Mouth:");
scanf("%d",&Mouth);
printf("Day:");
scanf("%d",&Day);
if((Year % 4 == 0)&&(Year % 100 != 0)||(Year % 400 == 0))
{
printf("Year is leap year");
DayofYear(Mouth,Day,pDays);
printf("第 %d 天",*pDays);
}
else
{
printf("Year is not leap year");
DayofYear1(Mouth,Day,pDays);
printf("第 %d 天",*pDays);
}
break;
case 2:
printf("Year:");
scanf("%d",&Year);
printf("YearDay:");
scanf("%d",&YearDay);
if((Year % 4 == 0)&&(Year % 100 != 0)||(Year % 400 == 0))
{
printf("Year is leap year");
DayofYear2(MouthDay,pMouth,YearDay);
printf("第 %d 月 第 %d 天",*pMouth,*MouthDay);
}
else
{
printf("Year is not leap year");
DayofYear3(MouthDay,pMouth,YearDay);
printf("第 %d 月 第 %d 天",*pMouth,*MouthDay);
}
break;
}
}
return 0;
}
void DayofYear(int Mouth,int Day,int *pDays)
{
int sum=0,i;
int a[]={31,29,31,30,31,30,31,31,30,31,30,31};
for(i=0;i<Mouth-1;i++)
{
sum=sum+a[i];
}
sum=Day+sum;
*pDays=sum;
}
void DayofYear1(int Mouth,int Day,int *pDays)
{
int sum=0,j;
int b[]={31,28,31,30,31,30,31,31,30,31,30,31};
for(j=0;j<Mouth-1;j++)
{
sum=sum+b[j];
}
sum=Day+sum;
*pDays=sum;
}
void DayofYear2(int *MouthDay,int *pMouth,int YearDay)
{
int i;
int a[]={31,29,31,30,31,30,31,31,30,31,30,31};
for(i=0;YearDay>a[i];i++)
{
YearDay=YearDay-a[i];
}
*pMouth=i;
*MouthDay=YearDay;
}
void DayofYear3(int *MouthDay,int *pMouth,int YearDay)
{
int j;
int b[]={31,28,31,30,31,30,31,31,30,31,30,31};
for(j=0;YearDay>b[j];j++)
{
YearDay=YearDay-b[j];
}
*pMouth=j;
*MouthDay=YearDay;
}
3、程序运行效果截图:
- MOOC网课程测试结果:
1、测试1: