Skip to content

Commit

Permalink
17~25
Browse files Browse the repository at this point in the history
  • Loading branch information
허기홍 authored and 허기홍 committed Aug 2, 2015
1 parent c286016 commit a92c885
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 17.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>

// issue : static field initialization
class Entity {
public:
static int nextSerialNo;
int serialNo;
Entity() {
serialNo = nextSerialNo++;
}
int GetSerialNo() {
return serialNo;
}
static int GetNextSerialNo() {
return nextSerialNo;
}
static void SetNextSerialNo(int value) {
nextSerialNo = value;
}
};

using namespace std;

int Entity::nextSerialNo = 0;

int main() {
Entity::SetNextSerialNo(1000);
Entity *e1 = new Entity();
Entity *e2 = new Entity();
cout << e1->GetSerialNo() << endl; // 1000
cout << e2->GetSerialNo() << endl; // 1001
cout << Entity::GetNextSerialNo() << endl; //1002
}
29 changes: 29 additions & 0 deletions 17.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

class Entity {
static int nextSerialNo;
int serialNo;
public Entity() {
serialNo = nextSerialNo++;
}
public int GetSerialNo() {
return serialNo;
}
public static int GetNextSerialNo() {
return nextSerialNo;
}
public static void SetNextSerialNo(int value) {
nextSerialNo = value;
}
}

class Test {
static void Main() {
Entity.SetNextSerialNo(1000);
Entity e1 = new Entity();
Entity e2 = new Entity();
Console.WriteLine(e1.GetSerialNo()); // 1000
Console.WriteLine(e2.GetSerialNo()); // 1001
Console.WriteLine(Entity.GetNextSerialNo()); //1002
}
}
80 changes: 80 additions & 0 deletions 18.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
#include <map>

using namespace std;

// issue : virtual function initialization
// issue : public inheritance
// issue : libirary hashtble

class Expression{
public:
virtual double Evaluate (map<string,double> *vars) = 0;
};

class Constant : public Expression{
double value;
public:
Constant(double value){
this->value = value;
}
double Evaluate(map<string,double> *vars){
return value;
}
};

class VariableReference: public Expression{
string name;
public :
VariableReference(string name){
this->name = name;
}
double Evaluate(map<string,double> *vars){
double value = (*vars)[name];
return value;
}
};

class Operation: public Expression {
Expression* left;
char op;
Expression* right;
public:
Operation(Expression* left, char op, Expression* right) {
this->left = left;
this->op = op;
this->right = right;
}

double Evaluate(map<string,double> *vars) {
double x = left->Evaluate(vars);
double y = right->Evaluate(vars);
switch (op) {
case '+': return x + y;
case '-': return x - y;
case '*': return x * y;
case '/': return x / y;
}
throw "Unknown operator";
}
};

int main(){
Expression *e = new Operation(
new VariableReference("x"),
'*',
new Operation(
new VariableReference("y"),
'+',
new Constant(2)
)
);
map<string,double> *vars = new map<string,double>();
(*vars)["x"] = 3;
(*vars)["y"] = 5;
cout << e->Evaluate(vars) << endl;
(*vars)["x"] = 1.5;
(*vars)["y"] = 9;
cout << e->Evaluate(vars) << endl;
}

74 changes: 74 additions & 0 deletions 18.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections;

public abstract class Expression {
public abstract double Evaluate(Hashtable vars);
}

public class Constant: Expression {
double value;
public Constant(double value) {
this.value = value;
}
public override double Evaluate(Hashtable vars) {
return value;
}
}

public class VariableReference: Expression{
string name;
public VariableReference(string name) {
this.name = name;
}
public override double Evaluate(Hashtable vars) {
object value = vars[name];
if (value == null) {
throw new Exception("Unknown variable: " + name); }
return Convert.ToDouble(value);
}
}

