1. 程式人生 > >C++ 簡單飯卡管理系統

C++ 簡單飯卡管理系統

此小應用是基於類和連結串列的
/***********Person.h****************/
#include<iostream>
#include"Main.h"

#ifndef PERSON_H
	#define PERSON_H
		
	class Person 
	{
		protected:
			SEX sex;
			IDNUM id;
	   public:
		Person(SEX _sex,IDNUM _id);
		Person(){}
	};
#endif



/***********Person.cpp****************/
#include"Person.h"

Person::Person(SEX _sex,IDNUM _id)
{
	sex = _sex;
	id = _id;
}



/***********Cstudent.h****************/
#include"CCard.h"
#include"Person.h"
#include"Main.h"

#ifndef CSTUDENT_H
	#define CSTUDENT_H		
	
class Cstudent :public CCard, public Person
{
	private:
		STUDENT stu;
		CLASSNUM classNum;
	public:
		Cstudent(STUDENT _stu,CLASSNUM _classNUM,NAME _name,MOMEY _balance,SEX _sex,IDNUM _id);
		Cstudent();
		~Cstudent(){}
		void showCstudent();
};

#endif



/***********Cstudent.cpp****************/
#include"Cstudent.h"
#include"Main.h"
#include"CCard.h"
#include"Person.h"
using namespace std;

Cstudent::Cstudent(STUDENT _stu,CLASSNUM _classNum,char* _name,MOMEY _balance,SEX _sex,IDNUM _id):Person( _sex, _id),CCard (_name, _balance)
{
	stu = _stu;
	classNum = _classNum;
}	

Cstudent::Cstudent():CCard(),Person()
{}

void Cstudent::showCstudent()
{
  cout<<"姓名:"<<name<<endl;
  cout<<"班級號碼 :"<<classNum<<endl;
  cout<<"ID號碼:"<<id<<endl;
  cout<<"性別: ";if(sex == 0) cout<<"男"<<endl;else if(sex == 1) cout<<"女"<<endl; else cout<<"其他性別"<<endl;
  cout<<"學生類別: ";if(stu == 0) cout<<"本科生"<<endl;else cout<<"研究生"<<endl;
  cout<<"餘額"<<balance<<endl<<endl;
}



/***********CCard.h****************/
#include <iostream>
#include "Main.h"
#include<ctime>

#ifndef CCARD_H
	#define CCARD_H
	
class CCard 
 {	
	protected:
		CARDNUM cardNum;
		NAME name;
		tm *_time;
		MOMEY balance;		
	public:
		CCard(char * _name,MOMEY _balance);
		CCard(){}
		~CCard(){}
		void ChargeCard(MOMEY m);
		void ConsumeCard(MOMEY m);
		void InquireCard();
		char* showName();
		tm*get_firstsettime();
};
#endif



/***********CCard.cpp****************/
#include "CCard.h"
#include<iostream>
using namespace std;
CCard::CCard(char * _name,MOMEY _balance)
{
	strcpy(name,_name);
	balance = _balance;
	time_t now;
	time(&now);
	_time = localtime(&now);
}

void CCard::ChargeCard(MOMEY m)
{
	balance += m;
	cout<<"充值成功!"<<endl;
	cout<<"當前餘額為:"<<balance<<endl;
}

void CCard::ConsumeCard(MOMEY m)
{
	if(balance>=m)
	{
		balance -= m;
		cout<<"付款完成!"<<endl;
		cout<<"當前餘額為:"<<balance<<endl;
	}
	else
	{
		cout<<"餘額不足請充值"<<endl;
	}
}
tm* CCard::get_firstsettime()
{
	return _time;
}
char* CCard::showName()
{
	return name;
}
/*CCard::InquireCard()
{
	//cout<<setw(15)<<"飯卡卡號:"<<card_ID<<endl;
	cout<<setw(15)<<"使用者姓名:"<<name<<endl;
	cout<<setw(15)<<"建卡時間:";	
	cout<<get_firstsettime()->tm_year+1900<<"-";
	cout<<get_firstsettime()->tm_mon+1<<"-";
	cout<<get_firstsettime()->tm_mday<<endl;
	cout<<setw(15)<<"使用者餘額:"<<momery<<endl;
}*/



