C and C++ example code to implement the personal balance system

  • 2020-10-07 18:49:49
  • OfStack

Yesterday, my friend entrusted me to do a course design based on C++. The topic is as follows:
Design a simple personal income and expense management system, including at least the following functions:

1. Input the details of personal income or expenditure one by one, and write them into the file for saving. Input and add them continuously.
The detailed data input includes: income and expenditure details category code, date of occurrence, amount and remarks.

1) To simplify the user's input, the category code is composed of letters and Numbers: "a" means revenue and "b" means expenditure
The number is the number under the big category. The category code and the corresponding category name are defined and organized in the program
And output display. For example, a1 represents the living expenses of the income category,b1 represents the learning of the expenditure category, and the number increases successively.
The number and name of the categories are drawn up according to daily needs.
Such as:
Income category: a1 with living expenses, a2 with scholarship, a3 with contributions
Expenditure categories: b1, b2, b3, games

2) The user enters the details of income and expenditure. The specific input format is as follows (each data is separated by one or more consecutive Spaces.
Enter enter directly to indicate the end of input) :
Please enter the category code, date of occurrence, amount and remark:
Input income and expense details: a1, 2020, 1, 26, 2000.0 Living expenses for January
Enter income and expenditure details: b1 2020, w3, w9 52.5 Buy books
...
Enter the income and expenditure details: b2 2020 buy desk lamp by 5 ° C and 978.00 ° C
3) Write the input detailed data to a file for saving, so as to facilitate subsequent append and processing.

2. Complete the summary, function design and code implementation according to the daily needs of personal income and expenditure management. Need to be
Statistics and output the summary of each income and expenditure category entered by the user in the month, after output, ask the user again whether input
Each detail of the month, the user answered yes, then according to the details of the type of ascending output; Answer no, do not output.
The input and output results are as follows:

Please enter the month in which the revenue and expenditure category data were aggregated: 20120.3
Summary of income and expenditure category data in March 2020
Income/expenditure detail category amount
Income living expenses 2000
...
Spend 200 on school supplies
...
The total income in March 2020 is 1500, and the total expenditure is 1200
Whether to output each detail of this month (y is output, others are not output) : y
Details of income and expenditure category data in March 2020
Class Income/expenditure date of occurrence amount notes
Living expenses Income 2020 w03 w01 2000.0 March living expenses
School supplies expenditure 2020 w03 w03 10.0 Pens
...

Oneself knocked 1 times, specific implementation is as follows:


#include <iostream>
#include <stdlib.h>
#include <string.h>
#define MAX_MARK 100

using namespace std;

struct Pay
{
	char payment[2];
	int inout;
	int year;
	int month;
	int day;
	int money;
	char mark[MAX_MARK];
}ioi;/* Date, income / spending   The amount of   Source of income / Spending on purpose */


void displayMenu()
 {
  cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
  cout <<"+     Personal income and expenditure management system       +"<<endl;
  cout <<"+     1.  Add the balance of payments        +"<<endl;
  cout <<"+     2.  To find the balance of payments        +"<<endl;
  cout <<"+     3.  Statistical balance of payments        +"<<endl;
  cout <<"+     4.  The output file        +"<<endl;
  cout <<"+     5.  End of the program        +"<<endl;
  cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
}

int addPayment(Pay ioi[],int num)
{
	char tempPayment[3];
	cout <<" Please enter the category code, date of occurrence, amount and remark "<<endl;
	cout <<" Input income and expenditure details: ";
	scanf("%s %d-%d-%d %d %s",ioi[num].payment,&ioi[num].year,&ioi[num].month,&ioi[num].day,&ioi[num].money,ioi[num].mark);
	if(ioi[num].payment[0]=='a'){
		ioi[num].inout = 1;
	}else{
		ioi[num].inout = 0;
	}
	num++;
	return num;
};

void findPayment(Pay ioi[],int num)
{
	int kind;
	int i;
	int flag=0;
	int tempMonth=0,tempYear=0;
	cout <<" Please enter the target you want to find ( 1 For years, 2 For the category) :"<<endl;
	cin >> kind;
	if(kind == 1){
		cout << " Please enter the date of the search (XXXX-XX):";
		scanf("%d-%d",&tempYear,&tempMonth);
		for(i=0;i<num;i++){
			if(ioi[i].year==tempYear && ioi[i].month== tempMonth){
				cout << " To find the !";
				if(ioi[i].inout == 1){
					cout <<" income  ";
				}
				if(ioi[i].inout == 0){
					cout <<" spending  ";
				} 
				cout <<ioi[i].money;
				cout << '\t';
				cout <<ioi[i].mark<<endl;
			}
		}
	}
	char tempPayment[2];
	if(kind == 2){
		cout <<" Please enter the category you are looking for :";
		cin >> tempPayment;
		for(i=0;i<num;i++){
			if(ioi[i].payment[0] == tempPayment[0] && ioi[i].payment[1] == tempPayment[1]){
				cout <<" To find the !";
				if(ioi[i].inout == 1){
					cout <<" income  ";
				}
				if(ioi[i].inout == 0){
					cout <<" spending  ";
				} 
				cout <<ioi[i].money;
				cout << '\t';
				cout <<ioi[i].mark<<endl;
			}
		}
	}
};


