c++课程设计日历记事本
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                c++课程设计日历记事本
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            /***********************************
****    日歷記事本    ****
************************************/#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <ctime>
#include <cassert>
#include <conio.h>
using namespace std;
class rebook
{
public:rebook();//默認構造函數void new_book();//新建備忘錄void alter_book();//修改備忘錄void delet_book();//刪除備忘錄void scan_book();//瀏覽記事本
private:string signs;//標題string data;//日期string where;//地點string things;//事件string what;//事件描述
};//日期類
class whatdata
{
public:whatdata();//默認構造函數int IsLeapYear(int Year); long YearDays(int Year);  long TotalDays(int Year, int Month, int Day);int GetLunar(int Y1, int M1, int D1, int &Y2, int &M2, int &D2); char inter_face();//首頁void search_data();//日歷查詢void book_manage();//備忘錄管理void select_day();//萬年歷之日歷查詢void select_month();//萬年歷之月歷查詢
private:rebook abook;string week;string month;string day;string year;
};
#define   BEGINYEAR   1900   
#define   YEARCOUNT   150   struct   LUNARINDEX   //農歷大小月數據索引結構,數據時間自1900年至2050年   
{  long     Days;       //   天數,表示該農歷年1月1日距公歷元年元月1日的天數   
short   Mon13;     //   該數據的0至12位分別標明農歷1至13月的大小,含閏月   
short   Leap;       //   該年的農歷閏月值,15表示無閏月。   
}LunarIndex[]={   {693626,   0X16D2,     8},   {694010,   0X0752,   15},   {694364,   0X0EA5,   15}, //1900{694719,   0X164A,     5},   {695102,   0X064B,   15},   {695456,   0X0A9B,   15},   {695811,   0X1556,     4},   {696195,   0X056A,   15},   {696549,   0X0B59,   15},   {696904,   0X1752,     2},   {697288,   0X0752,   15},   {697642,   0X1B25,    6},   {698026,   0X0B25,   15},   {698380,   0X0A4B,   15},   {698734,   0X14AB,     5}, //  1914{699118,   0X02AD,   15},   {699472,   0X056B,   15},   {699827,   0X0B69,     2},   {700211,   0X0DA9,   15},   {700566,   0X1D92,     7},   {700950,   0X0E92,   15},   {701304,   0X0D25,   15},   {701658,   0X1A4D,     5},   {702042,   0X0A56,   15},   {702396,   0X02B6,   15},   {702750,   0X15B5,     4},   {703135,   0X06D4,   15},   {703489,   0X0EA9,   15},   {703844,   0X1E92,     2},   {704228,   0X0E92,   15},   //1929 {704582,   0X0D26,     6},   {704965,   0X052B,   15},   {705319,   0X0A57,   15},  {705674,   0X12B6,     5},   {706058,   0X0B5A,   15},   {706413,   0X06D4,   15},   {706767,   0X0EC9,     3},   {707151,   0X0749,   15},   {707505,   0X1693,    7},   {707889,   0X0A93,   15},   {708243,   0X052B,   15},   {708597,   0X0A5B,     6},   {708981,   0X0AAD,   15},   {709336,   0X056A,   15},   {709690,   0X1B55,     4},  //1944 {710075,   0X0BA4,   15},   {710429,   0X0B49,   15},   {710783,   0X1A93,     2},   {711167,   0X0A95,   15},   {711521,   0X152D,     7},   {711905,   0X0536,   15},   {712259,   0X0AAD,   15},   {712614,   0X15AA,     5},   {712998,   0X05B2,   15},   {713352,   0X0DA5,   15},   {713707,   0X1D4A,     3},   {714091,   0X0D4A,   15},   {714445,   0X0A95,     8},   {714828,   0X0A97,   15},   {715183,   0X0556,   15},  //1959 {715537,   0X0AB5,     6},   {715921,   0X0AD5,   15},   {716276,   0X06D2,   15},   {716630,   0X0EA5,     4},   {717014,   0X0EA5,   15},   {717369,   0X064A,   15},   {717722,   0X0C97,     3},   {718106,   0X0A9B,   15},   {718461,   0X155A,     7},   {718845,   0X056A,   15},   {719199,   0X0B69,   15},   {719554,   0X1752,     5},   {719938,   0X0B52,   15},   {720292,   0X0B25,   15},   {720646,   0X164B,     4},  //1974 {721030,   0X0A4B,   15},   {721384,   0X14AB,     8},   {721768,   0X02AD,   15},   {722122,   0X056D,   15},   {722477,   0X0B69,     6},   {722861,   0X0DA9,   15},   {723216,   0X0D92,   15},   {723570,   0X1D25,     4},   {723954,   0X0D25,   15},   {724308,   0X1A4D,   10},   {724692,   0X0A56,   15},   {725046,   0X02B6,   15},   {725400,   0X05B5,     6},   {725784,   0X06D5,   15},   {726139,   0X0EA9,   15},  //1989 {726494,   0X1E92,     5},   {726878,   0X0E92,   15},   {727232,   0X0D26,   15},   {727586,   0X0A56,     3},   {727969,   0X0A57,   15},   {728324,   0X14D6,     8},   {728708,   0X035A,   15},   {729062,   0X06D5,   15},   {729417,   0X16C9,     5},   {729801,   0X0749,   15},   {730155,   0X0693,   15},   {730509,   0X152B,     4},   {730893,   0X052B,   15},   {731247,   0X0A5B,   15},   {731602,   0X155A,     2},  //2004{731986,   0X056A,   15},   {732340,   0X1B55,     7},   {732725,   0X0BA4,   15},   //2007{733079,   0X0B49,   15},   {733433,   0X1A93,     5},   {733817,   0X0A95,   15},   {734171,   0X052D,   15},   {734525,   0X0AAD,     4},   {734909,   0X0AB5,   15},   {735264,   0X15AA,     9},   {735648,   0X05D2,   15},   {736002,   0X0DA5,   15},   {736357,   0X1D4A,     6},   {736741,   0X0D4A,   15},   {737095,   0X0C95,   15}, //2019  {737449,   0X152E,     4},   {737833,   0X0556,   15},   {738187,   0X0AB5,   15},   {738542,   0X15B2,     2},   {738926,   0X06D2,   15},   {739280,   0X0EA5,     6},   {739664,   0X0725,   15},   {740018,   0X064B,   15},   {740372,   0X0C97,     5},   {740756,   0X0CAB,   15},   {741111,   0X055A,   15},   {741465,   0X0AD6,     3},   {741849,   0X0B69,   15},   {742204,   0X1752,   11},   {742588,   0X0B52,   15},   //2034{742942,   0X0B25,   15},   {743296,   0X1A4B,     6},   {743680,   0X0A4B,   15},   {744034,   0X04AB,   15},   {744388,   0X055B,     5},   {744772,   0X05AD,   15},   {745127,   0X0B6A,   15},   {745482,   0X1B52,     2},   {745866,   0X0D92,   15},   {746220,   0X1D25,     7},   {746604,   0X0D25,   15},   {746958,   0X0A55,   15},   {747312,   0X14AD,     5},   {747696,   0X04B6,   15},   {748050,   0X05B5,   15},  //2049{748405,   0X1DAA,     3}};   //2050int month1[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string week1[7]={"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
string month2[12]={"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
string weeks[7] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
string months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", 
"Oct", "Nov", "Dec"};//默認構造函數
rebook::rebook()
{where = "xxx";//地點things = "xxx";//事件what = "xxx";//事件描述signs = "xxx";//標題data = "xxx";//日期
}//新建備忘錄
void rebook::new_book()
{system("cls");cout <<endl <<"記事(以'#'表示結束)" <<endl;time_t timer;time(&timer);cout <<endl <<"時間 : "<<asctime(localtime(&timer)) <<endl;cout <<"標題 : ";cin >>signs;ofstream tempFile("record\\temp.txt");time(&timer);tempFile <<"時間 : "<<asctime(localtime(&timer)) <<endl<<"標題 : " <<signs <<endl <<endl<<"內容 : "; cout <<endl <<"內容 : " << endl;tempFile << endl << endl ;string str;while (1){ cin >>str;if (str[str.length()-1] == '#'){str[str.length()-1] = ' ';tempFile <<str <<endl <<endl;break;}tempFile <<str <<endl <<endl;}tempFile.close();ofstream reFile1("record\\title.txt", ios::app);reFile1 <<signs <<endl <<asctime(localtime(&timer)) <<endl;reFile1.close();string fname = "record\\"+signs+".txt";ofstream reFile(fname.c_str()); ifstream tempFile1("record\\temp.txt");assert(tempFile1);reFile <<tempFile1.rdbuf();tempFile1.close();reFile.close();cout <<endl <<endl <<"記錄已經保存!!!" <<endl <<endl;system("pause");   
}//修改備忘錄
void rebook::alter_book()
{while (1){system("cls");cout <<endl <<"請選擇你想修改的標題 : " <<endl <<endl;int i = 1;ifstream reFile("record\\title.txt");if (!reFile){ofstream reFile1("record\\title.txt");reFile1.close();ifstream reFile("record\\title.txt");}while (reFile >>signs){reFile.get();getline(reFile, data);cout <<i <<".\t" <<setiosflags(ios::left) <<setw(30) <<signs <<" "<<data <<endl <<endl;i++;}reFile.close();cout <<endl <<i <<".\t返回" <<endl <<endl;cout <<"選擇 : ";int select;cin >>select;if (select == i)return;ifstream reFile2("record\\title.txt");assert(reFile2);i = 1;while (reFile2 >>signs){reFile2.get();getline(reFile2, data);if (i == select){string fname = "record\\"+signs+".txt";system("cls");ifstream reFile3(fname.c_str());assert(reFile3);cout <<endl <<"你想修改的內容如下 : " <<endl <<endl;cout <<reFile3.rdbuf();reFile3.close();ofstream tempFile("record\\temp.txt");cout <<endl <<endl <<"請在下面輸入新的內容 (以'#'結束): " <<endl <<endl;tempFile <<"標題 : " <<signs <<endl <<endl;tempFile <<"內容 : ";cout <<"內容 : ";string str;while (1){cin >>str;if (str[str.length()-1] == '#'){str[str.length()-1] = ' ';tempFile <<str <<endl;break;}tempFile <<str <<endl;}tempFile.close();ifstream tempFile1("record\\temp.txt");assert(tempFile1);ofstream reFile4(fname.c_str());reFile4 <<tempFile1.rdbuf();tempFile1.close();reFile4.close();cout <<endl <<endl <<"內容更新完成!!!!" <<endl <<endl;system("pause");break;}i++;}reFile2.close();}
}//刪除備忘錄
void rebook::delet_book()
{while (1){system("cls");cout <<endl <<"請選擇一個你想刪除的標題 : " <<endl <<endl;ifstream reFile("record\\title.txt");if (!reFile){ofstream reFile1("record\\title.txt");reFile1.close();ifstream reFile("record\\title.txt");}int i = 1;while (reFile >>signs){reFile.get();getline(reFile, data);cout <<i <<".\t" <<setiosflags(ios::left) <<setw(30) <<signs <<" "<<data <<endl <<endl;i++;}reFile.close();cout <<endl <<i <<".\t返回" <<endl <<endl;int select;cout <<"選擇 : ";cin >>select;if (select == i)return;ifstream reFile2("record\\title.txt");assert(reFile2);ofstream tempFile("record\\temp.txt");i = 1;while (reFile2 >>signs){reFile2.get();getline(reFile2, data);if (i != select)tempFile <<signs <<endl <<data <<endl <<endl;else{system("cls");cout <<endl <<endl <<"你想刪除的備忘錄內容如下 : " <<endl <<endl;string fname = "record\\"+signs+".txt";ifstream reFile3(fname.c_str());assert(reFile3);cout <<reFile3.rdbuf();reFile3.close();}getline(reFile2, data);i++;}reFile2.close();tempFile.close();ifstream tempFile1("record\\temp.txt");assert(tempFile1);ofstream reFile4("record\\title.txt");reFile4 <<tempFile1.rdbuf();tempFile1.close();reFile4.close();cout <<endl <<endl <<"該條備忘錄已經刪除!!!" <<endl <<endl;system("pause");}
}//瀏覽記事本
void rebook::scan_book()
{while (1){system("cls");cout <<endl <<"記事本查詢" <<endl <<endl;ifstream reFile("record\\title.txt");if (!reFile){ofstream reFile1("record\\title.txt");reFile1.close();ifstream reFile("record\\title.txt");}int i = 1;cout <<"序號\t" <<setiosflags(ios::left) <<setw(30) <<"標題 " <<"\t日期" <<endl <<endl;while (reFile >>signs){reFile.get();getline(reFile, data);cout <<i <<".\t" <<setiosflags(ios::left) <<setw(30) <<signs <<" "<<data <<endl <<endl;i++;}reFile.close();cout <<i <<".\t返回";int select;cout <<endl <<endl <<"選擇 : ";cin >>select;if (select == i)return;i = 1;ifstream reFile2("record\\title.txt");assert(reFile2);while (reFile2 >>signs){reFile2.get();getline(reFile2, data);if (i == select){system("cls");string fname = "record\\"+signs+".txt";ifstream reFile3(fname.c_str());assert(reFile3);cout <<reFile3.rdbuf();reFile3.close();cout <<endl <<endl;system("pause");break;}getline(reFile, data);i++;}reFile2.close();}
}//默認構造函數
whatdata::whatdata():abook()
{week = "xxx";month = "xxx";day = "xxx";year = "xxx";
}//首頁
char whatdata::inter_face()
{system("cls");char choose;cout << endl;cout<<"\t  ●              日 歷 記 事 本                          ●" <<endl<<"\t  ==========================================================" <<endl<<"\t  ※                                                      ※" <<endl<<"\t  ※              1.  記事                                ※" <<endl<<"\t  ※                                                      ※" <<endl<<"\t  ※              2.  瀏覽                                ※" <<endl<<"\t  ※                                                      ※" <<endl<<"\t  ※              3.  管理                                ※" <<endl<<"\t  ※                                                      ※" <<endl<<"\t  ※              4.  日歷查詢                            ※" <<endl<<"\t  ※                                                      ※" <<endl<<"\t  ※              5.  關閉記事本                          ※" <<endl<<"\t  ※                                                      ※" <<endl<<"\t  ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※" <<endl <<endl <<"\t\t";choose = getch();return choose;
}//日歷查詢
void whatdata::search_data()
{while (1){system("cls");cout << endl << "\t\t萬年歷查詢" << endl << endl ;cout << "\t●\t1.  日歷查詢 " << endl << endl<< "\t●\t2.  月歷查詢" << endl << endl<< "\t●\t3.  返回" << endl << endl;char select = getch();switch (select){case '1':select_day();//萬年歷之日歷查詢break;case '2':select_month();//萬年歷之月歷查詢break;case '4':return;default:break;}}
}//備忘錄管理
void whatdata::book_manage()
{char choose;while (1){system("cls");cout <<endl <<"\t管理你的備忘錄!!!!!" <<endl <<endl;cout <<endl <<"\t●\t\t 1.  修改備忘錄" <<endl<<endl <<"\t●\t\t 2.  刪除備忘錄" <<endl<<endl <<"\t●\t\t 3.  返回首頁" <<endl <<endl <<"\t\t\t";choose = getch();switch (choose){case '1':abook.alter_book();//修改備忘錄break;case '2':abook.delet_book();//刪除備忘錄break;case '3':return;break;default:break;}}    
}//日歷查詢
void whatdata::select_day()
{system("cls");cout << endl << "\t萬年歷之日歷查詢" << endl << endl;cout << "\t請輸入年月日 (例如 : 2007 8 15) : ";cin >> year >> month >>day;int days = 0;for (int i = 1; i < atoi(year.c_str()); i++){if ((i%4 == 0 && i%100 != 0) || (i%400 == 0))days += 366;elsedays += 365;days = days%7;}if ((i%4 == 0 && i%100 != 0) || (i%400 == 0))month1[1] = 29;for (i = 0; i < atoi(month.c_str()) - 1; i++)days += month1[i];days += atoi(day.c_str());days = days%7;month1[1] = 28;cout <<endl << endl <<"陽歷 : "; cout << year << "  年  " << month << "  月  "<< day << "  日  \t" << week1[days - 1] << endl << endl ;cout << "陰歷 : ";int Y2, M2, D2;GetLunar(atoi(year.c_str()), atoi(month.c_str()), atoi(day.c_str()), Y2, M2, D2);cout << Y2 << "  年  " << M2 << "   月  " << D2 << "  日   "  << endl << endl;system ("pause");
}//月歷查詢
void whatdata::select_month()
{system("cls");cout << endl << "\t萬年歷之月歷查詢" << endl << endl;cout << "\t請輸入某年某月 (例如 : 2007 8) : " ;cin >> year >> month;cout << endl << endl <<year << "  年  " << month << "  月";if ((atoi(year.c_str())%4 == 0 && atoi(year.c_str())%100 != 0) || (atoi(year.c_str())%400 == 0)){month1[1] = 29;cout << "\t\t\t\t\t\t     閏年" << endl ;}else cout << "\t\t\t\t\t\t     平年" << endl ;cout << "=================================================================" <<endl <<endl;cout << "星期日   星期一    星期二    星期三    星期四    星期五    星期六" <<endl <<endl;int days = 0;for (int i = 1; i < atoi(year.c_str()); i++){if ((i%4 == 0 && i%100 != 0) || (i%400 == 0))days += 366;elsedays += 365;days = days%7;}for (i = 0; i < atoi(month.c_str()) - 1; i++)days += month1[i];days = (days + 1)%7;for (int j = 0; j < days; j++)cout << setw(10) << " ";cout << setw(3) << 1;for (j = 2; j <= month1[atoi(month.c_str()) - 1]; j++){days++;if (days%7 == 0)cout << endl << endl << setw(3) << j;elsecout << setw(10) << j ;}cout <<endl << endl <<"===================================================================" <<endl <<endl;month1[1] = 28;fflush(stdin);system("pause");
}//   判斷輸入的公歷年份是否為閏年,是閏年則函數返回1,否則返回0   
int whatdata::IsLeapYear(int Year)   
{ if ((Year%4==0 && Year%100!=0) || Year%400==0)return 1;else return 0;   
}   //   計算輸入公歷年份的1月1日為從公元1年1月1日以來的第幾天,返回天數   
long whatdata::YearDays(int Year)   
{ Year--; return(Year*365+Year/4-Year/100+Year/400+1);
}  //   計算輸入的公歷日期為從公元1年1月1日以來的第幾天,返回總天數   
long whatdata::TotalDays(int Year, int Month, int Day)   
{  const   int   MonthDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};   long   Days=0L; int   i;  if(Month<=0)Month=1;   if(Month>12){   Month--;   Year+=Month/12; Month%=12; Month++; }   for(i=1;i<Month;i++)Days+=MonthDays[i];   if(IsLeapYear(Year)&&Month>2)Days+=1;   Days+=YearDays(Year);return   Days+Day-1;   
}   //---------------------------------------------------------------------------   
//   輸入公歷年月日,返回農歷年月日,函數返回1表示成功,0表示失敗   
//   如果M2返回負值,則說明是農歷的閏月,月份值為其絕對值.
int whatdata::GetLunar(int Y1, int M1,  int D1, int &Y2, int &M2, int &D2)   
{short   MBit[13]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096};   long   TDays;  int   Sum,Lest,i,Offset;  Offset=Y1-BEGINYEAR;if(Offset<0||Offset>YEARCOUNT)return   0;   TDays=TotalDays(Y1,M1,D1);Y2=Y1;   if(LunarIndex[Offset].Days>TDays)   {if(Offset==0)return   0; Offset--;  Y2--;   }   Lest=(int)(TDays-LunarIndex[Offset].Days);   for(Sum=i=0;i<13;i++)   { if((LunarIndex[Offset].Mon13&MBit[i])!=0)   {  Sum+=30;  if(Sum>Lest){Sum-=30; break;}   }   else  { Sum+=29; if(Sum>Lest){  Sum-=29; break;  }  }   }   if(i==13)return   0;i++;   if(i>LunarIndex[Offset].Leap)   {  i--;  if(i==LunarIndex[Offset].Leap)i=-i; }   M2=i;  D2=Lest-Sum+1; return   1;   
}
int main()
{whatdata object;rebook abook;char select;while (select = object.inter_face()){switch (select){case '1':abook.new_book();//新建記事本break;case '2':abook.scan_book();//瀏覽break;case '3':object.book_manage();//備忘錄管理break;case '4':object.search_data();//日歷查詢break;case '5':cout << endl << "\t謝謝使用!!!!" << endl << endl << "\t";exit(0);break;default:break;}}return 0;
} 
 
 
                        
                        
                        
 
 
 
總結
以上是生活随笔為你收集整理的c++课程设计日历记事本的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 1、R语言工具部署
- 下一篇: 程序员常用的三款在线作图工具
