工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1726|回复: 17

[求助]救命啊~~~~~~~

[复制链接]
发表于 2004-10-19 23:27 | 显示全部楼层 |阅读模式
这个程序哪里不对劲了啊
#include<iostream.h>
class Date
{
public:
         void SetDate();
         void Next();
private:
         int year;
         int month;
         int day;
         int days;
};

void Date::SetDate()
{
        cout<<"Enter the year,month,day:";
        cin>>year>>month>>day;
        for(;year<0;)
        {
                cout<<"Enter a wrong year.\n";
                cin>>year;
        }
        for(;month<0||month>0;)
        {
                cout<<"Enter a wrong month\n";
                cout<<"Enter the month:";
                cin>>month;
        }
        switch(month)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:days=31;break;
        case 4:
        case 6:
        case 9:
        case 11:days=30;break;
        case 2:if((year%4==0&&year%100!=0)||(year%400==0))
                           days=28;
                   else days=28;
        }

void Date::Next(int year,int month,int day)//第二天
        {
                if(month=12&&day=days)//年底
                        ++year;
                month=1;
                day=1;
                else if(month!=12&&day=days)//月底
                        ++month;
                day=1;
                else ++day;
                cout<<"The next day is "<<day<<"/"month<<"/"<<year;
        }

int main()
        {
                Date ymd;
                ymd.SetDate();
                ymd.Next();
                return 0;
        }
发表于 2004-10-19 23:44 | 显示全部楼层
偶告诉你哪里不对劲吧……就是偶完全看不懂……真不对劲……
回复

使用道具 举报

 楼主| 发表于 2004-10-20 15:45 | 显示全部楼层
........................
回复

使用道具 举报

发表于 2004-10-21 17:26 | 显示全部楼层
for(;month<0||month>0;)
        
month无论小于0或大于0都会执行该循环,是个死循环。
只有month=0时才跳出循环。
但month=0时,switch语句没有执行到一个case,
days会成为没有初始化的变量,
在Next()方法里引用一个没有初始化的变量是不行的。
为了程序的健壮性,对day也应进行输入合法性判断。
大概就这些吧。
回复

使用道具 举报

发表于 2004-10-22 01:27 | 显示全部楼层
好象有N多个问题~
回复

使用道具 举报

 楼主| 发表于 2004-10-22 23:35 | 显示全部楼层
Originally posted by hjack at 2004-10-21 17:26:
for(;month<0||month>0;)
        
month无论小于0或大于0都会执行该循环,是个死循环。
只有month=0时才跳出循环。
但month=0时,switch语句没有执行到一个case,
days会 ...

呵呵~这个是笔误啦
不过很谢谢你
这个我打算重搞了
回复

使用道具 举报

发表于 2004-10-23 12:37 | 显示全部楼层
说说你编译有什么错误?

还有 ymd.Next();要有参数的。
回复

使用道具 举报

发表于 2004-10-23 13:03 | 显示全部楼层
给你一个正确的程序吧:

// a.cpp : 定义控制台应用程序的入口点。
//

#include \"stdafx.h\"

#include<iostream>
using namespace std;
class Date
{
public:
         void SetDate();
         void Next(int,int ,int);
public:
         int year;
         int month;
         int day;
         int days;
};

void Date::SetDate()
{
        cout<<\"Enter the year,month,day:\";
        cin>>year>>month>>day;
        for(;year<0;)
        {
                cout<<\"Enter a wrong year.\\n\";
                cin>>year;
        }
        for(;month<0||month>12;)
        {
                cout<<\"Enter a wrong month\\n\";
                cout<<\"Enter the month:\";
                cin>>month;
        }
        switch(month)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:days=31;break;
        case 4:
        case 6:
        case 9:
        case 11:days=30;break;
        case 2:if((year%4==0&&year%100!=0)||(year%400==0))
                           days=28;
                   else days=28;
        }
}
void Date::Next(int year,int month,int day)//第二天
{
        if(month==12&&day==days)//年底
                {      
                        ++year;
                        month=1;
                        day=1;
                }
        else if(month!=12&&day==days)//月底
                {     
                        ++month;
                        day=1;
                }
        else ++day;
        cout<<\"The next day is \"<<day<<\"/\"<<month<<\"/\"<<year;
}

int _tmain(int argc, _TCHAR* argv[])
{
        Date ymd;
    ymd.SetDate();
    ymd.Next(ymd.year,ymd.month,ymd.day);
        char c=getchar();
        while(c!=\'e\')
                c=getchar();
        return 0;
}


我在vc.net环境下运行通过了,实现了你要的功能。
你还可以扩展一下,在处理异常方面做得更好些。
自己慢慢体会。。。。
回复

使用道具 举报

 楼主| 发表于 2004-10-23 23:51 | 显示全部楼层
喔~~~~~~~~
谢谢高人指点!!
回复

使用道具 举报

 楼主| 发表于 2004-10-25 20:34 | 显示全部楼层
char c=getchar();
        while(c!=\'e\')
                c=getchar();
请问这个有什么作用的啊?
在vc.net环境下?什么意思啊?
还是不行啊,它说“Cannot open include file: \'stdafx.h\'
回复

使用道具 举报

发表于 2004-10-25 20:55 | 显示全部楼层
在visual c++.net下,你要先建一个项目,为win 32控制台项目。(你用的是什么编译环境?)
char c=getchar();
        while(c!=\'e\')
                c=getchar();
是为了查看结果,只有当输入字符e才退出程序。
如果你用VC6.0的话,上面的程序不要#include"stdafx.h"应该可以运行的吧。
回复

使用道具 举报

 楼主| 发表于 2004-10-25 22:03 | 显示全部楼层
是VC6.0的
不行啊
痛苦~~~~~
回复

使用道具 举报

发表于 2004-10-25 22:09 | 显示全部楼层
vc6.0下把#include"stdafx.h"去掉,
把主函数改为int main()就行啦。

多试试。。。
回复

使用道具 举报

 楼主| 发表于 2004-10-25 22:16 | 显示全部楼层
死循环啊~~~~~~~
超痛苦~~~~
简直想死~
回复

使用道具 举报

发表于 2004-10-25 22:27 | 显示全部楼层
不可能的,我在vc 6.0下都可以通过。
结果为:
Enter the year,month,day:2004 10 25
The next day is:26/10/2004

那里死循环???

这样做一定不会有错误了:
进入vc6.0,文件->新建->C++ Source file.
建一个cpp文件,输入:
#include<iostream>
using namespace std;
class Date
{
public:
。。。。//省略了,自己添上

int main()
{
        Date ymd;
    ymd.SetDate();
    ymd.Next(ymd.year,ymd.month,ymd.day);
        char c=getchar();
        while(c!=\'e\')
                c=getchar();
        return 0;
}


编译-执行。
ok
回复

使用道具 举报

 楼主| 发表于 2004-10-25 22:33 | 显示全部楼层
哦~~~~~~~
我输入错了
不过好像很不完善呢
跟想象中的差点了
呵呵`~~~~~~~~~
不过还是很谢谢你啊
回复

使用道具 举报

发表于 2004-10-25 22:37 | 显示全部楼层
我前面都说了,我这是按你的程序改的,
我已叫你对异常进行捕获,扩展一下它。
其实这个程序还有很多不足的,你自己扩展一下对自己很有帮助的。
回复

使用道具 举报

 楼主| 发表于 2004-10-25 22:43 | 显示全部楼层
呵呵~~~~~
知道了
等我不会这么痛苦的时候在研究它吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2024-5-15 20:43

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表