C#面向对象程序设计——上机实验报告(一)

本文通过一系列实验,详细介绍了C#中的数据类型、结构和类的使用,包括值类型与引用类型的差异、数组操作、类型转换以及逻辑运算。实验内容涵盖结构体与类的区别、数组(一维与多维)的创建与操作、值类型与引用类型之间的转换,以及逻辑操作符的运用。示例代码清晰地展示了这些概念在实际编程中的应用。
摘要由CSDN通过智能技术生成

实验目的

掌握C#语言中的数据类型、操作符和表达式、程序控制结构这几项基本要素

实验内容

P3_1(结构)

利用两个double字段的变量a、b,就可以表示出复数。创建结构可以将a、b封装在一起。ComplexNumber结构还包含了一个成员方法Write。主程序就是调用这个方法在控制台输出复数。
代码中用了Console类的WriteLine方法格式化输出。{0}被a替换,{1}被b替换。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P3_1
{
    class Program
    {
        static void Main()
        {
            ComplexNumber c1;//定义结构变量c1
            c1.a = 1.5;
            c1.b = 3;
            ComplexNumber c2 = c1;//定义结构变量c2
            c2.a = c2.b;
            Console.Write("c1 = ");
            c1.Write();
            Console.Write("c2 = ");
            c2.Write();
        }
    }
    struct ComplexNumber
    {
        public double a;
        public double b;

        public void Write()
        {
            Console.WriteLine("{0}+{1}i", a, b);
        }
    }
}

输出:
c1 = 1.5+3i
c2 = 3+3i

P3_2(类)

在P_1的基础上将struct更改成为class。但由于类是引用类型,所以必须将”ComplexNumber c1”更改为”ComplexNumber c1 = new ComplexNumber()”,这样就为c1.a和c1.b分配了内存。
“ComplexNumber c2 = c1”创建了c2。c1和c2共用内存。后面对c2的赋值会导致c1的值改变。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P3_2
{
    class Program
    {
        static void Main()
        {
            ComplexNumber c1 = new ComplexNumber();//创建c1对象
            c1.a = 1.5;
            c1.b = 3;
            ComplexNumber c2 = c1;//创建c2对象
            c2.a = c2.b;
            Console.Write("c1 = ");
            c1.Write();
            Console.Write("c2 = ");
            c2.Write();
        }
    }
    class ComplexNumber
    {
        public double a;
        public double b;

        public void Write()
        {
            Console.WriteLine("{0}+{1}i", a, b);
        }
    }
}

输出:
c1 = 3+3i
c2 = 3+3i

P3_3(一位数组)

P3_3代码加入了数组”ComplexNumber[] cs = new ComplexNumber[4]”。c1、cs[0]、cs[2]指向一个对象,cs[1]、cs[3]指向另一个对象。所以c1、cs[0]、cs[2]值相等,cs[1]、cs[3]值相等。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P3_3
{
    class Program
    {
        static void Main()
        {
            ComplexNumber c1 = new ComplexNumber();
            c1.a = 1.5;
            c1.b = 3;
            ComplexNumber[] cs = new ComplexNumber[4];
            cs[0] = c1;
            cs[1] = new ComplexNumber();
            cs[1].a = 4;
            cs[1].b = 5;
            cs[2] = c1;
            cs[3] = cs[1];
            cs[0].a = 8;
            cs[3].b = 9;
            Console.Write("c1 = ");
            c1.Write();
            Console.Write("cs[1] = ");
            cs[1].Write();
            Console.Write("cs[2] = ");
            cs[2].Write();
        }
    }
    class ComplexNumber
    {
        public double a;
        public double b;
        public void Write()
        {
            Console.WriteLine("{0}+{1}i", a, b);
        }
    }
}

输出:
c1 = 8+3i
cs[1] = 4+9i
cs[2] = 8+3i

P3_4(多维数组)

通过在元素类型后的中括号里加入逗号建立多维数组。数字的维数通过逗号前后的数字控制。逗号前后也可以不填入数字,在编写程序的时候可以通过大括号的匹配确定数组的维数并赋值。
数组名+.GelLength(i) 可以获取第i维的长度。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P3_4
{
    class Program
    {
        static void Main()
        {
            int[,] x = new int[2, 3] { { 1, 2, 3 }, { 3, 5, 8 } };
            int[,] y = new int[,] { { 10, 50 }, { 25, 75 }, { 50, 150 }, { 100, 80 } };
            int[,,] z = { { { 1, 2 }, { 3, 5 }, { 8, 13 } }, { { 1, 2 }, { 3, 5 }, { 8, 13 } } };
            Console.Write( "数组x长度为:");
            Console.WriteLine(x.Length);
            Console.WriteLine("各维长度:{0} * {1}", x.GetLength(0), x.GetLength(1));
            Console.Write("数组y长度为:");
            Console.WriteLine(y.Length);
            Console.WriteLine("各维长度:{0} * {1}", y.GetLength(0), y.GetLength(1));
            Console.Write("数组z长度为:");
            Console.WriteLine(z.Length);
            Console.WriteLine("各维长度:{0} * {1} * {2}", z.GetLength(0), z.GetLength(1), z.GetLength(2));
        }
    }
}

