实验名称:创建简单的C#应用程序及基本编程方法
一、实验目的:
(1)了解C# 的数据类型
(2)掌握各种变量的声明方式。 (3)理解运算符的优先级。
(4)掌握C#简单数据类型、运算符与表达式、数组的使用方法。
(5)理解C# 程序语法结构,掌握顺序结构、选择结构和循环结构语法的程序设计方法。 (6)通过以上内容,掌握C# 语言的编程规则。
二、上机内容:
(1)编写一个声明C#不同数据类型变量的程序。 (2)编写一个使用运算符、表达式、变量的程序。 (3)编写一个使用C#数组的的程序。 (4)编写使用不同选择结构的程序。 (5)编写使用不同循环结构结构的程序。
三、上机原理、方法和手段:
本上机练习C#的主要数据类型,原则主要C#中数据类型在应用时应该先进行变量的定义才可以使用,在C#中的数组及应用,以及使用数据进行表达式的计算。利用条件语句来实现功能选择功能,利用循环语句来解决重复计算要求的问题,设计程序并调试运行。
四、上机步骤:
(1)编程求一个一维和二维整数数组的最大值、最小值、平均值和所有数组元素的和。
一维数组代码:using System;
using System.Collections.Generic; using System.Text; namespace shiyan2 {
class Program {
static void Main(string[] args) {
Console.WriteLine(\请输入数组元素个数\ int a = int.Parse(Console.ReadLine()); int[] b = new int[a]; int sum = 0; int ave = 0;
for (int i = 0; i < a; i++) {
Console.WriteLine(\请输入第{0}个数\ b[i] = int.Parse(Console.ReadLine()); sum = sum + b[i];
}
ave = sum / a; int max = b[0]; int min = b[0];
for (int j = 0; j < a; j++) {
if (b[j] < min) {
min = b[j]; }
if (b[j] > max) {
max = b[j]; }
}
Console.WriteLine(\ Console.WriteLine(\ Console.WriteLine(\ Console.WriteLine(\ Console.Read(); } }
}运行结果如右图:
二维数组代码:using System;
using System.Collections.Generic; using System.Text;
namespace shuzu2 {
class Program {
static void Main(string[] args) {
Console.WriteLine(\请输入数组的行数\); int a = int.Parse(Console.ReadLine()); Console.WriteLine(\请输入数组的列数\); int b = int.Parse(Console.ReadLine()); int[,] d = new int[a, b]; int c = a * b; int sum = 0;
int ave = 0;
for (int i = 0; i < b; i++) {
for (int j = 0; j < a; j++) {
Console.WriteLine(\请输入第{0}行第{1}列的数\, i + 1, j + 1); d[i, j] = int.Parse(Console.ReadLine()); sum = sum + d[i, j];
} }
ave = sum / c; int max = d[0, 0]; int min = d[0, 0];
for (int m = 0; m < a; m++) {
for (int t = 0; t < b; t++) {
if (max < d[m, t]) { max = d[m, t]; } if (min > d[m, t]) { min = d[m, t]; } } }
Console.WriteLine(\, sum); Console.WriteLine(\ + ave); Console.WriteLine(\, max); Console.WriteLine(\, min); Console.ReadLine(); } }
}运行结果如图:
(2)利用ArrayList编写一个简单的统计程序,能够统计出一组数据均值、方差、2阶原点矩、2阶中心距、标准差、极值、极差等数字特征。
using System;
using System.Collections;
using System.Text;
namespace arraylistexample {
class Program {
static void Main(string[] args) {
ArrayList myal = new ArrayList(); double sum = 0;
int i = int.Parse(Console.ReadLine()); while (i > 0) {
myal.Add(i);
i = int.Parse(Console.ReadLine()); }
double ave=0.0; int max, min;
max = (int)myal[0]; min = (int)myal[0]; int jc = max - min; double bzc;
double y=0.0;double z=0.0; double fc = 0; double s = 0;
foreach (int j in myal) {
sum = sum + j; if (max < j) { max = j; } if (min > j) { min = j; } }
ave = sum / (myal.Count); jc = max - min;
foreach (int j in myal) {
s = s + Math.Pow((j - ave), 2); y = y + Math.Pow(j, 2);
z = z + Math.Pow((j - ave), 2); }
fc = s / myal.Count; bzc = Math.Pow(fc, 0.5); y = y / myal.Count; z = z / myal.Count;
Console.WriteLine(\,sum={1},ave={2},fc={3},bzc={4},z={5}\, y, sum, ave, fc, bzc,z); Console.ReadLine(); } } }
运行结果如下
编写一个数据类型转换的程序,能够完成C#提供的4种数据类型转换方式,并进行+、-、×、/、%运算。
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication13 {
class Program {
static void Main(string[] args) {
double i = Convert.ToDouble(Console.ReadLine()); int j = (int)(i / 2);
string c = i.ToString() + j.ToString(); long m = j % 3; long a = j * m; double b = i - j;
Console.WriteLine(\结果为:i=\ + i + \ + \ + j + \ + \ + c + \ + \ +
m + \ + \ + a + \ + \ + b + \); Console.ReadLine(); } } }