小金庫—有密碼,添賬,列單,按日期、ID查詢,刪除的功能(初始密碼為123456,可更改)
阿新 • • 發佈:2019-02-04
#include<stdio.h>
#include<string.h>
#include<conio.h> //問題:清除小金庫進入不了函式,刪除記錄,新小金庫//
#include<stdlib.h>
long size;
struct LogData
{
long logid;
char logdate[11];
char lognote[15];
double charge;
double balance;
};
int inputchoice(void)
{
int mychoice;
printf ("\n輸入你的選擇:\n");
printf("1-在原賬目上新增新增新記錄.\n2-列出所有賬單.\n");
printf("3-按照ID查詢記錄\n");
printf("4-查詢最後一次記錄.\n5-刪除小金庫.\n6-建立新的小金庫及其相關操作.\n7-按照日期查詢記錄.\n8-刪除記錄\n0-結束操作.\n");
scanf("%d",&mychoice);
return mychoice;
}
void removefile(void)
{
char filename[80];
printf("輸入要刪除的檔案:");
//gets(filename);
scanf("%s",filename);
if( remove(filename))
printf("can't delete the file %s",filename);
else
printf("OK");
}
long getLogcount(FILE *cfptr)
{
long begin,end,logcount;
fseek(cfptr,0L,0);
begin=ftell(cfptr);
fseek(cfptr,0,2);
end=ftell(cfptr);
logcount=(end-begin)/size;
return logcount;
}
/*void correctlog(FILE*pp,int n)
{
struct LogDate haa;
fseek(pp,size*(n-1),0);
fread(&haa,size,1,pp);
printf("輸入說明,金額");
scanf("%s%ld",haa.) ;
}*/
void deleteLine(FILE* fpp, int d)
{
int n= getLogcount(fpp);
struct LogData old[100];
int i;
for(i=1;i<=n;i++)
fread(&old[i],size,1,fpp);
for(i=0;(d+i)<n;i++)
old[d+i]=old[d+i+1];
old[i].charge=0.0;old[i].logid=0;
fclose(fpp);
fpp=fopen("cashbox.dat","rb+");
rewind(fpp);
for(i=1;i<n;i++)
fwrite(&old[i],size,1,fpp);
}
void ListAllLog(FILE *cfptr)
{
struct LogData log;
fseek(cfptr,0L,0);
fread(&log,size,1,cfptr);
printf(" logid logdate lognote charge left\n 0 0 0 0\n");
while(!feof(cfptr))
{
printf("%6ld%11s%15s%10.2lf%10.2lf\n",log.logid,log.logdate,log.lognote,log.charge,log.balance);
fread(&log,size,1,cfptr); /*每次讀完之後指標已經移動*/
}
}
void QueryLastLog(FILE *cfptr)
{
struct LogData log;
long logcount;
logcount=getLogcount(cfptr); /*logcount是結構體個數*/
if(logcount>0)
{
fseek(cfptr,size*(logcount-1),0);
fread(&log,size,1,cfptr);
printf("最後一次記錄是:\n");
printf("logid:%6ld\nlogdate:%11s\nlognote:15s\n",log.logid,log.logdate,log.lognote);
printf("charge:%10.2lf\nbalance:%10.2lf\n",log.logid,log.balance);
}
else
printf("no logs in file!\n");
}
void querybyID(FILE *cfptr,int n)
{
struct LogData logidd;
fseek(cfptr,size*(n-1),0); /*若第一條ID則不需要從第一條資料跳轉,所以乘的是n-1,如第二條從第一條結尾開始讀*/
fread(&logidd,size,1,cfptr);
printf("記錄是(若查詢的ID不存在則輸出第一條ID):\n");
printf("logid:%6ld\nlogdate:%11s\nlognote:15s\n",logidd.logid,logidd.logdate,logidd.lognote);
printf("charge:%10.2lf\nbalance:%10.2lf\n",logidd.logid,logidd.balance);
}
void AddNewLog(FILE *cfptr)
{
struct LogData log,lastlog;
long logcount;
printf("輸入新記錄日期(格式:2006-01-01):");
scanf("%s",log.logdate);
printf("輸入備註:");
scanf("%s",log.lognote);
printf("輸入金額:支出標負");
scanf("%lf",&log.charge);
logcount=getLogcount(cfptr);
if(logcount>0)
{
fseek(cfptr,size*(logcount-1),0);
fread(&lastlog,size,1,cfptr);
log.logid=lastlog.logid+1;
log.balance=log.charge+lastlog.balance;
}
else
{
log.logid=1;
log.balance=log.charge;
}
putchar(10);
rewind(cfptr);
printf("logid=%ld\n",log.logid);
fwrite(&log,sizeof(struct LogData),1,cfptr);
}
void findbylogdate(FILE *cfptr,char a[])
{
struct LogData logid2;
int i=1;
while(!feof(cfptr))
{
fseek(cfptr,size*i,0);
fread(&logid2,size,1,cfptr);
if(strcmp(a,logid2.logdate)==0)
{
printf("logid:%6ld\nlogdate:%s\nlognote:%s\n",logid2.logid,logid2.logdate,logid2.lognote);
printf("charge:%10.2lf\nbalance:%10.2lf\n",logid2.logid,logid2.balance); break;
}
i=i+1;
}
while(feof(cfptr)) printf("error");
}
void creatnewAu(void)
{
FILE *ffp;
char neww[20];
printf("請輸入你想建立金庫的名稱及其檔案型別(.dat):\n");
scanf("%s",neww);
if((ffp=fopen(neww,"wb+"))==NULL)
{
printf("不能開啟!\n");
exit(0);
}
printf("\n輸入你的選擇2:\n");
printf("1-在新賬目上新增新增新記錄.\n2-列出所有賬單.\n");
printf("3-按照ID查詢記錄\n");
int choice2;
scanf("%d",&choice2);
switch(choice2)
{
case 1:
AddNewLog(ffp);break;
case 2:
ListAllLog(ffp);break;
case 3:
{
int n2;
printf("請輸入要查詢的ID(若無此項則輸出第一條ID):\n");
scanf("%d",&n2);
querybyID(ffp,n2);
}
}
}
int main(void)
{
FILE *fp;
int choice;
printf("請輸入密碼:(五次機會)");
int times,code;
scanf("%d",&code);
for(times=1;times<=5;times++)
{
if(code!=123456)
{
printf("密碼錯誤,請重新輸入: ");
scanf("%d",&code);
}
if(code==123456) break;
if(times==5) return 0;
}
if((fp=fopen("cashbox.dat","ab+"))==NULL)
{
printf("can not open a file!\n");
return 0;
}
size=sizeof(struct LogData);
while((choice=inputchoice())!=0)
{
switch(choice)
{
case 1:
AddNewLog(fp);break;
case 2:
ListAllLog(fp);break;
case 3:
{
int id;
printf("請輸入要查詢的ID:");
scanf("%d",&id);
querybyID(fp,id);break;
}
case 4:
QueryLastLog(fp);break;
case 5:
removefile();break;
case 6:
creatnewAu();break;
case 7:
{
char datt[15];
printf("請輸入想要查詢的日期:\n");
scanf("%s",datt);
findbylogdate(fp,datt);
}
break;
case 8:
{
int want;
printf("請輸入想刪除的ID:\n");
scanf("%d",&want);
deleteLine(fp,want);
}
break;
case 9:
printf("Input Error.");break;
}
}
fclose(fp);
return 0;
}