Taras9

by pif on December 28th, 2009
No notes
Syntax: C++
Show lines - Hide lines - Show in textbox - Download
/* 
 * File:   main.cpp
 * Author: pif
 *
 * 1.Використати контейнер map або multimap для зберігання списку передплатних 
 * видань (назва, місце видання, передплатний індекс, вартість). 
 * 
 * 
 * пошук всіх елементів із заданим ключем; 
 * вставка нових елементів в контейнер; 
 * видалення елементів із заданим ключем.
 *
 *
 * Created on 22 грудня 2009, 14:18
 */
 
#include <iostream>
#include <map>
#include <string>
#include <fstream>
using namespace std;
 
struct magazine
{
    string name;
    string place;
    double price;
};
/*
 * 
 */
int main(int argc, char** argv)
{
    map<int, magazine> m;
    map<int, magazine>::iterator it;
    ifstream ff("in.txt");
    int n;
    ff>>n;
    for(int i=0;i<n;++i)
    {
        magazine value;
        int key;
        ff>>key;
        ff>>value.name>>value.place>>value.price;
        m.insert(make_pair(key,value));
    }
 
    for(it=m.begin();it!=m.end();++it)
    {
        cout<<"Index: "<<(*it).first<<endl;
        cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
    }
 
 
    while(true)
    {
        int c=0;
        cout<<"Input command #: "<<endl;
        cout<<"1 - Search by key"<<endl<<"2 - Search by value"<<endl<<"3 - Insert new Magazine"<<endl<<"4 - Delete by key"<<endl<<"5 - Delete by value"<<endl<<"6 - Output all magazines"<<endl;
        cin>>c;
        int k=0,type=0;double dd=0;magazine value;
        string v="", n="";
        switch(c)
        {
            case 1:
                cout<<"Enter key: ";
                cin>>k;
                it=m.find(k);
                if(it!=m.end())
                {
                    cout<<"Index: "<<(*it).first<<endl;
                    cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
                }
                break;
            case 2:
                cout<<"Which parameter do you want to search: "<<endl<<"1 - Name"<<endl<<"2 - Place"<<endl<<"3 - Price"<<endl;
                cin>>type;
                switch(type)
                {
                    case 1: 
                        cin>>n;
                        for(it=m.begin();it!=m.end();++it)
                            if((*it).second.name == n)
                            {
                                cout<<"Index: "<<(*it).first<<endl;
                                cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
                            }
                        break;
                    case 2:
                        cin>>n;
                        for(it=m.begin();it!=m.end();++it)
                            if((*it).second.place==n)
                            {
                                cout<<"Index: "<<(*it).first<<endl;
                                cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
                            }
                        break;
                    case 3:
                        cin>>dd;
                        for(it=m.begin();it!=m.end();++it)
                            if((*it).second.price==dd)
                            {
                                cout<<"Index: "<<(*it).first<<endl;
                                cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
                            }                        
                        break;
                }
                break;
            case 3:
                cout<<"Enter index, name, place, price of magazine: "<<endl;
 
                int key;
                cin>>key>>value.name>>value.place>>value.price;
                m.insert(make_pair(key,value));
                break;
            case 4:
                cout<<"Enter key: ";
                cin>>k;
                it=m.find(k);
                if(it!=m.end()) m.erase(it);
                break;
            case 5:
                cout<<"Which parameter do you want to search & delete: "<<endl<<"1 - Name"<<endl<<"2 - Place"<<endl<<"3 - Price"<<endl;
                cin>>type;
                switch(type)
                {
                    case 1:
                        cin>>n;
                        for(it=m.begin();it!=m.end();++it)
                            if((*it).second.name==n)
                                m.erase(it);
                        break;
                    case 2:
                        cin>>n;
                        for(it=m.begin();it!=m.end();++it)
                            if((*it).second.place==n)
                                m.erase(it);
                        break;
                    case 3:
                        cin>>dd;
                        for(it=m.begin();it!=m.end();++it)
                            if((*it).second.price==dd)
                                m.erase(it);
                        break;
                }
                break;
            case 6:
                for(it=m.begin();it!=m.end();++it)
                {
                    cout<<"Index: "<<(*it).first<<endl;
                    cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
                }
                break;
            default: return 0;
        }
    }
 
 
    ff.close();
    return 0;
}
 
 

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS