C++ 高手看看

来源:百度知道 编辑:UC知道 时间:2024/06/21 07:38:58
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class calendar
{
public:
calendar(string& special)
{
specialtime=special;
}
void calculate();

private:
calendar(){}
int months[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
int firstday; //指定的每年的第一天的星期
int monthday;
string specialtime;
void printmonth(int day,int stop);
};
void calendar::calculate()
{
int year,years;
int i,j,p=0,now;
int times=0;//每月第一天的星期
year=atoi(specialtime.substr(0,3).c_str());
years=year-1;
firstday=(years*365+year/4)%7;
if(year%4==0 && year%100 || year%400==0)
{
for(i=0;i<12;i++)
{
if(i!=0)
{
for(j=0;j<=i;j++)
times+=months[1][j];
monthday=(times%7+firstday)%7;
now=mon

类里面不能初始化变量,类只是对象的一种抽象,所以只能在构造函数中初始化变量
可以把你的months[2][12]定义在类外,如果是不变常量,建议定义为
using namespace std;
//.......
const int months[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};// 定义在这里
//......
class calender{//...
类中数组的初始化问题,这里有个小案例,可以作为参考:http://zhidao.baidu.com/question/38263251.html

//.....
now=months[1][p++];
printmonth(firsthday,now); //firsthday-->firstday

貌似 private 里不能赋值

做日历吗 ???看了 帮不上忙额