Bài tập xử lý dãy số trong C, C# - Lập trình hướng đối tượng

Loading...
Bài tập xử lý dãy số trong C# - Lập trình hướng đối tượng.
Bài tập này mình làm cho môn lập trình hướng đối tượng @.@ Nhắc đến lập trình là cũng hơi ngán rồi. Xử lý dãy số trong C này gồm những hàm sau: Cập nhật, tìm kiếm phần tử, tính tổng các phần tử trong dãy số, tính trung bình cộng các phần tử, in dãy con các số nguyên tố, sắp xếp các phần tử theo thứ tự tăng.
Bạn nào có cần bài tập xử lý dãy số trong C# - Lập trình hướng đối tượng thì tham khảo nhé !
Code:

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

class DaySo
{
//cac thuoc tinh
private int[] ds;
private int sopt;

//cac phuong thuc
public DaySo(int n)
{
this.ds = new int[n];
this.sopt = 0;
}
public void Nhap()
{
//string x;
Console.Write("\t Nhap so phan tu cua mang: ");
this.sopt = int.Parse(Console.ReadLine());
for (int i = 0; i < this.sopt; i++)
{
Console.Write("\t Nhap phan tu thu " + i + ": ");
this.ds[i]=int.Parse(Console.ReadLine());
}
}
public void CapNhat(int pos, int a)
{
this.ds[pos] = a;
}
public int TimKiem(int a)
{
for (int i = 0; i < this.sopt; i++)
{
if (this.ds[i] == a)
return i;
}
return -1;
}
public void Sapxep()
{
int tam;
for (int i = 0; i < this.sopt; i++)
for (int j = i + 1; j <= this.sopt; j++ )
{
if (this.ds[i] > this.ds[j])
{
tam = this.ds[i];
this.ds[i] = this.ds[j];
this.ds[j] = tam;
}
}
Console.WriteLine("\t Cac phan tu sau khi sap xep la: ");
for (int i = 1; i <= this.sopt; i++)
Console.WriteLine("\t" + this.ds[i]);
}
public int Doc(int pos)
{
return this.ds[pos - 1];
}
public long TinhTong()
{
long tg = 0;
for (int i = 0; i < this.sopt; i++)
{
tg += this.ds[i];
}
return tg;
}
public float TrungBinhCong()
{
return (this.TinhTong() / this.sopt);
}
public bool LaSoNguyenTo(int i)
{
if (this.ds[i] < 2)
return false;
for (int j = 2; j <= this.ds[i] / 2; j++)
if (this.ds[i] % j == 0)
return false;
return true;
}
public DaySo DayCon()
{
DaySo a = new DaySo(this.sopt);
for (int i = 0; i < this.sopt; i++)
{
if (this.LaSoNguyenTo(this.ds[i]))
{
a.ds[a.sopt] = ds[i];
a.sopt++;
}
}
return a;
}
public void Xuat()
{
Console.Write("Day phan tu: ");
for (int i = 0; i < this.sopt; i++)
Console.Write(this.ds[i] + " ");
}
public static void Menu(out byte chon)
{
Console.Clear();
Console.WriteLine("\n\t|| BAN DA LUA CHON 4 - THAO TAC DAY SO || ");
Console.WriteLine("\t||----------------------------------------||");
Console.WriteLine("\t|| 1. Nhap ||");
Console.WriteLine("\t|| 2. Cap nhat. ||");
Console.WriteLine("\t|| 3. Tim kiem. ||");
Console.WriteLine("\t|| 4. Tinh tong cac phan tu. ||");
Console.WriteLine("\t|| 5. Tinh trung binh cong cac phan tu. ||");
Console.WriteLine("\t|| 6. In day con cac so nguyen to. ||");
Console.WriteLine("\t|| 7. Sap xep phan tu theo thu tu tang ||");
Console.WriteLine("\t|| 8. Xuat day ||");
Console.WriteLine("\t|| 0. Ve menu chinh ||");
Console.WriteLine("\t||________________________________________||\n");
Console.Write("\n\tNHAP LUA CHON: ");
chon4 = byte.Parse(Console.ReadLine());
}
public void Main()
{
DaySo d1 = new DaySo(50);
byte chon;
int pos, gt;
do
{
Menu4(out chon);
switch (chon)
{
case 1:
d1.Nhap();
break;
case 2:
Console.Write("\t Vi tri can cap nhat: ");
pos = int.Parse(Console.ReadLine());
if (d1.TimKiem(pos) == -1)
{
Console.WriteLine("\t Vi tri khong hop le!!!");
}
else
{
Console.Write("\tNhap gia tri moi: ");
gt = int.Parse(Console.ReadLine());
d1.CapNhat(pos, gt);
}
break;
case 3:
Console.Write("\tNhap vi tri can truy xuat: ");
pos = int.Parse(Console.ReadLine());
if (d1.TimKiem(pos) == -1)
{
Console.WriteLine("\tVi tri khong hop le!!!");
}
else
{
Console.WriteLine("\tGia tri tai vi tri truy xuat la: " + d1.Doc(pos));
}
break;
case 4:
long tong;
tong = d1.TinhTong();
Console.Write("\n\t Tong cac phan tu la: {0}", tong);
break;
case 5:
float TBC;
TBC = d1.TrungBinhCong();
Console.Write("\n\t Trung Binh Cong cac phan tu la: {0} ",TBC);
break;
case 6:
//int[] i;
//Console.WriteLine("\t\n Cac so nguyen to: " +i );
break;
case 7:
//int n=this.sopt;
//int[] a=this.ds;
d1.Sapxep();
break;
case 8:
d1.Xuat();
break;
case 0:
Console.Clear();
break;
default:
Console.WriteLine("\n\n\t Khong co lua chon nay !!!");
break;
}
Console.ReadLine();
}
while (chon != 0);
}
}
Loading...
Chuyên mục: ,

0 nhận xét:

Đăng một bình luận