不同进制之间的转换的各种方法

不同进制之间的转换纯粹是数学上的计算。不过,你不必担心会有么复杂,无非是乘或除的计算。
生活中其实很多地方的计数方法都多少有点不同进制的影子。

比如我们最常用的10进制,其实起源于人有10个指头。如果我们的祖先始终没有摆脱手脚不分的境况,我想我们现在一定是在使用20进制。

至于二进制……没有袜子称为0只袜子,有一只袜子称为1只袜子,但若有两袜子,则我们常说的是:1双袜子。

生活中还有:七进制,比如星期。十二进制,比如“一打”,六十进制,比如分钟……

一、为什么需要八进制和十六进制?

编程中,我们常用的还是10进制……必竟VB是高级语言。
比如:a = 99;
不过,由于数据在计算机中的表示,最终以二进制的形式存在,所以有时候使用二进制,可以更直观地解决问题。但是二进制数太长了。比如int 类型占用4个字节,32位。比如100,用int类型的二进制数表达将是:0000 0000 0000 0000 0110 0100
面对这么长的数进行思考或操作,没有人会喜欢。因此,C,C++ 没有提供在代码直接写二进制数的方法。
 
用16进制或8进制可以解决这个问题。因为,进制越大,数的表达长度也就越短。不过,为什么偏偏是16或8进制,而不其它的,诸如9或20进制呢?

2、8、16,分别是2的1次方,3次方,4次方。这一点使得三种进制之间可以非常直接地互相转换。8进制或16进制缩短了二进制数,但保持了二进制数的表达特点。在下面的关于进制转换的课程中,你可以发现这一点。 阅读全文…

C#解决数学问题

在VF看见的一个题目:
HOHO,占字符……

计算:1/2×3+1/3×4+1/4×5+.....1/99×100

写了个基本程序,不过无法实现分数显示结果

using System;
using System.Collections.Generic;
using System.Text;

namespace Solve_1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2;
            int b = 3;
            float ans = 0;
            while (b < = 100)
            {
                float num1 = (float)a;
                float num2 = (float)b;
                ans =ans + 1 / num1 * num2;
                a++;
                b++;
             }
             Console.WriteLine(ans);
        }
    }
}

阅读全文…

C#第三章练习3.9.4

第三章的另一个练习
简单的加密

using System;
using System.Collections.Generic;
using System.Text;

namespace Chapter03Exercise394
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("What do you want to do? Encrypt(E) Or Crack(C)");
            string m = Console.ReadLine();
            if (m == "E" || m == "e")
            {
                Console.WriteLine("Please input what you want to encrypt");
                string str1 = Console.ReadLine();
                int count1 = str1.Length;
                for (int i = 0; i < count1; i++)
                {
                    char temp1 = str1[i];
                    int pass = (int)temp1;
                    pass = pass + 5;
                    char temp2 = (char)pass;
                    Console.Write(temp2);
                }
                Console.ReadLine();
            }

            else if (m == "C" || m == "c")
            {
                Console.WriteLine("Please input what you want to crack");
                string str2 = Console.ReadLine();
                int count2 = str2.Length;
                for (int i = 0; i < count2; i++)
                {
                    char temp1 = str2[i];
                    int pass = (int)temp1;
                    pass = pass - 5;
                    char temp2 = (char)pass;
                    Console.Write(temp2);
                }
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("Input Error!!!");
                Console.ReadLine();
            }
        }
    }
}

C#第三章练习3.9.3

练习,自己研究的
希望DX多多指点!

using System;
using System.Collections.Generic;
using System.Text;

namespace Chapter03Exercise393
{
    class Exercise3_9_3
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Input:");
            String str = Console.ReadLine();
            String str2 = str;
            String[] str1 = str.Split(new char[] { '+', '-', '*', '/' });
            string temp = str1[0];
            char mark = Convert.ToChar(str[temp.Length]);
            double ans = 0;
            double num1 = Convert.ToDouble(str1[0]);
            double num2 = Convert.ToDouble(str1[1]);
            switch (mark)
            {
                case '+': ans = num1 + num2;
                    break;
                case '-': ans = num1 - num2;
                    break;
                case '*': ans = num1 * num2;
                    break;
                case '/': ans = num1 / num2;
                    break;
                default: ans = 0;
                    break;
            }
            Console.WriteLine(str2 + "=" + ans);
            Console.ReadLine();
        }
    }
}

C#第三章学习札记

还是程序,自己参照书上例子写了一些。
结合原来学过的PASCAL,还是学得比较轻松的!

using System;
using System.Collections.Generic;
using System.Text;

namespace TestCSharpChapter03
{
    class Test
    {
        enum Name { ghSky, MicroTonyu, Lei, Pig };
        static void Main(string[] args)
        {
            /*--------------------------------------------------
            // Test1 -- a-- or --a
            int a = 10;
            a--;
            Console.WriteLine("a--:{0}",a);

            Console.WriteLine("WriteLine(a--):{0}", a--);
            Console.WriteLine();

            // Test2 -- enum
            Name name1 = Name.MicroTonyu;
            Name name2 = Name.Lei;
            Console.WriteLine("{0} Love {1}",name1,name2);
            ----------------------------------------------------*/

            /*---------------------------------------------------
            //Test3 -- Array
            int[] array1 = new int[10];
            for (int i = 0; i < array1.Length; i++)
                array1[i] = i;
            ShowArray1("array1", array1);

            int[,] array2 = new int[5, 5];
            int count = 1;
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                    array2[i, j] = count++;
            }
            ShowArray2("array2", array2);
            ----------------------------------------------------*/

            /*---------------------------------------------------
            //Test4 -- CloneArray
            int[] array = new int[10];
            int[] arrayClone2=new int[10];
            ShowArray1("array", array);
            int[] arrayPointer = array;
            int[] arrayClone = (int[])array.Clone();
            for (int i = 0; i < 10; i++)
                arrayClone2[i] = array[i];
            array[0] = 100;
            array[9] = 100;
            Console.WriteLine("--------------------------- After Modify ---------------------------");
            ShowArray1("array", array);
            ShowArray1("arrayPointer", arrayPointer);
            ShowArray1("arrayClone", arrayClone);
            ShowArray1("arrayClone2", arrayClone2);
            ---------------------------------------------------*/

            //Test5 -- SortArray
            int[] array = new int[10];
            Random r = new Random();
            for (int i = 0; i < 10; i++)
                array[i] = r.Next(0, 100);
            ShowArray1("array", array);
            Sort(array);
            Console.WriteLine("------------------------------- After Sort -------------------------------");
            ShowArray1("array", array);

        }

        static void ShowArray1(string arrayName, int[] array)
        {
            Console.Write(arrayName + ": ");
            foreach (int n in array)
                Console.Write(n + " ");
            Console.WriteLine();
        }

        static void ShowArray2(string arrayName, int[,] array)
        {
            Console.Write(arrayName + ":");
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                    Console.Write(" " + array[i, j]);
            }
            Console.WriteLine();
        }

        static void Sort(int[] array)
        {
            for (int i=0; i<10; i++)
                for (int j = 9; j > i; j--)
                {
                    if (array[j - 1] < array[j])
                    {
                        int temp = array[j - 1];
                        array[j - 1] = array[j];
                        array[j] = temp;
                    }
                }
        }
    }

}
Page 1 Of 212