输出:
数组x长度为:6
各维长度:2 * 3
数组y长度为:8
各维长度:4 * 2
数组z长度为:12
各维长度:2 * 3 * 2

P3_5(值类型之间的转换)

通过在值前面加括号可以实现显式转换。枚举类型本质上也是整数,0可以直接赋给枚举变量。
Weekday w1 = 0 和 Weekday w2 = (Weekday)3正确的转换为对应的Monday和Thursday
Weekday w3 = (Weekday)100 因为 100超出了Weekday的范围所以直接输出整数100
int x = (int)Weekday.Friday将Friday对应的整数输出

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P3_5
{
    class Program
    {
        static void Main()
        {
            Weekday w1 = 0;
            Weekday w2 = (Weekday)3;//显式转换
            Weekday w3 = (Weekday)100;//显式转换
            Console.WriteLine(w1);
            Console.WriteLine(w2);
            Console.WriteLine(w3);
            int x = (int)Weekday.Friday;//显式转换
            Console.WriteLine(x);
        }
    }
    enum Weekday
    {
        Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
    }
}

输出:
Monday
Thursday
100
4

P3_6(引用类型之间的转换)

Graduate g1 = new Graduate ();
Student s1 = g1;
因为Graduate是Student的派生类所以可以进行隐式转换
Graduate g2 = (Graduate)s1;
因为s1本质是Graduate对象所以可以进行显式转换
Student s2 = new Student ();
Graduate g3 = (Graduate)s2
因为s2本质是Student对象所以转换失败

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P3_6
{
    class Program
    {
        static void Main()
        {
            Graduate g1 = new Graduate();
            g1.name = "陈亮";
            Student s1 = g1;//隐式转换
            Student s2 = new Student();
            s2.name = "宋燕燕";
            Graduate g2 = (Graduate)s1;//显式转换
            Console.WriteLine(g2.name);
            Graduate g3 = (Graduate)s2;//错误,转换失败!
        }
    }

    class Student
    {
        public string name;
        public int age;
        public int grade;
        public void Register() { }
    }

    class Graduate : Student
    {
        public string supervisor;
    }
}

输出:
陈亮
未经处理的异常: System.InvalidCastException: 无法将类型为“P3_6.Student”的对象强制转换为类型“P3_6.Graduate”。
在 P3_6.Program.Main() 位置 D:\P3_6\Program.cs:行号 20

P3_7(逻辑操作符)

程序用一个bool数组记录各位考官的选择
x[i] = (Console.ReadLine() == “1”)
如选择的是1,那么x[i] = 1,否则x[i] = 0

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P3_7
{
    class Program
    {
        static void Main()
        {
            bool[] x = new bool[5];
            Console.WriteLine("请依次输入每名考官的评分,1通过,0不通过:");
            x[0] = (Console.ReadLine() == "1");
            x[1] = (Console.ReadLine() == "1");
            x[2] = (Console.ReadLine() == "1");
            x[3] = (Console.ReadLine() == "1");
            x[4] = (Console.ReadLine() == "1");
            int i = 0;
            bool b = (x[i++]) && (x[i++] || x[i]) && (x[++i] || x[++i]);

            Console.WriteLine("投票结果为:{0}", b);
            Console.WriteLine("判断次数为:{0}", i);
        }
    }
}

程序用i++、i、++i遍历了所有考官的选择
要求主考同意并且至少1名本单位考官和1名外单位考官同意,考核通过。

int i = 0;
bool b = (x[i++]) && (x[i++] || x[i]) && (x[++i] || x[++i]);

输出:
请依次输入每名考官的评分,1通过,0不通过:
1
0
0
1
1
投票结果为:False
判断次数为:2

P3_12(foreach循环结构)

foreach用于遍历整个集合或数组
foreach(char ch in s)用于遍历字符串中每一个字符
通过每个字符与’a’判断,统计出字符串中字符”a”出现的次数

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P3_12
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("请输入一个字符串:");
            string s = Console.ReadLine();
            int i = 0;
            foreach(char ch in s)
            {
                if (ch == 'a')
                    i++;
            }
            Console.WriteLine("a共出现{0}次", i);
        }
    }
}

输出:
请输入一个字符串:
Harry Potter and the Philosopher’s Stone
a共出现2次

总结

C#语言不同于之前学习的C语言,存在着对象的概念,数据类型有值类型和引用类型之分。在编程的时候需要注意如果是引用类型的话,创建对象后需要分配内存。C#语言定义数组和类型转换的方式都与C语言有着很大的区别。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值