zoukankan      html  css  js  c++  java
  • <C#任务导引教程>练习五

    //27,创建一个控制台应用程序,声明两个DateTime类型的变量dt,获取系统的当前日期时间,然后使用Format格式化进行规范
    using System;
    class Program
    {
        static void Main()
        {
            DateTime dt = DateTime.Now;
            string strDate = String.Format("{0:D}", dt);
            Console.WriteLine("今天的日期是:" + strDate);
        }
    }
    //28,创建一个控制台应用程序,声明一个string类型变量str1,并初始化为“我钟爱C#语言程序设计”,然后用copy方法复制字符串str1,并赋值给字符串str2,最后输出字符串str2
    using System;
    class Program
    {
        static void Main()
        {
            string str1 = "我钟爱C#语言程序设计";
            string str2;
            str2 = string.Copy(str1);
            Console.WriteLine(str2);
        }
    }
    //29,CopTo方法
    using System;
    class Program
    {
        static void Main()
        {
            string str = "我钟爱C#语言程序设计";
            Console.WriteLine("原字符串: " + str);
            char[] myChar = new char[5];
            str.CopyTo(0, myChar, 0, 5);//CopyTo(int需要复制的字符的起始位置,cha[]目标字符数名,指定目标数组中的开始存放位置,int指定要复制的字符个数)
            Console.WriteLine("复制的字符串:");
            Console.WriteLine(myChar);
        }
    }


    我可以给你们大概算一下有多少个模块 就单纯说些代码的 硬件部门不算
    构成设定CLI ,装置维护CLI,环境设定CLI,装置信息查看CLI.快照备份CLI,数据分割CLI,资源分割CLI,性能优化CLI。。。

    server, mainserver 设定server,状态监视server,性能监视server,snapshotserver,备份还原server......

    还有各种外部tools,还不算OS部门和FW部门

    //30,字符串的加密与解密示例
    using System;
    class Program
    {
        static void Main()
        {
            string list = "kczutmhsuasasahsuihsuw";
            char[] str = new char[80];
            int i, j;
            Console.Write("请输入一小写字母串(长度小于80):");
            string c = Console.ReadLine();
            Console.Write("加密后成为:");
            for (i = 0; i < c.Length; i++)
            {
                str[i] = c[i];
                j = str[i] - 97;
                str[i] = list[j];
                Console.Write("{0}", str[i]);
            }
            Console.WriteLine();
        }
    }
    //31,字符串的解密示例
    using System;
    class Program
    {
        static void Main()
        {
            string list = "qwertyuioplkjhgfdsazxcvbnm";
            char[ ] str = new char[80];
            int i, j;
            Console.Write("请输入需解密的字符串:");
            string c = Console.ReadLine();
            Console.Write("原字符串是:");
            for (i = 0; i < c.Length; i++)
            {
                str[i] = c[i];//
                j = 0;
                while (str[i] != list[j])
                    j++;
                str[i] = (char)(j + 97);
                    Console.Write("{0}",str[i]);
            }
            Console.WriteLine();
        }
    }
    //32,有三个字符串,要求找出其中最大者
    using System;
    class Program
    {
        static void Main()
        {
            Console.WriteLine("请先后输入三个字符串,每输入一个请按Enter键确认!");
            string str;
            Console.Write("请输入第1个字符串:");
            string a = Console.ReadLine();
            Console.Write("请输入第2个字符串:");
            string b = Console.ReadLine();
            Console.Write("请输入第3个字符串:");
            string c = Console.ReadLine();
            int m = String.Compare(a, b);
            if (m > 0)
                str = String.Copy(a);
            else
                str = String.Copy(b);
            int n = String.Compare(c, str);
            if (n > 0)
                str = String.Copy(c);
            Console.WriteLine("最大的字符串是:{0}", str);
        }
    }
    //33,选择排序
    using System;
    class Program
    {
        static void Main()
        {
            string[ ] names = new string[5];
            string max;
            int i, j;
            for(i=0;i<5;i++)
            {
                Console.Write("请输入{0}个国家的名字:",i+1);
                names[i] = Console.ReadLine( );
            }
            for (i = 0; i < names.Length - 1; i++)
            {
                for(j=i+1;j<names.Length;j++)
                {
                    int m = String.Compare(names[i], names[j]);
                    if (m < 0)
                    {
                        max = String.Copy(names[i]);
                        names[i] = String.Copy(names[j]);
                        names[j] = String.Copy(max);
                    }
                }
            }
            Console.WriteLine("排序结果:");
            for (i = 0; i < 5; i++)
            {
                Console.Write("{0}  ", names[i]);
            }
            Console.WriteLine();
        }
    }
    //34.ArrayList数组集合
    using System;
    using System.Collections;//ArrayList位于Collections中
    class Program
    {
        static void Main()
        {
            ArrayList myAL = new ArrayList( );
            myAL.Add("Hello");
            myAL.Add("World");
            myAL.Add("!");
            Console.WriteLine("myAL Count:  {0}", myAL.Count);//显示ArrayList的元素个数
            Console.Write("       Value:");
            foreach (Object obj in myAL)
                Console.Write("     {0}", obj);
            Console.WriteLine();
        }
    }
    //35,输出一个表格的表头,调用方法
    using System;
    class Program{
        static void printstar()
        {
            Console.WriteLine("*************");
        }
        static void print_message()
        {
            Console.WriteLine("* XSCJFXTJB *");
        }
        static void Main()
        {
            printstar();
            print_message();
            printstar();
        }
    }
    //36,方法返回值,return语句的一半格式
    using System;
    class MainClass
    {
        static int power(int x, int n)
        {
            int pow = 1;
            while (n > 0)
            {
                n--;
                pow *= x;
            }
            return pow;
        }
        static void Main()
        {
            int n = 3;
            int x = 4;
            char c = 'a';
            Console.WriteLine("power({0},{1},{2})", x, n, power(x, n));
            Console.WriteLine("power('{0}',{1})={2}", c, n, power(c, n));
            Console.WriteLine("power({0},{1}))={2}", n,x, power(n,x));
        }
    }
    //37,输入两个整数,通过方法的调用输出其中较大的数,求两个整数中的较大数,用方法完成
    using System;
    class Program
    {
        static int max(int x, int y)
        {
            return (x >= y) ? x : y;
        }
        static void Main()
        {
            Console.WriteLine("请输入两个整数:");
            Console.Write("a= ");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.Write("b= ");
            int b=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("{0:D}和{1:D}较大的数是:{2:D}",a,b,max(a,b));
        }
    }
    //38,试编程实现方法的嵌套调用
    using System;
    class Program
    {
        static void prnline( )
    {
        Console.Write(" - ");
    }
        static void print()
        {
            Console.Write(" * ");
            prnline();
            return;
        }
        static void Main()
        {
            int i, j;
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    print();
                }
                Console.WriteLine( );
            }
        }
    }
    //39,阶乘
    using System;
    class MainClass
    {
        static int fac(int n)
        {
            int y;
            if (n == 1)
                y = 1;
            else
                y = n * fac(n - 1);
            return y;
        }
        static void Main()
        {
            Console.WriteLine("4!={0}",fac(4));
        }
    }
    //40,n+n^2+n^3+---n^n
    //n(1+n(1+n(1+n----)))
    using System;
    class MainClass
    {
        static int fun(int n, int m)
        {
            if (m == 1)
                return n;
            else
                return n * (1 + fun(n, m - 1));
        }
        static void Main()
        {
            Console.WriteLine("n=1:{0}", fun(1, 10));
            Console.WriteLine("n=2:{0}", fun(2, 10));
            Console.WriteLine("n=3:{0}", fun(3, 10));
        }

    }
    //41,汉诺塔问题
    using System;
    class Program
    {
        static void hanoi(int n, char A, char B, char C)
        {
            if (n == 1)
                Console.WriteLine("将第{0}个盘子从{1}柱搬到{2}柱上;", n, A, C);
            else
            {
                hanoi(n-1,A,C,B);
                Console.WriteLine("将第{0}个盘子从{1}柱搬到{2}柱上",n,A,C);
                hanoi(n-1,B,A,C);
            }
        }
        static void Main()
        {
            char A = 'A', B = 'B', C = 'C';
            Console.WriteLine("请输入A柱上的盘子总数:");
            int n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("当A柱上有{0}个盘子时,移动步骤依次为:", n);
            hanoi(n, A, B, C);
        }
    }
    //42,引用参数ref使用示例
    using System;
    class Program
    {
        static void Swap(ref int x, ref int y)
        {
            int temp = x;
            x = y;
            y = temp;
        }
        static void Main()
        {
            int i = 1, j = 2;
            Swap(ref i,ref j);
            Console.WriteLine("i={0},j={1}", i, j);
        }
    }
    //43,输出参数out使用示例
    using System;
    class Program
    {
        static void Divide(int x, int y, out int result, out int reminder)
        {
            result = x / y;
            reminder = x % y;
        }
        static void Main()
        {
            int res, rem;
            Divide(10, 3, out res, out rem);
            Console.WriteLine("res={0} rem={1}", res, rem);
        }
    }
    //44.用引用参数ref实现对8名学生各两门成绩的统计,求每门成绩的最低分
    using System;
    class Program
    {
        static void Main()
        {
            int[,] score = { { 98, 82 },{80,76}, { 96, 80 }, { 88, 90 }, { 98, 87 }, { 75, 84 }, { 68, 92 }, { 83, 66 } };
            int min1 = score[0, 0];
            int min2 = score[0, 1];
            minmum(score, ref min1, ref min2);
            Console.WriteLine("课程1最低分:" + min1 + " 课程2最低分" + min2);
        }
        static void minmum(int[,] score, ref int min1, ref int min2)
        {
            for (int i = 1; i < score.GetLength(0); i++)
                if (score[i, 0] < min1)
                    min1 = score[i, 0];
            for (int i = 1; i < score.GetLength(0); i++)
                if (score[i, 1] < min2)
                    min2 = score[i,1];
        }
    }
    //45,用输出参数out实现对8名学生各两门成绩的统计,求每门的最高分
    using System;
    class Program
    {
        static void Main()
        {
            int[ , ]score={{98,82},{80,76},{96,80},{88,90},{98,87},{75,84},{68,92},{83,66}};
            int max1, max2;
            maxmum(score, out max1, out max2);
            Console.WriteLine("课程1最高分:" + max1 + " 课程2最高分:" + max2);
        }
        static void maxmum(int[,] score, out int max1, out int max2)
        {
            max1 = score[0, 0];
            max2 = score[0, 1];
            for (int i = 1; i < score.GetLength(0); i++)
                if (score[i, 0] > max1)
                    max1 = score[i, 0];
            for (int i = 1; i < score.GetLength(0); i++)
            {
                if (score[i, 1] > max2)
                    max2 = score[i, 1];
            }
        }
    }
    //46,定义了3个学生代表,每个学生4次测试成绩对应一个3x4的数组,求所以学生各次考试成绩中的最高成绩
    using System;
    class Program
    {
        static void Main()
        {
            int[,] s = new int[3, 4] { { 68, 77, 73, 86 }, { 87, 96, 78, 89 }, { 90, 70, 81, 86 } };
            Console.WriteLine("最高成绩是:{0}", max(s, 3, 4));
        }
        static int max(int[,] g, int p, int t)
        {
            int m = 0;
            for(int i=0;i<p;i++)
                for(int j=0;j<t;j++)
                    if(g[i,j]>m)
                        m=g[i,j];
            return m;
        }
    }
    //47,输出双精度示例
    using System;
    class Program
    {
        static void Main()
        {
            double a = 123.456789012345;
            Console.WriteLine(a);
            Console.WriteLine("{0:F6}", a);
            Console.WriteLine("{0:F2}", a);
        }
    }
    //48,设置输出值得货币值与输出宽度示例
    using System;
    class Program
    {
        static void Main()
        {
            int a = 80;
            int b = 8000;
            Console.WriteLine("{0:C2}", a);
            Console.WriteLine("{0:C2}", b);
            Console.WriteLine("{0,5}", a);
            Console.WriteLine("{0,6}", b);
        }
    }
    //49,利用控制符完成两位小数输出
    using System;
    class Program
    {
        static void Main()
        {
            float x = 168.56f;
            Console.WriteLine("每件平均单价为:{0:F2}", x / 3);
            Console.WriteLine("每件平均单价为:{0:C2}", x / 3);
        }
    }
    //50,由键盘输入一个华氏温度,将其转化为摄氏温度
    using System;
    class Program
    {
        static void Main()
        {
            Console.Write("请输入华氏温度:");
            String s = Console.ReadLine();
            float f = float.Parse(s);
            float c=(f-32)/1.8f;
            Console.WriteLine("相应的摄氏温度是:{0:F1}", c);
        }
    }
    //51,由键盘输入三角形的三条边a,b,c,试编程用海伦公式计算任意三角形的面积
    using System;
    class Program
    {
        static void Main()
        {
            double s, area;
            Console.WriteLine("请输入三角形的三条边长:");
            Console.Write("a= ");
            string x = Console.ReadLine();
            Console.Write("b= ");
            string y = Console.ReadLine();
            Console.Write("c= ");
            string z= Console.ReadLine();
            double a = double.Parse(x);
            double b = double.Parse(y);
            double c = double.Parse(z);
            s = 1.0 / 2 * (a + b + c);
            area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
            Console.WriteLine("a={0:F2} b={0:F2} c={0:F2} s={3:F2}", a, b, c, s);
            Console.WriteLine("area={0:F2}", area);
        }
    }
    //52.设计一个方法,根据三角形的三边长求面积,如果不能构成三角形,给出提示信息
    using System;
    class Program
    {
        static double TriangleArea(float a, float b, float c)
        {
            if ((a + b <= c) || (a + c <= b) || (b + c) <= a)
                return -1;
            double s = (a + b + c) / 2.0;
            return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
        }
        static void Main()
        {
            Console.WriteLine("请输入a,b,c:");
            Console.Write("a= ");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.Write("b= ");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.Write("c= ");
            int c = Convert.ToInt32(Console.ReadLine());
            double area = TriangleArea(a, b, c);
            if(area==-1)
                Console.WriteLine("{0},{1},{2}不能构成三角形!",a,b,c);
            else
            Console.WriteLine("边长为{0},{1},{2}三角形的面积:{3:F1}",a,b,c,area);
        }
    }
    //53,用方法求两个不等长的数组元素的平均值
    using System;
    class Program
    {
        static void Main()
        {
            double[] a = new double[5] { 2, 3, 6, 8, 10 };
            double[] b = new double[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            double aver1, aver2;
            aver1 = average(a, 5);
            Console.WriteLine("a数组元素的平均值:{0}", aver1);
            aver2 = average(b,10);
            Console.WriteLine("a数组元素的平均值:{0}", aver2);
        }
        static double average(double[] array, int len)
        {
            double sum = 0;
            for (int i = 0; i < len; i++)
            {
                sum += array[i];
            }
            return sum / len;
        }
    }

  • 相关阅读:
    【引用】webkit内核浏览器支持的特殊CSS样式和CSS3.0
    catalina.home catalina.base 定义 位子 位置
    gvim 启动 全屏
    Log4j 配置文件(log4j.properties)的所在路径问题(转)
    ie6 div height bug css注意点(转)
    【引用】jdbc.properties 包含多种数据库驱动链接版本
    【引用】ActionContext和ServletActionContext介绍
    【引用】在Eclipse中将java Project转换成Dynamic Web Project
    flex查询xml字段绑定DataGrid小结
    Jquery 每天记一点200972
  • 原文地址:https://www.cnblogs.com/zhangyongjian/p/3622380.html
Copyright © 2011-2022 走看看