主题:个人帐薄管理系统设计——课程设计
/*
个人帐薄管理系统设计
记录某人每月的全部收入及各项开支情况,包括食品消费,房租,子女教育费用,水电费,储蓄等。进入系统后可以输入和修改某月的收支情况,可以对每月的收支从小到大进行排序,可以根据输入的月份查询每月的收支情况。
*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
typedef struct
{
int number; //大开本月后所做的操作。
int income;
int sum;
int food;
int rent;
int education;
int expense;
int save;
int others;
int month;
}Datalist;//读取文件用到得结构体
typedef struct
{
string str;
int money;
}Elem;//排序用的链表元素
int Change(Datalist &data);
int Inquire(Datalist &data);
int Inorder(Datalist &data);
int Creat(int n);
int Open(Datalist &data);
int Save(Datalist &data);
int Clear(Datalist &data);
int Close(Datalist &data);
/*修改函数*/
int Change(Datalist &data)
{
int i,j;
char ch[10];
cout<<endl<<"**************************"<<endl<<endl;
cout<<"a.收入 b.食品消费 c.房租 d.教育支出 e.水电费 f.储蓄 "<<endl;
cout<<"g.其它支出 h.返回上层 i.重选月份 j.保存 k.退出 "<<endl;
cout<<endl<<"请选择要修改的数据:";
cin>>ch;
while(strlen(ch)!=1||ch[0]<'a'||ch[0]>'k')
{
cout<<endl<<"输入错误,请重新输入:";
cin>>ch;
}//while
for(;ch[0]!='h';)
{
if(ch[0]=='i') {Save(data);Open(data);return 0;}
if(ch[0]=='j') Save(data);
if(ch[0]=='k') {Close(data);}
else{
cout<<endl<<"请输入本项变动的数字 :" ;
j=scanf("%d",&i);
while(getchar()!='\n');
while(j!=1)
{
cout<<endl<<"输入错误,请重新输入本项变动的数字:";
j=scanf("%d",&i);
while(getchar()!='\n');
}//while
switch(ch[0])
{
case 'a': data.income+=i;break;
case 'b': data.food+=i;data.sum+=i;break;
case 'c': data.rent+=i;data.sum+=i;break;
case 'd': data.education+=i;data.sum+=i;break;
case 'e': data.expense+=i;data.sum+=i;break;
case 'f': data.save+=i;data.sum+=i;break;
case 'g': data.others+=i;data.sum+=i;break;
default : ;//"default"后必须加一个 “;”或者一对大括号。
}//switch
}//else
cout<<endl<<"a.收入 b.食品消费 c.房租费用 d.教育支出 e.水电费 f.储蓄 "<<endl;
cout<< "g.其它支出 h.返回上层 i.重选月份 j.保存 k.退出"<<endl;
cout<<endl<<"请选择要修改的数据:";
cin>>ch;
while(strlen(ch)!=1||ch[0]>'k'||ch[0]<'a')
{
cout<<endl<<"输入错误,请重新输入:";
cin>>ch;
}//while
}//for
data.number++;
return 0;
}//Change
/*查询函数*/
int Inquire(Datalist &data)
{
cout<<"本月收支情况如下:"<<endl;
cout<<"收入:"<<data.income<<endl;
cout<<"总支出:"<<data.sum<<endl;
cout<<"食品消费:"<<data.food<<endl;
cout<<"房租:"<<data.rent<<endl;
cout<<"子女教育支出:"<<data.education<<endl;
cout<<"水电费:"<<data.expense<<endl;
cout<<"储蓄:"<<data.save<<endl;
cout<<"其它支出:"<<data.others<<endl;
data.number++;
cout<<endl<<"查询完成"<<endl;
return 0;
}//Inquire
/*排序函数*/
int Inorder(Datalist &data)
{ int i,j;
Elem ele;
Elem Array[6]={{"food",data.food},{"rent",data.rent},{"education",data.education},{"expense",data.expense},{"save",data.save},{"others",data.others}};
for(i=0;i<6;i++)
for(j=0;j<6-i;j++)
if(Array[j].money>Array[j+1].money) {ele=Array[j];Array[j]=Array[j+1];Array[j+1]=ele;}
cout<<"本月各项支出从小到大如下:"<<endl;
for(i=0;i<6;i++)
cout<<Array[i].str<<":"<<Array[i].money<<endl;
data.number++;
cout<<endl<<"排序完成"<<endl;
return 0;
}//Inorder
/*如果数据导入失败,则会用到的创建文件函数*/
int Creat(int n)
{
switch(n)
{
case 1: {ofstream outfile("f1.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<00<<' '<<00<<' '<<00<<' '<<00<<' '<<00<<' '<<00<<' '<<00<<' '<<00; outfile.close(); break;}//原来的空格用双引号引起了是不对(下面的几行也一样),应该用单引号: case 1: {ofstream outfile("f1.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<0; outfile.close(); break;}
case 2: {ofstream outfile("f2.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 3: {ofstream outfile("f3.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 4: {ofstream outfile("f4.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 5: {ofstream outfile("f5.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 6: {ofstream outfile("f6.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 7: {ofstream outfile("f7.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 8: {ofstream outfile("f8.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 9: {ofstream outfile("f9.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 10: {ofstream outfile("f10.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 11: {ofstream outfile("f11.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 12: {ofstream outfile("f12.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
default: {}
}//switch
}//Creat
/*数据导入*/
int Open(Datalist &data)
{
int i,j;
cout<<endl;
cout<<"请输入要处理的月份(1-12,0则直接退出):";
j=scanf("%d",&i);
for(;getchar()!='\n';);
while(j!=1)
{
cout<<endl<<"输入错误,请重新输入将要处理的月份(1-12,0则直接退出):";
j=scanf("%d",&i);
while(getchar()!='\n');
}
cout<<"**********"<<endl;
cout<<"**********"<<endl;
while(i<0||i>12)
{
cout<<"输入错误,请重新输入(1-12,0则直接退出):";
cin>>i;
}
data.month=i;
switch(i)
{
case 0: {Close(data);break;}
case 1: {ifstream infile("f1.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}//这里的 “case”后,为何非要加大括号?
case 2: {ifstream infile("f2.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 3: {ifstream infile("f3.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 4: {ifstream infile("f4.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 5: {ifstream infile("f5.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 6: {ifstream infile("f6.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 7: {ifstream infile("f7.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 8: {ifstream infile("f8.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 9: {ifstream infile("f9.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 10: {ifstream infile("f10.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 11: {ifstream infile("f11.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 12: {ifstream infile("f12.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
default:{}
}//switch
data.number=0;
return 0;
}//Open
个人帐薄管理系统设计
记录某人每月的全部收入及各项开支情况,包括食品消费,房租,子女教育费用,水电费,储蓄等。进入系统后可以输入和修改某月的收支情况,可以对每月的收支从小到大进行排序,可以根据输入的月份查询每月的收支情况。
*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
typedef struct
{
int number; //大开本月后所做的操作。
int income;
int sum;
int food;
int rent;
int education;
int expense;
int save;
int others;
int month;
}Datalist;//读取文件用到得结构体
typedef struct
{
string str;
int money;
}Elem;//排序用的链表元素
int Change(Datalist &data);
int Inquire(Datalist &data);
int Inorder(Datalist &data);
int Creat(int n);
int Open(Datalist &data);
int Save(Datalist &data);
int Clear(Datalist &data);
int Close(Datalist &data);
/*修改函数*/
int Change(Datalist &data)
{
int i,j;
char ch[10];
cout<<endl<<"**************************"<<endl<<endl;
cout<<"a.收入 b.食品消费 c.房租 d.教育支出 e.水电费 f.储蓄 "<<endl;
cout<<"g.其它支出 h.返回上层 i.重选月份 j.保存 k.退出 "<<endl;
cout<<endl<<"请选择要修改的数据:";
cin>>ch;
while(strlen(ch)!=1||ch[0]<'a'||ch[0]>'k')
{
cout<<endl<<"输入错误,请重新输入:";
cin>>ch;
}//while
for(;ch[0]!='h';)
{
if(ch[0]=='i') {Save(data);Open(data);return 0;}
if(ch[0]=='j') Save(data);
if(ch[0]=='k') {Close(data);}
else{
cout<<endl<<"请输入本项变动的数字 :" ;
j=scanf("%d",&i);
while(getchar()!='\n');
while(j!=1)
{
cout<<endl<<"输入错误,请重新输入本项变动的数字:";
j=scanf("%d",&i);
while(getchar()!='\n');
}//while
switch(ch[0])
{
case 'a': data.income+=i;break;
case 'b': data.food+=i;data.sum+=i;break;
case 'c': data.rent+=i;data.sum+=i;break;
case 'd': data.education+=i;data.sum+=i;break;
case 'e': data.expense+=i;data.sum+=i;break;
case 'f': data.save+=i;data.sum+=i;break;
case 'g': data.others+=i;data.sum+=i;break;
default : ;//"default"后必须加一个 “;”或者一对大括号。
}//switch
}//else
cout<<endl<<"a.收入 b.食品消费 c.房租费用 d.教育支出 e.水电费 f.储蓄 "<<endl;
cout<< "g.其它支出 h.返回上层 i.重选月份 j.保存 k.退出"<<endl;
cout<<endl<<"请选择要修改的数据:";
cin>>ch;
while(strlen(ch)!=1||ch[0]>'k'||ch[0]<'a')
{
cout<<endl<<"输入错误,请重新输入:";
cin>>ch;
}//while
}//for
data.number++;
return 0;
}//Change
/*查询函数*/
int Inquire(Datalist &data)
{
cout<<"本月收支情况如下:"<<endl;
cout<<"收入:"<<data.income<<endl;
cout<<"总支出:"<<data.sum<<endl;
cout<<"食品消费:"<<data.food<<endl;
cout<<"房租:"<<data.rent<<endl;
cout<<"子女教育支出:"<<data.education<<endl;
cout<<"水电费:"<<data.expense<<endl;
cout<<"储蓄:"<<data.save<<endl;
cout<<"其它支出:"<<data.others<<endl;
data.number++;
cout<<endl<<"查询完成"<<endl;
return 0;
}//Inquire
/*排序函数*/
int Inorder(Datalist &data)
{ int i,j;
Elem ele;
Elem Array[6]={{"food",data.food},{"rent",data.rent},{"education",data.education},{"expense",data.expense},{"save",data.save},{"others",data.others}};
for(i=0;i<6;i++)
for(j=0;j<6-i;j++)
if(Array[j].money>Array[j+1].money) {ele=Array[j];Array[j]=Array[j+1];Array[j+1]=ele;}
cout<<"本月各项支出从小到大如下:"<<endl;
for(i=0;i<6;i++)
cout<<Array[i].str<<":"<<Array[i].money<<endl;
data.number++;
cout<<endl<<"排序完成"<<endl;
return 0;
}//Inorder
/*如果数据导入失败,则会用到的创建文件函数*/
int Creat(int n)
{
switch(n)
{
case 1: {ofstream outfile("f1.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<00<<' '<<00<<' '<<00<<' '<<00<<' '<<00<<' '<<00<<' '<<00<<' '<<00; outfile.close(); break;}//原来的空格用双引号引起了是不对(下面的几行也一样),应该用单引号: case 1: {ofstream outfile("f1.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<0<<" "<<0; outfile.close(); break;}
case 2: {ofstream outfile("f2.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 3: {ofstream outfile("f3.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 4: {ofstream outfile("f4.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 5: {ofstream outfile("f5.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 6: {ofstream outfile("f6.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 7: {ofstream outfile("f7.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 8: {ofstream outfile("f8.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 9: {ofstream outfile("f9.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 10: {ofstream outfile("f10.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 11: {ofstream outfile("f11.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
case 12: {ofstream outfile("f12.txt",ios::out); if(! outfile){cerr<<"open error!"<<endl;exit(1);} outfile<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0<<' '<<0; outfile.close(); break;}
default: {}
}//switch
}//Creat
/*数据导入*/
int Open(Datalist &data)
{
int i,j;
cout<<endl;
cout<<"请输入要处理的月份(1-12,0则直接退出):";
j=scanf("%d",&i);
for(;getchar()!='\n';);
while(j!=1)
{
cout<<endl<<"输入错误,请重新输入将要处理的月份(1-12,0则直接退出):";
j=scanf("%d",&i);
while(getchar()!='\n');
}
cout<<"**********"<<endl;
cout<<"**********"<<endl;
while(i<0||i>12)
{
cout<<"输入错误,请重新输入(1-12,0则直接退出):";
cin>>i;
}
data.month=i;
switch(i)
{
case 0: {Close(data);break;}
case 1: {ifstream infile("f1.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}//这里的 “case”后,为何非要加大括号?
case 2: {ifstream infile("f2.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 3: {ifstream infile("f3.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 4: {ifstream infile("f4.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 5: {ifstream infile("f5.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 6: {ifstream infile("f6.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 7: {ifstream infile("f7.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 8: {ifstream infile("f8.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 9: {ifstream infile("f9.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 10: {ifstream infile("f10.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 11: {ifstream infile("f11.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
case 12: {ifstream infile("f12.txt",ios::in); if(! infile){Creat(i);Clear(data);} else {infile>>data.income>>data.sum>>data.food>>data.rent>>data.education>>data.expense>>data.save>>data.others; infile.close();} break;}
default:{}
}//switch
data.number=0;
return 0;
}//Open