RSS

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

2007年10月18日 | ghSky | 浏览: 2,872   

2 Comments

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

比如我们最常用的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#解决数学问题

2007年08月06日 | ghSky | 浏览: 840   

3 Comments

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

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

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

下载: solve1.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Solve_1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int a = 2;
  12.             int b = 3;
  13.             float ans = 0;
  14.             while (b < = 100)
  15.             {
  16.                 float num1 = (float)a;
  17.                 float num2 = (float)b;
  18.                 ans =ans + 1 / num1 * num2;
  19.                 a++;
  20.                 b++;
  21.              }
  22.              Console.WriteLine(ans);
  23.         }
  24.     }
  25. }

阅读全文…

C#第三章练习3.9.4

2007年08月06日 | ghSky | 浏览: 763   

0 Comments

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Chapter03Exercise394
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("What do you want to do? Encrypt(E) Or Crack(C)");
  12.             string m = Console.ReadLine();
  13.             if (m == "E" || m == "e")
  14.             {
  15.                 Console.WriteLine("Please input what you want to encrypt");
  16.                 string str1 = Console.ReadLine();
  17.                 int count1 = str1.Length;
  18.                 for (int i = 0; i < count1; i++)
  19.                 {
  20.                     char temp1 = str1[i];
  21.                     int pass = (int)temp1;
  22.                     pass = pass + 5;
  23.                     char temp2 = (char)pass;
  24.                     Console.Write(temp2);
  25.                 }
  26.                 Console.ReadLine();
  27.             }
  28.  
  29.             else if (m == "C" || m == "c")
  30.             {
  31.                 Console.WriteLine("Please input what you want to crack");
  32.                 string str2 = Console.ReadLine();
  33.                 int count2 = str2.Length;
  34.                 for (int i = 0; i < count2; i++)
  35.                 {
  36.                     char temp1 = str2[i];
  37.                     int pass = (int)temp1;
  38.                     pass = pass - 5;
  39.                     char temp2 = (char)pass;
  40.                     Console.Write(temp2);
  41.                 }
  42.                 Console.ReadLine();
  43.             }
  44.             else
  45.             {
  46.                 Console.WriteLine("Input Error!!!");
  47.                 Console.ReadLine();
  48.             }
  49.         }
  50.     }
  51. }

C#第三章练习3.9.3

2007年08月06日 | ghSky | 浏览: 767   

0 Comments

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Chapter03Exercise393
  6. {
  7.     class Exercise3_9_3
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Please Input:");
  12.             String str = Console.ReadLine();
  13.             String str2 = str;
  14.             String[] str1 = str.Split(new char[] { '+', '-', '*', '/' });
  15.             string temp = str1[0];
  16.             char mark = Convert.ToChar(str[temp.Length]);
  17.             double ans = 0;
  18.             double num1 = Convert.ToDouble(str1[0]);
  19.             double num2 = Convert.ToDouble(str1[1]);
  20.             switch (mark)
  21.             {
  22.                 case '+': ans = num1 + num2;
  23.                     break;
  24.                 case '-': ans = num1 - num2;
  25.                     break;
  26.                 case '*': ans = num1 * num2;
  27.                     break;
  28.                 case '/': ans = num1 / num2;
  29.                     break;
  30.                 default: ans = 0;
  31.                     break;
  32.             }
  33.             Console.WriteLine(str2 + "=" + ans);
  34.             Console.ReadLine();
  35.         }
  36.     }
  37. }

C#第三章学习札记

2007年08月06日 | ghSky | 浏览: 621   

0 Comments

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace TestCSharpChapter03
  6. {
  7.     class Test
  8.     {
  9.         enum Name { ghSky, MicroTonyu, Lei, Pig };
  10.         static void Main(string[] args)
  11.         {
  12.             /*--------------------------------------------------
  13.             // Test1 -- a-- or --a
  14.             int a = 10;
  15.             a--;
  16.             Console.WriteLine("a--:{0}",a);
  17.  
  18.             Console.WriteLine("WriteLine(a--):{0}", a--);
  19.             Console.WriteLine();
  20.  
  21.             // Test2 -- enum
  22.             Name name1 = Name.MicroTonyu;
  23.             Name name2 = Name.Lei;
  24.             Console.WriteLine("{0} Love {1}",name1,name2);
  25.             ----------------------------------------------------*/
  26.  
  27.             /*---------------------------------------------------
  28.             //Test3 -- Array
  29.             int[] array1 = new int[10];
  30.             for (int i = 0; i < array1.Length; i++)
  31.                 array1[i] = i;
  32.             ShowArray1("array1", array1);
  33.  
  34.             int[,] array2 = new int[5, 5];
  35.             int count = 1;
  36.             for (int i = 0; i < 5; i++)
  37.             {
  38.                 for (int j = 0; j < 5; j++)
  39.                     array2[i, j] = count++;
  40.             }
  41.             ShowArray2("array2", array2);
  42.             ----------------------------------------------------*/
  43.  
  44.             /*---------------------------------------------------
  45.             //Test4 -- CloneArray
  46.             int[] array = new int[10];
  47.             int[] arrayClone2=new int[10];
  48.             ShowArray1("array", array);
  49.             int[] arrayPointer = array;
  50.             int[] arrayClone = (int[])array.Clone();
  51.             for (int i = 0; i < 10; i++)
  52.                 arrayClone2[i] = array[i];
  53.             array[0] = 100;
  54.             array[9] = 100;
  55.             Console.WriteLine("--------------------------- After Modify ---------------------------");
  56.             ShowArray1("array", array);
  57.             ShowArray1("arrayPointer", arrayPointer);
  58.             ShowArray1("arrayClone", arrayClone);
  59.             ShowArray1("arrayClone2", arrayClone2);
  60.             ---------------------------------------------------*/
  61.  
  62.             //Test5 -- SortArray
  63.             int[] array = new int[10];
  64.             Random r = new Random();
  65.             for (int i = 0; i < 10; i++)
  66.                 array[i] = r.Next(0, 100);
  67.             ShowArray1("array", array);
  68.             Sort(array);
  69.             Console.WriteLine("------------------------------- After Sort -------------------------------");
  70.             ShowArray1("array", array);
  71.  
  72.         }
  73.  
  74.         static void ShowArray1(string arrayName, int[] array)
  75.         {
  76.             Console.Write(arrayName + ": ");
  77.             foreach (int n in array)
  78.                 Console.Write(n + " ");
  79.             Console.WriteLine();
  80.         }
  81.  
  82.         static void ShowArray2(string arrayName, int[,] array)
  83.         {
  84.             Console.Write(arrayName + ":");
  85.             for (int i = 0; i < 5; i++)
  86.             {
  87.                 for (int j = 0; j < 5; j++)
  88.                     Console.Write(" " + array[i, j]);
  89.             }
  90.             Console.WriteLine();
  91.         }
  92.  
  93.         static void Sort(int[] array)
  94.         {
  95.             for (int i=0; i<10; i++)
  96.                 for (int j = 9; j > i; j--)
  97.                 {
  98.                     if (array[j - 1] < array[j])
  99.                     {
  100.                         int temp = array[j - 1];
  101.                         array[j - 1] = array[j];
  102.                         array[j] = temp;
  103.                     }
  104.                 }
  105.         }
  106.     }
  107.  
  108. }
Page 1 Of 212