1. 程式人生 > >在map中儲存struct,及map的檔案輸入輸出

在map中儲存struct,及map的檔案輸入輸出

用函式的方法實現:

從map輸出到檔案,map的value是struct

#include<map>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

struct studentID {
        string name;
        string address;
};

studentID MakeStudentID(const string name, const string address)
{
        studentID c={name, address};
        return c;
}

int main()
{
        int number[2]={49,21};
        map<int, studentID> studentID_table;
        studentID_table.insert(make_pair(number[0],MakeStudentID("Tom","Dalian")));
        studentID_table.insert(make_pair(number[1],MakeStudentID("Lee","Shenyangi")));
        ofstream outFile("newfile.txt");
        map<int, studentID>::const_iterator map_it = studentID_table.begin();
        while(map_it!= studentID_table.end())
        {
                outFile << map_it->first<<endl;
                outFile << map_it->second.name<<endl;
                outFile << map_it->second.address<<endl;
                map_it++;
        }
        return 0;
        outFile.close();
}

用構造constructor的方法:

#include<map>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

struct studentID {
        studentID(): name(),address() {}
        studentID(string newname, string newaddress): name(newname), address(newaddress) {}
        string name;
        string address;
};

 
int main()
{
        int number[2]={49,21};
        map<int, studentID> studentID_table;
        studentID_table.insert(make_pair(number[0],studentID("Tom","Dalian")));
        studentID_table.insert(make_pair(number[1],studentID("Lee","Shenyangi")));
        ofstream outFile("newfile.txt");
        map<int, studentID>::const_iterator map_it = studentID_table.begin();
        while(map_it!= studentID_table.end())
        {
                outFile << map_it->first<<endl;
                outFile << map_it->second.name<<endl;
                outFile << map_it->second.address<<endl;
                map_it++;
        }
        return 0;
        outFile.close();
}

其中關於向map中插入值一段:

        int number[2]={49,21};
        map<int, studentID> studentID_table;
        studentID_table.insert(make_pair(number[0],studentID("Tom","Dalian")));
        studentID_table.insert(make_pair(number[1],studentID("Lee","Shenyangi")));

等價於:


        map<int, studentID> studentID_table;
        studentID_table[49]=studentID("Tom","Dalian");
        studentID_table[21]=studentID("Lee","Shenyangi");

輸出結果如下

-bash-4.1$ more newfile.txt
21
Lee
Shenyangi
49
Tom
Dalian

不管建立map時哪個學號先建立,順序輸出時永遠是按照key值由小到大排列的,這是map資料結構本身的優點

從檔案輸入:

#include<map>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

struct studentID {
        studentID(): name(),address() {}
        studentID(string newname, string newaddress): name(newname), address(newaddress) {}
        string name;
        string address;
};

int main()
{
        map<int, studentID> studentID_table;
        int key;
        string temp_name, temp_address;
        ifstream inFile("newfile.txt");
        while(!inFile.eof())
        {
                inFile >> key >> temp_name >> temp_address;
                studentID_table[key]= studentID(temp_name,temp_address);

        }

        inFile.close();
        map<int, studentID>::iterator map_it = studentID_table.begin();

        for( map_it = studentID_table.begin(); map_it != studentID_table.end(); map_it++) {
                cout << "number:" <<endl;
                cout <<  map_it->first << endl;
                cout << "the name is" << endl;
                cout << map_it->second.name<<endl;
                cout << "the address is"<< endl;
                cout << map_it->second.address<<endl;
        }
        return 0;

}

查詢key是否存在在map中,如果存在就輸出,如果不存在什麼也不做

#include<map>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;


struct studentID {
        studentID(): name(),address() {}
        studentID(string newname, string newaddress): name(newname), address(newaddress) {}
        string name;
        string address;
};


int main()
{
        map<int, studentID> studentID_table;
        int key;
        string temp_name, temp_address;
        ifstream inFile("newfile.txt");
        while(!inFile.eof())
        {
                inFile >> key >> temp_name >> temp_address;
                studentID_table[key]= studentID(temp_name,temp_address);
        }


        inFile.close();
        map <int, studentID>::iterator map_it;
        if((map_it=studentID_table.find(66))!= studentID_table.end()) {
                cout << studentID_table.find(66)->second.name << studentID_table.find(66)->second.address << endl;
        }
        return 0;

}

題目要求:

Test Program

(24-2-2012)

BANKING SYSTEM PROJECT

Description: This C++ programs on BANKINGSYSTEM has account class with data members like account number. name, deposit,withdraw amount and type of account. Customer data is read from file (Createyour own file format) and add them to customer class attributes. A customer candeposit and withdraw amount in his account. All the fields must be accessedthrough setters and getters.

Output should look like:

Main Menu

1. New Account

2. Deposit amount

3. WithDraw amount

4. Exit

Select your option: 1

%%%%%%%%%New Entry Account Form%%%%%%%%%

Enter the account number: 92837

Enter Account holder Name: pi*og2

