家庭收支记账软件
完成的基本功能
1.菜单 2.收支明细 3.登记收入 4.登记支出 5.退出系统
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct MyFamilyAccount {
int flag = 0;
char details[3000];
double balance;
};
char key;
int loop = 1;
char note[20];
double money;
char temp[100];
char choice;
void showDetails(struct MyFamilyAccount* myFamilyAccount) {
if ((*myFamilyAccount).flag) {
printf("%s", (*myFamilyAccount).details);
}
else {
printf("\n当前没有任何收支明细,来一笔吧!");
}
return;
}
void income(struct MyFamilyAccount* myFamilyAccount) {
printf("本次收入金额:");
scanf("%lf", &money);
getchar();
(*myFamilyAccount).balance += money;
printf("\n本次收入说明:");
scanf("%s", note);
getchar();
sprintf(temp, "\n收入\t%.2f\t\t%.2f\t\t%s", money, (*myFamilyAccount).balance, note);
strcat((*myFamilyAccount).details, temp);
(*myFamilyAccount).flag = 1;
return;
}
void pay(struct MyFamilyAccount* myFamilyAccount) {
printf("本次支出金额:");
scanf("%lf", &money);
getchar();
if (money > (*myFamilyAccount).balance) {
printf("\n余额不足!");
return;
}
printf("\n本次支出说明:");
scanf("%s", note);
getchar();
(*myFamilyAccount).balance -= money;
sprintf(temp, "\n支出\t%.2f\t\t%.2f\t\t%s", money, (*myFamilyAccount).balance, note);
strcat((*myFamilyAccount).details, temp);
(*myFamilyAccount).flag = 1;
}
void myExit() {
printf("\n你是否确定退出? y/n\n:");
do {
scanf("%c", &choice);
getchar();
if (choice == 'y' || choice == 'n') {
break;
}
printf("\n输入有误,请重新输入y/n\n:");
} while (1);
if (choice == 'y') {
loop = 0;
}
}
void mainMenu(struct MyFamilyAccount* myFamilyAccount) {
do {
printf("\n\n----------------家庭收支记账软件-------------");
printf("\n 1.收支明细");
printf("\n 2.登记收入");
printf("\n 3.登记支出");
printf("\n 4.退出");
printf("\n 请选择(1-4): ");
scanf("%c", &key);
getchar();
switch (key) {
case '1':
showDetails(myFamilyAccount);
break;
case '2':
income(myFamilyAccount);
break;
case '3':
pay(myFamilyAccount);
break;
case '4':
myExit();
}
} while (loop);
printf("你退出了软件!");
getchar();
}
int main() {
struct MyFamilyAccount myFamilyAccount;
myFamilyAccount.flag = 0;
myFamilyAccount.balance = 1000;
memset(myFamilyAccount.details, 3000, 0);
strcpy(myFamilyAccount.details, "\n----------------当前收支明细------------------\n收支\t收支金额\t账户金额\t说明");
mainMenu(&myFamilyAccount);
getchar();
return 0;
}
运行结果展示
初级版本代码(未进行封装)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
char key;
int loop = 1;
char details[3000]="\n----------------当前收支明细------------------\n收支\t收支金额\t账户金额\t说明";
char note[20];
double money;
double balance = 1000.0;
char temp[100];
char choice;
int flag = 0;
do {
printf("\n\n----------------家庭收支记账软件-------------");
printf("\n 1.收支明细");
printf("\n 2.登记收入");
printf("\n 3.登记支出");
printf("\n 4.退出");
printf("\n 请选择(1-4): ");
scanf("%c", &key);
getchar();
switch (key){
case '1':
if (flag) {
printf("%s", details);
}
else {
printf("\n当前没有任何收支明细,来一笔吧!");
}
break;
case '2':
printf("本次收入金额:");
scanf("%lf", &money);
getchar();
balance += money;
printf("\n本次收入说明:");
scanf("%s", note);
getchar();
sprintf(temp, "\n收入\t%.2f\t\t%.2f\t\t%s", money, balance, note);
strcat(details, temp);
flag = 1;
break;
case '3':
printf("本次支出金额:");
scanf("%lf", &money);
getchar();
if (money > balance) {
printf("\n余额不足!");
break;
}
printf("\n本次支出说明:");
scanf("%s", note);
getchar();
balance -= money;
sprintf(temp, "\n支出\t%.2f\t\t%.2f\t\t%s", money, balance, note);
strcat(details, temp);
flag = 1;
break;
case '4':
printf("\n你是否确定退出? y/n\n:");
do {
scanf("%c", &choice);
getchar();
if (choice == 'y' || choice == 'n') {
break;
}
printf("\n输入有误,请重新输入y/n\n:");
} while (1);
if (choice == 'y') {
loop = 0;
}
}
} while (loop);
printf("你退出了软件!");
return 0;
}
|