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;
}




 

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,我可以为您提供一个简单饭卡管理系统的代码示例,如下所示: ```cpp #include <iostream> #include <string> #include <vector> using namespace std; // 饭卡类 class Card { public: Card(string name, int cardNo, double balance) { this->name = name; this->cardNo = cardNo; this->balance = balance; } string getName() { return name; } int getCardNo() { return cardNo; } double getBalance() { return balance; } void deposit(double amount) { balance += amount; } bool withdraw(double amount) { if (balance < amount) { return false; } balance -= amount; return true; } private: string name; // 持卡人姓名 int cardNo; // 卡号 double balance; // 余额 }; // 饭堂类 class Canteen { public: void addCard(Card card) { cards.push_back(card); } bool removeCard(int cardNo) { for (int i = 0; i < cards.size(); i++) { if (cards[i].getCardNo() == cardNo) { cards.erase(cards.begin() + i); return true; } } return false; } bool deposit(int cardNo, double amount) { for (int i = 0; i < cards.size(); i++) { if (cards[i].getCardNo() == cardNo) { cards[i].deposit(amount); return true; } } return false; } bool withdraw(int cardNo, double amount) { for (int i = 0; i < cards.size(); i++) { if (cards[i].getCardNo() == cardNo) { return cards[i].withdraw(amount); } } return false; } private: vector<Card> cards; // 饭卡列表 }; int main() { Canteen canteen; // 添加饭卡 Card card1("张三", 1001, 100); Card card2("李四", 1002, 200); canteen.addCard(card1); canteen.addCard(card2); // 充值 canteen.deposit(1001, 50); cout << "张三的余额为:" << canteen.withdraw(1001, 80) << endl; // 消费 cout << "李四的余额为:" << canteen.withdraw(1002, 50) << endl; // 删除饭卡 canteen.removeCard(1001); return 0; } ``` 在上面的代码中,我们定义了一个 `Card` 类和一个 `Canteen` 类。`Card` 类表示饭卡,包括持卡人姓名、卡号和余额等属性,以及充值和消费等方法。`Canteen` 类表示饭堂,包括饭卡列表和添加、删除、充值和消费等方法。 在 `main` 函数中,我们首先创建了一个 `Canteen` 对象,然后添加了两张饭卡,分别属于张三和李四。接着我们对张三的饭卡进行了充值和消费操作,并输出了余额。最后我们删除了张三的饭卡
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值