c语言编程初学。用c语言写:计算从出生到现在一共多少天?要用到switch 语句或for循环。

2025-04-05 22:02:58
推荐回答(3个)
回答1:

楼主,你会JAVA吗?我刚用JAVA写了一个,语法可能和你的C语言不一样,但算法应该可以看懂
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

//计算出生了多少天

public class Tian {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int year=0,month=0,day=0;
int tianshu;
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年");
year = scanner.nextInt();
System.out.print("请输入月");
month = scanner.nextInt();
System.out.print("请输入日");
day = scanner.nextInt();
tianshu=manyday(year , month , day );
System.out.println("您生活了:"+tianshu+"天");
}

//输入年月日,输出天数
private static int manyday(int year,int month,int day){
int shu = 0;
int todayyear,todaymonth,todayday;
String sNow = "";
int sheng=0; //某日期到年底的天数

Date today = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sNow = sdf.format(today);
todayyear = Integer.parseInt(sNow.substring(0,4));
todaymonth = Integer.parseInt(sNow.substring(4,6));
todayday = Integer.parseInt(sNow.substring(6));

//System.out.println(year + " . " + month + " . " + day + " . " + todayyear + " . " + todaymonth + " . " + todayday);

for(int i = year + 1;i < todayyear;i ++){
if (i % 4 == 0) {
shu = shu + 366;
} else {
shu = shu + 365;
}
}

sheng=howday(year, month, day);
if(year % 4 == 0){
sheng = 366 - sheng ;
}else {
sheng = 365 - sheng;
}
shu = shu + sheng + howday(todayyear, todaymonth, todayday);

return shu;
}
//输入年月日 返回该年的天数

private static int howday(int year,int month,int day){
int da[] = {1,3,5,7,8,10,12};
int xiao[] = {4,6,9,11};
int tian = 0;
if(month == 1){
tian = day;
}
else{
for(int i = 1;i < month;i++){
if(panduan(da, i)){
tian = tian + 31;
}
else if(panduan(xiao, i)){
tian = tian + 30;
}
else if ((i == 2)&&(year%4 == 0)) {
tian = tian + 29;
}else if ((i == 2)&&(year%4 != 0)) {
tian = tian + 28;
}
}
tian = tian + day;
}
return tian;
}

//判断变量是否属于数组
private static boolean panduan(int[] a,int bianliang){
boolean boo = false;
for(int i = 0;i < a.length;i ++){
if(a[i] == bianliang){
boo = true;
break;
}
}
return boo;
}

}

回答2:

int year,s=0;
for (year = y1; year //y1是你出生的年份,y2是现在的年份
switch(year mod 4)
{
case 0:
{
if((year mod 100 == 0) && (year mod 400 != 0)) s+=365;
else s+=366;
break;
}
default:s+=365;
}
//再减去你出生那年年初至你出生的日子
for(month=1;month//m1是你出生月份
switch(month)
{
case 1:s-=31;break;
case 2:s-=28;break;//或29,这个就手动写好了。。
……
case 12:s-=31;break;
}
s-=d1;//d1是你出生日期
……
//再加上今年已经经过的日子数,方法同上。就OK了

回答3:

#include
#include

int calc(int year,int month, int day)
{
struct tm *tp;
time_t t = time(0);
int now_year, now_month, now_day;
int count;
int i;
int a[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int n;

tp = localtime(&t);
now_year = tp->tm_year+1900;
now_month = tp->tm_mon;
now_day = tp->tm_mday;

count = 0;

if(year != now_year)
{
if(month <= 2)
count += (year%400==0)||(year%4==0&&year%100!=0)?366:365;
else
count += (now_year%400==0)||(now_year%4==0&&now_year%100!=0)?366:365;
}

for(i=year+1;i count += ((i%400==0)||(i%4==0&&i%100!=0))?366:365;
n=day;

for(i=1;i n += a[i];
if(now_year%400==0 || (now_year%4==0&&now_year%100!=0))
n += 1;

return count + tp->tm_yday+1 - n;
}

int main()
{
int year,month,day;

scanf("%d%d%d",&year,&month,&day);
printf("%d\n",calc(year,month,day));

return 0;
}