GDPU 3

实验三:

目录

实验三:

1.分段函数

2.三角形的周长和面积

3.月份判断

4.能否整除

5.判断闰年

6.日期合法性

7.日期合法性

8.某年某月某日是当年的第几天

9.某年某月某日是当年的第几天

10.某年某月某日是当年的第几天

11.某年某月某日是当年的第几天(数组)

12.某年某月某日是当年的第几天


1.分段函数

#include<stdio.h>
int main()
{
	double x,y;
	scanf("%lf",&x);
	if(x<1)
		y=x;
	if(1<=x<10)
		y=x-1;
	if(x>=10)
		y=3*x-11;
	printf("y=%.2lf",y);
	return 0;
}

2.三角形的周长和面积

#include<stdio.h>
#include<math.h>
int main()
{
	double x1,x2,x3,y1,y2,y3;
	double a,b,c,s,L,S;
	scanf("%lf,%lf",&x1,&y1);
	scanf("%lf,%lf",&x2,&y2);
	scanf("%lf,%lf",&x3,&y3);
	a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
	b=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
	c=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
	s=(a+b+c)/2;
	L=a+b+c;
	S=sqrt(s*(s-a)*(s-b)*(s-c));
	
	if(a+b>c && a+c>b && b+c>a)
		printf("primeter=%.2lf,area=%.2lf",L,S);
	else
		printf("Impossible");
	return 0;
}

也可放在if里面 

#include<stdio.h>
#include<math.h>
int main()
{
	double x1,x2,x3,y1,y2,y3,S,L;
	double a,b,c,s;
	scanf("%lf,%lf",&x1,&y1);
	scanf("%lf,%lf",&x2,&y2);
	scanf("%lf,%lf",&x3,&y3);
	a=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
	b=sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
	c=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
	if(a+b>c && a+c>b && b+c>a && a>0 && b>0 && c>0){
		s=(a+b+c)/2; 
		S=sqrt(s*(s-a)*(s-b)*(s-c));
		L=a+b+c;
		printf("primeter=%.2f,area=%.2f",L,S);
	}	
	else
		printf("Impossible");
	return 0;
}

3.月份判断

#include<stdio.h>
int main()
{
	int y;
	scanf("%d",&y);
	if(y>=2 && y<=4)
		printf("Spring");
	if(y>=5 && y<=7)
		printf("Summer");
	if(y>=8 && y<=10)
		printf("Autumn");
	if(y>=11 && y<=12 || y==1)
		printf("Winter");
	if(y>12 || y<1)
		printf("Error");
	return 0;
}

4.能否整除

#include<stdio.h>
int main()
{
	int x;
	scanf("%d",&x);
	if(x%5==0 && x%7==0)
		printf("A");
	else if(x%5==0)
		printf("B");
	else if(x%7==0)
		printf("C");
	else if(x%5!=0 && x%7!=0)
		printf("D");
	return 0;
}

5.判断闰年

#include<stdio.h>
int main()
{
	int year,leap;
	//leap为0,不是闰年;leap为1,是闰年
    scanf("%d",&year);
    if(year%4==0)
	{/*能被4整除的情况*/
		if(year%100==0)
		{
			if(year%400==0)
				leap=1;
			else
				leap=0;
		}			
	}
	else 
    	leap=0;//不能被4整除,不是闰年 如:1973
	if(leap)
		printf("yes");
	else
		printf("no");
	return 0;
}

6.日期合法性

#include<stdio.h>
int judgeyear(int year)
{
    int flag=0;//平年返回0
    if ((year%4==0 && year%100 !=0) || year%400==0)
		flag=1;//闰年返回1
	return flag;
}

int main()
{
    int year,month,day;
    int flag_1=0;
    scanf("%d%d%d",&year,&month,&day);
    //判断输入的合法性
    if(day>=1 && day<=31 && (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12))//大月的月份
		flag_1=1;
    else if(day>=1 && day<=30 &&(month==4||month==6||month==9||month==11))
	//小月的日的范围
		flag_1=1;
    else if(day>=1 && day<=28+judgeyear(year) && month==2)
           flag_1=1;

    if(flag_1)
		printf("yes");
    else
		printf("no");
	return 0;
}

7.日期合法性

(先假设平年,列出所有不合法情形,其余合法)

#include<stdio.h>

int judgeyear(int year)
{

/*此函数的功能为判断一个年份year(整数)是不是闰年,闰年返回1,否则返回0

先假设所有的年份都是平年,然后判断是否满足闰年的条件,若满足,修改返回值*/

    int flag=0;//平年返回0

    if (year%400==0 || year%4==0 && year%100!=0)
		flag=1; 
    return flag;
}
int main()
{
    int year,month,day;
    int flag_1=1; //1合法,2非法
    scanf("%d%d%d",&year,&month,&day);
    
    if(day>31 || day<1 || month>12 || month<0 || year<0)
    	flag_1=0;
    else if(day==31 && (month==4 && month==6 && month==9 && month==11))
    	flag_1=0;
    else if(day==30 && month==2)
        flag_1=0;
    else if(month==2)
	{
	if(day>28+judgeyear(year))
		flag_1=0; 
//考虑2月的情况,平年超过28,闰年超过29,都是非法的
    }

    if(flag_1)
		printf("yes");
    else
		printf("no");
	return 0;
}

8.某年某月某日是当年的第几天

switch(表达式)

