講義メモ

・p.81「add01.cs」から再開します。

p.81 算術演算子(再掲載)

・すでに学習した2項+演算子、2項-演算子、2項*演算子、2項/演算子で加減乗除算が可能
・加えて、2項%演算子(剰余算 p.84)、単項++演算子(インクリメント p.85)、単項--演算子(デクリメント p.85)がある
・2項+演算子は加算と結合の2つの演算が可能で、これを演算子のオーバーロード(多重定義)という
・2項+演算子は、2項の両方が加算可能であれば加算し、どちらかまたは両方が文字列ならば連結する

p.81 add01.cs

//p.81 add01.cs
using System;
class add01
{
    public static void Main()
    {
        Console.WriteLine(3 + 6); //加算になり9
        Console.WriteLine(3.0 + 6); //加算になり9.0だが表示は9
        Console.WriteLine("3.5" + 6); //連結になり"3.56"
        Console.WriteLine(3.5 + "6"); //連結になり"3.56"
        Console.WriteLine(); //改行
        Console.WriteLine("(3.0 + 6)の型は{0}", (3.0 + 6).GetType()); //9.0だからDouble型
        Console.WriteLine("文字列3.5 + intの6の型は{0}", ("3.5" + 6).GetType()); //"3.56"だからString型
        Console.WriteLine("doubleの3.5 + 文字列6の型は{0}", (3.5 + "6").GetType()); //同上
    }
}

p.82(型の異なる数値型の加算)

・基本的に範囲の大きい方の型に合わせてから加算する。
 例:double d = 3.14; int i = 5; d = i + d; とすると内部的には d = (double)i + d; となる
・ただし、decimal型は例外で、誤差が前提であるdouble/float型からの変換は不可

p.83(整数除算)

・2項/演算子による除算は整数型にも実数型にも対応しているが「整数 / 整数」は整数商(小数点以下切捨て)なので注意

p.83 division01.cs

//p.83 division01.cs
using System;
class division01
{
    public static void Main()
    {
        Console.WriteLine("10 / 3 = {0}", 10 / 3); //3.33なので小数点以下切捨てで3
        Console.WriteLine("(10 / 3)の型は{0}", (10 / 3).GetType()); //整数(int)なのでInt32
        Console.WriteLine("10 / 3.0 = {0}", 10 / 3.0); //3.33がそのまま表示
        Console.WriteLine("(10 / 3.0)の型は{0}", (10 / 3.0).GetType()); //実数(double)なのでDouble
    }
}

p.84 剰余演算子

・2項%演算子で、C#では実数除算にも対応している(C,C++,Java等では非対応)
・式「a / b = c … d」は「a = b * c + d」であることを示す
 例: 13.53÷2.5=5…1.03 なので 13.53 % 2.5 = 1.03 であり、13.53 = 2.5 * 5 + 1.03

p.84 mod01.cs

//p.84 mod01.cs
using System;
class mod01
{
    public static void Main()
    {
        Console.WriteLine("10 % 3 = {0}", 10 % 3); //10÷3=3…1 なので 1
        Console.WriteLine("13.53 % 2 = {0}", 13.53 % 2); //13.53÷2=6…1.53 なので 1.53
        Console.WriteLine("13.53 % 2.5 = {0}", 13.53 % 2.5); //13.53÷2.5=5…1.03 なので 1.03
    }
}

アレンジ演習:p.84 mod01.cs

・金種計算を行おう
・画面から金額を整数で受け取り(int.Parseで整数化)
・100円玉、50円玉、10円玉、5円玉、1円玉で各何枚かを表示したい。
・ただし、合計枚数が最小になること
ヒント
・①100円玉の枚数:入力金額÷100の整数商
・② 50円玉の枚数:(入力金額ー100×①)÷50の整数商
・③ 10円玉の枚数:(入力金額ー100×①ー50×②)÷10の整数商
・④  5円玉の枚数:(入力金額ー100×①ー50×②ー10×③)÷5の整数商
・⑤  1円玉の枚数:入力金額を5で割った余り

作成例 その1

