Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c++ solutions for ques 6, 7, 8, 9, 10, 11, 12 for chap 17 #132

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions c++/Chapter 17/Question17_10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <memory>

using namespace std;

/** Attribute class */
class Attribute {

private:
string tag;
string value;

public:
Attribute(); // def cons
Attribute(string, string); // cons
~Attribute(); // destructor

/** getters */
string get_tag() { return tag; }

string get_value() { return value; }

/** other functions */
string get_tag_code();
};

/** Attribute constructor and functions */
Attribute::Attribute() { }

Attribute::Attribute(string tag, string value) {
this->tag = tag;
this->value = value;
}

Attribute::~Attribute() { }

string Attribute::get_tag_code() {
if (this->tag == "family")
return "1";
else if (this->tag == "person")
return "2";
else if (this->tag == "firstname")
return "3";
else if (this->tag == "lastname")
return "4";
else if (this->tag == "state")
return "5";
else
return "--";
}

/** Element Class */
class Element {

private:
string name;
string value;
vector<Attribute> attributes;
vector<Element> children;

public:
Element(); // def cons
Element(string); // cons 1
Element(string, string); // cons 2

~Element(); // des

/** getters */
string get_name() { return name; }

string get_value() { return value; }

vector<Attribute> get_attributes() { return attributes; }

vector<Element> get_children() { return children; }

/** other functions */
string get_name_code();
void insert_Att(Attribute att) {
attributes.push_back(att);
}
void insert_Elem(Element elem) {
children.push_back(elem);
}
};

/** Element constructor and functions */
Element::Element() { }

Element::Element(string name) {
this->name = name;
}

Element::Element(string name, string value) {
this->name = name;
this->value = value;
}

Element::~Element() { }

string Element::get_name_code() {
if (this->name == "family")
return "1";
else if (this->name == "person")
return "2";
else if (this->name == "firstname")
return "3";
else if (this->name == "lastname")
return "4";
else if (this->name == "state")
return "5";
else
return "--";
}

/* Function declarations */
void encode(Element root, string& str);
void encode(Attribute attr, string& str);
void encode(string str1, string& str2);
string encode_xml_to_str(Element& root);

/** Common functions definitions */

void encode(Element root, string& str) {

/* Append the root's name code */
encode(root.get_name_code(), str);

/* Recurse over the attributes */
for (auto& attr : root.get_attributes()) {
encode(attr, str);
}

encode("0", str); // this is the end of the element hence append as '0'

/* Recurse over the child Elements */
if (root.get_value() != "") {
encode(root.get_value(), str);
}
else {
for (auto& child : root.get_children()) {
encode(child, str);
}
}
encode("0", str);
}

void encode(Attribute attr, string& str) {
encode(attr.get_tag_code(), str);
encode(attr.get_value(), str);
}

void encode(string str1, string& str2) {
str2 += str1 + " ";
}

string encode_xml_to_str(Element& root) {
string str = "";
encode(root, str);
return str;
}

/****** MAIN FUNCTION ******/

int main() {

/** Forming the XML */
Element root("family");
Attribute a1("lastname", "McDowell");
Attribute a2("state", "CA");
root.insert_Att(a1);
root.insert_Att(a2);

Element child("person", "Some Message");
Attribute a3("firstname", "Gayle");
child.insert_Att(a3);
root.insert_Elem(child);

/** Calling the encode xml function */
string res = encode_xml_to_str(root);

cout << res << endl;

return 0;
}

/*

Commands to run
----------------

g++ -std=c++11 Question17_10.cpp -o Question17_10
./Question17_10

*/
63 changes: 63 additions & 0 deletions c++/Chapter 17/Question17_11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <iostream>
#include <stdlib.h>

using namespace std;

#define NUMBER_5 5

int rand5();
int rand7();
int rand7_alternate();

int rand5() {
return (rand() % NUMBER_5);
}

int rand7() {
while (true) {
int num = 5 * rand5() + rand5();
if (num < 21) {
return num % 7;
}
}
}

int rand7_alternate() {
while (true) {
int r1 = 2 * rand5();
int r2 = rand5();
if (r2 != 4) {
int rand1 = r2 % 2;
int num = r1 + rand1;
if (num < 7)
return num;
}
}
}

int main() {

cout << "\n--------------- Using Method rand7() ---------------\n" << endl;
for (int i=0; i<10; i++) {
cout << rand7() << ", ";
}
cout << endl;

cout << "\n--------------- Using Method rand7_alternate() ---------------\n" << endl;
for (int i=0; i<10; i++) {
cout << rand7_alternate() << ", ";
}
cout << endl;

return 0;
}

/*

Commands to run
----------------

g++ -std=c++11 Question17_11.cpp -o Question17_11
./Question17_11

*/
50 changes: 50 additions & 0 deletions c++/Chapter 17/Question17_12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void pair_sum(vector<int>, int);

void pair_sum(vector<int> a, int target_sum) {

/** Sort the vector */
sort(a.begin(), a.end());

int left = 0, right = a.size()-1, curr_sum = 0;

while (left < right) {
curr_sum = a[left] + a[right];
if (curr_sum == target_sum) {
cout << "Found Pair : {" << a[left] << ", " << a[right] << "}" << endl;
left++;
right--;
}
else if (curr_sum > target_sum) {
right--;
}
else {
left++;
}
}
}

int main() {

int arr[] = {9, 3, 6, 5, 7, -1, 13, 14, -2, 12, 0};
int size = sizeof(arr)/sizeof(*arr);
vector<int> ar(arr, arr+size);
int target_sum = 12;

pair_sum(ar, target_sum);
}

/*

Commands to run
----------------

g++ -std=c++11 Question17_12.cpp -o Question17_12
./Question17_12

*/
Loading