高手帮忙写个c++程序!!!
要求如下:(a代表犯错次数)
1、假设有家公司,以2010年7月16号开始计,40天为一个周期,如此类推;
2、在一个周期里,员工犯错次数满三次就会有收到一次警告(用表达式说明就是:a<3,不收到警告;6>a>=3,一次警告;9>a>=6,两次警告,如此类推)收到的警告次数将会进行保存记录;
3、在新一个周期里,犯错次数的计算将从头开始(即从a=0开始)(这样的话如果一个员工在每个周期里最多只犯2次,则他就都不会收到警告,因为下一周期开始,就会从头计算)但警告次数会进行累加;
现在要写一个C++程序,程序的输入和输出如下所述:
输入:今天你犯错的次数;
输出:a、离现在这个周期结束还有几天;
b、在这个周期里你已犯了几次错;
c、由7月16号到现在已收到几次警告。
给出可执行代码!!!
答案:#include <time.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
#define db_print printf
bool isLeapYear(unsigned int year)
{
if ((year % 4 == 0 && year %100 != 0) || (year % 400 == 0))
{
db_print("This year is leap year! %d\
", year);
return true;
}
return false;
}
void getCurrentDate(unsigned int &year, unsigned int &month, unsigned int &day)
{
db_print("Enter get current date!\
");
time_t t;
time(&t);
tm * today = localtime(&t);
year = today->tm_year + 1900;
month = today->tm_mon;
day = today->tm_mday;
db_print("Exit, year:%d, month:%d, day:%d\
", year, month + 1, day);
}
unsigned int getDeltaYearsDays(unsigned int startYear,
unsigned int endYear)
{
int sumTotalDays = 0;
for (unsigned int iter = startYear; iter < endYear; iter++)
{
if (isLeapYear(iter))
sumTotalDays += 366;
else
sumTotalDays += 365;
}
return sumTotalDays;
}
unsigned int getDaysFromYearBegins(unsigned int year,
unsigned int month,
unsigned int day)
{
unsigned int sumDays = 0;
unsigned int monthDays[12]
= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year))
monthDays[1] = 29;
else
monthDays[1] = 28;
for (unsigned int i = 0; i <= month; i++)
{
sumDays += monthDays[i];
}
sumDays += day;
return sumDays;
}
unsigned int calcDeltaDays(unsigned int startYear,
unsigned int startMonth,
unsigned int startDay,
unsigned int nowYear,
unsigned int nowMonth,
unsigned int nowDay)
{
return getDeltaYearsDays(startYear, nowYear) +
getDaysFromYearBegins(nowYear, nowMonth, nowDay) -
getDaysFromYearBegins(startYear, startMonth, startDay);
}
int main(int argc, char * argv[])
{
unsigned int startYear = 2010;
unsigned int startMonth = 7 - 1; //从0开始。
unsigned int startDay = 10;
unsigned int nowYear, nowMonth, nowDay;
getCurrentDate(nowYear, nowMonth, nowDay);
unsigned int howManyDays = calcDeltaDays(startYear, startMonth, startDay,
nowYear, nowMonth, nowDay);
ifstream is;
is.open("rec.dat");
unsigned int historyAlarmCount = 0;
unsigned int lastSaveCycleCount = 0;
unsigned int lastSaveFaultCount = 0;
unsigned int thisCycleAlarmCount = 0;
unsigned int thisCycleFaultCount = 0;
if (is.is_open())
{
is >> lastSaveCycleCount >> lastSaveFaultCount >> historyAlarmCount;
}
is.close();
cout << "This time your fault times:";
cin >> thisCycleFaultCount;
if (howManyDays / 40 == lastSaveCycleCount)
{
thisCycleFaultCount = lastSaveFaultCount + thisCycleFaultCount;
}
thisCycleAlarmCount += thisCycleFaultCount / 3;
historyAlarmCount += thisCycleAlarmCount - (lastSaveFaultCount / 3);
cout << "离这个周期结束还有几天:" << (40 - howManyDays % 40) << endl;
cout << "在这个周期里总共犯的错误为:" << thisCycleFaultCount << endl;
cout << "总共收到的警告次数为:" << historyAlarmCount << endl;
system("pause");
ofstream os;
os.open("rec.dat");
if (os.is_open())
{
os << (unsigned int)(howManyDays / 40) << thisCycleFaultCount << historyAlarmCount;
}
os.close();
}ds
上一个:c或c++编写的万年历
下一个:学习编程C++的问题