编程之道2

来源:互联网 发布:快速格式化数据恢复 编辑:程序博客网 时间:2024/05/01 14:37

using System;

namespace _02_01

{

       class Class_02_01

       {

              public static void Main(String[] args)

              {

                     short x = 32766;

                     ushort y = 65534;

                     x++;

                     y++;

                     Console.WriteLine("x = {0}", x);

                     Console.WriteLine("y = {0}", y);

                     x++;

                     y++;

                     Console.WriteLine("x = {0}", x);

                     Console.WriteLine("y = {0}", y);

              }

       }

}

using System;

 

namespace _02_02

{

       class Class_02_02

       {

              public static void Main(String[] args)

              {

                     bool x = true;

                     bool y = false;

                     Console.WriteLine("x = {0}", x);

                     Console.WriteLine("y = {0}", y);

                     x = !x;

                     y = !y;

                  Console.WriteLine("x = {0}", x);

                     Console.WriteLine("y = {0}", y);

              }

       }

using System;

 

namespace _02_03

{

       class Class_02_03

       {

              public static void Main(String[] args)

              {

                     float x = 3.14159F;

                     double y = 3.14159265358979;

 

                     Console.WriteLine("x = {0}", x);

                     Console.WriteLine("y = {0}", y);

 

                     float t = x;

                     x = (float)y;

                     y = t;

 

                     Console.WriteLine("x = {0}", x);

                     Console.WriteLine("y = {0}", y);

              }

       }

}

using System;

 

namespace _02_04

{

       class Class_02_04

       {

              public static void Main(String[] args)

              {

                     decimal x = 3.14159265358979m;

                     double y = 3.14159265358979;

                     Console.WriteLine("x = {0}", x);

                     Console.WriteLine("y = {0}", y);

                     x = 5 * x;

                     y = 5 * y;

                     Console.WriteLine("x = {0}", x);

                     Console.WriteLine("y = {0}", y);

                     x = x / 15;

                     y = y / 15;

                     Console.WriteLine("x = {0}", x);

                     Console.WriteLine("y = {0}", y);

              }

       }

}

using System;

namespace _02_05

{

       class Class_02_05

       {

              public static void Main(String[] args)

              {

                     char x = 'A';

                     char y = '';

                     Console.WriteLine("x = {0}", x);

                     Console.WriteLine("y = {0}", y);

              }

       }

}

using System;

 

namespace _02_06

{

       struct Fraction

       {

              public int numerator;

              public int denominator;

       }

 

       class Class_02_06

       {

              public static void Main(String[] args)

              {

                     Fraction x;

                     x.numerator = 1;

                     x.denominator = 2;

                     Fraction y = x;

 

                     Console.WriteLine("x = {0}/{1}", x.numerator, x.denominator);

                     Console.WriteLine("y = {0}/{1}", y.numerator, y.denominator);

 

                     x.numerator = 3;

                     y.denominator = 3;

 

                     Console.WriteLine("x = {0}/{1}", x.numerator, x.denominator);

                     Console.WriteLine("y = {0}/{1}", y.numerator, y.denominator);

              }

       }

}

sing System;

 

namespace _02_07

{

       struct Fraction

       {

              public int numerator;

              public int denominator;

       }

      

       struct ComplexFraction

       {

              public Fraction numerator;

              public int denominator;

       }

 

       class Class_02_07

       {

              public static void Main(String[] args)

              {

                     ComplexFraction x;

                     x.numerator.numerator= 1;

                     x.numerator.denominator = 2;

                     x.denominator = 3;

 

                     Console.WriteLine("x = {0}/{1} / {2}", x.numerator.numerator,

                     x.numerator.denominator, x.denominator);

              }

       }

}

using System;

 

namespace _02_08

{

       class Fraction

       {

              public int numerator;

              public int denominator;

       }

 

       class Class_02_08

       {

              public static void Main(String[] args)

