函数附加练习1
1、写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输入。
主要代码:
static void Main(string[] args) { Program function = new Program(); Console.Write("请输入:"); double num1 = double.Parse(Console.ReadLine()); Console.Write("请输入:"); double num2 = double.Parse(Console.ReadLine()); double num = function.divisor(num1, num2); double Num = function.multiple(num1, num2); Console.WriteLine("最大公约数为:{0},最小公倍数为:{1}。", num, Num); Console.ReadLine(); } /// <summary> /// 最大公约数 /// </summary> /// <param name="num1"></param> /// <param name="num2"></param> /// <returns></returns> public double divisor(double num1, double num2) { double temp = 0; double num = 0; if (num1 < num2) { temp = num1; num1 = num2; num2 = temp; } while (num2 != 0) { num = num1 % num2; num1 = num2; num2 = num; } return num1; } /// <summary> /// 最小公倍数 /// </summary> /// <param name="num1"></param> /// <param name="num2"></param> /// <returns></returns> public double multiple(double num1, double num2) { double num = 0; Program method = new Program(); double Num = method.divisor(num1, num2); num = num1 * num2 / Num; return num; }
结果:
2、求方程ax2+bx+c=0的根,用3个函数分别求当b2-4ac大于0、等于0和小于0时的根并输出,从主函数输入a,b,c的值。
主要代码:
static void Main(string[] args) { Program function = new Program(); Console.Write("请输入a:"); double a = double.Parse(Console.ReadLine()); Console.Write("请输入b:"); double b = double.Parse(Console.ReadLine()); Console.Write("请输入c:"); double c = double.Parse(Console.ReadLine()); if (b * b - 4 * a * c > 0) { double x1 = function.equation0(a, b, c); double x2 = function.equation1(a, b, c); Console.WriteLine("x1={0:f2},x2={1:f2}", x1, x2); } else if (b * b - 4 * a * c == 0) { double x = function.equation2(a, b, c); Console.WriteLine("x={0:f2}", x); } else function.equation(); Console.ReadLine(); } /// <summary> /// 求一元二次方程,△>0 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="c"></param> /// <returns></returns> public double equation0(double a, double b, double c) { double D = b * b - 4 * a * c; double x = (((-b) + Math.Sqrt(D)) / (2 * a)); return x; } public double equation1(double a, double b, double c) { double D = b * b - 4 * a * c; double x = (((-b) - Math.Sqrt(D)) / (2 * a)); return x; } /// <summary> /// 求一元二次方程,△=0 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="c"></param> /// <returns></returns> public double equation2(double a, double b, double c) { double D = b * b - 4 * a * c; double x = (((-b) + Math.Sqrt(D)) / (2 * a)); return x; } /// <summary> /// 求一元二次方程,△<0 /// </summary> public void equation() { Console.WriteLine("此方程无实根!"); }
static void Main(string[] args) { //△>0的另一种写法。 Program function = new Program(); double x1; double x2; function.equation(out x1, out x2); Console.WriteLine("x1={0:f2},x2={1:f2}", x1, x2); Console.ReadLine(); } public void equation(out double x1, out double x2) { Console.Write("请输入a:"); double a = double.Parse(Console.ReadLine()); Console.Write("请输入b:"); double b = double.Parse(Console.ReadLine()); Console.Write("请输入c:"); double c = double.Parse(Console.ReadLine()); double D = b * b - 4 * a * c; x1 = (((-b) + Math.Sqrt(D)) / (2 * a)); x2 = (((-b) - Math.Sqrt(D)) / (2 * a)); }
结果:
1)△>0
2)△=0
3)△<0
3、写一个判断素数的函数,在主函数输入一个整数,输出是否为素数的信息。
主要代码:
static void Main(string[] args) { Program method = new Program(); Console.Write("请输入:"); int a = int.Parse(Console.ReadLine()); method.number(a); Console.ReadLine(); } public void number(int n) { int count = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { count++; } } if (count == 2) Console.WriteLine("你输入的是素数!"); else Console.WriteLine("你输入的不是素数!"); }
结果:
4、写一个函数,是给定的一个nxn的二维整形数组转置
主要代码:
static void Main(string[] args) { //法一 Program method = new Program(); Console.Write("请输入维数n:"); int n = int.Parse(Console.ReadLine()); double[,] m = new double[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write("请输入:"); m[i, j] = double.Parse(Console.ReadLine()); } } Console.WriteLine("原数组:"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(m[i, j] + "\t"); } Console.WriteLine(); } Console.WriteLine("转置数组:"); method.array(m, n); Console.ReadLine(); } public void array(double[,] m, double n) { double temp = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { temp = m[i, j]; m[i, j] = m[j, i]; m[j, i] = temp; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(m[i, j] + "\t"); } Console.WriteLine(); } }
static void Main(string[] args) { //法二 Program method = new Program(); Console.Write("请输入维数n:"); int n = int.Parse(Console.ReadLine()); double[,] m = new double[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write("请输入:"); m[i, j] = double.Parse(Console.ReadLine()); } } Console.WriteLine("原数组:"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(m[i, j] + "\t"); } Console.WriteLine(); } Console.WriteLine("转置数组:"); double[,] m1 = method.array(m, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(m1[i, j] + "\t"); } Console.WriteLine(); } Console.ReadLine(); } public double[,] array(double[,] m, int n) { double temp = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { temp = m[i, j]; m[i, j] = m[j, i]; m[j, i] = temp; } } return m; }
结果: