| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1126 人关注过本帖
标题:求助!这是怎么回事?
只看楼主 加入收藏
loookc
Rank: 2
等 级:论坛游民
帖 子:24
专家分:11
注 册:2009-10-15
收藏
得分:0 
经过大家的共同努力,我们成功了!下面的源代码是改好的
它的功能是计算任意一天是星期几
请大家分享
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int bYear[]={31,29,31,30,31,30,31,31,30,31,30,31};
int sYear[]={31,28,31,30,31,30,31,31,30,31,30,31};
cout<<"enter year,month,day:"<<endl;
int year,month,day;
int s1=0,s2=0,s=0,fact=0;
int i;
cin>>year>>month>>day;
s1=(year-1)+((year-1)/4)-((year-1)/100)+((year-1)/400);
if(year%400||year%4)
{
   for(i=0;i<=(month-1);++i)
      {
      s2=s2+bYear[i];
      }   
}
if(year%400!=0||year%4!=0)
{
     for(i=0;i<=(month-1);++i)
     {
        s2=s2+sYear[i];
     }
}
s2=s2+day;
s=s1+s2;
fact=s%7;
switch(fact)
{
   case 0:
      cout<<"today is sunday\n";
      break;
   case 1:
      cout<<"today is monday\n";
      break;
   case 2:
      cout<<"today is tuseday\n";
      break;
   case 3:
      cout<<"today is wednesday\n";
      break;
   case 4:
      cout<<"today is thurday\n";
      break;
   case 5:
      cout<<"today is friday\n";
      break;
   case 6:
      cout<<"today is saturday\n";
      break;
   default:cout<<"this program has bugs.";
}
return 0;
}      
谢谢大家!!!!!!!!!!!

[ 本帖最后由 loookc 于 2009-10-17 13:16 编辑 ]
2009-10-17 12:24
最左边那个
Rank: 4
等 级:业余侠客
威 望:3
帖 子:97
专家分:201
注 册:2009-4-24
收藏
得分:0 
回复 10楼 东海一鱼
可是int fact 的话,会默认为是unsigned int fact吗?
我个人觉得是不会,所以,如果是我,我会做一些处理!
不过只是个人理解,还请你给我解释一下,谢谢!
2009-10-17 13:43
东海一鱼
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:48
帖 子:757
专家分:4760
注 册:2009-8-10
收藏
得分:0 
以下是引用最左边那个在2009-10-17 13:43:02的发言:

可是int fact 的话,会默认为是unsigned int fact吗?
我个人觉得是不会,所以,如果是我,我会做一些处理!
不过只是个人理解,还请你给我解释一下,谢谢!
  声明int fact当然不会自动做unsigned int转换。
  所以我说必须将fact声明为unsigned int型才可以。

  或者就像你所说的自己做一些处理。比如说你可以用数学函数abs求绝对值。

  也可以自定义一个宏:
  #define   MYABS(x) ((x)<0?-(x):(x))

  switch(MYABS(fact))
        {
         。。。。。。
         。。。。。。
        }

都可以,你只要明白其中的道理就行。

举世而誉之而不加劝,举世而非之而不加沮,定乎内外之分,辩乎荣辱之境,斯已矣。彼其于世未数数然也。
2009-10-17 14:17
loookc
Rank: 2
等 级:论坛游民
帖 子:24
专家分:11
注 册:2009-10-15
收藏
得分:0 
以下是引用东海一鱼在2009-10-17 14:17:51的发言:

  声明int fact当然不会自动做unsigned int转换。
  所以我说必须将fact声明为unsigned int型才可以。

  或者就像你所说的自己做一些处理。比如说你可以用数学函数abs求绝对值。

  也可以自定义一个宏:
  #define ...
应该不用
因为根据算“任意一天是星期几的算法看来”fact是不会为负的
2009-10-17 14:44
loookc
Rank: 2
等 级:论坛游民
帖 子:24
专家分:11
注 册:2009-10-15
收藏
得分:0 
经过测试,上面的程序有误
2009-10-17 15:05
loookc
Rank: 2
等 级:论坛游民
帖 子:24
专家分:11
注 册:2009-10-15
收藏
得分:0 
改了的
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int bYear[]={31,29,31,30,31,30,31,31,30,31,30,31};
int sYear[]={31,28,31,30,31,30,31,31,30,31,30,31};
cout<<"enter year,month,day:"<<endl;
int year,month,day;
int s1=0,s2=0,s=0,fact=0;
int i;
cin>>year>>month>>day;
s1=(year-1)+((year-1)/4)-((year-1)/100)+((year-1)/400);
     if((year%4==0)&&(year%100!=0)||(year%400==0))
     {
         for(i=0;i<=(month-2);++i)
         {
         s2=s2+bYear[i];
         }   
     }
     else
     {  
         for(i=0;i<=(month-2);++i)
         {
         s2=s2+sYear[i];
         }
     }
      
