日记用txt文件保存。
示例代码:
// shili2.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h”
#include “console.h”
#pragma comment(lib,“console.lib”)
#include
#include
#include
#include
using namespace std;
int circulation=1;
int interface_num=0;
char aircraft_name[20];
int aircraft_initial_year;
#define WRITE 0
#define LOOK 1
int State=10;
char *data_path=“D:\MyLog”;
void Initial(); // 0
void MainInterface(); // 1
void Answer(); // 2
bool Leap_year(int year);
void Cut_apart(const char *result0,int *year,int *month,int *day);
int Valid(int year,int month,int day);
extern COORD mouse;
int main()
{
Screen screen;
screen.SetTitle(“示例2”);
screen.ShowOrHideCursor(0);
Event event;
event.AddMouseMode();
Initial();
while(circulation)
{
screen.Clear_screen();
switch(interface_num)
{
case 0:
MainInterface();
break;
case 1:
Answer();
break;
}
}
event.RestoreMode();
//screen.Pause();
screen.Close_handle();
return 0;
}
void Initial()
{
Screen screen;
screen.SetSize(80,25);
screen.SetTitle(“日记”);
//生成数据文件
_mkdir(data_path);
File file;
file.HideFileOrFolder(data_path);
_chdir(data_path);
FILE *fp;
char *plane_name="作者姓名.txt";
if(_access(plane_name,0)==-1)
{
screen.MoveCursorTo(12,10);
screen.Write("请输入作者姓名:");
Textbox textbox1=Textbox(27,10,20);
char *name=textbox1.InputString(true);
screen.MoveCursorTo(12,11);
screen.Write("请输入初始年份:");
Textbox textbox2=Textbox(27,11,10);
char *initial_y=textbox2.InputString(true);
fopen_s(&fp,plane_name,"w");
fprintf(fp,"%s %s\n",name,initial_y);
fclose(fp);
}
if(_access("pass.dat",0)==-1)
{
screen.MoveCursorTo(12,13);
screen.Write("请设置密码:");
Textbox textbox1=Textbox(27,13,20);
char *pass=textbox1.InputString(true);
fopen_s(&fp,"pass.dat","w");
fprintf(fp,"%s",pass);
fclose(fp);
screen.Clear_screen();
}
char pass1[20];
fopen_s(&fp,"pass.dat","r");
fscanf_s(fp,"%s",pass1,20);
fclose(fp);
screen.MoveCursorTo(12,10);
screen.Write("请输入密码:");
Textbox textbox1=Textbox(27,10,20);
char *pass=NULL;
int times=0;
while(1)
{
pass=textbox1.InputString(false);
if(strcmp(pass,pass1)!=0)
{
times++;
if(times==3)
{
circulation=0;
break;
}
}
else
{
break;
}
}
}
void MainInterface()
{
Screen screen;
screen.SetSize(80,25);
screen.SetTitle(“日记”);
Form form;
SMALL_RECT rc={10,1,61,16};
form.form_color_num=4;
form.DrawBox(rc);
form.DrawSeparatorBar_H(12,rc.Left,rc.Right);
Button Write_button=Button(34,5,"填写");
Write_button.button_color_num=3;
Write_button.Display_button();
Button Look_button=Button(34,9,"查阅");
Look_button.button_color_num=3;
Look_button.Display_button();
Button Modify_button=Button(18,14,"修改密码");
Modify_button.button_color_num=2;
Modify_button.Display_button();
Button EXIT_button=Button(45,14,"退出程序");
EXIT_button.button_color_num=2;
EXIT_button.Display_button();
Event event;
while(1)
{
event.ReadEvent();
if(event.Mouse_Event())
{
if(EXIT_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
circulation=0;
return;
}
if(Write_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
State=WRITE;
interface_num=1;
break;
}
if(Look_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
State=LOOK;
interface_num=1;
break;
}
if(Modify_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
Window window=Window(11,18,50,5);
screen.MoveCursorTo(12,20);
screen.Write("请设置新密码:");
Textbox textbox1=Textbox(27,20,20);
char *pass=textbox1.InputString(true);
_chdir(data_path);
FILE *fp;
fopen_s(&fp,"pass.dat","w");
fprintf(fp,"%s",pass);
fclose(fp);
window.Clear_window();
}
}
if(event.Key_Event()&&event.Key_Down())
{
char ch=event.Get_ch();
WORD vk=event.Get_vk();
if(ch=='q'||vk==VK_ESCAPE)
{
circulation=0;
return;
}
}
}
}
void Answer()
{
Screen screen;
screen.SetSize(130,35);
// 获得今年今月今日
time_t t;
struct tm now;
t=time(NULL);
localtime_s(&now,&t);
int This_year=1900+now.tm_year;
int This_month=1+now.tm_mon;
int Today=now.tm_mday;
// 生成目录
_chdir(data_path);
FILE *fp;
char *plane_name="作者姓名.txt";
fopen_s(&fp,plane_name,"r");
fscanf_s(fp,"%s %d",aircraft_name,20,&aircraft_initial_year);
fclose(fp);
screen.SetTitle(aircraft_name);
_mkdir(aircraft_name);
_chdir(aircraft_name);
int year;
for(year=aircraft_initial_year;year<=This_year;year++)
{
char year1[10];
sprintf_s(year1,10,"%d",year);
_mkdir(year1);
}
// 显示标题
char Title[60];
year=This_year;
sprintf_s(Title,60,"%s%d年的日记",aircraft_name,year);
screen.text_color_num=6;
screen.DisplayText((screen.wide-(int)strlen(Title))/2,2,Title);
// 显示状态
screen.SetTextColor(6);
if(State==WRITE)
{
screen.MoveCursorTo(3,3);
screen.Write("填写");
}
if(State==LOOK)
{
screen.MoveCursorTo(3,3);
screen.Write("查阅");
}
// 画表格
int left=0,top=4,column=32,line=13,Form_wide=4,Form_high=2;
Form form;
form.form_color_num=6;
form.DrawForm(0,top,32,13,4,2);
SMALL_RECT rc={left,top,column*Form_wide+1+rc.Left,line*Form_high+rc.Top};
// 月 1-12,日 1-31
COORD pos;
screen.SetTextColor(3);
int day=1;
for(pos.X=rc.Left+4+2,pos.Y=rc.Top+1;day<32;pos.X+=4,day++)
{
screen.MoveCursorTo(pos);
screen.Write(day);
}
int month=1;
for(pos.X=rc.Left+2,pos.Y=rc.Top+3;month<13;pos.Y+=2,month++)
{
screen.MoveCursorTo(pos);
screen.Write(month);
}
// 标注今天
pos.Y=This_month*2+(rc.Top+1);
pos.X=Today*4+(rc.Left+1)+1;
screen.Character_color_num=3;
screen.DisplayCharacter(pos,'T',1);
//标注无效日期
int month1[5]={2,4,6,9,11};//||month==6||month==9||month==11)
int day1[3]={29,30,31};
screen.Character_color_num=9;
if(!Leap_year(year))
{
pos.Y=month1[0]*2+(rc.Top+1);
pos.X=day1[0]*4+(rc.Left+1)+1;
screen.DisplayCharacter(pos,'N',1);
}
pos.Y=month1[0]*2+(rc.Top+1);
pos.X=day1[1]*4+(rc.Left+1)+1;
screen.DisplayCharacter(pos,'N',1);
for(int i=0;i<5;i++)
{
pos.Y=month1[i]*2+(rc.Top+1);
pos.X=day1[2]*4+(rc.Left+1)+1;
screen.DisplayCharacter(pos,'N',1);
}
// 标注今年已有记录
char Work_path[MAX_PATH];
year=This_year;
sprintf_s(Work_path,MAX_PATH,"%s\\%s\\%d",data_path,aircraft_name,year);
screen.Character_color_num=5;
LinkedList_string Mresult;
PNode_string temp1;
Mresult.CreateFromDirectory(Work_path,"\\*.txt",false);
temp1=Mresult.head;
while(temp1!=NULL)
{
Cut_apart(temp1->name,&year,&month,&day);
pos.Y=month*2+(rc.Top+1);
pos.X=day*4+(rc.Left+1)+2;
screen.DisplayCharacter(pos,'*',1);
temp1=temp1->next;
}
//
Textbox date_textbox=Textbox(55,33,20);
date_textbox.textbox_textcolor_num=3;
Button RETURN_button=Button(122,3,"[返回]");
RETURN_button.Display_button();
Button PrevYear_button=Button(0,32,"【上一年】");
PrevYear_button.Display_button();
Button NextYear_button=Button(120,32,"【下一年】");
NextYear_button.Display_button();
Event event;
while(1)
{
event.ReadEvent();
if(event.Mouse_Event())
{
event.Get_mouse_position();
if(mouse.X>rc.Left+4&&mouse.Xrc.Top+2&&mouse.Yname,filename)==0)break;
temp1=temp1->next;
index++;
}
//
char date[20];
Cut_apart(Mresult.NodeString(index),&year,&month,&day);
sprintf_s(date,20,"%d年%02d月%02d日",year,month,day);
Window window1=Window(0,0,130,35);
window1.GetText_window();
window1.Clear_window();
window1.AddWindowWithinBorder();
Form form;
form.DrawSeparatorBar_H(4,window1.cx0,window1.cx0+window1.wide-1);
form.DrawSeparatorBar_H(30,window1.cx0,window1.cx0+window1.wide-1);
Textbox textbox=Textbox(window1.cx0+2,window1.cy0+2,window1.wide-4);
textbox.text_position=1;
Window window2=Window(window1.cx0+2,window1.cy0+5,window1.wide-4,25);
//window2.Paint_window(1);
textbox.DisplayString_textbox(date);
window2.DisplayFileEx_SinglePageWindow(Mresult.NodeString(index));
Button Prev_button=Button(window1.cx0+10,window1.cy0+window1.heigh-3,"上一条");
Prev_button.Display_button();
Button Next_button=Button(window1.cx0+25,window1.cy0+window1.heigh-3,"下一条");
Next_button.Display_button();
Button Cancel_button=Button(window1.cx0+50,window1.cy0+window1.heigh-3,"取消");
Cancel_button.Display_button();
Event event;
while(1)
{
event.ReadEvent();
if(event.Mouse_Event())
{
if(Prev_button.MousePosition_At_button()&&event.Left_Button_Pressed())
{
if(index>0)
{
index--;
Cut_apart(Mresult.NodeString(index),&year,&month,&day);
sprintf_s(date,20,"%d年%02d月%02d日",year,month,day);
textbox.DisplayString_textbox(date);
window2.Clear_window();
window2.DisplayFileEx_SinglePageWindow(Mresult.NodeString(index));
}
}
if(Next_button.MousePosition_At_button()&&event.Left_Button_Pressed())
{
if(indexaircraft_initial_year)
{
screen.Character_color_num=9;
temp1=Mresult.head;
while(temp1!=NULL)
{
Cut_apart(temp1->name,&year,&month,&day);
pos.Y=month*2+(rc.Top+1);
pos.X=day*4+(rc.Left+1)+2;
screen.DisplayCharacter(pos,' ',1);
temp1=temp1->next;
}
// 新的一年
year--;
sprintf_s(Title,60,"%s%d年的日记",aircraft_name,year);
screen.DisplayText((screen.wide-(int)strlen(Title))/2,2,Title);
// 2月29日
pos.Y=2*2+(rc.Top+1);
pos.X=29*4+(rc.Left+1)+1;
if(Leap_year(year))
{
screen.DisplayCharacter(pos,' ',1);
}
else
{
screen.DisplayCharacter(pos,'N',1);
}
//
sprintf_s(Work_path,MAX_PATH,"%s\\%s\\%d",data_path,aircraft_name,year);
Mresult.CreateFromDirectory(Work_path,"\\*.txt",false);
screen.Character_color_num=5;
temp1=Mresult.head;
while(temp1!=NULL)
{
Cut_apart(temp1->name,&year,&month,&day);
pos.Y=month*2+(rc.Top+1);
pos.X=day*4+(rc.Left+1)+2;
screen.DisplayCharacter(pos,'*',1);
temp1=temp1->next;
}
}
}
if(NextYear_button.MousePosition_At_button()&&event.Left_Button_Pressed())
{
if(yearname,&year,&month,&day);
pos.Y=month*2+(rc.Top+1);
pos.X=day*4+(rc.Left+1)+2;
screen.DisplayCharacter(pos,' ',1);
temp1=temp1->next;
}
// 新的一年
year++;
sprintf_s(Title,60,"%s%d年的日记",aircraft_name,year);
screen.DisplayText((screen.wide-(int)strlen(Title))/2,2,Title);
// 2月29日
pos.Y=2*2+(rc.Top+1);
pos.X=29*4+(rc.Left+1)+1;
if(Leap_year(year))
{
screen.DisplayCharacter(pos,' ',1);
}
else
{
screen.DisplayCharacter(pos,'N',1);
}
//
sprintf_s(Work_path,MAX_PATH,"%s\\%s\\%d",data_path,aircraft_name,year);
Mresult.CreateFromDirectory(Work_path,"\\*.txt",false);
screen.Character_color_num=5;
temp1=Mresult.head;
while(temp1!=NULL)
{
Cut_apart(temp1->name,&year,&month,&day);
pos.Y=month*2+(rc.Top+1);
pos.X=day*4+(rc.Left+1)+2;
screen.DisplayCharacter(pos,'*',1);
temp1=temp1->next;
}
}
}
if(RETURN_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
Mresult.DeleteLb();
interface_num=0;
return;
}
}
}
}
bool Leap_year(int year)
{
if((year%40&&year%100!=0)||(year%4000))return true;
else return false;
}
void Cut_apart(const char *result0,int *year,int *month,int *day)
{
string temp(result0);
*year=atoi(temp.substr(0,4).c_str());
*month=atoi(temp.substr(4,2).c_str());
*day=atoi(temp.substr(6,2).c_str());
}
int Valid(int year,int month,int day)
{
if(month1||month3||month5||month7||month8||month10||month12)
{
if(day<=31)return 1; else return 0;
}
else if(month2)
{
if((year%40&&year%100!=0)||(year%4000))
{
if(day<=29)return 1; else return 0;
}
else
{
if(day<=28)return 1; else return 0;
}
}
else if(month4||month6||month9||month11)
{
if(day<=30)return 1; else return 0;
}
else return 0;
}
运行效果: