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 Test_01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void richTextBox_1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button_1_Click(object sender, EventArgs e)
{
/* 题目--2.4 */
int a = 7, b = 7, c = 6;
string str = " ";
for (int i=0;i<a;i++)
{
for (int j = 0; j < b; j++)
{
if ((i==j) | ((i!=0) & ((i + j) == c)))
{
str += "O ";
}
else if ((i==0)&(j==c))
{
str += "O\n ";
}
else if (j == c)
{
str += ".\n ";
}
else
{
str += ". ";
}
}
}
richTextBox_1.Text = str;
Console.WriteLine(richTextBox_1.Text);
}
private void button_2_Click(object sender, EventArgs e)
{
/* 题目--2.5 */
int N = 1000;
string str_Sum = " ";
for (int i = 1; i <= N; i++)
{
int Sum = 0;
for (int j = 1; j < i; j++)
{
if (i % j == 0)
{
Sum += j;
}
}
if (Sum == i)
{
str_Sum += Sum.ToString() + "\n ";
}
}
richTextBox_1.Text = str_Sum;
Console.WriteLine(richTextBox_1.Text);
}
private void button_3_Click(object sender, EventArgs e)
{
/* 题目--3.1 */
int[] arr = { 3, 34, 42, 2, 11, 19, 30, 55, 20, 1 };
string str_res = " 从数组[";
foreach (int i in arr)
{
str_res += i.ToString() + ",";
}
str_res += "]中选出偶数数字:\n ";
// 偶数
foreach (int i in arr)
{
if (i % 2 == 0)
{
str_res += i.ToString() + "\n ";
}
}
richTextBox_1.Text = str_res;
Console.WriteLine(richTextBox_1.Text);
}
private void button_4_Click(object sender, EventArgs e)
{
/* 题目--3.2 */
int[] arr = { 3, 34, 42, 2, 11, 19, 30, 55, 20, 1 };
string str_res = " 从数组[";
foreach (int i in arr)
{
str_res += i.ToString() + ",";
}
str_res += "]中选出7的整数倍数字:\n ";
// 7的倍数
foreach (int i in arr)
{
if (i % 7 == 0)
{
str_res += i.ToString() + "\n ";
}
}
richTextBox_1.Text = str_res;
Console.WriteLine(richTextBox_1.Text);
}
private void button_5_Click(object sender, EventArgs e)
{
/* 题目--3.3 对数组进行排序-顺序*/
int[] arr = { 3, 34, 42, 2, 11, 19, 30, 55, 20, 1 };
string str_res = " 对数组[";
foreach (int i in arr)
{
str_res += i.ToString() + ",";
}
str_res += "]进行排序-从小到大排序:\n ";
// 排序
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = i; j < arr.Length; j++)
{
int tmp;
if (arr[i] > arr[j])
{
tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
}
// 输出
str_res += " [";
foreach (int i in arr)
{
str_res += i.ToString() + ",";
}
str_res += "].";
richTextBox_1.Text = str_res;
Console.WriteLine(richTextBox_1.Text);
}
private void button_6_Click(object sender, EventArgs e)
{
/* 题目--3.3 对数组进行排序-顺序*/
int[] arr = { 3, 34, 42, 2, 11, 19, 30, 55, 20, 1 };
string str_res = " 对数组[";
foreach (int i in arr)
{
str_res += i.ToString() + ",";
}
str_res += "]进行排序-从大到小排序:\n ";
// 排序
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = i; j < arr.Length; j++)
{
int tmp;
if (arr[i] < arr[j])
{
tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
}
// 输出
str_res += " [";
foreach (int i in arr)
{
str_res += i.ToString() + ",";
}
str_res += "].";
richTextBox_1.Text = str_res;
Console.WriteLine(richTextBox_1.Text);
}
private void button_7_Click(object sender, EventArgs e)
{
/* 题目--3.4*/
string str = " 《守株待兔》“段落一:古时候,有一个种田的人在田里干活,突然从树林里窜出一只兔子,一下撞在树桩上死了。那个种田人急忙跑过去,乐滋滋地把兔子捡回家去了。段落二: 从此以后,他丢下了锄头,整天坐在树桩旁等着,看还有没有兔子撞死在树桩上。最后,他的庄稼全都枯死了。 ”";
string str_res = " 题目:\n" + str + "\n\n 要求:请将该故事中的“死”字替换为“*”,并输出该故事的标题以及分别输出段落一与段落二的内容,注意去除段落开头与结尾的空格部分.\n\n 结果:\n";
string str1 = str.Replace("死", "*");
string[] separatingStrings1 = { "《", "》" };
string[] words1 = str1.Split(separatingStrings1, StringSplitOptions.RemoveEmptyEntries);
str_res += "标题为:" + words1[1] + "\n";
string[] separatingStrings2 = { "段落一:", "段落二:", " " };
string[] words2 = words1[2].Split(separatingStrings2, StringSplitOptions.RemoveEmptyEntries);
str_res += "段落一:" + words2[1] + "\n";
str_res += "段落二:" + words2[2] + "\n";
richTextBox_1.Text = str_res;
Console.WriteLine(richTextBox_1.Text);
}
private void button_8_Click(object sender, EventArgs e)
{
/* 题目--3.5*/
string str = "Title:Text\r\nX,Y,Z\r\n1,2,3\r\n4,5,6\r\n7,8,9\r\n10,11,12\r\n13,14,14\r\n";
string str_res = " 题目:\n" + str + "\n\n 要求:请输出其中的Title值与各个X、Y、Z的值。\n\n 结果:\n";
string[] separatingStrings = { "\r\n"};
string[] words = str.Split(separatingStrings, StringSplitOptions.RemoveEmptyEntries);
//Console.WriteLine($"{words.Length} substrings in text:");
string x = "", y = "", z = "";
foreach (var word in words)
{
if (word.Contains(':'))
{
string[] separating1 = { ":" };
string[] wd1 = word.Split(separating1, StringSplitOptions.RemoveEmptyEntries);
str_res += wd1[1] + "\n";
}
else
{
string[] separating1 = { "," };
string[] wd1 = word.Split(separating1, StringSplitOptions.RemoveEmptyEntries);
x += wd1[0] + ",";
y += wd1[1] + ",";
z += wd1[2] + ",";
}
}
str_res += x + "\n" + y + "\n" + z + "\n";
richTextBox_1.Text = str_res;
Console.WriteLine(richTextBox_1.Text);
}
private void button1_Click(object sender, EventArgs e)
{
/* 题目--2.4 */
int a = 7, b = 7, c = 6;
richTextBox_1.Text = " ";
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
if ((i == j) | ((i != 0) & ((i + j) == c)))
{
richTextBox_1.AppendText("O ");
}
else if ((i == 0) & (j == c))
{
richTextBox_1.AppendText("O\n ");
}
else if (j == c)
{
richTextBox_1.AppendText(".\n ");
}
else
{
richTextBox_1.AppendText(". ");
}
}
}
}
}
}
2.5 所谓“完数”是指一个数恰好等于它的所有因子之和。例如6是完数,因为6=1+2+3,求1000之内的所有“完数”。