void sumPayment(Pay ioi[],int num)
{
	int i;
	int sumYear=0;
	int sumMonth=0;
	int judge=0;
	cout <<" Please enter the month in which the revenue and expenditure category data is summarized :";
	scanf("%d-%d",&sumYear,&sumMonth);
	printf("%d years %d Summary of monthly revenue and expenditure data: \n",sumYear,sumMonth); 
	cout << " Balance of payments \t The detail \t The amount of " <<endl;
	for(i=0;i<num;i++){
		if(ioi[i].year==sumYear && ioi[i].month==sumMonth){
			if(ioi[i].inout == 1){
				cout <<" income  ";
			}
			if(ioi[i].inout == 0){
				cout <<" spending  ";
			}
			cout <<ioi[i].money;
			cout << '\t';
			cout <<ioi[i].mark<<endl;
		}
	}
	
	int sumIn=0,sumOut=0;
	for(i=0;i<num;i++){
		if(ioi[i].inout == 1){
			sumIn+= ioi[i].money;
		}
		if(ioi[i].inout == 0){
			sumOut+= ioi[i].money;
		}
	}
	printf("%d years %d The total monthly income is: %d  The total expenditure is: %d\n",sumYear,sumMonth,sumIn,sumOut); 
	
	cout <<" Whether to output each detail: ( is 1 no 0):";
	cin >>judge;
	if(judge==1){
		printf("%d years %d Breakdown of monthly revenue and expenditure category data: \n",sumYear,sumMonth); 
		cout <<" category \t income \t spending \t In the date \t The amount of \t note "<<endl;
		for(i=0;i<num;i++){
			if(ioi[i].payment[0] == 'a' &&ioi[i].payment[0] == '1')
					cout <<" The cost of living  ";
			if(ioi[i].payment[0] == 'a' &&ioi[i].payment[0] == '2')
					cout <<" The scholarship  ";
			if(ioi[i].payment[0] == 'a' &&ioi[i].payment[0] == '3')
					cout <<" The payment  ";
			if(ioi[i].payment[0] == 'b' &&ioi[i].payment[0] == '1')
					cout <<" School supplies  ";
			if(ioi[i].payment[0] == 'b' &&ioi[i].payment[0] == '2')
					cout <<" Articles for daily use  ";
			if(ioi[i].payment[0] == 'b' &&ioi[i].payment[0] == '3')
					cout <<" The game  ";
		
			if(ioi[i].inout == 1){
				cout <<" income \t";
			}
			if(ioi[i].inout == 0){
				cout <<" spending \t";
			}
			cout <<ioi[i].money;
			cout << '\t';
			cout <<ioi[i].mark<<endl;
		}
	}
};

void writeFile(Pay ioi[])
{
	FILE *fp=NULL;
	int i=0;

	fp=fopen("payment.txt","wb");

	if(fp==NULL)
	{
		cout <<" Error opening file !"<<endl;
		exit(0);
	}
	fwrite(&ioi,sizeof(struct Pay),1,fp);
	cout <<" The information has been saved to text payment.txt In the "<<endl;
	fclose(fp);
};

void sortMoney(Pay ioi[],int length)	// Bubble sort  
{
	int i=0,j=0,k=0;
	Pay temp;
	for(i=0;i<length;i++){
		k=i;
		for(j=i+1;j<length;j++)
			if(ioi[j].money>ioi[k].money)
				k=j;
  temp=ioi[k];
  ioi[k]=ioi[i];
  ioi[i]=temp;
	}
}

int main()
{
	int allNum=0;
	int choice=0;
	struct Pay ioi[50];
 while(1)
 {
		displayMenu();
		cout<<" Please select your action (1,2,3,4,5)"<<endl;
  cin>>choice;
		switch(choice)
  {
			case 1:
				allNum = addPayment(ioi,allNum);
				break;
			case 2:
				findPayment(ioi,allNum);
				break;
			case 3:
				sortMoney(ioi,allNum);
				sumPayment(ioi,allNum);
				break;
			case 4:
				writeFile(ioi);
				break;
			case 5:
				exit(0);
				break;
			default:
				cout<<" The input is not valid. Reinput "<<endl;
				break;
		}
	}
}

conclusion


Related articles: