现在的位置: 首页 > 综合 > 正文

第五课作业

2013年08月11日 ⁄ 综合 ⁄ 共 2575字 ⁄ 字号 评论关闭

     没有按要求用c-free写对话框程序,因为布希饭c-free的界面和大写字母。不过写控制台应用程序更快更吃皮~
     C语言和C#中都是用GetLogicalDrives()方法,所以没多大区别。只是在C#中有Environment空间,更方便获取系统信息。

C代码:(二进制码的移动用了昨天看的《编程之美》里的方法)

Code:
  1. #include "windows.h"  
  2. #include "stdio.h"  
  3.   
  4. int main()  
  5. {  
  6.     //本机所有磁盘  
  7.     DWORD DWdisk=GetLogicalDrives();  
  8.     char disk[256];  
  9.     itoa(DWdisk,disk,2);  
  10.     puts(disk);  
  11.     //算法发现:位移   
  12.     int Count(DWORD v)  
  13.     {  
  14.         int num=0;  
  15.         while(v)  
  16.         {  
  17.             v&=v-1;  
  18.             num++;   
  19.         }  
  20.         return num;  
  21.     }  
  22.     //本机磁盘个数  
  23.     printf("本机共有%d逻辑磁盘。/n", Count(DWdisk));   
  24.       
  25.     //DRIVE_NO_ROOT_DIR:无效盘符   
  26.     if (GetDriveType("Z:") != DRIVE_NO_ROOT_DIR)  
  27.         printf("有一堆Z盘/n");  
  28.     else  
  29.         printf("摸得Z盘得!/n");  
  30.       
  31.     return 0;  
  32. }  

C#代码:

Code:
  1. /* 
  2.  * Created by SharpDevelop. 
  3.  * User: Sconfield 
  4.  * Date: 2009/9/2 
  5.  * Time: 16:46 
  6.  *  
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers. 
  8.  */  
  9.   
  10. using System;  
  11. namespace ComputerEnvironment  
  12. {  
  13.     class Disk  
  14.     {  
  15.         public static void Main()  
  16.         {  
  17.             #region 磁盘信息  
  18.               
  19.             //本机操作系统版本  
  20.             Console.WriteLine("My Operating System:{0}",Environment.OSVersion);  
  21.             //此文件路径  
  22.             Console.WriteLine("我在"+Environment.CurrentDirectory);  
  23.             //本机所有磁盘(包括虚拟光驱)  
  24.             bool diskD=false;  
  25.             string[] drives=Environment.GetLogicalDrives();  
  26.             for(int i=0;i<drives.Length;i++)  
  27.             {  
  28.                 Console.WriteLine("磁盘{0}: {1} ", i+1, drives[i]);  
  29.                 //判断是否存在G盘  
  30.                 if(drives[i]==@"D:/")  
  31.                 { diskD=true; }  
  32.             }  
  33.             if(diskD)  
  34.             {Console.WriteLine("有一个D盘");}  
  35.             //堆栈信息  
  36.             Console.WriteLine(Environment.StackTrace);  
  37.             //系统启动后经过的时间(不是启动时间)  
  38.             Console.WriteLine("时间:"+Environment.TickCount);  
  39.              
  40.             #endregion  
  41.               
  42.             #region 圆点面积  
  43.               
  44.             Console.WriteLine("请输入圆的半径:");  
  45.             double radius=Convert.ToDouble(Console.ReadLine());           
  46.             Console.WriteLine("半径为{0}的园面积为:{1}", radius, Area(radius));  
  47.              
  48.             #endregion  
  49.         }  
  50.         /// <summary>  
  51.         /// 圆点面积  
  52.         /// </summary>  
  53.         /// <param name="rad">半径</param>  
  54.         /// <returns>返回面积</returns>  
  55.         private static double Area(double rad)  
  56.         {  
  57.             //返回的时候没有用变量来储存“圆的面积”,这样可以避免溢出吗?  
  58.             //系统默认的数据类型貌似也是double  
  59.             return Math.PI*Math.Pow(rad,2);  
  60.         }  
  61.     }  
  62. }  

抱歉!评论已关闭.