永发信息网

跪求一个300多行的C++程序 ?

答案:2  悬赏:20  手机版
解决时间 2021-04-21 03:11
  • 提问者网友:喧嚣尘世
  • 2021-04-20 18:10
只要是能够运行的 程序就好 300多行就行 额 我能给的最高分可以都给你
最佳答案
  • 五星知识达人网友:零点过十分
  • 2021-04-20 19:48

#include <iostream>
using namespace std;
class Date
{
private:
int year;
int month;
int day;
friend void inputDate(Date& d);
friend int goday(Date& d);
friend void frontDate(Date& d);
friend void backDate(Date& d);
friend int distanceDate(Date& d1,Date& d2);
public:
Date(int y=0,int m=0,int d=0):year(y),month(m),day(d)
{}
void print()
{
cout<<year<<"\t"<<month<<"\t"<<day<<"\t";
}


};
void inputDate(Date& d)
{
cout<<"Please input year:"<<"\t";
cin>>d.year;
cout<<"Please input month:"<<"\t";
cin>>d.month;
cout<<"Please input day:"<<"\t";
cin>>d.day;
}
int goday(Date& d)
{
if((d.year%4==0&&d.year%100!=0)||d.year%400==0)
{

if(d.month<=2)
return (d.month-1)*31+d.day;
else if(d.month>2&&d.month<=7)
{
if(d.month%2==1)
return 60+d.day+61*(d.month-3)/2;
else if(d.month%2==0)
return 60+d.day+61*(d.month-4)/2+31;
}
else if(d.month>7)
{
if(d.month%2==0)
return 213+d.day+61*(d.month-8)/2;
else if(d.month%2==1)
return 213+d.day+61*(d.month-9)/2+31;
}


}
else
{
if(d.month<=2)
return (d.month-1)*31+d.day;
else if(d.month>2&&d.month<=7)
{
if(d.month%2==1)
return 59+d.day+61*(d.month-3)/2;
else if(d.month%2==0)
return 59+d.day+61*(d.month-4)/2+31;
}
else if(d.month>7)
{
if(d.month%2==0)
return 212+d.day+61*(d.month-8)/2;
else if(d.month%2==1)
return 212+d.day+61*(d.month-9)/2+31;
}


}
}
void frontDate(Date& d)
{
if(d.month==1&&d.day==1)
{
--d.year;
d.month=12;
d.day=31;
}
else if(d.month==2&&d.day==1)
{
d.month=1;
d.day=31;
}
else if(d.month==3&&d.day==1)
{
if(d.year%4==0&&d.year%100!=0||d.year%400==0)
{
d.month=2;
d.day=29;
}
else
{
d.month=2;
d.day=28;
}


}
else if(d.month>3&&d.month<=7&&d.day==1)
{
if(d.month%2==0)
{
--d.month;
d.day=31;
}
else
{
--d.month;
d.day=30;
}
}
else if(d.month==8&&d.day==1)
{
--d.month;
d.day=31;
}


else if(d.month>8&&d.day==1)
{
if(d.month%2==0)
{
--d.month;
d.day=30;
}
else
{
--d.month;
d.day=31;
}
}
else if(d.day!=1)
{
--d.day;
}

cout<<d.year<<"\t"<<d.month<<"\t"<<d.day<<"\t";
}
void backDate(Date& d)
{
if(d.month==12)
{
if(d.day==31)
{
++d.year;
d.month=1;
d.day=1;
}
else
{
++d.day;
}
}
else if(d.month==1)
{
if(d.day==31)
{
++d.year;
d.month=2;
d.day=1;
}
else
++d.day;
}


else if(d.month=2)
{
if(d.year%4==0&&d.year%100!=0||d.year%400==0)
{
if(d.day==29)
{
d.day=1;
d.month=3;
}
else
{
++d.day;
}
}
else
{
if(d.day==28)
{
d.day=1;
d.month=3;
}
else
{
--d.day;
}
}
}
else if(d.month!=2&&d.month!=12)
{
if(d.month<=7)
{
if(d.month%2==1)
{
if(d.day==31)
{
d.day=1;
++d.month;
}
else
{
++d.day;
}
}
else
{
if(d.day==30)
{
d.day=1;
++d.month;
}
else
{
++d.day;
}
}
}
else if(d.month>7)
{
if(d.month%2==0)
{
if(d.day==31)
{
d.day=1;
++d.month;
}
else
{
++d.day;
}
}
else
{
if(d.day==30)
{
d.day=1;
++d.month;
}
else
{
++d.day;
}
}
}
}
cout<<d.year<<"\t"<<d.month<<"\t"<<d.day<<"\t";
}
int distanceDate(Date& d1,Date& d2)
{
int m,n;
m=goday(d1);
n=goday(d2);
if((d2.year-d1.year)>=0)
{

if((d2.year-d1.year)%4==0)
return 1461*(d2.year-d1.year)/4+n-m;
if((d2.year-d1.year)%4==1)
{
if(d1.year%4==0&&d1.year%100!=0||d1.year%400==0)
return 1461*(d2.year-d1.year-1)/4+n-m+366;
else
return 1461*(d2.year-d1.year-1)/4+n-m+365;
}
if((d2.year-d1.year)%4==2)
{
if((d1.year%4==0&&d1.year%100!=0||d1.year%400==0)||((d1.year+1)%4==0&&(d1.year+1)%100!=0||(d1.year+1)%400==0))
return 1461*(d2.year-d1.year-2)/4+n-m+365+366;
else
return 1461*(d2.year-d1.year-2)/4+n-m+365+365;
}
if((d2.year-d1.year)%4==3)
{
if((d1.year%4==0&&d1.year%100!=0||d1.year%400==0)||((d1.year+1)%4==0&&(d1.year+1)%100!=0||(d1.year+1)%400==0)||((d1.year+2)%4==0&&(d1.year+2)%100!=0||(d1.year+2)%400==0))


return 1461*(d2.year-d1.year-3)/4+n-m+366+365+365;
else
return 1461*(d2.year-d1.year-3)/4+n-m+365+365+365;
}
}
else
{
if((d1.year-d2.year)%4==0)
return 1461*(d1.year-d2.year)/4+m-n;
if((d1.year-d2.year)%4==1)
{
if(d2.year%4==0&&d2.year%100!=0||d2.year%400==0)
return 1461*(d1.year-d2.year-1)/4+m-n+366;
else
return 1461*(d1.year-d2.year-1)/4+m-n+365;
}
if((d1.year-d2.year)%4==2)
{
if((d2.year%4==0&&d2.year%100!=0||d2.year%400==0)||((d2.year+1)%4==0&&(d2.year+1)%100!=0||(d2.year+1)%400==0))
return 1461*(d1.year-d2.year-2)/4+m-n+365+366;
else
return 1461*(d1.year-d2.year-2)/4+m-n+365+365;
}
if((d1.year-d2.year)%4==3)
{
if((d2.year%4==0&&d2.year%100!=0||d2.year%400==0)||((d2.year+1)%4==0&&(d2.year+1)%100!=0||(d2.year+1)%400==0)||((d2.year+2)%4==0&&(d2.year+2)%100!=0||(d2.year+2)%400==0))


return 1461*(d1.year-d2.year-3)/4+m-n+366+365+365;
else
return 1461*(d1.year-d2.year-3)/4+m-n+365+365+365;
}
}


}
int main()
{
Date d1,d2,d;
int m;
inputDate(d1);
inputDate(d2);
d1.print();
d2.print();
m=distanceDate(d1,d2);
cout<<"d2 distance d1 is:"<<"\t";
cout<<m<<endl;


cout<<"distance from the year's first day is:"<<"\t";
cout<<goday(d1)<<endl;
cout<<"The front of date d1 is:"<<"\t";
frontDate(d1);
cout<<"The date d1 is:"<<"\t";
backDate(d1);
cout<<"The back of date d1 is:"<<"\t";
backDate(d1);

}