/***********Main.h****************/
#ifndef MAIN_H
	#define MAIN_H

#include<cstring>	
	#define Max 4294967295  //定義最大數
	
	typedef char NAME[20] ; //定義各種變數型別
	typedef int CARDNUM [6];
	typedef int TIME ;
	typedef int MOMEY ;
	typedef int IDNUM;
	typedef int CLASSNUM;
	
	typedef int SEX ;
	typedef int STUDENT ;
	
#endif 



/***********Main.cpp****************/

/***************************************
	 連結串列管理例程
	 主要實現功能為:
	 0、新建連結串列
	 1、插入成員
	 2、查詢成員
	 3、刪除成員
	 4、資料瀏覽
***************************************/
//#include "StdAfx.h"
#include <iostream>
#include <cstring>
#include<cstdlib>
#include"Cstudent.h"
using namespace std;

/*類宣告*/
typedef struct student				//定義student結構體
{
 int num;
 Cstudent stu;
 struct student * next;
}stud;												//用stud 代替 student類 型別 同typedef int NUM一樣原理

/* 函式宣告 */
stud * create_list();        												//建立連結串列
int insert_list(stud * head,int n);									//插入成員
int del_list(stud * head, char *name);							//刪除成員
stud * find_list(stud * head, char * name);						//查詢成員
void brow_list(stud * head);											//顯示全部
void showTable();
void chargeCard(stud * head, char * name);					 //飯卡充值
void consumeCard(stud * head, char * name);				 //飯卡消費
/*主函式*/
void main()
{
 stud * head;	 //定義stud型別的指標
 int choice;
 char name[8];
 
 head = NULL;		//指標賦空值
 head = create_list();
 showTable();
 do
 {
  system("cls");
  showTable();
  cin>>choice;					 //輸入功能選擇
  if (choice>5||choice<0) 
  {
   cout<<"輸入錯誤\n";
   continue;
  }
  
  switch (choice) 
  {	
  case 1:
	   if (head==NULL)     //功能1: 插入成員
	   {
		cout<<"連結串列未建立\n";
		break;
	   }
		insert_list(head,-1);
	   break;
   
   case 2:							//功能2: 刪除成員
	   cout<<"輸入姓名:";
	   cin>>name;
	   del_list(head,name);
	   break;

   case 3:							//功能3: 飯卡充值
	   cout<<"輸入姓名:";
	   cin>>name;
	   chargeCard(head,name);
	   break;
   
   case 4:							//功能4: 飯卡消費
	   cout<<"輸入姓名:";
	   cin>>name;
	   consumeCard(head,name);
	   break;
   
 
   case 5:							//功能5:查詢功能
	   cout<<"<1. 按姓名查詢 2.按卡號順序列表>"<<endl;
	   char selectCase5;
	   cin>>selectCase5;
	   if(selectCase5 == '1') {
			 cout<<"輸入姓名 :"<<endl;
			 cin>>name;
			 find_list(head,name);
	   }
	   else if(selectCase5 == '2')
			brow_list(head);
	   else 
		   cout<<"輸入有誤"<<endl;
	   break;
   
  default:
   return;
  }
  system("pause");
 } while (1);
}

/*函式實現*/
stud *create_list() 	//新建連結串列
{
	
 stud * head;
 head=new stud;
 
 if (head!=NULL)
  cout<<"連結串列已建立\n";
 else
  cout<<"沒有足夠儲存空間\07\n";
 head->next=NULL;
 head->num=0;
 
 return head;
}

