Пример #1
0
int main()
{
    DisplayOpeningScreen("Firstname:Lastname:A00123456:csc34201",
        "Submission 05: Building and Displaying Power Sets");
    cout << "\nThis program prompts for an integer value n and builds "
        "the set\n{1, 2, 3, ..., n}. It then calls a (recursive) function "
        "powerSet()\nthat returns the power set of the set. The power set "
        "is the set of\nall subsets of the set and has 2^n elements. "
        "After displaying the\nsize of the power set, the program calls "
        "the function WriteAllSubsets()\nthat outputs each subset using "
        "the notation {e_1, e_2, ..., e_m},\nfour sets per line.\n\n";
    Pause();

    int n;
    ReadInt("Enter a non-negative integer "
    	"(<= 6 if you don't want a lot of output): ", n);

    set<int> sInt; //Set of integers whose power set we construct
    for (int i=1; i<=n; i++) sInt.insert(i); //Build set {1,2,3, ..., n}

    set<set<int>> pSet = powerSet(sInt); //Build the power set

    //Output number of subsets, followed by all subsets, four per line
    cout << "\nNumber of subsets = " << pSet.size()
        << ", and here they are:\n";
    WritePowerSet(pSet);
    Pause();
}
Пример #2
0
//option 6
void AddNewMember
    (
    fstream& ioFile,
    bool& successful
    )
{
    if(!ioFile.is_open())
    {
        cout << "\nBinary file not yet open.\nReturning to menu." << endl;
        Pause();
        successful = false;
    }
    else
    {
        ioFile.clear();
        string newMemberName;
        int newMemberAge;
        double newMemberBalance;
        //ask for info for the new club member
        ReadThisLine("Enter name of new member: ",newMemberName);
        ReadInt("Enter age of new member: ", newMemberAge);
        ReadDouble("Enter opening balance of new member: ",newMemberBalance);
        //clear the array
        memset(member.name,'\0',31);
        for(unsigned int i = 0 ; i < newMemberName.length(); i++)
            member.name[i] = newMemberName[i];
        //store the info into the struc
        member.age = newMemberAge;
        member.balance = newMemberBalance;
        ioFile.seekg(0, ios::end);
        //write the struc into the binary file
        ioFile.write((char *)&member, sizeof(ClubMember));
        ioFile.seekg(0);
        successful = true;
    }
}
Пример #3
0
//option 7
void UpdateMemberData
    (
    fstream& ioFile,
    bool& successful
    )
{
    //store the position of the stream(later to be used for seekg/p)
    streamoff position;
    ioFile.clear();
    int count = 0;
    if(!ioFile.is_open())
    {
        cout << "\nBinary file not yet open.\nReturning to menu." << endl;
        Pause();
        successful = false;
    }
    else
    {
        string identity;
        ReadThisLine("Enter enough of the member's name to "
            "identify him/her: ", identity);
        //start at beginning
        ioFile.seekg(0);
        while(!ioFile.eof())
        {
            //make a temporary position to hold the beginning of the struc
            streamoff tempPos = ioFile.tellg();
            //read the binary file
            ioFile.read((char *)&member, sizeof(ClubMember));
            if(!ioFile.eof())
            {
                if(((string)member.name).find(identity) != string::npos)
                {
                    //put the beginning of the struc into the position
                    position = tempPos;
                    //print club member
                    cout << "====================\n" << member.name << endl;
                    cout << "Age: " << member.age << "  " << "Balance: $"
                        << member.balance << endl;
                    count++;
                }
            }
        }
        if(count != 1)
        {
            cout << "\n=====>Either more than one member, or no members, "
                "matched your input.\n=====>You need to identify a "
                "unique club member to update.\n=====>Try again with "
                "more specific input.\n" << endl;
            Pause();
            successful = false;
        }
        if(count == 1)
        {
            cout << "\nUpdating data for the above club member." << endl;
            string choice;
            do
            {
                cout << "Enter n/a/b to update name/age/balance, or "
                    "q to quit updating this member: ";
                getline(cin,choice);
                if(choice == "q")
                {
                    cout << "\nOK, no more updates for this club member "
                        "at this time." << endl;
                    Pause();
                    successful = true;
                }
                if(choice == "n")
                {
                    string revisedName;
                    ReadThisLine("Enter revised name: ", revisedName);
                    //clear the array
                    memset(member.name,'\0',31);
                    for(unsigned int i = 0 ; i < revisedName.length(); i++)
                        //store the new name into the struc
                        member.name[i] = revisedName[i];
                }
                if(choice == "a")
                {
                    int newAge;
                    ReadInt("Enter revised age: ", newAge);
                    //store new age in struc
                    member.age = newAge;
                }
                if(choice == "b")
                {
                    double newBalance;
                    ReadDouble("Enter revised balance: ", newBalance);
                    //store new balance in struc
                    member.balance = newBalance;
                }
            }
            while(choice != "q");
        }
        ioFile.clear();
        //go to the position 
        ioFile.seekg(position);
        ioFile.seekp(position);
        //finally write the info back into the binary file
        ioFile.write((char *)&member, sizeof(ClubMember));
        successful = true;
    }
}