public class Operation: Expression {
Expression left;
char op;
Expression right;

public Operation(Expression left, char op, Expression right) {
this.left = left;
this.op = op;
this.right = right;
}

public override double Evaluate(Hashtable vars) {
double x = left.Evaluate(vars);
double y = right.Evaluate(vars);
switch (op) {
case '+': return x + y;
case '-': return x - y;
case '*': return x * y;
case '/': return x / y;
}
throw new Exception("Unknown operator");
}
}

class Test {
static void Main() {
Expression e = new Operation(
new VariableReference("x"),
'*',
new Operation(
new VariableReference("y"),
'+',
new Constant(2)
)
);
Hashtable vars = new Hashtable();
vars["x"] = 3;
vars["y"] = 5;
Console.WriteLine(e.Evaluate(vars));
vars["x"] = 1.5;
vars["y"] = 9;
Console.WriteLine(e.Evaluate(vars));
}
}
26 changes: 26 additions & 0 deletions 20.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <list>
#include <vector>
using namespace std;

// Issue : object class, main class, main function
class Test {
public:
static void F() { cout << ("F()") << endl; }
// static void F(object x) { cout << ("F(object)") << endl; }
static void F(int x) { cout << ("F(int)") << endl; }
static void F(double x) { cout << ("F(double)") << endl; }
template<typename T> static void F(T x) { cout << ("F<T>(T)") << endl; }
static void F(double x, double y) { cout << ("F(double, double)") << endl; }
};

int main(){
Test::F();
Test::F(1);
Test::F(1.0);
Test::F("abc");
Test::F((double)1);
// Test::F((object)1);
Test::F<int>(1);
Test::F(1, 1);
}
22 changes: 22 additions & 0 deletions 20.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;

// overloading
class Test {
static void F() { Console.WriteLine("F()"); }
static void F(object x) { Console.WriteLine("F(object)"); }
static void F(int x) { Console.WriteLine("F(int)"); }
static void F(double x) { Console.WriteLine("F(double)"); }
static void F<T>(T x) { Console.WriteLine("F<T>(T)"); }
static void F(double x, double y) { Console.WriteLine("F(double, double)"); }
static void Main() {
F();
F(1);
F(1.0);
F("abc");
F((double)1);
F((object)1);
F<int>(1);
F(1, 1);
}
}
24 changes: 24 additions & 0 deletions 23.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
#include <list>
#include <vector>
using namespace std;

// issue: list library (operator [])
// issue: string library (ToUpper)
int main(){
// list<string> *names = new list<string>();
vector<string> *names = new vector<string>();
names->push_back("Liz");
names->push_back("Martha");
names->push_back("Beth");

locale loc;
for(int i = 0; i < names->size(); i++){
string s = (*names)[i];
for (string::iterator p = s.begin(); s.end() != p; ++p)
*p = toupper(*p);
(*names)[i] = s;
cout << (*names)[i] << endl;
}
}

16 changes: 16 additions & 0 deletions 23.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;

class Test {
static void Main() {
List<string> names = new List<string>();
names.Add("Liz");
names.Add("Martha");
names.Add("Beth");
for(int i = 0; i < names.Count; i++){
string s = names[i];
names[i] = s.ToUpper();
Console.WriteLine(names[i]);
}
}
}
16 changes: 16 additions & 0 deletions 25.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <list>

using namespace std;

int main (){
list<int>* a = new list<int>();
a->push_back(1);
a->push_back(2);
list<int>* b = new list<int>();
b->push_back(1);
b->push_back(2);
cout << (a == b) << endl;
b->push_back(3);
cout << (a == b) << endl;
}
16 changes: 16 additions & 0 deletions 25.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;

class Test {
static void Main() {
List<int> a = new List<int>();
a.Add(1);
a.Add(2);
List<int> b = new List<int>();
b.Add(1);
b.Add(2);
Console.WriteLine(a == b); // The manual says "true", Mono says "false"
b.Add(3);
Console.WriteLine(a == b); // false
}
}

0 comments on commit a92c885

Please sign in to comment.