全部回答
  • 1楼网友:笑迎怀羞
  • 2021-04-20 19:58

#ifndef CLASS_CLASS #define CLASS_CLASS

#include "Student.h"

class Class { private: Student s[50]; string speciality; int student_num; public: Class(char *s="", int n=0);

void Input(); void Print() const; double AverageOfSubject(int idx); void Sort(); };

bool cmpByAverage( Student& a, Student& b);

#endif // CLASS_CLASS

#ifndef GENDER_ENUM #define GENDER_ENUM

enum Gender {female, male};

void printGender(Gender g); void inputGender(Gender& g);

#endif // GENDER_ENUM

#ifndef STUDENT_CLASS #define STUDENT_CLASS

#include "Gender.h"

#include <iostream> #include <string> #include <algorithm> using namespace std;

#define Num 3

class Student { friend class Class; private: unsigned int sid; string name; Gender gender; double score[Num + 1]; public: Student() {} Student(unsigned int id, char* sn, Gender g, double s[]);

void Input(); void Print() const; double Average(); };

#endif // STUDENT_CLASS

#include "Class.h"

Class::Class(char *s, int n) :speciality(s), student_num(n) {}

void Class::Input() { cout << "Please enter speciality:\t"; getline(cin, speciality); cout << "Please enter student number:\t"; cin >> student_num; for (int i=0; i < student_num; ++i) s[i].Input(); }

void Class::Print() const { cout << speciality << "\t" << student_num << endl; for (int i=0; i < student_num; ++i) { cout << "No." << i + 1 << "\t"; s[i].Print(); } }

double Class::AverageOfSubject(int idx) { double sum = 0; for (int i = 0; i < student_num; ++i) sum += s[i].score[idx]; return sum /= student_num; }

void Class::Sort() { sort(s, s + student_num, cmpByAverage); }

bool cmpByAverage( Student& a, Student& b) { return a.Average() > b.Average(); } #include <iostream> using namespace std;

#include "Gender.h"

void printGender(Gender g) { if (g == female) cout << "female"; else if (g == male) cout << "male"; }

void inputGender(Gender& g) { char ch; cout << "Please enter gender:\t"; cin >> ch; if (ch == 'f') g = female; else if (ch == 'm') g = male; }

#include "Student.h" #include "Class.h"

void main() { Class c;//("computer science", 30); c.Input(); c.Print(); c.Sort(); } #include "Student.h"

#define Num 3

Student::Student(unsigned int id, char* sn, Gender g, double s[]):sid(id),name(sn), gender(g) { for (int i = 0; i < Num; ++i) score[i] = s[i]; }

void Student::Input() { cout << "Please enter sid:\t"; cin >> sid; cin.get(); cout << "Please enter name:\t"; getline(cin, name); cin.get(); inputGender(gender); cout << "Please enter 3 scores:\t"; for (int i = 0; i < Num; ++i) cin >> score[i]; }

void Student::Print() const { cout << sid << "\t" << name << "\t";// << gender; printGender(gender); for (int i = 0; i < Num; ++i) cout << "\t" << score[i] << "\t"; cout << endl; }

double Student::Average() { score[Num] = 0; for (int i = 0; i < Num; ++i) score[Num] += score[i]; score[Num] /= Num; return score[Num]; }

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