配色: 字号:
个人财务管理系统
2012-09-05 | 阅:  转:  |  分享 
  
//个人财务管理系统.cpp:Definestheentrypointfortheconsoleapplication.

//



#include"stdafx.h"

#include

#include

#include

#include

#include





//用户信息

structstPerson

{

charszName[30]; //用户名

charszPass[30]; //密码

longlIncome; //收入记录数

longlSpending; //支出记录数

};



structExpenditure

{

charszExName[20];//支出名称

doubledSingle; //支出金额

charszTime[20]; //时间

};



structIncome

{

charszInName[20]; //收入名称

doubledRevenue; //收入金额

charszTime[20]; //时间

};



structFinancial

{

doubledTotalRevenue; //总收入

structExpenditureedt[1000]; //支出

structIncomeic[1000]; //收入

};



//初始化

voidinit();



//登录菜单选择

voidSignInOrRst(structstPerson,structFinancial);



//登录

voidSignIn(structstPerson,structFinancial);



//注册

voidRegistration(structstPerson,structFinancial);



//主菜单

voidMenus(structFinancial,structstPerson);



//获取系统时间

charTime();



//查看本月总收入

voidAllIncome(structFinancial);



//本月支出

voidExpend(structFinancial,structstPerson);



//本月收入

voidIncomes(structFinancial,structstPerson);



//输出本月所以收支

voidPrintAll(structFinancial,structstPerson_tp);



//查看本月所有收入

voidPrintIncomes(structFinancial,structstPerson_tp);



//查看本月所有支出

voidPrintExpend(structFinancial,structstPerson_tp);



//退出程序

voidExit(structFinancial,structstPerson);



//保存数据

voidSaveData(structFinancial,structstPerson);



/

全局变量

/

intnnNum=0; //用户总数

intnnNow=0; //当前用户下标





int_tmain(intargc,_TCHARargv[])

{



init(); //初始化

stPersontp=(stPerson)malloc(sizeof(stPerson));

Financialst=(Financial)malloc(sizeof(Financial));



SignInOrRst(tp,st);

return0;

}



//

/初始化/

//



voidinit()

{

FILEfp;



fp=fopen("admin.dat","rb");

if(fp!=NULL)

{

fscanf(fp,"总人数有%d人。\n",&nnNum);

fclose(fp);

}

}



//

/登录菜单选择/

//



voidSignInOrRst(structstPerson_tp,structFinancial_fn)

{

system("cls"); //清除屏幕文字

system("color2b"); //设置背景颜色和字体颜色



charch;



printf("\n\n\t\t\t欢迎使用\n\n");

printf("\t\t个人财务管理系统\n\n");

printf("\t\t\t请选择去向...\n\n\n");

printf("\t\t\t1.登陆...\n");

printf("\t\t\t2.注册...\n\n");

printf("\t\t\t请选择...");



for(inti=0;i<3;i++)

{

ch=getch();

switch(ch){

case''1'':

SignIn(_tp,_fn);

break;

case''2'':

Registration(_tp,_fn);

break;

default:

{

if(i==2)

{

printf("您输入次数已达到三次,谢谢使用!\n");

exit(0);

}

else

{

printf("输入有误\n");

printf("请重新输入\n");

}

}

}

}

}







//

/用户注册/

//



voidRegistration(structstPerson_tp,structFinancial_fn)

{

system("CLS");

system("color2b");



FILEfp; //保存人员总数的文件指针

FILEfp1; //读取注册文件内容

FILEfp2; //向注册文件中添加内容

FILEfp3; //向财务文件中添加一个空间



charszName[30]; //用户名

charszPass[30]; //密码



//给两个字符串初始化

memset(szName,''\0'',30);

memset(szPass,''\0'',30);



while(true)

{

system("CLS");

printf("\n\n\t\t\t欢迎注册\n\n");

for(inti=0;i<3;i++)

{

printf("\t\t\t用户名:");



fflush(stdin); //清除缓冲区内容



scanf("%s",szName);



if(!strlen(szName))

{

printf("\t\t\t用户名或密码不能为空!\n");

printf("\t\t\t请重新输入...\n");

Sleep(1500);

system("cls");

continue;

}



fp1=fopen("person.dat","rb");

if(fp1==NULL)

{

break;

}

else

{

intj;

for(j=0;j
{

fseek(fp1,sizeof(stPerson)j,SEEK_SET);

fread(_tp,sizeof(stPerson),1,fp1);

if(!strcmp(_tp->szName,szName))

break;

}

if(j!=nnNum)

{

printf("该用户名已存在\n请重新输入...\n");

Sleep(1500);

system("cls");

continue;

}

else

break;

fclose(fp1);

}

if(i==2)

{

printf("您输入错误达到三次\n请选择去向...\n");

charcha;

printf("1.继续注册\n");

printf("2.返回选择页面\n");

printf("3.退出程序\n");



fflush(stdin);



cha=getch();



switch(cha)

{

case''1'':

i=0;

break;

case''2'':

SignInOrRst(_tp,_fn);

break;

case''3'':

exit(0);

default:

printf("输入错误...\n");

printf("程序将自动返回注册页面...\n");

printf("请稍等...");

Sleep(1500);

Registration(_tp,_fn);

}

}



}

for(inti=0;i<3;i++)

{

printf("\t\t\t密码:");



fflush(stdin); //清除缓冲区内容



scanf("%s",szPass);



if(!strlen(szPass))

{

printf("\t\t\t用户名或密码不能为空!\n");

printf("\t\t\t请重新输入...\n");

Sleep(1500);

system("cls");

continue;

}

if(i==2)

{

printf("错误次数达到三次...");

Sleep(1000);

i=0;

}

break;

}

break;

}



memset(_tp->szName,0,sizeof(_tp->szName));

strcpy(_tp->szName,szName); //复制用户名到结构体中

memset(_tp->szPass,0,sizeof(_tp->szPass));

strcpy(_tp->szPass,szPass); //复制密码到结构体中

_tp->lIncome=0;

_tp->lSpending=0;

fp2=fopen("person.dat","a+b"); //使用写入的方式打开文件

if(fp2!=NULL)

{

fseek(fp2,sizeof(stPerson)nnNum,SEEK_SET);

intcount=fwrite(_tp,sizeof(stPerson),1,fp2);

fclose(fp2);

if(count) //将用户名和密码写入文件中的指定位置

{



fp=fopen("admin.dat","wb+");

if(fp!=NULL)

{

fprintf(fp,"总人数有%d人。\n",++nnNum); //写入成功后将总人数加一

fclose(fp);

}

fp3=fopen("financial.dat","ab+");

if(fp3!=NULL)

{

fseek(fp3,sizeof(stPerson)(nnNum-1),SEEK_SET);

fwrite(_fn,sizeof(stPerson),1,fp3);

fclose(fp3);

}

printf("\t\t\t注册成功!\n\t\t\t正在跳转,请稍等...");

Sleep(2000);

SignInOrRst(_tp,_fn);

}

}



}



//

/用户登陆/

//



voidSignIn(structstPerson_tp,structFinancial_fn)

{

system("cls");

system("color2b");



Financialfn=(Financial)malloc(sizeof(Financial));



FILEfp; //保存人员总数的文件指针

FILEfp1; //读取注册文件内容

FILEfp2; //用于读取财务信息



charszName[30]; //用户名

charszPass[30]; //密码



//给两个字符串初始化

memset(szName,''\0'',29);

memset(szPass,''\0'',29);



while(true)

{

system("CLS");

printf("\n\n\t\t\t欢迎登陆\n\n");

printf("\t\t\t用户名:");



fflush(stdin); //清除缓冲区内容



scanf("%s",szName);



printf("\t\t\t密码:");



fflush(stdin); //清除缓冲区内容



scanf("%s",szPass);



if(!strlen(szName)||!strlen(szPass))

{

printf("\t\t\t用户名或密码不能为空!\n");

printf("\t\t\t请重新输入...\n");

Sleep(2000);

system("cls");

continue;

}

fp1=fopen("person.dat","rb");

if(fp1!=NULL)

{

intj;





for(j=0;j
{

fseek(fp1,sizeof(stPerson)j,SEEK_SET);

fread(_tp,sizeof(stPerson),1,fp1);

if(!strcmp(_tp->szName,szName)&&!strcmp(_tp->szPass,szPass)) //如果找到名字相同的用户名则跳出循环

{

nnNow=j;

break;

}



}

fclose(fp1);

if(j==nnNum) //判断j是否跳到末尾,也就是判断是否有重复用户名,j==nnNum表示没有重复

{

printf("用户名或密码错误\n");

printf("请重新输入...\n");

Sleep(2000);

continue;

}

else

{

fp2=fopen("financial.dat","rb");

if(fp2!=NULL)

{

fseek(fp2,sizeof(Financial)nnNow,SEEK_SET);

fread(fn,sizeof(Financial),1,fp2);

fclose(fp2);

}

if(fn->dTotalRevenue<=0)

{

fn->dTotalRevenue=0;

}

system("CLS");

printf("登陆成功...\n");

Sleep(2000);

Menus(fn,_tp);

}

}

else

{

printf("你是第一次登录,请先注册!\n");

printf("谢谢合作!\n");

printf("正在跳转请稍等...\n");

Sleep(2000);

Registration(_tp,_fn);

break;

}

}

}







//

/主菜单/

//



voidMenus(structFinancial_fn,structstPerson_tp)

{



while(true)

{

system("cls");

system("color2b");

charch;



printf("\n\n\n\t\t\t欢迎使用\n");

printf("\t\t个人财务管理系统\n");

printf("\t\t\t请选择...\n");

printf("\t\t\t1.查看本月总收入\n");

printf("\t\t\t2.录入本月支出\n");

printf("\t\t\t3.录入本月浮动收入\n");

printf("\t\t\t4.查看本月所有收支\n");

printf("\t\t\t5.查看本月收入\n");

printf("\t\t\t6.查看本月支出\n");

printf("\t\t\t7.退出程序\n");

printf("\t\t\t8.切换账号\n");

printf("\t\t\t请选择...\n");



ch=getch();



switch(ch){

case''1'':

AllIncome(_fn);

break;

case''2'':

Expend(_fn,_tp);

break;

case''3'':

Incomes(_fn,_tp);

break;

case''4'':

PrintAll(_fn,_tp);

break;

case''5'':

PrintIncomes(_fn,_tp);

break;

case''6'':

PrintExpend(_fn,_tp);

break;

case''7'':

Exit(_fn,_tp);

break;

case''8'':

SignIn(_tp,_fn);

break;

default:

printf("您输入不在我们菜单范围之内\n");

printf("请重新输入...\n");

}

}

}



//

/获取系统时间/

//



charTime()

{

time_tt=time(0);

staticchartmp[32];

strftime(tmp,sizeof(tmp),"%Y/%m/%d%X",localtime(&t));

returntmp;

}



//

/查看本月总收入/

//



voidAllIncome(structFinancial_fn)

{

system("cls");



printf("本月总收入\n");

printf("%.2lf\n",_fn->dTotalRevenue);



system("pause");



printf("\n");

printf("正在返回,请稍等...\n");



Sleep(1000);



}



//

/录入本月支出/

//



voidExpend(structFinancial_fn,structstPerson_tp)

{

while(true)

{

system("cls");

printf("请输入支出名称:");



fflush(stdin);



scanf("%s",_fn->edt[_tp->lSpending].szExName);



printf("请输入金额:");

scanf("%lf",&_fn->edt[_tp->lSpending].dSingle);



_fn->dTotalRevenue-=_fn->edt[_tp->lSpending].dSingle; //总收入-支出



strcpy(_fn->edt[_tp->lSpending].szTime,Time());



_tp->lSpending++;

printf("继续?(y/n)\n");

charch;

ch=getch();

if(ch==''n''||ch==''N'')

{

SaveData(_fn,_tp);

printf("录入成功\n正在返回,请稍等...\n");

Sleep(2000);

break;

}

}



}





//

/浮动收入/

//



voidIncomes(structFinancial_fn,structstPerson_tp)

{

while(true)

{

system("cls");

printf("请输入收入名称:");



fflush(stdin);



scanf("%s",_fn->ic[_tp->lIncome].szInName);



printf("请输入金额:");



scanf("%lf",&_fn->ic[_tp->lIncome].dRevenue);



_fn->dTotalRevenue+=_fn->ic[_tp->lIncome].dRevenue; //总收入+浮动收入



strcpy(_fn->ic[_tp->lIncome].szTime,Time());



_tp->lIncome++;



printf("继续?(y/n)\n");

charch;

ch=getch();

if(ch==''n''||ch==''N'')

{

SaveData(_fn,_tp);

printf("录入成功\n正在返回,请稍等...\n");

Sleep(2000);

break;

}

}

}



//

/输出本月所以收支/

//



voidPrintAll(structFinancial_fn,structstPerson_tp)

{

system("cls"); //清屏



printf("\t\t\t\t本月收支详单如下:\n");

printf("\t\t\t本月总收入:");

printf("\t\t\t%.2lf元\n\n",_fn->dTotalRevenue);



printf("详细收支如下:\n\n\n");



printf("\n\n\n");



printf("\t\t本月收入详情\n");

printf("\t名称\t金额\t时间\n");



for(inti=0;i<_tp->lIncome;i++)

{

printf("\t%s\t%.2lf\t%s\n",_fn->ic[i].szInName,_fn->ic[i].dRevenue,_fn->ic[i].szTime);

}



printf("\n\n");



printf("\t\t本月支出详情\n");

printf("\t名称\t金额\t时间\n");



for(inti=0;i<_tp->lSpending;i++)

{

printf("\t%s\t%.2lf\t%s\n",_fn->edt[i].szExName,_fn->edt[i].dSingle,_fn->edt[i].szTime);

}



printf("\n\n\n");



system("pause");

}



//

/查看本月所有收入/

//



voidPrintIncomes(structFinancial_fn,structstPerson_tp)

{

system("cls");



printf("\t\t本月收入详情\n");

printf("\t名称\t金额\t时间\n");



for(inti=0;i<_tp->lIncome;i++)

{

printf("\t%s\t%.2lf\t%s\n",_fn->ic[i].szInName,_fn->ic[i].dRevenue,_fn->ic[i].szTime);

}



printf("\n\n\n");



system("pause");

}



//

/查看本月所有支出/

//



voidPrintExpend(structFinancial_fn,structstPerson_tp)

{

system("cls");



printf("\t\t本月支出详情\n");

printf("\t名称\t金额\t时间\n");



for(inti=0;i<_tp->lSpending;i++)

{

printf("\t%s\t%.2lf\t%s\n",_fn->edt[i].szExName,_fn->edt[i].dSingle,_fn->edt[i].szTime);

}



printf("\n\n\n");



system("pause");

}



//

/退出程序/

//



voidExit(structFinancial_fn,structstPerson_tp)

{

system("cls");



SaveData(_fn,_tp);

printf("\t\t\t谢谢使用本软件\n");



printf("\t\t下次再见...\n");



exit(0);

}





//

/写入文件/

//



voidSaveData(structFinancial_fn,structstPerson_tp)

{

FILEfp,fp1;



fp=fopen("financial.dat","rb+");



if(fp!=NULL)

{

fseek(fp,sizeof(Financial)nnNow,SEEK_SET);

fwrite(_fn,sizeof(Financial),1,fp);

fclose(fp);

}

else

{

printf("文件保存失败!\n");

}



fp1=fopen("person.dat","rb+");



if(fp1!=NULL)

{

fseek(fp1,sizeof(stPerson)nnNow,SEEK_SET);

fwrite(_tp,sizeof(stPerson),1,fp1);

fclose(fp1);

}

else

{

printf("文件保存失败!\n");

}

}
献花(0)
+1
(本文系我为宇宙首藏)