给出年月日,计算该日是该年的第几天
#include<stdio.h>
int leapYear(int y)
{
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))//判断是否闰年是返回1,不是返回0
{
return 1;
}
else return 0;
}
int judge(int y, int m, int d)
{
int x = 1;
switch (m)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if (d < 1 || d>31)
{
x = 0;
}
break;
case 2:
if (leapYear(y))
{
if (d < 1 || d>29)
x = 0;
}
else
if (d < 1 || d>28)
x = 0;
break;
case 4: case 6: case 9: case 11:
if (d < 1 || d>30)
x = 0;
break;
}
if (y < 0 || m < 1 || m>12)
x = 0;
return x;
}
int main()
{
int y[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//定义一维数数组,里面存入每个月的天数
int ye, m, d;
int sum = 0;
c: scanf("%d%d%d", &ye, &m, &d);
if (judge(ye, m, d))
{
if (leapYear(ye))//如果为闰年,将28重新赋值为29,如果,不是按照原来的数进行计算
{
y[1] = 29;
printf("%d年%d月%d日\n", ye, m, d);
for (int i = 0; i < m - 1; i++)
//m-1为所在前一个月的时间进行计算,在本月加所走过的天数sum + d
{
sum += y[i];
}
printf("这是该年的第%d天", sum + d);
}
else
{
printf("%d年%d月%d日\n", ye, m, d);
for (int i = 0; i < m - 1; i++)
{
sum += y[i];
}
printf("这是该年的第%d天", sum + d);
}
}
else
{
printf("输入错误\n"); goto c;
}
return 0;
}
求方程ax^2 +bx+c=0的根,用三个函数分别求当b^2-4ac大于0,等于0,小于0时,并输出结果
#include<stdio.h>
#include<math.h>
void f1(double a, double b, double c)
{
double x1, x2;
if (b*b - 4 * a*c > 0)
{
x1 = -b + sqrt(b*b - 4 * a*c) / (2 * a);
x2= -b -sqrt(b*b - 4 * a*c) / (2 * a);
printf("%f %f", -b + sqrt(b*b - 4 * a*c) / (2 * a), -b -sqrt(b*b - 4 * a*c) / (2 * a));
}
}
void f2(double a,double b,double c)
{
if (b*b - 4 * a*c == 0)
{
printf("%f", -b / 2 * a);
}
}
void f3(double a, double b, double c)
{
if (b*b - 4 * a*c < 0) //小于0,判断复根。也可以之间输出此函数无解
{
//printf("此函数无解");
printf("%f+%f %f-%f", -b/2*a , sqrt(-(b*b - 4 * a*c)) / (2 * a), -b , sqrt(-(b*b - 4 * a*c)) / (2 * a));
}
}
int main()
{
double a, b, c,x;
printf("输入三个数:");
scanf("%lf%lf%lf", &a, &b, &c);
f1(a, b, c);
f3(a, b, c);
f2(a, b, c);
}