{

        case 1:

        case 2:

        case 3:

        case 4:

        case 5:

        case 6:

        ...... ......

        case n:

        default: ......break;

}

switch:(处理多分支选择)(break语句结束循环)

①每个语句都使用break(输入-->结束,一次)

②不使用break(不结束,执行完当前语句后执行下一个语句)

③某些语句使用break(某些语句执行完就结束)

#include<stdio.h>
int judgeyear(int year)
{
    int flag=0;//平年返回0
    if ((year%4==0 && year%100 !=0) || year%400==0)
	    flag=1;//闰年返回1
    return flag;
}

/*本题不考虑日期的合法性,输入的日期是合法的*/

int main(){
    int year,month,day;
    int dayNumber=0;
    scanf("%d%d%d",&year,&month,&day);
    switch (month)
	{
    	case 12:dayNumber +=30;
    	case 11:dayNumber +=31;
		case 10:dayNumber +=30;
		case 9 :dayNumber +=31;
		case 8 :dayNumber +=31;
		case 7 :dayNumber +=30;
		case 6 :dayNumber +=31;
		case 5 :dayNumber +=30;
		case 4 :dayNumber +=31;
		case 3 :dayNumber +=28+judgeyear(year);//3月的情况(三月天数+二月天数)
		case 2 :dayNumber +=31;//2月的情况
		case 1 :dayNumber +=day;break;//1月的情况
		default:printf("Input error!");
    }
	printf("%d",dayNumber);
	return 0;
}

9.某年某月某日是当年的第几天

本题指到某天后,用总天数(dayNumber)-后面的天数

!!!dayNumber-=30 <==> dayNumber=dayNunber-30

#include<stdio.h>
int judgeyear(year)
{
	int flag=0;//平年返回0
	if((year%4==0 && year%100!=0) || year%400==0)
		flag=1;//闰年返回1
	return flag; 
}

int main()
{
	int year,month,day;
	int dayNumber=365;
	scanf("%d%d%d",&year,&month,&day);
	
	if (judgeyear(year))
	dayNumber=366;
	switch(month)
	{
		//dayNumber-=30 == dayNumber=dayNumber-30
		case 1:dayNumber-=31;
		case 2:dayNumber-=28+judgeyear(year);
		case 3:dayNumber-=31;
		case 4:dayNumber-=30;
		case 5:dayNumber-=31;
		case 6:dayNumber-=30;
		case 7:dayNumber-=31;
		case 8:dayNumber-=31;
		case 9:dayNumber-=30;
		case 10:dayNumber-=31;
		case 11:dayNumber-=30;
		case 12:dayNumber-=31;break;
		dafault:
			printf("Input error!");
	}
	printf("%d",dayNumber+day);
	return 0;
}

10.某年某月某日是当年的第几天

#include<stdio.h>
int main()
{
	int year,month,day,Feb,sum;
	scanf("%d%d%d",&year,&month,&day);
	if((day>31) || (day<1))
	{
		printf("input error!");
	return 0;
	}
	if((year%4==0 && year%100!=0) || (year%400==0)) 
		{Feb=29;}
	else
		{Feb=28;}
	
	switch(month)
	{
		case 1:sum=day;break;
		case 2:if(day>Feb)
				{
					printf("input error\n");
					return 0;
				} 
				else
				{
					sum=31+day;break;
				}
		case 3:sum=31+Feb+day;break;
		case 4:sum=62+Feb+day;break;
		case 5:sum=92+Feb+day;break;
		case 6:sum=123+Feb+day;break;
		case 7:sum=153+Feb+day;break;
		case 8:sum=184+Feb+day;break;
		case 9:sum=215+Feb+day;break;
		case 10:sum=245+Feb+day;break;
		case 11:sum=276+Feb+day;break;
		case 12:sum=306+Feb+day;break;
		defult:printf("input error\n");
	}	
		printf("%d",sum);
		return 0;
}

11.某年某月某日是当年的第几天(数组)

#include<stdio.h>
int main(){
    int month_day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//第一个元素a0
    int year,month,day;
    int i;
    scanf("%d%d%d",&year,&month,&day);
    if(month<0||month>12)
           printf("date error");
    else
	{
           for(i=1;i<month;i++)
		   {
                  day=day+month_day[i];
                  if (i==2 && year%400==0 || (year%400==0 && year%100!=0))
                         day++;

           }
    }
    printf("%d",day);
	return 0; 
}

12.某年某月某日是当年的第几天(循环)

#include<stdio.h>
int main(){
    int year,month,day,week;
    scanf("%d%d%d",&year,&month,&day);
    scanf("%d",&week);
    if(month<0 || month>12)
           printf("date error");
    else{
    	int i;
		for(i=1;i<month;i++)
		{
			int month_day=0;
			switch(month-1)
			{
				case 12:month_day+=31;
				case 11:month_day+=30;
				case 10:month_day+=31;
				case 9:month_day+=30;
				case 8:month_day+=31;
				case 7:month_day+=31;
				case 6:month_day+=30;
				case 5:month_day+=31;
				case 4:month_day+=30;
				case 3:month_day+=31;
				case 2:
					if((year%4==0 && year%100!=0) || year%400==0)
						month_day+=29;
					else
						month_day+=28;
				case 1:month_day+=31;break;
				defaut:printf("Input error!"); 
			}	
			day=day+month_day;
        }
    }
    printf("%d",(day+week-1)%7);
	return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值