回 帖 发 新 帖 刷新版面

主题:个人帐薄管理系统设计——课程设计

/*
个人帐薄管理系统设计
    记录某人每月的全部收入及各项开支情况,包括食品消费,房租,子女教育费用,水电费,储蓄等。进入系统后可以输入和修改某月的收支情况,可以对每月的收支从小到大进行排序,可以根据输入的月份查询每月的收支情况。
*/
#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

回复列表 (共3个回复)

沙发



/*数据导出*/
int Save(Datalist &data)
{
    switch(data.month)
    {
    case 1:  {ofstream outfile("f1.txt",ios::out);   if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    case 2:  {ofstream outfile("f2.txt",ios::out);   if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    case 3:  {ofstream outfile("f3.txt",ios::out);   if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    case 4:  {ofstream outfile("f4.txt",ios::out);   if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    case 5:  {ofstream outfile("f5.txt",ios::out);   if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    case 6:  {ofstream outfile("f6.txt",ios::out);   if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    case 7:  {ofstream outfile("f7.txt",ios::out);   if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    case 8:  {ofstream outfile("f8.txt",ios::out);   if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    case 9:  {ofstream outfile("f9.txt",ios::out);   if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    case 10: {ofstream outfile("f10.txt",ios::out);  if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    case 11: {ofstream outfile("f11.txt",ios::out);  if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    case 12: {ofstream outfile("f12.txt",ios::out);  if(! outfile){cerr<<"open error!"<<endl;exit(1);}  outfile<<data.income<<' '<<data.sum<<' '<<data.food<<' '<<data.rent<<' '<<data.education<<' '<<data.expense<<' '<<data.save<<' '<<data.others;  outfile.close();  break;}
    default: {}
    }//switch
    data.number++;
    cout<<endl<<"保存完成"<<endl; 
    return 0;
}//Save

板凳



/*清零*/
int Clear(Datalist &data)
{
    data.income=0;data.sum=0;data.food=0;data.rent=0;data.education=0;data.expense=0;
    data.save=0;
    data.others=0;
    cout<<endl<<"清零完成。"<<endl; 
    return 0;
}

int Close(Datalist &data)
{
    char ch[10];
    if(!data.number) exit(0);
    cout<<"**************************"<<endl;
    cout<<"是否保存后在退出(是 y ;否 n ):";
    cin>>ch;
    while(strlen(ch)!=1||(ch[0]!='y'&&ch[0]!='n'))
    {
        cout<<"输入错误,请重新输入。"<<endl;
        cout<<"是否保存后在退出(是 y ;否 n ):" ;
        cin>>ch;
    }
    if(ch[0]=='y')    Save(data); 
    exit(0);
    return 0;
}
    
/*主函数*/
int main()
{
    Datalist data={0};
    char ch[10];
    int i,j;
    cout<<"家庭收支管理系统"<<endl<<endl; 
    Open(data);
    cout<<"a.修改 b.查询 c.排序 d.保存 e.重新选择月份 f.清零 g.退出"<<endl;
    cout<<"请选择要进行操作的字母: ";
    cin>>ch;
    while(strlen(ch)!=1||ch[0]>'g'||ch[0]<'a')
    {
        cout<<"输入错误,请重新输入:";
        cin>>ch;
    } //while
    while(ch[0]!='g')
    {
        switch(ch[0])
        {
        case 'e': Save(data);cout<<endl<<"##########################"<<endl<<"##########################"<<endl;i=Open(data);break;
        case 'a': Change(data);break;
        case 'b': Inquire(data);break;
        case 'c': Inorder(data);break;
        case 'd': Save(data);break;
        case 'f': Clear(data);break;
        default : {}
        }
        if(ch[0]!='e') cout<<"**************************"<<endl;
        cout<<endl<<"a.修改 b.查询 c.排序 d.保存 e.重新选择月份 f.清零 g.退出"<<endl;
        cout<<endl<<"请选择要进行操作的字母: ";
        cin>>ch;
        while(strlen(ch)!=1||ch[0]>'g'||ch[0]<'a')
        {
            cout<<endl<<"输入错误,请重新输入:";
            cin>>ch;
        } //while
    } //while
    cout<<endl<<"**************************"<<endl;
    cout<<endl<<"是否保存后在退出(是 y ;否 n ):";
    cin>>ch;
    while(strlen(ch)!=1||(ch[0]!='y'&&ch[0]!='n'))
    {
        cout<<endl<<"输入错误,请重新输入。"<<endl;
        cout<<endl<<"是否保存后在退出(是 y ;否 n ):" ;
        cin>>ch;
    }
    if(ch[0]=='y')    Save(data); 
    system("pause");
    return 0;
}//main

3 楼


起码给个图吧

我来回复

您尚未登录,请登录后再回复。点此登录或注册