              {

                     Fraction x = new Fraction();

                     x.numerator = 1;

                     x.denominator = 2;

                     Fraction y = x;

 

                     Console.WriteLine("x = {0}/{1}", x.numerator, x.denominator);

                     Console.WriteLine("y = {0}/{1}", y.numerator, y.denominator);

 

                     x.numerator = 3;

 

                     Console.WriteLine("x = {0}/{1}", x.numerator, x.denominator);

                     Console.WriteLine("y = {0}/{1}", y.numerator, y.denominator);

 

                     y.denominator = 3;

 

                     Console.WriteLine("x = {0}/{1}", x.numerator, x.denominator);

                     Console.WriteLine("y = {0}/{1}", y.numerator, y.denominator);

              }

       }

}

using System;

 

namespace _02_09

{

       class Class_02_09

       {

              public static void Main(String[] args)

              {

                     int x = 10;

                     Console.WriteLine("x = {0}", x);

                     x += 5;

                     Console.WriteLine("x = {0}", x);

                     x -= 2;

                     Console.WriteLine("x = {0}", x);

                     x *= 10;

                     Console.WriteLine("x = {0}", x);

                     x /= 8;

                     Console.WriteLine("x = {0}", x);

              }

       }

}

using System;

 

namespace _02_10

{

       class Class_02_10

       {

              public static void Main(String[] args)

              {

                     int x, y, z, w, u;

 

                     x = 17 + 5;

                     y = 17 - 5;

                     z = 17 * 5;

                     w = 17 / 5;

                     u = 17 % 5;

 

                     Console.WriteLine("x = {0}", x);

                     Console.WriteLine("y = {0}", y);

                     Console.WriteLine("z = {0}", z);

                     Console.WriteLine("w = {0}", w);

                     Console.WriteLine("u = {0}", u);

 

                     double x1, y1, z1, w1, u1;

 

                     x1 = 18.5 + 3.5;

                     y1 = 18.5 - 3.5;

                     z1 = 18.5 * 3.5;

                     w1 = 18.5 / 3.5;

                     u1 = 18.5 % 3.5;

 

                     Console.WriteLine("x1 = {0}", x1);

                     Console.WriteLine("y1 = {0}", y1);

                     Console.WriteLine("z1 = {0}", z1);

                     Console.WriteLine("w1 = {0}", w1);

                     Console.WriteLine("u1 = {0}", u1);

              }

       }

}

using System;

 

namespace _02_11

{

       class Class_02_11

       {

              public static void Main(String[] args)

              {

                     bool x;

                     int y = 10, z = 3;

 

                     x = (y == z);

                     Console.WriteLine("x = {0}", x);

                     x = (y != z);

                     Console.WriteLine("x = {0}", x);

                     x = (y > z);

                     Console.WriteLine("x = {0}", x);

                     x = (y < z * 4);

                     Console.WriteLine("x = {0}", x);

                     x = (y >= z * 3);

                     Console.WriteLine("x = {0}", x);

                     x = (y <= z + 7);

                     Console.WriteLine("x = {0}", x);

              }

       }

using System;

 

namespace _02_12

{

       class Class_02_12

       {

              public static void Main(String[] args)

              {

                     bool x;

 

                     x = (true && false);

                     Console.WriteLine("x = {0}", x);

                     x = (false || true);

                     Console.WriteLine("x = {0}", x);

                     x = (!false);

                     Console.WriteLine("x = {0}", x);

 

                     int y = 0x000f0005, z = 0x00f0f003;

                     Console.WriteLine("y = 0x{0:x8}, z = 0x{1:x8}", y, z);

                     y = ~y;

                     z = ~z;

                     Console.WriteLine("y = 0x{0:x8}, z = 0x{1:x8}", y, z);

                     y = y & z;

                     Console.WriteLine("y = 0x{0:x8}, z = 0x{1:x8}", y, z);

                     y = y | z;

                     Console.WriteLine("y = 0x{0:x8}, z = 0x{1:x8}", y, z);

                     y = y ^ z;

                     Console.WriteLine("y = 0x{0:x8}, z = 0x{1:x8}", y, z);

              }

       }

}

sing System;

 

namespace _02_13

{

       class Class_02_13

       {

              public static void Main(String[] args)

              {

                     uint x = 8, y = 127, z = 65535;

                     Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z);

 

                     x = z >> 2;

                     y = z << 16;

                     z = z << 17;

                     Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z);

              }

       }

}

using System;

 

namespace _02_14

{

       class Class_02_14

       {

              public static void Main(String[] args)

              {

                     int x = 5, y = 6, z = 7;

                     bool w, u, v;

                     Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z);

                     x = y = z;

                     Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z);

 

                     y = 2 + x * 10;

                     z = (2 + x) * 10;

                     Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z);

 

                     y = ++x * 5;

                     z = 5 + 13 % 5 * 3;

                     Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z);

 

                     w = 1 + 1 == 2 && 2 + 2 == 4;

                     u = false || true ^ false;

                     v = false && !u;

                     Console.WriteLine("w = {0}, u = {1}, v = {2}", w, u, v);

              }

       }

}

using System;

 

namespace _02_15

{

       class Class_02_15

       {

              public static void Main(String[] args)

              {

                     int iMark;

                     string sTemp;

 

                     sTemp = Console.ReadLine();

                     iMark = Int32.Parse(sTemp);

 

                     if(iMark == 100)

                     {

                            Console.WriteLine("满分");

                     }

                     else if(iMark >= 85)

                     {

                            Console.WriteLine("优秀");

                     }

                     else if(iMark >= 60)

                     {

                            Console.WriteLine("通过");

                     }

                     else

                     {

                            Console.WriteLine("未通过");

                     }

              }

       }

}

using System;

 

namespace _02_16

{

       class Class_02_16

       {

              public static void Main(String[] args)

              {

                     int iMark;

                     string sTemp;

 

                     sTemp = Console.ReadLine();

                     iMark = Int32.Parse(sTemp);

 

                     if(iMark > 100 || iMark < 0)

                     {

                            Console.WriteLine("不可能的分数");                       

                     }

                     else if(iMark == 100)

                     {

                            Console.WriteLine("满分");

                     }

                     else if(iMark >= 85)

                     {

                            Console.WriteLine("优秀");

                     }

                     else if(iMark >= 60)

                     {

                            Console.WriteLine("通过");

                     }

                     else

                     {

                            Console.WriteLine("未通过");

                     }

              }

       }

}

using System;

 

namespace _02_17

{

       class Class_02_17

       {

              public static void Main(String[] args)

              {

                     string sFruit;

 

                     Console.WriteLine("你喜欢吃什么水果?");

                     sFruit = Console.ReadLine();

 

                     switch(sFruit)

                     {

                            case "Apple":

                                   Console.WriteLine("我是一个苹果,果果果果果果……");

                                   break;

                            case "Pineapple":

                                   Console.WriteLine("我是一个菠萝,萝萝萝萝萝萝……");

                                   break;

                            case "Strawberry":

                                   Console.WriteLine("我是一个草莓,莓莓莓莓莓莓……");

                                   break;

                            default:

                                   Console.WriteLine("我不知道这是什么……");

                                   break;

                     }

              }

       }

}

using System;

 

namespace _02_18

{

       class Class_02_18

       {

              public static void Main(String[] args)

              {

                     string sTemp;

                     int iCount;

 

                     Console.WriteLine("请输入一个数字: ");

                     sTemp = Console.ReadLine();

                     iCount = Int32.Parse(sTemp);

                    

                     while(iCount-- > 0)

                     {

                            Console.Write("* ");

                     }

              }

       }

}

using System;

 

namespace _02_19

{

       class Class_02_19

       {

              public static void Main(String[] args)

              {

                     string sInput;

                     int i = 0;

 

                     do

                     {

                            ++i;

                            Console.WriteLine("{0}次循环,需要退出么?", i);

                            sInput = Console.ReadLine();

                     }while(sInput != "y");

              }

       }

}

using System;

 

namespace _02_20

{

       class Class_02_20

       {

              public static void Main(String[] args)

