有问题先搜索一下: 高级搜索

已解决问题 - 浏览243次

下一个已解决问题
helllkzhelllkz

高级书童

求一个简单的C++程序

一个简单的程序
悬赏分:50 - 离问题结束还有 14 天 14 小时
1.定义一个结构体变量(包括年、月、日),输入年、月、日。要求编一C++程序:
A.计算该日在本年中是第几天?注意闰年问题
闰年的条件:能被4整除,但不能被100整除,能被100
整除,又能被400整除的年份都是闰年。
B.计算这一年每月最后一天是本年中的第几天?并将结
果存放于文件中。

请教高手,谢谢了,能在关键地方写些注释更好。另外程序希望不要太复杂。

还可输入300个字

请输入上图中的验证码,字母不区分大小写。

hdk520hdk520

高级书童

最佳答案 - 由投票者2008-07-01 08:42:01选出

#include<iostream.h>
#include<fstream.h>
class Date
{
private:
int year;
int month;
int day;
public:
Date(){}
void SetDate(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
int IsLeap(int temp);
void JudgeDay(int y,int m,int d);
void JudgeMonth(int y,int m,int d);
};

int Date::IsLeap(int temp)
{
int leap;
if(temp%4==0)
{
if(temp%100==0)
{
if(temp%400==0)
leap=1;
else
leap=0;
}
else
leap=1;
}
else
leap=0;
return leap;
}

void Date::JudgeDay(int y,int m,int d)
{
int days[12]={31,0,31,30,31,30,31,31,30,31,30,31};
int count=0;
if(IsLeap(y))
days[1]=29;
else
days[1]=28;
for(int i=0;i<m-1;i++)
{
count+=days[i];
}
cout<<y<<"."<<m<<"."<<d<<" Is The "<<count+d<<" Day Of This Year."<<endl;
ofstream fout("Date.txt");
if(!fout)
cout<<"Error ! Cannot Open Input File."<<endl;
fout<<y<<".";
fout<<m<<".";
fout<<d<<"."<<endl;
fout<<y<<"."<<m<<"."<<d<<" Is The "<<count+d<<" Day Of This Year.";
fout.close();
}

void Date::JudgeMonth(int y,int m,int d) {}

void main()
{
int year,month,day;
cout<<"Enter The Year:"<<endl;
cin>>year;
cout<<"Enter The Month:"<<endl;
cin>>month;
cout<<"Enter The Day:"<<endl;
cin>>day;
Date date;
date.SetDate(year,month,day);
date.IsLeap(year);
date.JudgeDay(year,month,day);
}


程序通过编译,成功运行了。

学编程还是要版主你自己来写代码,所以留了JudgeMonth(int y,int m,int d)方法给你,跟JudgeDay(int y,int m,int d)方法差不多,你自己试试吧!其余的代码可以直接使用了

0 0

还可输入300个字

请输入上图中的验证码,字母不区分大小写。

其他回答(1)

  • 夏天夏天

    举人

    #include "iostream.h"
    struct a{
    int year;
    int month;
    int day;
    }b; //定义结构体(可以用类取代 比较好)
    main()
    {
    cout<<"input a num(year)"<<b.year;
    if(b.year%4==0&&b.year.....) //这里判断闰年的条件
    cout<<b.year<<"-- 是闰年" //以上是A部分


    }

    实现B的代码的原理:
    1.判断年性
    2.一个月一般是30天和31天交差的可以用++的方法实现记录天数
    3.判断特殊情况 2月的28天再循环中用if语句实现
    4.把记录的数字存储再一个数组中 要是输出到文件中要用文件操作函数
    基本思路就真样了 具体实现自己弄吧 思路清了 代码就快了

    还可输入300个字

    请输入上图中的验证码,字母不区分大小写。

返回知识堂首页>>

猜你喜欢看

更多

1
关于雅虎 | 帮助中心 | 联系我们 | 诚聘英才 | 广告服务 | 壹推广服务 | 搜索竞价 | 雅虎百业窗 | 网站联盟 [更多]
Copyright © 2008 Yahoo.com.cn 版权所有 不得转载 | 使用须知 | 著作权声明 | 京ICP证000022号 | 国家药监局(京)-经营性-2004-0039
全球阿里巴巴 - 阿里巴巴网络:中国站 国际站 日文站 | 淘宝站 | 支付宝 | 中国雅虎 | 口碑网 | 阿里软件 | 阿里妈妈