開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487312-194-7)の 第7章(クラスとオブジェクト)6.8(練習問題)練習7-2.を解いてみる。
その他参考書籍
練習7-2.
コード
using System;
class Math
{
public static double Add(double a, double b)
{
return a + b;
}
public static double Substract(double a, double b)
{
return a - b;
}
public static double Multiply(double a, double b)
{
return a * b;
}
public static double Divide(double a, double b)
{
return a / b;
}
}
class Tester
{
public void Run()
{
// Math m = new Math();
double a = 5, b = 6;
Console.WriteLine("{0}, {1}", a, b);
Console.WriteLine("和: {0}", Math.Add(a, b));
Console.WriteLine("差: {0}", Math.Substract(a, b));
Console.WriteLine("積: {0}", Math.Multiply(a, b));
Console.WriteLine("商: {0}", Math.Divide(a, b));
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
5, 6 和: 11 差: -1 積: 30 商: 0.833333333333333 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(BBEdit)
var MyMath = function( ) {},
a = 5,
b = 6,
result = a + ", " + b + "\n",
m;
MyMath.add = function(a,b){
return a + b;
};
MyMath.subtract = function( a, b ) {
return a - b;
};
MyMath.multiply = function( a, b ) {
return a * b;
};
MyMath.divide = function ( a, b ) {
return a / b;
};
// m = new MyMath();
result += [MyMath.add(a, b), MyMath.subtract(a, b), MyMath.multiply(a, b), MyMath.divide(a, b)].join("\n");
$('#pre0').text(result);
pythonの場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
class Math:
@staticmethod
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
# m = Math()
a = 5
b = 6
print("{0}, {1}".format(a, b))
for func in [Math.add, Math.subtract, Math.multiply, Math.divide]:
print(func(a, b))
入出力結果(Terminal)
$ ./sample.py 5, 6 11 -1 30 0.8333333333333334 $
0 コメント:
コメントを投稿