#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <fstream>
using namespace std;
class eCash
{
public:
eCash(){
Money = 0;
}
void store(){
cin >> m;
if(m < 0){
cout << "eCash: 請輸入大餘0的金額" << endl;
} else {
Money = Money + m;
cout << "eCash: 您存了" << m << "元" << endl;
}
}
void pay(){
cin >> m;
if(m < 0){
cout << "eCash: 請輸入大餘0的金額" << endl;
} else {
if(m > Money){
cout << "eCash: 您的錢不夠" << endl;
} else {
Money = Money - m;
cout << "eCash: 您花了" << m << "元" << endl;
}
}
}
void display(){
cout << "eCash: 您尚有"<< Money << "元" << endl;
}
private:
int Money;
int m;
char str[80];
};
int main(){
eCash p1;
int flog = 1;
char c, str[80];
cout << "===歡迎使用eCash===" << endl;
cout << "eCash: 請輸入您的帳號: ";
cin >> str ;
cout << "eCash: 帳號不存在,第一次使用!\n" << endl;
cout << str <<"您好,請選擇項目:" << endl;
cout << "s: 儲值\np: 消費\nd: 查詢餘額\nq: 離開\n" << endl ;
while(flog != 0){
cin >> c;
if(c == 's'){cout << "請輸入儲存金額:" << endl;
p1.store();
}
if(c == 'p'){cout << "請輸入消費金額:" << endl;
p1.pay();
}
if(c == 'd'){
p1.display();
}
if(c == 'q'){
cout << "eCash: 帳號登出,已存檔!\n謝謝 , Bye Bye!" << endl;
break;
}
}
return 0;
}
主程式(eCash.cpp):
#include <iostream>
#include <fstream>
#include <map>
#include <string>
using namespace std;
template<class K, class V>
class eCash{
public:map<K,V> data;
public:void load(map<string, int> &data) {
ifstream file;
file.open("eCash.txt");
if(file.is_open()) {
while(file.good()) {
string account;
int money;
file >> account;
file >> money;
data[account] = money;
}
file.close();
}
else {
cout << "[!]系統文件遺失: eCash.txt" << endl;
exit(EXIT_FAILURE);
}
}
public:void refresh(map<string, int> &data) {
ofstream file;
file.open("eCash.txt");
if(file.is_open()) {
for(const auto& x : data) {
file << x.first << " " << x.second << " ";
}
file.close();
}
else {
cout << "[!]系統文件遺失: eCash.txt" << endl;
exit(EXIT_FAILURE);
}
}
};
int main() {
eCash<string, int> database;
map<string, int>::iterator iter;
string account;
database.load(database.data);
cout << "=== 歡迎使用eCash ===" << endl;
while(true) {
cout << "eCash: 請輸入你的帳號: ";
cin >> account;
iter = database.data.find(account);
if(iter == database.data.end()) {
cout << "查無資料!" << endl;
cout << "正在建立..." << endl;
database.data[account] = 0;
cout << "建立完成!" << endl << endl;
}
else {
cout << "帳號開啟完成!";
int flag = 1;
while(flag) {
char select;
cout << account << "您好,請選擇項目:" << endl;
cout << "s: 儲值" << endl;
cout << "p: 消費" << endl;
cout << "d: 查詢餘額" << endl;
cout << "q: 離開" << endl;
cout << "> ";
cin >> select;
switch(select) {
int money;
case 's':
{
cin >> money;
if(money < 0)
cout << "eCash: 請輸入大餘0的金額" << endl << endl;
else {
database.data[account] += money;
cout << "eCash: 您存了" << money << "元" << endl << endl;
}
break;
}
case 'p':
{
cin >> money;
if(money < 0){
cout << "eCash: 請輸入大餘0的金額" << endl << endl;
} else {
if(money > database.data[account]){
cout << "eCash: 您的錢不夠" << endl << endl;
}
else {
database.data[account] -= money;
cout << "eCash: 您花了" << money << "元" << endl << endl;
}
}
break;
}
case 'd':
cout << "eCash: 您尚有: " << iter->second << "元" << endl << endl;
break;
default:
{
flag = 0;
cout << endl;
break;
}
}
database.refresh(database.data);
}
}
}
}
額外檔案(eCash.txt):
admin -1
附註:eCash.txt 一定得先有一筆資料。
可以參考 std::fstream - C++ Reference 其中的用法範例
std::fstream fs("filename", std::fstream::in | std::fstream::out);
/* 對 fs 操作 */
fs.close();