标题:小弟学C#不久,请高手优化代码。
只看楼主
hntygi889
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-11-4
 问题点数:0 回复次数:0 
小弟学C#不久,请高手优化代码。
程序代码:
using System;
using System.Collections.Generic;
using System.Text;

namespace shenfenzheng
{
    class Program
    {

        static void Main(string[] args)
        {

            System.DateTime date = new System.DateTime();
            date = System.DateTime.Now;
            int year = date.Year;
            int month = date.Month;
            int day = date.Day;
            //生成随机年代码
            int maxyear = year - 19;
            int minyear = year - 50;
            Random ran=new Random();
            int nyear = ran.Next(minyear, maxyear);
            //生成随机月份代码
            int nmonth = ran.Next(1, 12);
            //生成随机日代码
            int maxnday=0;

            switch (nmonth)
            {               
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    maxnday = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    maxnday = 30;
                    break;
                case 2:
                    if (nyear % 4 == 0 && nyear != 0 || nyear % 400 == 0)
                    {
                        maxnday = 29;
                    }
                    else
                    {
                        maxnday = 28;
                    }
                    break;
            }
            int nday = ran.Next(1, maxnday);
            int mindq = 510901;
            int maxdq = 510903;
            int ndq=ran.Next(mindq,maxdq);
            int code1 = ran.Next(0, 9);
            int code2 = ran.Next(0, 9);
            int code3 = ran.Next(1, 9);
            //判断性别
            string sex = "";
            if (code3 % 2 == 0)
            {
                sex = "";
            }
            else
            {
                sex = "";
            }
            //算出最后身份证最后一位校验码
            int[] nndq = GetIntArray(ndq);//得到地区数组
            int[] nnyear = GetIntArray(nyear);//得到年份数组
            int[] nnmonth = GetIntArray(nmonth);//得到月份数组
            int[] nnday = GetIntArray(nday);//得到日数组
            int t1 = Getintzone(nndq[0], 7);
            int t2 = Getintzone(nndq[1],9);
            int t3 = Getintzone(nndq[2], 10);
            int t4 = Getintzone(nndq[3], 5);
            int t5 = Getintzone(nndq[4], 8);
            int t6 = Getintzone(nndq[5], 4);
            int t7 = Getintzone(nnyear[0], 2);
            int t8 = Getintzone(nnyear[1], 1);
            int t9 = Getintzone(nnyear[2], 6);
            int t10 = Getintzone(nnyear[3], 3);
            int t11 = 0;
            int t12 = 0;
            if (nmonth < 10)
            {
                 t11 = 0;
                 t12 = Getintzone(nnmonth[0], 9);

            }
            else
            {
                 t11 = Getintzone(nnmonth[0], 7);
                 t12 = Getintzone(nnmonth[1], 9);
            }
            int t13 = 0;
            int t14 = 0;
            if (nday < 10)
            {
                 t13 = 0;
                 t14 = Getintzone(nnday[0], 5);
            }
            else
            {
                 t13 = Getintzone(nnday[0], 10);
                 t14 = Getintzone(nnday[1], 5);
            }
            int t15 = Getintzone(code1, 8);
            int t16 = Getintzone(code2, 4);
            int t17 = Getintzone(code3, 2);
            int sum = t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10 + t11 + t12 + t13 + t14 + t5 + t6 + t17;
            int sum2 = sum / 11;
            int sum4 = sum2 * 11;
            int sum3 = sum - sum4;
            string st18="";
        
            switch (sum3)
            {
                case 0:
                    st18 = "1";
                    break;
                case 1:
                    st18 = "0";
                    break;
                case 2:
                    st18 = "x";
                    break;
                case 3:
                    st18 = "9";
                    break;
                case 4:
                    st18 = "8";
                    break;
                case 5:
                    st18 = "7";
                    break;
                case 6:
                    st18 = "6";
                    break;
                case 7:
                    st18 = "5";
                    break;
                case 8:
                    st18 = "4";
                    break;
                case 9:
                    st18 = "3";
                    break;
                case 10:
                    st18 = "2";
                    break;
            }
             

               string stndq= Convert.ToString(ndq);
               string styear = Convert.ToString(nyear);
               string stt11;
               string stt12;
               string stt13;
               string stt14;
               //string stmonth = Convert.ToString(nmonth);
               if (t11 != 0)
               {
                    stt11 = Convert.ToString(t11 / 7);
               }
               else
               {
                   stt11 = Convert.ToString(t11);
               }
               if (t12 != 0)
               {
                    stt12 = Convert.ToString(t12 / 9);
               }
               else
               {
                    stt12 = Convert.ToString(t12);
               }
               if (t13 != 0)
               {
                   stt13 = Convert.ToString(t13 / 10);
               }
               else
               {
                    stt13 = Convert.ToString(t13);
               }
               if (t14 != 0)
               {
                    stt14 = Convert.ToString(t14 / 5);
               }
               else
               {
                   stt14 = Convert.ToString(t14);
               }  

               //string stt12 = Convert.ToString(t12);
               //string stt13 = Convert.ToString(t13);
               //string stt14 = Convert.ToString(t14);
               string stcode1 = Convert.ToString(code1);
               string stcode2 = Convert.ToString(code2);
               string stcode3 = Convert.ToString(code3);
               string sfzhm = stndq + styear + stt11 + stt12 + stt13 + stt14 + stcode1 + stcode2 + stcode3 + st18;



               Console.WriteLine("身份证号码为{0}", sfzhm);
            //Console.WriteLine(sndq);
            //Console.WriteLine("和为{0}", sum2);
            //Console.WriteLine("每一个数{0}",t1);
            //Console.WriteLine("性别{0}", sex);
            //Console.WriteLine("身份证最后第4位{0}",code1);
            //Console.WriteLine("身份证最后第3位{0}",code2);
            //Console.WriteLine("身份证最后第2位{0}", code3);
            //Console.WriteLine("当前地区{0}", ndq);
            //Console.WriteLine("当月最大天数{0}", maxnday);
            //Console.WriteLine("日{0}",nday);
            //Console.WriteLine("年{0}",nyear);
            //Console.WriteLine("月{0}",nmonth);
            Console.ReadKey();
        }
       
       
       
            static int[] GetIntArray(int num)
            {
                int len = num.ToString().Length;
                int[] arr = new int[len];
                for (int i = 0; i < len; i++)
                {
                    arr[i] = num % 10;
                    num /= 10;
                }
                Array.Reverse(arr);
                return arr;
            }

        static int Getintzone(int num, int xh)
        {
            int tem;
            if (num > 0)
            {
                tem = num * xh;
            }
            else
            {
                tem = 0;
            }
            return tem;
        }

 


    }
}
搜索更多相关主题的帖子: 优化 
2012-04-12 00:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-365616-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056621 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved