函数附加练习2

1、写一个函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串。

主要代码:

        static void Main(string[] args)
        {
            Program method = new Program();
            ArrayList a = new ArrayList();
            Console.Write("请输入长度:");
            int n = int.Parse(Console.ReadLine());
            for (int i = 0; i < n; i++)
                a.Add(Console.ReadLine());
            ArrayList b = (ArrayList)method.word(a);
            foreach (object c in b)
                Console.Write(c + "\t");
            Console.ReadLine();
        }
        public object word(object a)
        {
            ArrayList b = (ArrayList)a;
            b.Reverse();
            return b;
        }

结果:

2、写一个函数,将两个字符串连接。

主要代码:

        static void Main(string[] args)
        {
            Program method = new Program();
            Console.Write("请输入长度:");
            int n = int.Parse(Console.ReadLine());
            string[] a = new string[n];
            string[] b = new string[n];
            for (int i = 0; i < n; i++)
            {
                Console.Write("a=");
                a[i] = Console.ReadLine();
                Console.Write("b=");
                b[i] = Console.ReadLine();
            }
            method.array1(a, b, n);
            Console.ReadLine();
        }
        public void array1(string[] a, string[] b, int n)
        {
            string[] c = new string[2 * n];
            for (int i = 0; i < n; i++)
                c[i] = a[i];
            for (int j = 0; j < n; j++)
                c[n + j] = b[j];
            foreach (string d in c)
                Console.Write(d + "\t");
        }

结果:

3、写一个函数,将一个字符串中的元音字母复制到另一字符串,然后输出。

主要代码:

        static void Main(string[] args)
        {
            Program method = new Program();
            ArrayList a = new ArrayList();
            Console.Write("请输入长度:");
            int n = int.Parse(Console.ReadLine());
            for (int i = 0; i < n; i++)
                a.Add(Console.ReadLine());
            ArrayList b = (ArrayList)method.character(a);
            foreach (object c in b)
                Console.Write(c + "\t");
            Console.ReadLine();
        }
        public object character(object a)
        {
            ArrayList b = (ArrayList)a;
            ArrayList c = new ArrayList();
            for (int i = 0; i < b.Count; i++)
            {
                if (b[i].ToString() == "a" || b[i].ToString() == "A" || b[i].ToString() == "e" || b[i].ToString() == "E" || b[i].ToString() == "i" || b[i].ToString() == "I" || b[i].ToString() == "o" || b[i].ToString() == "O" || b[i].ToString() == "u" || b[i].ToString() == "U")
                    c.Add(b[i]);
            }
            return c;
        }

结果:

4、写一个函数,输入一个4位数字,要求输出这4个数字字符,但每两个数字之间空一个空格。

主要代码:

        static void Main(string[] args)
        {
            Program function = new Program();
            Console.Write("请输入:");
            int n = int.Parse(Console.ReadLine());
            if (n > 999 && n < 10000)
                function.number(n);
            else
                Console.WriteLine("输入有误!");
            Console.ReadLine();
        }
        public void number(int n)
        {
            int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
            n1 = n / 1000;
            n2 = (n - n1 * 1000) / 100;
            n3 = (n - n1 * 1000 - n2 * 100) / 10;
            n4 = n % 10;
            Console.WriteLine("{0}\t{1}\t{2}\t{3}", n1, n2, n3, n4);
        }

结果:

5、写一个函数,输入一行字符,将此字符串中最长的单词输出。

主要代码:

        static void Main(string[] args)
        {
            Program function = new Program();
            Console.Write("请输入长度:");
            int n = int.Parse(Console.ReadLine());
            string[] character = new string[n];
            for (int i = 0; i < n; i++)
            {
                Console.Write("请输入单词:");
                character[i] = Console.ReadLine();
            }
            function.word(character, n);
            Console.ReadLine();
        }
        public void word(string[] character, int n)
        {
            string ml = character[0];
                for (int i = 0; i < n; i++)
                {
                    if (ml.Length<character[i].Length)
                    {
                        ml = character[i];
                    }
                }
            Console.WriteLine("最长单词为{0}。", ml);
        }

结果:

 

6、用牛顿迭代法求根。方程为ax3+bx2+cx+d=0,系数a,b,c,d的值依次为1,2,3,4,由主函数输入。求x在1附近的一个实根,求出跟后由主函数输出。