s2=s2+day;
s=s1+s2;
fact=s%7;
switch(fact)
{
   case 0:
      cout<<"this day is sunday\n";
      break;
   case 1:
      cout<<"this day is monday\n";
      break;
   case 2:
      cout<<"this day is tuseday\n";
      break;
   case 3:
      cout<<"this day is wednesday\n";
      break;
   case 4:
      cout<<"this day is thurday\n";
      break;
   case 5:
      cout<<"this day is friday\n";
      break;
   case 6:
      cout<<"this day is saturday\n";
      break;
   default:cout<<"this program has bugs.";
}
return 0;
}      
2009-10-17 15:25
wghost
Rank: 2
等 级:论坛游民
帖 子:47
专家分:31
注 册:2009-6-6
收藏
得分:0 
8楼说的对,下面是我做的一些修改
#include<iostream>  
using namespace std;                                   //c++中开头用此两行就可以
int main()  
{  
int bYear[]={0,31,29,31,30,31,30,31,31,30,31,30,31};  //如果要是i=0必须这样定义
int sYear[]={0,31,28,31,30,31,30,31,31,30,31,30,31};  
cout<<"enter year,month,day:"<<endl;  
int year,month,day;  
int s1,s2,s,fact;  
int i;  
s2=0;
cin>>year>>month>>day;  
s1=(year-1)+((year-1)/4)-((year-1)/400);               //将两行调换,否则出想你说的错误
if(year%400&&year%4)  
{for(i=0;i<=(month-1);++i)  
      {  
      cout<<endl;  
      s2=s2+bYear[i];  
      }  
  s2=s2+day;}  
if(year%400!=0&&year%4!=0)  
{  
     for(i=0;i<=(month-1);++i)  
   {  
   s2+=sYear[i];  
   }  
   s2=s2+day;  
}  
s=s1+s2;  
fact=s%7;  
switch(fact)  
{  
   case 0:  
      cout<<"today is sunday";  
      break;  
   case 1:  
      cout<<"today is monday";  
      break;  
   case 2:  
      cout<<"today is tuseday";  
      break;  
   case 3:  
      cout<<"today is wednesday";  
      break;  
   case 4:  
      cout<<"today is thurday";  
      break;  
   case 5:  
      cout<<"today is friday";  
      break;  
   case 6:  
      cout<<"today is saturday";  
      break;  
   default:cout<<"this program has bugs.";  
}  
return 0;  
}         
2009-10-17 15:27
w273732573
Rank: 1
等 级:新手上路
帖 子:6
专家分:5
注 册:2008-5-28
收藏
得分:0 
你利用的是哪个公式呀?
你那边润年,非润年的好像很有问题。。。非常有问题
2009-10-17 19:27
w273732573
Rank: 1
等 级:新手上路
帖 子:6
专家分:5
注 册:2008-5-28
收藏
得分:0 
#include<iostream>  
using std::cin;  
using std::cout;  
using std::endl;  
int main()  
{  
int bYear[]={31,29,31,30,31,30,31,31,30,31,30,31};  
int sYear[]={31,28,31,30,31,30,31,31,30,31,30,31};  
cout<<"enter year,month,day:"<<endl;  
int year,month,day;  
int s1,s2=0,s,fact;  
int i;  
cin>>year>>month>>day;  
s1=(year-1)+(year-1)/4-((year-1)/100)+((year-1)/400);  //这年份的公式写错。。
 
if(year%400==0||(year%100!=0&&year%4==0))  //润年的判断出错。。
{
   for(i=0;i<(month-1);++i)  //月份不要等号, 等号下去就相当于多加了一月的天数。。。还有i最好也初始化下。。。
      {  
      cout<<endl;  
      s2=s2+bYear[i];  
      }  
   s2=s2+day;  
}
else
{  
     for(i=0;i<(month-1);++i)  
   {  
   s2+=sYear[i];  
   }  
   s2=s2+day;  
}  
s=s1+s2;  
fact=s%7;  
switch(fact)  
{  
   case 0:  
      cout<<"today is sunday";  
      break;  
   case 1:  
      cout<<"today is monday";  
      break;  
   case 2:  
      cout<<"today is tuseday";  
      break;  
   case 3:  
      cout<<"today is wednesday";  
      break;  
   case 4:  
      cout<<"today is thursday";  
      break;  
   case 5:  
      cout<<"today is friday";  
      break;  
   case 6:  
      cout<<"today is saturday";  
      break;  
   default:cout<<"this program has bugs.";  
}  
cout<<endl;
return 0;  
}         
2009-10-17 19:49
loookc
Rank: 2
等 级:论坛游民
帖 子:24
专家分:11
注 册:2009-10-15
收藏
得分:0 
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int bYear[]={31,29,31,30,31,30,31,31,30,31,30,31};
int sYear[]={31,28,31,30,31,30,31,31,30,31,30,31};
cout<<"enter year,month,day:"<<endl;
int year,month,day;
int s1=0,s2=0,s=0,fact=0;
int i;
cin>>year>>month>>day;
s1=(year-1)+((year-1)/4)-((year-1)/100)+((year-1)/400);
     if((year%4==0)&&(year%100!=0)||(year%400==0))
     {
         for(i=0;i<=(month-2);++i)
         {
         s2=s2+bYear[i];
         }   
     }
     else
     {  
         for(i=0;i<=(month-2);++i)
         {
         s2=s2+sYear[i];
         }
     }
      
s2=s2+day;
s=s1+s2;
fact=s%7;
switch(fact)
{
   case 0:
      cout<<"this day is sunday\n";
      break;
   case 1:
      cout<<"this day is monday\n";
      break;
   case 2:
      cout<<"this day is tuseday\n";
      break;
   case 3:
      cout<<"this day is wednesday\n";
      break;
   case 4:
      cout<<"this day is thurday\n";
      break;
   case 5:
      cout<<"this day is friday\n";
      break;
   case 6:
      cout<<"this day is saturday\n";
      break;
   default:cout<<"this program has bugs.";
}
return 0;
}      
大家再看看
2009-10-17 22:21
快速回复:求助!这是怎么回事?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032706 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved