刘一辰的软件工程随笔
实验一 语言基础
一、实验目的
1. 熟悉Visual Stido.NET 实验环境;
2. 掌握控制台程序的编写方法;
3. 掌握C#程序设计语言的语法基础;
4. 掌握控制语句和数组的使用。
二、实验要求
根据题目要求,编写 C#程序,并将程序代码和运行结果写入实验报告。
三、实验内容
1. 编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出。
2. 编写一个控制台应用程序,可根据输入的月份判断所在季节。
3. 编写程序,用 while 循环语句实现下列功能:有一篮鸡蛋,不止一个,有人两个两
个数,多余一个,三个三个数,多余一个,再四个四个地数,也多余一个,请问这篮鸡蛋至
少有多少个。
4. 编写程序,计算数组中奇数之和和偶数之和。
5. 编写程序,找一找一个二维数组中的鞍点(即该位置上的元素值在行中最大,在该
列上最小。有可能数组没有鞍点)。要求:
u 二维数组的大小、数组元素的值在运行时输入;
u 程序有友好的提示信息。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public TextBox text1 = new TextBox();
public TextBox text2 = new TextBox();
public TextBox text3 = new TextBox();
public TextBox text4 = new TextBox();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text.Equals("三角形"))
{
if (comboBox2.Text.Equals("面积"))
{
int a = int.Parse(gettext("text1"));
int b = int.Parse(gettext("text2"));
int c = int.Parse(gettext("text3"));
double d = (Math.Pow(b, 2) + Math.Pow(c, 2) - Math.Pow(a, 2)) / (2 * b * c);
MessageBox.Show(d.ToString());
double q = Math.Sqrt(1 - Math.Pow(d, 2));
MessageBox.Show(q.ToString());
double S = (b * c) / 2 * q;
MessageBox.Show("三角形面积为" + S);
}
else if (comboBox2.Text.Equals("周长"))
{
int a = int.Parse(gettext("text1"));
int b = int.Parse(gettext("text2"));
int c = int.Parse(gettext("text3"));
double C = a + b + c;
MessageBox.Show("三角形周长为" + C);
}
}
else if (comboBox1.Text.Equals("正方形"))
{
if (comboBox2.Text.Equals("面积"))
{
int a = int.Parse(gettext("text1"));
double S = a * a;
MessageBox.Show("正方形面积为" + S);
}
else if (comboBox2.Text.Equals("周长"))
{
int a = int.Parse(gettext("text1"));
double C = a * 4;
MessageBox.Show("正方形周长为" + C);
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox text1 = new TextBox();
text1.Location = new Point(80, 100);
text1.Size = new Size(100, 30);
text1.Name = "text1";
TextBox text2 = new TextBox();
text2.Location = new Point(180, 100);
text2.Size = new Size(100, 30);
text2.Name = "text2";
TextBox text3 = new TextBox();
text3.Location = new Point(280, 100);
text3.Size = new Size(100, 30);
text3.Name = "text3";
if (comboBox1.Text.Equals("三角形"))
{
if (isexist("text1"))
{
delete("text1");
}
else
{
Controls.Add(text1);
}
if (isexist("text2"))
{
delete("text2");
}
else
{
Controls.Add(text2);
}
if (isexist("text3"))
{
delete("text3");
}
else
{
Controls.Add(text3);
}
}
else if (comboBox1.Text.Equals("正方形"))
{
if (isexist("text1"))
{
delete("text1");
}
else
{
Controls.Add(text1);
}
if (isexist("text2"))
{
delete("text2");
}
if (isexist("text3"))
{
delete("text3");
}
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
public bool isexist(string str)
{
foreach (Control item in this.Controls)
{
if (item.Name.Equals(str))
{
return true;
}
}
return false;
}
public void delete(string str)
{
foreach (Control item in this.Controls)
{
if (item.Name.Equals(str))
{
Controls.Remove(item);
}
}
}
public string gettext(string str)
{
foreach (Control item in this.Controls)
{
if (item.Name.Equals(str))
{
return item.Text;
}
}
return "";
}
public void settext(string str, string text)
{
foreach (Control item in this.Controls)
{
if (item.Name.Equals(str))
{
item.Text = text;
}
}
}
private void label4_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string str = comboBox3.Text;
if (str.Equals("一月") || str.Equals("二月") || str.Equals("三月"))
{
MessageBox.Show("现在是春天");
}
else if (str.Equals("四月") || str.Equals("五月") || str.Equals("六月"))
{
MessageBox.Show("现在是夏天");
}
else if (str.Equals("七月") || str.Equals("八月") || str.Equals("九月"))
{
MessageBox.Show("现在是秋天");
}
else
{
MessageBox.Show("现在是冬天");
}
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
int i = 4;
while (!((i - 1) % 2 == 0 && (i - 1) % 3 == 0 && (i - 1) % 4 == 0))
{
i++;
}
settext("textBox1", i.ToString());
}
private void label7_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
string str = textBox2.Text;
int js = 0, os = 0;
string[] key = str.Split(',');
foreach (string value in key)
{
int temp = int.Parse(value);
if (temp % 2 == 0)
{
os = os + temp;
}
else if (temp % 2 != 0)
{
js = js + temp;
}
}
settext("textBox3", js.ToString());
settext("textBox4", os.ToString());
}
private void button5_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox5.Text);
int b = int.Parse(textBox6.Text);
string[] key = textBox7.Text.Split(',');
int[,] shu=new int[a,b];
int count = 0;
for (int i = 0; i < a; i++)
{
for (int k = 0; k < b; k++)
{
shu[i, k] = int.Parse(key[count]);
count++;
}
}
string[] hang=new string[a];
string[] lie = new string[b];
int x = 0, y = 0;
for (int i = 0; i < a; i++)
{
int value = shu[i, 0];
x = i; y = 0;
for (int k = 0; k < b; k++)
{
if (value < shu[i, k])
{
value = shu[i, k];
x = i;y = k;
}
}
hang[i] = x.ToString() + ',' + y.ToString();
Console.WriteLine("hang"+hang[i]);
}
int x2 = 0, y2 = 0;
for (int i = 0; i < b; i++)
{
int value2 = shu[0, i];
x2 = 0;y2 = i;
for (int k = 0; k < a; k++)
{
if (value2 > shu[i, k])
{
value2 = shu[i, k];
x2 = i; y2 = k;
}
}
lie[i] = x2.ToString() + ',' + y2.ToString();
Console.WriteLine("lie" + lie[i]);
}
for (int i = 0; i < b; i++)
{
for (int k = 0; k < a; k++)
{
if(hang[i].Equals(lie[k]))
{
string[] zuobiao= hang[i].Split(',');
if (textBox8.Text == "")
{
textBox8.Text= shu[int.Parse(zuobiao[0]), int.Parse(zuobiao[1])].ToString();
}
else
{
textBox8.Text = textBox8.Text + "," + shu[int.Parse(zuobiao[0]), int.Parse(zuobiao[1])].ToString();
}
}
}
}if(textBox8.Text == "")
{
textBox8.Text = "无";
}
}
}
}
四、实验总结
对问题理解不够深刻,编代码时出现逻辑错误。动态添加控件时,获取值经常获取不到,查资料后解决
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!