int insert_list(stud * head,int n) //插入成員
{
 stud *p, *q, *s;
 s=new stud;
 
 if (s==NULL) 
 {
  cout<<"沒有足夠空間!\07\n";
  return 0;
 }
 
 q=head;
 p=head->next;
 while (p!=NULL&&n!=q->num) 
 {
  q=p;
  p=p->next;
 }
 q->next=s;
 s->next=p;
/**************************************
	Cstudent(STUDENT _stu,CLASSNUM _classNUM,NAME _name,MOMEY _balance,SEX _sex,IDNUM _id)
	***************************************/
    STUDENT stu_temp;
	CLASSNUM classNum_temp;
	NAME name_temp;
	MOMEY balance_temp;
	SEX sex_temp;
	IDNUM id_temp;
	/**************************************/
	cout<<"輸入辦卡人姓名:"<<endl;
	cin>>name_temp;
	cout<<"輸入辦卡人班級號:"<<endl;
	cin>>classNum_temp;
	cout<<"輸入充值金額:"<<endl;
	cin>>balance_temp;
	cout<<"輸入性別<0.男 1.女>"<<endl;
	cin>>sex_temp;
	cout<<"輸入ID號:"<<endl;
	cin>>id_temp;
	cout<<"輸入辦卡人類別<0.本科生 1.研究生>"<<endl;
	cin>>stu_temp;
	/**************************************/
	Cstudent cstudent_temp(stu_temp,classNum_temp,name_temp,balance_temp,sex_temp,id_temp);
	(s->stu)=cstudent_temp;
	cstudent_temp.~Cstudent();
 s->num=q->num+1;
 return 1;
}

stud * find_list(stud * head, char * name) //查詢成員
{
 stud * p;
 p=head;
 while(p!=NULL&&strcmp(p->stu.showName(),name))
 {
  p=p->next;
 }
 if (p!=NULL)
 {
   cout<<"學號:"<<p->num<<endl;
   (p->stu).showCstudent();
 }
 else
  cout<<"查無此人!\n";
 return p;
}

int del_list(stud * head, char *name)  //刪除成員
{
 stud *p, *q;
 q=head;
 p=head->next;
 while (p!=NULL&&strcmp(p->stu.showName(),name))
 {
  q=p;
  p=p->next;
 }
 if (p!=NULL)
 {
  q->next=p->next;
  delete p;
  cout<<"刪除完成\n";
  return 1;
 }
 else
 {
  cout<<"查無此人\07\n";
  return 0;
 }
}

void brow_list(stud * head)			//顯示全部成員
{
 stud *P;
 P=head->next;
 while (P!=NULL)
 {
  cout<<"學號:"<<P->num<<endl;
  (P->stu).showCstudent();
  P=P->next;
 }
}

void chargeCard(stud * head, char * name) //飯卡充值
{
 stud * p;
 p=head;
 while(p!=NULL&&strcmp(p->stu.showName(),name))
 {
  p=p->next;
 }
 if (p!=NULL)
 {
   MOMEY momey_temp;
   cout<<"輸入充值數額:"<<endl;
   cin>>momey_temp;
   (p->stu).ChargeCard(momey_temp);
 }
 else
  cout<<"查無此人!\n";
}

void consumeCard(stud * head, char * name) //飯卡充值
{
 stud * p;
 p=head;
 while(p!=NULL&&strcmp(p->stu.showName(),name))
 {
  p=p->next;
 }
 if (p!=NULL)
 {
   MOMEY momey_temp;
   cout<<"輸入消費數額:"<<endl;
   cin>>momey_temp;
   (p->stu).ConsumeCard(momey_temp);
 }
 else
  cout<<"查無此人!\n";
}

void showTable()
{
	cout<<"************************"<<endl;
	cout<<"**寧波大學飯卡管理系統**"<<endl;
	cout<<"************************"<<endl;
	cout<<"************************"<<endl;
	cout<<"**# --幫助            **"<<endl;
	cout<<"**1 --新建飯卡        **"<<endl;
	cout<<"**2 --撤銷飯卡        **"<<endl;
	cout<<"**3 --飯卡充值        **"<<endl;
	cout<<"**4 --飯卡消費        **"<<endl;
	cout<<"**5 --飯卡查詢和排序  **"<<endl;
	cout<<"**0 --退出系統        **"<<endl;
	cout<<"************************"<<endl;
}