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

 我来答
匆匆催人老
2011-10-09 · TA获得超过338个赞
知道小有建树答主
回答量:303
采纳率:0%
帮助的人:225万
展开全部
楼主,你会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;
}

}
紫月居士
2011-10-01 · TA获得超过416个赞
知道小有建树答主
回答量:256
采纳率:0%
帮助的人:164万
展开全部
int year,s=0;
for (year = y1; year <y2; 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;m++)
//m1是你出生月份
switch(month)
{
case 1:s-=31;break;
case 2:s-=28;break;//或29,这个就手动写好了。。
……
case 12:s-=31;break;
}
s-=d1;//d1是你出生日期
……
//再加上今年已经经过的日子数,方法同上。就OK了
追问
for后面怎么写,
追答
for 后面就是switch。
结构:
计算增加的年份中的天数 for{switch{case...}}
计算减去的月份中的天数 {for{switch{case...}};s-=d1;}
计算增加的月份中的天数 for{switch{case...}}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
无病呻吟2010
2011-10-01 · TA获得超过963个赞
知道小有建树答主
回答量:527
采纳率:0%
帮助的人:613万
展开全部
#include <stdio.h>
#include <time.h>

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<now_year;i++)
count += ((i%400==0)||(i%4==0&&i%100!=0))?366:365;
n=day;

for(i=1;i<month;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;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式