Enter type of account: savings

Enter Initial amount: 500

Your account created successfully...

我的檔案:

bankaccountInfo.txt

bank.cc

bank.hh

main.cc

bankaccountInfo.txt:

2012
John
savings
1000
3264
Lauri
savigns
3000
92837
prog2
savings
0

bank.hh:

-bash-4.1$ more bank.hh
#ifndef BANK_HH
#define BANK_HH

#include<iostream>
#include<fstream>
#include<string>
#include<map>
using namespace std;

const string filename = "bankaccountInfo.txt";

struct accountinfo {
accountinfo():  name(), type_account(), amount(0){}
accountinfo(string newname, string newtype, double newamount): name(newname), ty
pe_account(newtype), amount(newamount) {}
string name;
string type_account;
double amount;
};

class Bank {
public:
void newAccount();
void depositAmount();
void withdrawAmount();
Bank();
private:
int account_number;
void sendtoFile();
map <int,accountinfo> customers;
};

#endif

bank.cc:

#include "bank.hh"

Bank::Bank(void)
{
        ifstream inFile(filename.c_str());
        int key;
        string temp_name, temp_type_account;
        double temp_amount;
        while(!inFile.eof()) {
                inFile >> key >> temp_name >> temp_type_account >> temp_amount;
                customers[key] = accountinfo(temp_name, temp_type_account, temp_amount);
        }
        inFile.close();
}

void Bank::sendtoFile(void)
{
        ofstream outFile;
        outFile.open(filename.c_str());
        map <int,accountinfo>::iterator map_it;
        for (map_it = customers.begin(); map_it != customers.end(); map_it++) {
                outFile << map_it->first << endl;
                outFile << map_it -> second.name << endl;
                outFile << map_it -> second.type_account << endl;
                outFile << map_it -> second.amount << endl;
        }
        outFile.close();
}

void Bank::newAccount(void)
{
        int someone_account_number;
        cout << "%%%%%%%%%New Entry Account Form%%%%%%%%%\n";
        cout << "Enter the account number: ";
        cin >> someone_account_number;
        map <int,accountinfo>::iterator map_it;
        map_it = customers.find(someone_account_number);
        if(map_it != customers.end())
                cout << "the account number already existed\nfail to create account\n";
        else {
                int key = someone_account_number;
                string temp_name, temp_type_account;
                double temp_amount;
                cout << "\nEnter Account holder Name: ";
                cin >> temp_name;
                cout << "\nEnter type of account: ";
                cin >> temp_type_account;
                cout << "\nEnter Initial amount: ";
                cin >> temp_amount;
                cout << endl;
                customers[key] = accountinfo(temp_name, temp_type_account, temp_amount);
                sendtoFile();
                cout << "Your account created successfully...\n";
        }
}

void Bank::depositAmount(){
        int key;
        double deposit_amount;
        cout << "%%%%%%%%%Deposit Amount%%%%%%%%%\n";
        cout << "Enter the account number: ";
        cin >> key;
        map <int,accountinfo>::iterator map_it;
        map_it = customers.find(key);
        if(map_it == customers.end())
                cout << "No such account\n";
        else {
                cout << "\nEnter deposit amount: ";
                cin >> deposit_amount;
                map_it->second.amount += deposit_amount;
                sendtoFile();
                cout << "deposit successfully now you have " << map_it->second.amount << " in the bank\n";
        }
}

void Bank::withdrawAmount()
{
        int key;
        int loop_index = 1;
        double withdraw_amount;
        cout << "%%%%%%%%%Deposit Amount%%%%%%%%%\n";
        cout << "Enter the account number: ";
        cin >> key;
        map <int,accountinfo>::iterator map_it;
        map_it = customers.find(key);
        if(map_it == customers.end())
                cout << "No such account\n";
        else {
                do {
                        cout << "you have " << map_it->second.amount << " in the bank\nEnter withdraw amount: ";
                        cin >> withdraw_amount;
                        if (withdraw_amount <= map_it->second.amount) {
                                map_it->second.amount -= withdraw_amount;
                                sendtoFile();
                                cout << "\nwithdraw successfully now you have " << map_it->second.amount << " in the bank\n";
                                loop_index = 0;
                        }

                        else {
                                cout << "you don't have enough balance\n";
                        }
                 } while(loop_index);
        }
}

main.cc:

-bash-4.1$ more main.cc
#include "bank.hh"
#include <cstdlib>

using namespace std;


int main(void)
{
        int choice;
        Bank accountOperation;
        cout << "Main Menu\n1. New Account\n2. Deposit amount\n3. WithDraw amount\n4. Exit\n";
        cout << "Select your opetion: ";
        cin >> choice;
        cout << endl;
        switch(choice) {
        case 1:
                accountOperation.newAccount();
                break;
        case 2:
                accountOperation.depositAmount();
                break;
        case 3:
                accountOperation.withdrawAmount();
                break;
        case 4:
                exit(EXIT_SUCCESS);
        }
        return 0;
}