Code From Class 2026/04/22

Problem 1

#include<iostream>
#include "ssm.cpp"

int main(){
    ssm::set<long> A;
    A.insert(91); A.insert(95); A.insert(5); A.insert(51); A.insert(44);
    A.insert(33); A.insert(5);
    std::cout<<"The size of the set is: "<<A.size()<<"\n";
    A.erase(7);
    std::cout<<"Erased 7, which was not in the set. The size is "<<A.size()<<"\n";
    A.erase(51);
    std::cout<<"Erased 51. The size is "<<A.size()<<"\n";
    std::cout<<"The smallest element is "<<A[0]<<"\n";
    std::cout<<"The next smallest is "<<A[1]<<"\n";
    std::cout<<"The next is "<<A[2]<<"\n";
    std::cout<<"Then, we have "<<A[3]<<"\n";
    std::cout<<"The last is "<<A[4]<<"\n";
    return 0;
}

Problem 2

// Create a program that reads words from user input until the user 
// gives the word "end"
// At that time the input is over.
// Print all the words that the user entered in alphabetical order.
// Then, the user gives another word x.
// Determine whether this word is in the set. If it is, print its position.
// Position 0 means the alphabetically first word.
// Position 1 means alphabeticall second word ...

#include<iostream>
#include "ssm.cpp"
int main(){
    ssm::set<std::string> A;
    std::string userInput;
    std::cin>>userInput;
    while(userInput!="end"){
        A.insert(userInput);
        std::cin>>userInput;
    }
    std::cout<<"Printing all of the words.\n";
    for(long i=0;i<A.size();++i){
        std::cout<<A[i]<<" ";
    }
    std::cout<<"\n";
    std::string x;
    std::cout<<"Give me the word x.\n";
    std::cin>>x;
    long index;
    index=A.find(x);
    if(index==-1){
        std::cout<<"Not found.\n";
    }
    else{
        std::cout<<"The position of the word "<<x<<" is: ";
        std::cout<<index<<"\n";
    }
    return 0;
}

Problem 3

// The user input consists of positive integer. 
// The input ends with a negative integer.
// For each element of the input, print how many times it 
// appears.

#include<iostream>
#include"ssm.cpp"
class ExtendedInteger{
public:
    long content;
    long counter;
    ExtendedInteger();
    int operator<(const ExtendedInteger& ) const;
};
ExtendedInteger::ExtendedInteger(){counter=0;}
int ExtendedInteger::operator<(const ExtendedInteger& b) const{
    if(content < b.content){return 1;}
    return 0;
}
int main(){
    ssm::set<ExtendedInteger> A;
    long uI;
    std::cin>>uI;
    ExtendedInteger tmp;
    long index;
    while(uI>0){
        tmp.content=uI;
        index=A.find(tmp);
        if(index==-1){ 
            tmp.counter=1;
            A.insert(tmp);
        }
        else{
            tmp=A[index];//This reasures that tmp.counter 
            // is exactly the same as the counter that 
            // appears in the set A
            ++tmp.counter;
            //Now we need to re-insert this updated 
            // extended integer tmp.
            // However, insert is impossible, because tmp
            // is already in the set.
            A.erase(tmp);
            A.insert(tmp);
        }
        std::cin>>uI;
    }
    for(long i=0;i<A.size();++i){
        std::cout<<A[i].content<<" appears "<<A[i].counter<<" times.\n";
    }
    return 0;
}