主要代码:

        static void Main(string[] args)
        {
            Program function = new Program();
            int a = 1, b = 2, c = 3, d = 4;
            Console.WriteLine("x={0}", function.method(a, b, c, d));
            Console.ReadLine();
        }
        public double method(int a, int b, int c, int d)
        {
            double x0 = 0;
            double x1 = 1;
            while(Math.Abs(x1 - x0) >= 1e-5)
            {
                x0 = x1;
                x1 = x0 - ((x0 * x0 * x0 + 2 * x0 * x0 + 3 * x0 + 4) / (3 * x0 * x0 - 4 * x0 + 3));
            }
            return x1;
        }

结果:

牛顿迭代法:

7、输入10个学生5门课的成绩,用函数实现下列功能:

1)计算每个学生的平均分;

2)计算每门课的平均分;

3)找出所有50个分数中最高的分数所对应的学生和课程;

4)计算平均分方差:f=(1/n)∑xi2-((∑xi)/n)2,其中xi为某一学生的平均分。

主要代码:

 1         static void Main(string[] args)
 2         {
 3             Program function = new Program();
 4             string[] name = new string[10];
 5             for (int i = 0; i < 10; i++)
 6             {
 7                 Console.Write("请输入姓名:");
 8                 name[i] = Console.ReadLine();
 9             }
10             Console.WriteLine();
11             string[] classes = new string[] { "语文", "数学", "英语", "物理", "化学" };
12             double[,] score = new double[10, 5];
13             for (int i = 0; i < 10; i++)
14             {
15                 Console.WriteLine("请输入{0}的成绩", name[i]);
16                 for (int j = 0; j < 5; j++)
17                 {
18                     Console.Write("{0}:", classes[j]);
19                     score[i, j] = double.Parse(Console.ReadLine());
20                 }
21                 Console.WriteLine();
22             }
23             function.grade(name, classes, score);
24             Console.ReadLine();
25         }
26         public void grade(string[] name, string[] classes, double[,] score)
27         {
28             //求每人的平均分。
29             double[] averge = new double[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
30             for (int i = 0; i < 10; i++)
31             {
32                 double sum = 0;
33                 for (int j = 0; j < 5; j++)
34                 {
35                     sum += score[i, j];
36                 }
37                 averge[i] = sum / 5;
38                 Console.WriteLine("{0}的平均分为:{1:f1}分。", name[i], averge[i]);
39             }
40             Console.WriteLine();
41             //求每门课的平均分
42             double[,] mark = new double[5, 10];//定义一个新数组,用来接收score数组的转置。
43             for (int i = 0; i < 5; i++)
44             {
45                 for (int j = 0; j < 10; j++)
46                 {
47                     mark[i, j] = score[j, i];
48                 }
49             }
50             for (int i = 0; i < 5; i++)//求每门课的平均分
51             {
52                 double sum = 0;
53                 double avg = 0;
54                 for (int j = 0; j < 10; j++)
55                 {
56                     sum += mark[i, j];
57                 }
58                 avg = sum / 10;
59                 Console.WriteLine("本班的{0}的平均分为:{1:f1}分。", classes[i], avg);
60             }
61             Console.WriteLine();
62             //求最高分,并确定是哪个学生的哪门课的分数
63             double hight = score[0, 0];
64             int c = 0, r = 0;
65             for (int i = 0; i < 10; i++)
66             {
67                 for (int j = 0; j < 5; j++)
68                 {
69                     if (score[i, j] > hight)//比较大小,只留下大值。
70                     {
71                         hight = score[i, j];
72                         r = i;
73                         c = j;
74                     }
75                 }
76             }
77             Console.WriteLine("最高分为{0:f1},是{1}的{2}成绩。", hight, name[r], classes[c]);
78             Console.WriteLine();
79             //求平均分的方差
80             double variance = 0, sum1 = 0, sum2 = 0;
81             for (int i = 0; i < 10; i++)
82             {
83                 sum1 += averge[i] * averge[i];
84                 sum2 += averge[i];
85             }
86             variance = sum1 / 10 - (sum2 / 10) * (sum2 / 10);
87             Console.WriteLine("每人平均分方差为{0:f2}。", variance);
88         }

结果:

 

posted on 2016-05-14 21:28  bosamvs  阅读(229)  评论(0编辑  收藏  举报

导航