//p.84 mod01.cs
using System;
class mod01
{
    public static void Main()
    {
        Console.Write("金額:"); int n = int.Parse(Console.ReadLine());
        int x100 = n / 100;
        int  x50 = (n - x100 * 100) / 50;
        int  x10 = (n - x100 * 100 - x50 * 50) / 10;
        int   x5 = (n - x100 * 100 - x50 * 50 - x10 * 10) / 5;
        Console.WriteLine("100円玉 = {0}枚", x100);
        Console.WriteLine(" 50円玉 = {0}枚", x50);
        Console.WriteLine(" 10円玉 = {0}枚", x10);
        Console.WriteLine("  5円玉 = {0}枚", x5);
        Console.WriteLine("  1円玉 = {0}枚", n % 5);
    }
}

作成例 その2

//p.84 mod01.cs
using System;
class mod01
{
    public static void Main()
    {
        Console.Write("金額:"); int n = int.Parse(Console.ReadLine());
        int sum = 0; //払い出し済みの金額
        int x100 =  n        / 100; sum =      x100 * 100;
        int  x50 = (n - sum) /  50; sum = sum + x50 *  50;
        int  x10 = (n - sum) /  10; sum = sum + x10 *  10;
        int   x5 = (n - sum) /   5; sum = sum +  x5 *   5;
        Console.WriteLine("100円玉 = {0}枚", x100);
        Console.WriteLine(" 50円玉 = {0}枚", x50);
        Console.WriteLine(" 10円玉 = {0}枚", x10);
        Console.WriteLine("  5円玉 = {0}枚", x5);
        Console.WriteLine("  1円玉 = {0}枚", n % 5);
    }
}

作成例 その3

//p.84 mod01.cs
using System;
class mod01
{
    public static void Main()
    {
        Console.Write("金額:"); int n = int.Parse(Console.ReadLine());
        Console.WriteLine("100円玉 = {0}枚", n       / 100);
        Console.WriteLine(" 50円玉 = {0}枚", n % 100 /  50);
        Console.WriteLine(" 10円玉 = {0}枚", n %  50 /  10);
        Console.WriteLine("  5円玉 = {0}枚", n %  10 /   5);
        Console.WriteLine("  1円玉 = {0}枚", n %   5);
    }
}

p.84 インクリメント、デクリメント演算子

・「変数の値に1を加える」ことが多いので「a = a + 1」を「a++」と記述できる
・この「++」をインクリメントという。
・同様に、「a = a - 1」を「a--」と記述でき、「--」をデクリメントという
・インクリメント、デクリメント演算子には前置と後置があり、単独で行うと同じ結果になる
 例: int a = 5; a++; // aは6になる
 例: int a = 5; ++a; // aは6になる
・しかし、評価と加算の順序は異なり、前置は先に加算してから評価、後置は評価してから加算する
 例: int a = 5; Console.Write(a++); // aは6になる、表示は加算前の5
 例: int a = 5; Console.Write(++a); // aは6になる、表示は加算後の6

アレンジ演習:p.86 increment01.cs

・aを2回後置でインクリメントすると、aの値はどうなるか確認しよう
・bを2回前置でインクリメントすると、bの値はどうなるか確認しよう

作成例

//アレンジ演習:p.86 increment01.cs
using System;
class increment01
{
    public static void Main()
    {
        int a = 10;
        Console.WriteLine("1回目で{0}, 2回目で{1}", a++, a++); //aは12になる。「1回目で10, 2回目で11」と表示
        Console.WriteLine(a);   //12
        int b = 10;
        Console.WriteLine("1回目で{0}, 2回目で{1}", ++b, ++b); //bは12になる。「1回目で11, 2回目で12」と表示
        Console.WriteLine(b);   //12
    }
}

p.87 increment02.cs

//p.87 increment02.cs
using System;
class increment02
{
    public static void Main()
    {
        double a = 1.25;
        decimal d = -12.3M;
        Console.WriteLine(++a); //2.25になる
        Console.WriteLine(--d); //-13.3Mになる(表示は-13.3)
    }
}

アレンジ演習:p.87 increment02.cs

・int m = int.MaxValue に対して、++m するとどうなるか確認しよう
・uint u = 0; に対して、--u するとどうなるか確認しよう

作成例

//アレンジ演習:p.87 increment02.cs
using System;
class increment02
{
    public static void Main()
    {
        double a = 1.25;
        decimal d = -12.3M;
        Console.WriteLine(++a); //2.25になる
        Console.WriteLine(--d); //-13.3Mになる(表示は-13.3)
        int m = int.MaxValue; //【以下追加】
        uint u = 0;
        Console.WriteLine(++m); //-2147483648(int.MinValue)になる
        Console.WriteLine(--u); //4294967295(uint.MaxValue)になる
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *