C语言 家庭财务管理

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>


#define MAXSIZE (10)


struct node
{
char user[MAXSIZE];
char detail[MAXSIZE];
char time[MAXSIZE];
float money;
struct node *next;
};


typedef  struct HomeFinance HomeFinance;


struct HomeFinance
{
char name[MAXSIZE];
char begin_date[MAXSIZE];
char end_date[MAXSIZE];
float balance;
float max_income;
float max_payout;
float min_income;
float min_payout;
int income_count;
int payout_count;
struct node *income_payout;
};




HomeFinance* init();
void show(HomeFinance *H);
void menu();
struct node* building_node();
void add_item(HomeFinance *H);
void printlist(struct node *L);
void print_single(struct node *L);
void del_item(HomeFinance *H);
struct node* find(struct node *L,char detail_f[],char time_f[]);
void rev_item(HomeFinance *L);
void src_item(HomeFinance *L);
void income_stat(HomeFinance *L);
void payout_stat(HomeFinance *L);




int main()
{
HomeFinance *mytest;
mytest = init();
show(mytest);
int choice = 1;
while(choice != 8)
{
  menu();
  printf("你的选择:"); 
scanf("%d",&choice);
switch(choice)
 {
     case 1:add_item(mytest);show(mytest);break; 
     case 2:del_item(mytest);show(mytest);break;
     case 3:rev_item(mytest);show(mytest);break;
     case 4:src_item(mytest);break;
     case 5:show(mytest);break;
     case 6:payout_stat(mytest);break;
     case 7:income_stat(mytest);break; 
 }
}  



HomeFinance* init()
{
HomeFinance *H;
H = (HomeFinance *)malloc(sizeof(struct HomeFinance));
if(H == NULL)
 return NULL;
   printf("*创建财务管理系统*\n");
printf("给账本取个名吧:");
scanf("%s",H->name);
printf("创建日期(例如:20180401):");
scanf("%s",H->begin_date);
strcpy(H->end_date,"unknown");
printf("目前金额总数:"); 
scanf("%f",&H->balance); 
   H->max_income = 0;
H->max_payout = 0;
H->min_income = 100000;
H->min_payout = 100000;
H->income_count = 0;
H->payout_count = 0;
H->income_payout = NULL;

return H;
}




void show(HomeFinance *H)
{
printf("%s\n",H->name);
printf("创建日期:%s\n",H->begin_date);
printf("最近更新:%s\n",H->end_date);
printf("余额:%.2f\n",H->balance);
if(H->income_payout != NULL)
{
 printf("收支详情:\n");
 printlist(H->income_payout);
}
}




void menu()
{
printf("****************************\n");
printf("*****家庭财务管理系统*******\n");
printf("***1.记一笔*****2.删一笔****\n");
printf("***3.改一改*****4.查一查****\n");
printf("***5.秀一下*****6.花的钱****\n");
printf("***7.赚的钱*****8.下次看****\n");
}


struct node* building_node()
{
struct node *d;
d = (struct node *)malloc(sizeof(struct node));
if(d == NULL)
 return NULL;
   printf("请输入用户名:"); 
scanf("%s", d->user);
printf("请输入备注信息:");
scanf("%s", d->detail);
printf("请输入时间:");
scanf("%s", d->time);
printf("请输入收支数量(正负号分别表示收或者支):");
scanf("%f", &d->money);
 
   d->next = NULL;
   return d;
}




void add_item(HomeFinance *H)
{
struct node *d;
d = building_node();
d->next = H->income_payout;
H->income_payout = d;
H->balance += d->money;
if(d->money >= 0)
{
H->income_count++;
if(d->money > H->max_income)
 H->max_income = d->money;
      if(d->money < H->min_income)
        H->min_income = d->money;
}
else
{
H->payout_count++;
if(fabs(d->money) < fabs(H->min_payout))
 H->min_payout = d->money;
      if(fabs(d->money) > fabs(H->max_payout))
        H->max_payout = d->money;
}  
strcpy(H->end_date,d->time);
}


void printlist(struct node *L)
{
struct node *tmp;
tmp = L;
printf("********************\n");
while(tmp != NULL)
{
printf("用户名:%s\n", tmp->user);
printf("备注信息:%s\n", tmp->detail);
printf("收支日期:%s\n", tmp->time);
printf("收支额度:%.2f\n", tmp->money);
tmp = tmp->next;
}
printf("********************\n");
}




void print_single(struct node *L)
{
struct node *tmp;
tmp = L;
printf("********************\n");
printf("用户名:%s\n", tmp->user);
printf("备注信息:%s\n", tmp->detail);
printf("收支日期:%s\n", tmp->time);
printf("收支额度:%.2f\n", tmp->money);
printf("********************\n");
}




void del_item(HomeFinance *H)
{
struct node *pb,*pf,*tmp;
pb = H->income_payout;
if(H->income_payout == NULL)
{
printf("链表为空\n");
return;
}
char user_f[MAXSIZE],time_f[MAXSIZE];
printf("请输入你想删除的账单用户及时间:\n");
scanf("%s%s",user_f,time_f);
while((strcmp(pb->user,user_f) != 0 || strcmp(pb->time,time_f) != 0) && pb->next != NULL)
{
pf = pb;
pb = pb->next;
}
if(strcmp(pb->user,user_f) == 0 && strcmp(pb->time,time_f) == 0)
{
if(pb == H->income_payout)
{
H->income_payout = pb->next;
tmp = pb;
if(tmp->money >= 0)
  {
    H->income_count--;
    H->balance += tmp->money*(-1);
    }
    else
    {
  H->payout_count--;
  H->balance += tmp->money*(-1);
  }
}
else
{
pf->next = pb->next;
tmp = pb;
if(tmp->money >= 0)
  {
    H->income_count--;
    H->balance += tmp->money*(-1);
    }
    else
    {
  H->payout_count--;
  H->balance += tmp->money*(-1);
  }
}
free(pb);
}
else
 printf("此账单不存在!\n");
}




void income_stat(HomeFinance *L)
{
struct node *p;
p = L->income_payout;
printf("收入总数:%d\n",L->income_count); 
while(p != NULL)
{
if(p->money >= 0)
   {
   printf("用户名:%s\n", p->user);
   printf("备注信息:%s\n", p->detail);
   printf("收入日期:%s\n", p->time);
   printf("收入额度:%.2f\n\n", p->money);
   }
p = p->next;

}


void payout_stat(HomeFinance *L)
{
struct node *p;
p = L->income_payout;
printf("支出总数:%d\n",L->payout_count); 
while(p != NULL)
{
if(p->money < 0)
   {
   printf("用户名:%s\n", p->user);
   printf("备注信息:%s\n", p->detail);
   printf("支出日期:%s\n", p->time);
   printf("支出额度:%.2f\n\n", p->money);
   printf("\n");
   }
p = p->next;

}




void rev_item(HomeFinance *L)
{
struct node *tmp,*p;
int choose; 
char time_f[MAXSIZE],detail_f[MAXSIZE];
tmp = L->income_payout;
printlist(tmp);
printf("请输入您要修改的收支记录的备注及时间:\n");
scanf("%s%s",detail_f,time_f);
   p = find(tmp,detail_f,time_f);
   print_single(p);
if(p != NULL) 
{
printf("请输入你要修改的选项:\n"); 
  printf("1.用户名\t2.备注信息\n3.账单日期\t4.收支额度\n你的选择:");
  scanf("%d",&choose);
  switch(choose)
  {
  case 1:printf("请修改备注名:");
         scanf("%s",p->user);break;
  case 2:printf("请修改备注信息:");
         scanf("%s",p->detail);break;
  case 3:printf("请修改账单日期:");
         scanf("%s",p->time);break; 
         case 4:L->balance += p->money*(-1);
         printf("请修改收支额度:");
         scanf("%f",&p->money);
         L->balance += p->money;
         if(p->money >= 0)
            {
           L->income_count++;
           if(p->money > L->max_income)
             L->max_income = p->money;
                  if(p->money < L->min_income)
                    L->min_income = p->money;
                }
            else
            {
            L->payout_count++;
            if(fabs(p->money) < fabs(L->min_payout))
             L->min_payout = p->money;
                  if(fabs(p->money) > fabs(L->max_payout))
                    L->max_payout = p->money;
            }  
break;  
 }
printf("修改成功!\n");
}
}




void src_item(HomeFinance *L)
{
struct node *p;
p = L->income_payout;
char time_t[MAXSIZE];
   printf("请输入账单日期查找:");
scanf("%s",time_t); 
while(p != NULL)
{
      if(strcmp(p->time,time_t) == 0)
        print_single(p);
      p = p->next;
}
p = L->income_payout;
while(p != NULL)
{
if(p->money == L->max_income)
{
printf("%s的账单最大收入:%.2f\n",L->name,L->max_income);
printf("用户名:%s\n", p->user);
printf("备注信息:%s\n", p->detail);
printf("收支日期:%s\n", p->time);
printf("--------------------------\n");
}
if(p->money == L->max_payout)
{
printf("最大支出:%.2f\n",L->max_payout);
printf("用户名:%s\n", p->user);
printf("备注信息:%s\n", p->detail);
printf("收支日期:%s\n", p->time);
printf("--------------------------\n");
}
      if(p->money == L->min_income)
{
printf("最小收入:%.2f\n",L->min_income);
printf("用户名:%s\n", p->user);
printf("备注信息:%s\n", p->detail);
printf("收支日期:%s\n", p->time);
printf("--------------------------\n");
}
  if(p->money == L->min_payout)
{
printf("最小支出:%.2f\n",L->min_payout);
printf("用户名:%s\n", p->user);
printf("备注信息:%s\n", p->detail);
printf("收支日期:%s\n", p->time);
printf("--------------------------\n");
}
p = p->next;  
}
}


struct node* find(struct node *L,char detail_f[],char time_f[])
{
struct node *p;
p = L;
while(p != NULL)
{
if(strcmp(p->detail,detail_f) == 0)
  if(strcmp(p->time,time_f) == 0)
    return p;
p = p->next;
}
printf("位置不存在\n");
return NULL;
}

发布了76 篇原创文章 · 获赞 24 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览