64,646
社区成员
1.设计一个Date类,包括:
三个私有数据: year, month, day 均为int类型
两个构造函数:
(1) Date(int y=1900, int m=1, int d=1);
(2) Date(const string& s);
接受 格式为“yyyy/mm/dd”或“yyyy-mm-dd”的字符串
若干公有成员函数:
(1)void set( int y, int m, int d ); 设置日期
(2)bool isValid(); 判断年月日的值是否构成一个有效日期
(3)bool isLeapYear(); 判断是否闰年
(4)int toInt( ) ; 把日期转换成一个整数(YYYYMMDD),比如20210328
(5)Date lastDay( ); 返回当前对象的前一天的日期
(6)Date nextDay( ); 返回当前对象的后一天的日期
(7)void print(); 打印日期,格式参考:“yyyy/mm/dd”或“yyyy-mm-dd”
(8)int getYear(); 返回当前对象的year值
(9)int getMonth(); 返回当前对象的month值
(10)int getDay(); 返回当前对象的day值
(11)void setYear(int y); 把y的值赋值给当前对象的year
(12)void setMonth( int m); 把m的值赋值给当前对象的month
(13)void setDay( int d); 把d的值赋值给当前对象的day
(14)int compare(const Date &d); 判断当前对象和d的大小关系
返回值==0,表示两个日期相等;返回值>0,表示当前对象的日期更大(更新);返回值<0,表示d的日期更大(更新)。
同时,要求编写主函数,在主函数中,定义Date类的对象,并自行设计合理流程调用构造函数及成员函数进行充分测试(每个成员函数都测试到)。
想知道(14)怎么写,最后的主函数怎么写啊
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
bool calc(double &result, int a, int b, char type);
int main()
{
double r;
int a, b;
char c;
cout << "输入操作数和运算符" << endl;
cin >> a >> b >> c;
//_getch();
cout << "您的计算答案:";
cin >> r;
if (calc(r, a, b, c))
cout << "回答正确!\n";
else
cout << "回答错误!\n";
system("pause");
return 0;
}
bool calc(double &result, int a, int b, char type)
{
double ans = 0.0;
switch (type)
{
case '+':
ans = a + b;
break;
case '-':
ans = a - b;
break;
case '*':
ans = a * b;
break;
case '/':
if (b == 0)
{
cout << "Error!\n";
break;
}
else
ans = a / b;
break;
}
if (ans == result)
return true;
return false;
}
[/quote]
你好啊,我这个是要输入两个数和运算符然后计算 ,不是让自己计算然后它判断对错[/quote]
改一下calc函数不就好了吗[/quote]
主要是不会
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
bool calc(double &result, int a, int b, char type);
int main()
{
double r;
int a, b;
char c;
cout << "输入操作数和运算符" << endl;
cin >> a >> b >> c;
//_getch();
cout << "您的计算答案:";
cin >> r;
if (calc(r, a, b, c))
cout << "回答正确!\n";
else
cout << "回答错误!\n";
system("pause");
return 0;
}
bool calc(double &result, int a, int b, char type)
{
double ans = 0.0;
switch (type)
{
case '+':
ans = a + b;
break;
case '-':
ans = a - b;
break;
case '*':
ans = a * b;
break;
case '/':
if (b == 0)
{
cout << "Error!\n";
break;
}
else
ans = a / b;
break;
}
if (ans == result)
return true;
return false;
}
[/quote]
你好啊,我这个是要输入两个数和运算符然后计算 ,不是让自己计算然后它判断对错[/quote]
改一下calc函数不就好了吗
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
bool calc(double &result, int a, int b, char type);
int main()
{
double r;
int a, b;
char c;
cout << "输入操作数和运算符" << endl;
cin >> a >> b >> c;
//_getch();
cout << "您的计算答案:";
cin >> r;
if (calc(r, a, b, c))
cout << "回答正确!\n";
else
cout << "回答错误!\n";
system("pause");
return 0;
}
bool calc(double &result, int a, int b, char type)
{
double ans = 0.0;
switch (type)
{
case '+':
ans = a + b;
break;
case '-':
ans = a - b;
break;
case '*':
ans = a * b;
break;
case '/':
if (b == 0)
{
cout << "Error!\n";
break;
}
else
ans = a / b;
break;
}
if (ans == result)
return true;
return false;
}
[/quote]
你好啊,我这个是要输入两个数和运算符然后计算 ,不是让自己计算然后它判断对错
//head
#define DATE_H
#include <iostream>
#include <iomanip>
#include <string>
class Date
{
private:
int year;
int month;
int day;
int isLeapYear(int _year)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return 1;
else
return 0;
}
public:
Date() : year(1990), month(1), day(1) { std::cout << "Constructor run\n"; }
Date(int _y, int _m, int _d) : year(_y), month(_m), day(_d) { std::cout << "Constructor run\n"; }
Date(const Date &d) : year(d.year), month(d.month), day(d.day) { std::cout << "CopyConstructor run\n"; }
Date& operator=(const Date&);
~Date() { std::cout << "Destructor run\n"; }
void setYear(int y) { year = y; }
int getYear() { return year; }
void setMonth(int m) { month = m; }
int getMonth() { return month; }
void setDay(int d) { day = d; }
int getDay() { return day; }
int nyear(int _y);
int nmonth(int _y, int _m);
int getDays(int y, int m);
int weekDay();
void printMonthCalendar();
void chineseFormat();
void amerciaFormat();
bool ifMonthOverload(int y, int m, int d);
bool ifDayOverload(int y, int m, int d);
void tomorrow();
void yesterday();
};
Date& Date::operator=(const Date &rdate)
{
year = rdate.year;
month = rdate.month;
day = rdate.day;
return *this;
}
inline int Date::nyear(int _y)
{
if (isLeapYear(_y))
return 366;
else
return 365;
}
inline int Date::nmonth(int _y, int _m)
{
if (_m == 1 || _m == 8 || _m == 5 || _m == 7 || _m == 8 || _m == 10 || _m == 12)
return 31;
else if (nyear(_y) == 366 && _m == 2)
return 29;
else if (nyear(_y) == 365 && _m == 2)
return 28;
else
return 30;
}
inline int Date::getDays(int y, int m)
{
int sum = 0;
for (int i = 1990; i < year; i++)
sum += nyear(i);
if (m > 1)
for (int i = 1; i < m; i++)
sum += nmonth(y, i);
return sum;
}
inline void Date::printMonthCalendar()
{
int sum = getDays(year, month);
int front_space = (sum + 1) % 7;
int days = nmonth(year, month);
std::cout << "SunMonTueWedThuFriSat" << std::endl;
for (int i = 0; i < front_space; i++)
std::cout << std::setw(3) << " ";
for (int i = 1, j = front_space + 1; i <= days; i++, j++)
{
std::cout << std::setw(3) << i;
if (j % 7 == 0)
std::cout << std::endl;
}
std::cout << std::endl;
}
inline void Date::chineseFormat()
{
std::cout << year << "年" << month << "月" << day << "日" << std::endl;
}
inline void Date::amerciaFormat()
{
std::string mon[12] = {
"Jan", "Feb", "Mar",
"Api", "May", "Jun",
"Jul", "Aug", "Sep",
"Oct", "Nov", "Dec" };
std::cout << mon[month - 1] << " " << day << ", " << year << std::endl;
}
bool Date::ifMonthOverload(int y, int m, int d)
{
if (m < 1 || m > 12)
return true;
return false;
}
bool Date::ifDayOverload(int y, int m, int d)
{
int curDay = nmonth(y, m);
if (d < 1 || d > curDay)
return true;
return false;
}
inline void Date::tomorrow()
{
day += 1;
if (ifDayOverload(year, month, day))
{
day = 1;
month += 1;
if (ifMonthOverload(year, month, day))
month = 1;
}
}
inline void Date::yesterday()
{
day -= 1;
if (ifDayOverload(year, month, day))
{
month -= 1;
if (ifMonthOverload(year, month, day))
month = 12;
day = nmonth(year, month);
}
}
int Date::weekDay()
{
int y = year, m = month, d = day;
if (m == 1 || m == 2)
{
m += 12;
y--;
}
int curWeek = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
return curWeek;
}
//main
#include "Date.h"
#include <cstdlib>
int main()
{
int year, month, day;
Date d1;
Date d2(d1);
std::cin >> year >> month >> day;
d1.setYear(year);
d1.setMonth(month);
d1.setDay(day);
d1.yesterday();
d1.chineseFormat();
std::cin >> year >> month >> day;
d2.setYear(year);
d2.setMonth(month);
d2.setDay(day);
d2.tomorrow();
d2.amerciaFormat();
d2.printMonthCalendar();
system("pause");
return 0;
}
#include "Chapter6.h"
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std;
bool cmp(Point a, Point b) { return a.disToO() < b.disToO(); }
int main()
{
int n;
cin >> n;
vector<Point> vec;
for (int i = 0; i < n; i++)
{
int x, y;
cin >> x >> y;
Point p(x, y);
vec.push_back(p);
}
sort(vec.begin(), vec.end(), cmp);
for (int i = 0; i < n; i++)
cout << "(" << vec[i].x() << ", " << vec[i].y() << ")" << endl;
for (int i = 0; i < n; i++)
{
vec[i].moveto(vec[i].x() + 1, vec[i].y() + 1);
cout << "(" << vec[i].x() << ", " << vec[i].y() << ")" << endl;
}
system("pause");
return 0;
}
//head
#define POINT_H
#include <cmath>
class Point
{
private:
double _x;
double _y;
public:
Point(double x0 = 0.0, double y0 = 0.0) : _x(x0), _y(y0) {}
void moveto(double x0, double y0);
double x() { return _x; }
double y() { return _y; }
double disToO() { return sqrt(_x * _x + _y * _y); }
};
inline void Point::moveto(double x0, double y0)
{
_x = x0;
_y = y0;
}
//main
#include "Chapter6.h"
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std;
bool cmp(Point a, Point b) { return a.disToO < b.disToO; }
int main()
{
int n;
cin >> n;
vector<Point> vec;
for (int i = 0; i < n; i++)
{
int x, y;
cin >> x, y;
Point p(x, y);
vec.push_back(p);
}
sort(vec.begin(), vec.end(), cmp);
for (int i = 0; i < n; i++)
cout << "(" << vec[i].x << ", " << vec[i].y << ")" << endl;
for (int i = 0; i < n; i++)
{
vec[i].moveto(vec[i].x + 1, vec[i].y + 1);
cout << "(" << vec[i].x << ", " << vec[i].y << ")" << endl;
}
system("pause");
return 0;
}
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
bool calc(double &result, int a, int b, char type);
int main()
{
double r;
int a, b;
char c;
cout << "输入操作数和运算符" << endl;
cin >> a >> b >> c;
//_getch();
cout << "您的计算答案:";
cin >> r;
if (calc(r, a, b, c))
cout << "回答正确!\n";
else
cout << "回答错误!\n";
system("pause");
return 0;
}
bool calc(double &result, int a, int b, char type)
{
double ans = 0.0;
switch (type)
{
case '+':
ans = a + b;
break;
case '-':
ans = a - b;
break;
case '*':
ans = a * b;
break;
case '/':
if (b == 0)
{
cout << "Error!\n";
break;
}
else
ans = a / b;
break;
}
if (ans == result)
return true;
return false;
}
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int accelerate(int n, int i);
int myExp(int e);
int main()
{
int n, m;
cin >> n >> m;
cout << "小球一共经过:" << accelerate(n, m - 1) << "米\n";
cout << "然后会弹起:" << n / myExp(m) << "米\n";
system("pause");
return 0;
}
int myExp(int e)
{
int ans = 1;
while (e--)
ans *= 2;
return ans;
}
int accelerate(int n, int i)
{
if (i == 0)
return n;
return n + 2 * n - (n / myExp(i - 1));
}