              {

                     string sTemp;

                     int iCount;

 

                     Console.WriteLine("请输入一个数字: ");

                     sTemp = Console.ReadLine();

                     iCount = Int32.Parse(sTemp);

                    

                     for(int i = 0; i < iCount; ++i)

                     {

                            Console.Write("* ");

                     }

              }

       }

}

using System;

 

namespace _02_21

{

       class Class_02_21

       {

              public static void Main(String[] args)

              {

                     string sTemp;

                     int iRow, iColumn;

 

                     Console.WriteLine("请输入行数: ");

                     sTemp = Console.ReadLine();

                     iRow = Int32.Parse(sTemp);

                    

                     Console.WriteLine("请输入列数: ");

                     sTemp = Console.ReadLine();

                     iColumn = Int32.Parse(sTemp);

 

                     for(int i = 0; i < iRow; ++i)

                     {

                            for(int j = 0; j < iColumn; ++j)

                            {

                                   Console.Write("* ");

                            }

                            Console.WriteLine();

                     }

              }

       }

}

using System;

 

namespace _02_22

{

       class Class_02_22

       {

              public static void Main(String[] args)

              {

                     for(int i = 0; i < 10; ++i)

                     {

                            if(i == 5) continue;

 

                            Console.Write("{0} ", i);

                     }

              }

       }

using System;

 

namespace _02_23

{

       class Class_02_23

       {

              public static void Main(String[] args)

              {

                     for(int i = 0; i < 4; ++i)

                     {

                            for(int j = 0; j < 3; ++j)

                            {

                                   if((i == 1 || i == 2) && (j == 2))

                                          continue;

 

                                   Console.Write("* ");

                            }

                            Console.WriteLine();

                     }

              }

       }

}

using System;

 

namespace _02_24

{

       class Class_02_24

       {

              public static void Main(String[] args)

              {

                     string sTemp;

                     int iCount;

 

                     Console.WriteLine("请输入一个数字: ");

                     sTemp = Console.ReadLine();

 

                     try{

                            iCount = Int32.Parse(sTemp);

                           

                            for(int i = 0; i < iCount; ++i)

                            {

                                   Console.Write("* ");

                            }

                     }catch(Exception e)

                     {

                            Console.WriteLine("你输入的'{0}'不是一个有效的整数", sTemp);  

                     }

              }

       }

}

using System;

 

namespace _02_25

{

       class Class_02_25

       {

              public static void Main(String[] args)

              {

                     string sTemp;

                     int iCount = 0;

 

                     Console.WriteLine("请输入一个数字: ");

                     sTemp = Console.ReadLine();

 

                     try{

                            iCount = Int32.Parse(sTemp);

                           

                            for(int i = 0; i < iCount; ++i)

                            {

                                   Console.Write("* ");

                            }

                     }catch(Exception e)

                     {

                            Console.WriteLine("你输入的'{0}'不是一个有效的整数", sTemp);  

                     }finally

                     {

                            Console.WriteLine("输出了{0}个星号", iCount);

                     }

              }

       }

}

using System;

 

namespace _02_26

{

       class Class_02_26

       {

              public static void Main(String[] args)

              {

                     string sTemp;

                     int iNum = new Random().Next() % 100;

                     int iGuess = 0, iCount = 0;

 

                     Console.WriteLine("请猜猜我想到的一个0100之间的数字: ");

                     do

                     {

                            sTemp = Console.ReadLine();

 

                            try{

                                   iGuess = Int32.Parse(sTemp);

                                  

                                   if(iGuess > iNum)

                                   {

                                          Console.WriteLine("太大");

                                   }

                                   else if(iGuess < iNum)

                                   {

                                          Console.WriteLine("太小");                                     

                                   }

                            }catch(Exception e)

                            {

                                   Console.WriteLine("你输入的'{0}'不是一个有效的整数", sTemp);  

                            }finally

                            {

                                   Console.WriteLine("你已经猜测了{0}", ++iCount);

                            }

                     }while(iGuess != iNum);

 

                     Console.WriteLine("恭喜你猜对了,这个数字是{0}", iNum);

              }

       }

}