Spring 2020

PIC 10B: Intermediate Programming

Discussion Section 2A

Wonjun Lee



Week5

Tuesday

Link to the video

Enter password:

About Polymorphism

Solution
#include < iostream >

using namespace std;

class FunnyClass
{
public:
  FunnyClass();
  FunnyClass(double a, double b, double c);

  double get_a() const;
  double get_b() const;
  double get_c() const;

  FunnyClass& operator+=(const FunnyClass& right);
  FunnyClass& operator++(); // ++ a prefix
  FunnyClass  operator++(int unused); // a ++ postfix

  void print() const{
    cout << "(" << a << ", " << b << ", " << c << ")";
  }

  double compare(const FunnyClass& right) const{
    double leftvalue  = a*b - c;
    double rightvalue = right.get_a() * right.get_b() - right.get_c();

    return leftvalue - rightvalue;
  }

  // TODO: Finish overloading the rest of the operators
  // FunnyClass& operator-=( ... );
  // FunnyClass& operator--();
  // FunnyClass& operator--(int ... );
private:
  double a;
  double b;
  double c;
};

// b = (1,2,3) c = (2,3,4)
// a = left + right = (3,5,7)
FunnyClass operator+(const FunnyClass& left, const FunnyClass& right){
  FunnyClass result(left.get_a() + right.get_b(), left.get_b() + right.get_b(), left.get_c() + right.get_c());
  return result;
}

// left  = (1,2,3)
// right = (2,3,4)
// -1 > 2
bool operator>(const FunnyClass& left, const FunnyClass& right){
  double value = left.compare(right);
  if(value > 0) return true;
  return false;
}
bool operator>=(const FunnyClass& left, const FunnyClass& right){
  double value = left.compare(right);
  if(value >= 0) return true;
  return false;
}

bool operator<(const FunnyClass& left, const FunnyClass& right){
  double value = left.compare(right);
  if(value < 0) return true;
  return false;
}

bool operator<=(const FunnyClass& left, const FunnyClass& right){
  double value = left.compare(right);
  if(value <= 0) return true;
  return false;
}

FunnyClass::FunnyClass(): a(0), b(0), c(0) {}
FunnyClass::FunnyClass(double a, double b, double c): a(a), b(b), c(c) {}

double FunnyClass::get_a() const{
  return this->a;
}
double FunnyClass::get_b() const{
  return this->b;
}
double FunnyClass::get_c() const{
  return this->c;
}

FunnyClass& FunnyClass::operator+=(const FunnyClass& right){
  a += right.a;
  b += right.b;
  c += right.c;
  return *this;
}

// prefix ++a;
FunnyClass& FunnyClass::operator++(){
  ++a;
  ++b;
  ++c;
  return *this;
}

// postfix a++;
FunnyClass FunnyClass::operator++(int unused){
  FunnyClass clone(a,b,c);
  ++a;
  ++b;
  ++c;
  return clone;
}

ostream& operator<<(ostream& out, const FunnyClass& value){
  out << "(" << value.get_a() << ", " << value.get_b() << ", " << value.get_c() << ")";
  return out;
}

int main(){
  FunnyClass n1(1,2,3); // -1
  FunnyClass n2(2,3,4); // 2

  cout << ++n1 << endl;

  cout << n1 << endl;

}

Thursday

Link to the video

Enter password:

About Streams
Chapter 9 Exercise 6
formula random formula random

database.txt

template.txt

Solution
#include < iostream >
#include < fstream >
#include < vector >
#include < string >

using namespace std;


void save_template(const vector< string >& linevector){
  ifstream tempfile;
  tempfile.open("template.txt");
  if(tempfile.fail()){
    cout << "FAIL TO OPEN" << endl;
  }

  string doc = "";

  char x;
  bool first_bar = false;

  while(tempfile.get(x)){
    if(x == '|' && first_bar == false){
      first_bar = true;
    }else if(x == '|' && first_bar == true){
      first_bar = false;
    }else if(x != '|' && first_bar == true){
      // replace by the linevector
      int ind = (int) x - '0'; // this only works for single digit
      doc += linevector[ind-1];
    }else{
      doc += x;
    }
  }

  tempfile.close()

  ofstream outfile;
  outfile.open("JUNK " + linevector[1] + ".txt");

  outfile << doc;
  outfile.close();
}

void run_database(ifstream& infile){
  string line;

  while(getline(infile, line)){

    // line = "Mr.|Harry|Hacker|1105 Torre Ave.|Cupertino|CA|95014 "

    vector< string > linevector;

    while(line.find("|") != string::npos){ // if you cannot find "|", then stop the while loop
      linevector.push_back(line.substr(0, line.find("|"))); // Mr.
      line = line.substr(line.find("|")+1); 
    }
    linevector.push_back(line);
    // linevector = {"Mr.", "Harry", ... }

    save_template(linevector);
  }

}

int main(){

  ifstream infile;
  infile.open("database.txt");

  if(infile.fail()){
    cout << "FAIL to open" << endl;
    return 1;
  }

  run_database(infile);

  infile.close();
}