Skip to content

Commit

Permalink
c++ completed.
Browse files Browse the repository at this point in the history
  • Loading branch information
RryLee committed Mar 7, 2016
1 parent b6e8d99 commit 0976a14
Show file tree
Hide file tree
Showing 37 changed files with 1,456 additions and 3 deletions.
21 changes: 21 additions & 0 deletions C++/Classes/1-c-tutorial-struct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <string>

using namespace std;

struct Student
{
int age;
string first_name;
string last_name;
int standard;
};

int main() {
Student st;

cin >> st.age >> st.first_name >> st.last_name >> st.standard;
cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;

return 0;
}
70 changes: 70 additions & 0 deletions C++/Classes/2-c-tutorial-class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

class Student
{
public:
int age;
int standard;
string first_name;
string last_name;

void set_age(int age) {
this->age = age;
}
void set_standard(int standard) {
this->standard = standard;
}
void set_first_name(string first_name) {
this->first_name = first_name;
}
void set_last_name(string last_name) {
this->last_name = last_name;
}

int get_age() {
return age;
}
int get_standard() {
return standard;
}
string get_first_name() {
return first_name;
}
string get_last_name() {
return last_name;
}
string to_string() {
stringstream a, s;
a << age;
string age_str = a.str();
s << standard;
string standard_str = s.str();

return age_str + "," + first_name + "," + last_name + "," + standard_str;
}
};

int main() {
int age, standard;
string first_name, last_name;

cin >> age >> first_name >> last_name >> standard;

Student st;
st.set_age(age);
st.set_standard(standard);
st.set_first_name(first_name);
st.set_last_name(last_name);

cout << st.get_age() << "\n";
cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
cout << st.get_standard() << "\n";
cout << "\n";
cout << st.to_string();

return 0;
}
55 changes: 55 additions & 0 deletions C++/Classes/3-classes-objects.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

class Student
{
public:

int scores[5];

void input() {
for (int i = 0; i < 5; ++i) {
scanf("%d", &scores[i]);
}
}

int calculateTotalScore() {
int sum = 0;
for (int i = 0; i < 5; ++i) {
sum += scores[i];
}

return sum;
}
};

int main() {
int n; // number of students
cin >> n;
Student *s = new Student[n]; // an array of n students

for(int i = 0; i < n; i++){
s[i].input();
}

// calculate kristen's score
int kristen_score = s[0].calculateTotalScore();

// determine how many students scored higher than kristen
int count = 0;
for(int i = 1; i < n; i++){
int total = s[i].calculateTotalScore();
if(total > kristen_score){
count++;
}
}

// print result
cout << count;

return 0;
}
66 changes: 66 additions & 0 deletions C++/Classes/4-c-class-templates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

template <typename T>
class AddElements
{
public:
AddElements(const T& element):
element {element} {}

T add(const T& other)
{
return element + other;
}

private:
T element;
};

template <>
class AddElements<string>
{
public:
AddElements(const string& element):
element {element} {}

string concatenate(const string& other)
{
return element + other;
}
private:
string element;
};

int main () {
int n,i;
cin >> n;
for(i=0;i<n;i++) {
string type;
cin >> type;
if(type=="float") {
double element1,element2;
cin >> element1 >> element2;
AddElements<double> myfloat (element1);
cout << myfloat.add(element2) << endl;
}
else if(type == "int") {
int element1, element2;
cin >> element1 >> element2;
AddElements<int> myint (element1);
cout << myint.add(element2) << endl;
}
else if(type == "string") {
string element1, element2;
cin >> element1 >> element2;
AddElements<string> mystring (element1);
cout << mystring.concatenate(element2) << endl;
}
}
return 0;
}
131 changes: 131 additions & 0 deletions C++/Classes/5-box-it.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int BoxesCreated,BoxesDestroyed;

// Box class
class Box
{
public:
Box() {
length = 0;
breadth = 0;
height = 0;
BoxesCreated ++;
}
Box(int l, int b, int h) {
length = l;
breadth = b;
height = h;
BoxesCreated ++;
}
Box(const Box &b) {
length = b.length;
breadth = b.breadth;
height = b.height;
BoxesCreated ++;
}
~Box() {
BoxesDestroyed ++;
}

int getLength() {
return length;
}
int getBreadth() {
return breadth;
}
int getHeight() {
return height;
}
long long CalculateVolume() {
long long volume = 1;
volume = (long long)length * (long long)breadth * (long long)height;

return volume;
}

bool operator<(Box &b) {
if (length < b.getLength()) {
return true;
}
if (length == b.getLength() && breadth < b.getBreadth()) {
return true;
}
if (length == b.getLength() && breadth == b.getBreadth() && height < b.getHeight()) {
return true;
}

return false;
}

private:
int length;
int breadth;
int height;
};

ostream& operator<<(ostream& out, Box B) {
return out << B.getLength() << " " << B.getBreadth() << " " << B.getHeight();
}

void check2()
{
int n;
cin>>n;
Box temp;
for(int i=0;i<n;i++)
{
int type;
cin>>type;
if(type ==1)
{
cout<<temp<<endl;
}
if(type == 2)
{
int l,b,h;
cin>>l>>b>>h;
Box NewBox(l,b,h);
temp=NewBox;
cout<<temp<<endl;
}
if(type==3)
{
int l,b,h;
cin>>l>>b>>h;
Box NewBox(l,b,h);
if(NewBox<temp)
{
cout<<"Lesser"<<endl;
}
else
{
cout<<"Greater"<<endl;
}
}
if(type==4)
{
cout<<temp.CalculateVolume()<<endl;
}
if(type==5)
{
Box NewBox(temp);
cout<<NewBox<<endl;
}

}
}

int main()
{
BoxesCreated = 0;
BoxesDestroyed = 0;
check2();
cout<<"Boxes Created : "<<BoxesCreated<<endl<<"Boxes Destroyed : "<<BoxesDestroyed<<endl;
}
Loading

0 comments on commit 0976a14

Please sign in to comment.