Дан текст. Подсчитать количество букв k в последнем ее слове.
private void button1_Click(object sender, EventArgs e)
{
using (StreamReader reader = new StreamReader(@"C:\Users\Abrorbek\Documents\AT
kafedra\DTT\Lab8.1.txt", true))
{
string allText = reader.ReadToEnd();
string[] M = allText.Split(' ');
string shart = M[M.Length - 1];
int t=0,a=0;
while(shart.Contains('k'))
{
a=shart.LastIndexOf('k');
shart = shart.Remove(a);
t++;
}
if (t != 0)
textBox1.Text = t + " ta k bor";
else
textBox1.Text = "k harfi yo`q";
}
}
8.2 Дан двумерный массив размером n*m.
1. Заменить максимальный элемент каждой строки нулем
2. Вставить после каждого столбца, содержащего максимальный элемент массива, столбец
из нулей.
3. Удалить все столбцы, в которых встретится нечетный положительный элемент.
4. Поменять местами первый и предпоследний столбцы.
8.3 В проект, выполненный по индивидуальному заданию второго уровня, добавить вывод
результатов программы в исходный текстовый файл с комментариями.
namespace Lab8._2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int n, m;
int[,] M1;
string satr1;
private void button1_Click(object sender, EventArgs e)
{
label1.Text = null;
try
{
StreamReader reader =
File.OpenText(@"C:\Users\Abrorbek\Documents\ATkafedra\DTT\Lab8.2.txt");
string read = null;
int a = 0;
while ((read = reader.ReadLine()) != null)
{
if (a == 0)
n = Convert.ToInt32(read);
if (a == 1)
m = Convert.ToInt32(read);
if(a>1)
satr1+=read+" ";
a++;
}
reader.Close();
reader.Dispose();
a = 0;
string[] M = satr1.Split(' ');
M1 = new int[n, m];
for (int i=0;i
{for (int j=0;j
{M1[i, j] = Convert.ToInt32(M[a]);
a++;}}
dataGridView1.ColumnHeadersVisible = false;
dataGridView1.RowHeadersVisible = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.RowCount = n;
dataGridView1.ColumnCount = m;
for (int i=0;i
for (int j=0;j
dataGridView1.Rows[i].Cells[j].Value = M1[i, j].ToString();
label1.Text = "n=" + n + "\n";
label1.Text += "m=" + m + "\n";
label1.Text += "Yaratish yakunlandi";}
catch(Exception ex)
label1.Text = "Yaratishda xatolik";}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
StreamReader reader =
File.OpenText(@"C:\Users\Abrorbek\Documents\ATkafedra\DTT\Lab8.2.txt");
string read = null;
int a = 0;
while ((read = reader.ReadLine()) != null)
{if (a == 0)
n = Convert.ToInt32(read);
if (a == 1)
m = Convert.ToInt32(read);
if (a > 1)
satr1 += read + " ";
a++;}
reader.Close();
reader.Dispose();
a = 0;
string[] M = satr1.Split(' ');
M1 = new int[n, m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{M1[i, j] = Convert.ToInt32(M[a]);
a++;}
dataGridView2.ColumnHeadersVisible = false;
dataGridView2.RowHeadersVisible = false;
dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
if(comboBox1.SelectedIndex==0)
{dataGridView2.RowCount = n;
dataGridView2.ColumnCount = m;
a = 0;
int[] satr = new int[n];
for (int i = 0; i < n;i++ )
{satr[i]=M1[i,0];
for(int j=0;j
if (satr[i] < M1[i, j])
satr[i] = M1[i, j];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{if (M1[i, j] == satr[i])
dataGridView2.Rows[i].Cells[j].Value = "0";
else
dataGridView2.Rows[i].Cells[j].Value = M1[i, j].ToString();}
try
{FileInfo file = new FileInfo(@"C:\Users\Abrorbek\Documents\AT kafedra\DTT\Lab8.3.txt");
StreamWriter writer = file.CreateText();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
writer.Write(dataGridView2.Rows[i].Cells[j].Value + " ");
writer.WriteLine();}
writer.Close();
label2.Text = "Ma`lumot faylga yozildi";
}
catch(Exception ex)
{
label2.Text = "Yozishda xatolik";
}
}
if (comboBox1.SelectedIndex == 1)
{
a = M1[0, 0];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (a < M1[i, j])
a = M1[i, j];
int b = 0;
bool[] ustun = new bool[m];
for (int j = 0; j < m; j++)
{
ustun[j] = false;
for (int i = 0; i < n; i++)
if (a == M1[i, j])
ustun[j] = ustun[j] || true;
if (ustun[j])
b++;
}
int c = 0;
dataGridView2.ColumnCount = m + b;
dataGridView2.RowCount = n;
for (int j = 0; j < m; j++)
{
for (int i = 0; i < n; i++)
dataGridView2.Rows[i].Cells[c].Value = M1[i, j];
c++;
if (ustun[j])
{for (int i = 0; i < n; i++)
dataGridView2.Rows[i].Cells[c].Value = "0";
c++;}
}
try
{
FileInfo file = new FileInfo(@"C:\Users\Abrorbek\Documents\AT kafedra\DTT\Lab8.3.txt");
StreamWriter writer = file.CreateText();
for (int i=0;i
{
for (int j=0;j
{
writer.Write(dataGridView2.Rows[i].Cells[j].Value + " ");
}
writer.WriteLine();
}
writer.Close();
label2.Text = "Ma`lumot faylga yozildi";
}
catch
{
label2.Text = "Yozishda xatolik";
}
}
if(comboBox1.SelectedIndex==2)
{
a = 0;
bool[] ustun = new bool[m];
for (int j = 0; j < m; j++)
{
ustun[j] = false;
for (int i = 0; i < n; i++)
{
if (Math.Abs(M1[i, j]) % 2 == 1 && M1[i, j] > 0)
ustun[j] = ustun[j] || true;
}
if (ustun[j])
a++;
}
dataGridView2.ColumnCount = m - a;
dataGridView2.RowCount = n;
a = 0;
for (int j = 0; j < m; j++)
{
for (int i = 0; i < n; i++)
{
if (!ustun[j])
dataGridView2.Rows[i].Cells[a].Value = M1[i, j];
}
if (!ustun[j])
a++;
}
try
{
FileInfo file = new FileInfo(@"C:\Users\Abrorbek\Documents\AT kafedra\DTT\Lab8.3.txt");
StreamWriter writer = file.CreateText();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m - a; j++)
{
writer.Write(dataGridView2.Rows[i].Cells[j].Value + " ");
}
writer.WriteLine();
}
writer.Close();
label2.Text = "Ma`lumot faylga yozildi";
}
catch
label2.Text = "Yozishda xatolik";
if(comboBox1.SelectedIndex==3)
{
a = 0;
dataGridView2.ColumnCount = m;
dataGridView2.RowCount = n;
for (int j = 0; j < m; j++)
for (int i = 0; i < n; i++)
{
if (j == 0)
dataGridView2.Rows[i].Cells[j].Value = M1[i, m - 2];
else
if (j == m-2)
dataGridView2.Rows[i].Cells[j].Value = M1[i, 0];
else
dataGridView2.Rows[i].Cells[j].Value = M1[i, j];
}
try
{
FileInfo file = new FileInfo(@"C:\Users\Abrorbek\Documents\AT kafedra\DTT\Lab8.3.txt");
StreamWriter writer = file.CreateText();
for (int i = 0; i < n; i++)
{for (int j = 0; j < m; j++)
writer.Write(dataGridView2.Rows[i].Cells[j].Value + " ");
writer.WriteLine();}
writer.Close();
label2.Text = "Ma`lumot faylga yozildi";}
catch
label2.Text = "Yozishda xatolik";
}
Do'stlaringiz bilan baham: |