PDA

View Full Version : Ôn tập C# - File



AmGian
23-12-2010, 02:53 PM
Ở đây sẽ có 4 bài
+ Tạo trong ổ đĩa D của bạn một File van.txt có nội dung
+ Xuất nội dung file đó ra màn hình
+ Đếm số dòng trong file
+ Đếm số từ trong file

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Write("Bai 1\n");
Console.Write("Bai 1 da ta mot file van.txt vao o dia D voi duong dan D:/van.txt\n");
string file = "D:/van.txt";
StreamWriter myfile = File.CreateText(file);
myfile.WriteLine("Bai Van Hai Con Chim ");
myfile.WriteLine("Ngay xua ngay xua ");
myfile.WriteLine("Xua rat la xua ");
myfile.WriteLine("Xua lau lam roi ");
myfile.WriteLine("ta cung khong nho nua ");
myfile.WriteLine("Hinh nhu cach day khoang 2-3 nam gi do ");
myfile.WriteLine("Co hai con chim ");
myfile.WriteLine("Chim trai la anh, chim gai la em ");
myfile.Close();
Console.Write("Bai 2\n");
string file1 = "D:/van.txt";
StreamReader myfile1 = File.OpenText(file1);
string s;
do
{
s = myfile1.ReadLine();
Console.WriteLine(s);
}
while (s != null);
Console.ReadLine();
myfile1.Close();
Console.Write("Bai 3\n");
string file2 = "D:/van.txt";
StreamReader myfile2 = File.OpenText(file2);
string i;
int a = 0;
do
{
i = myfile2.ReadLine();
a = a + 1;
} while (i != null);
Console.WriteLine("Bai van tren co {0} dong", a - 1);
Console.ReadLine();
myfile2.Close();
Console.Write("Bai 4\n");
string file3 = "D:/van.txt";
StreamReader myfile3 = File.OpenText(file3);
string t;
int b = 0;
do
{
t = myfile3.ReadLine();
if (t != null)
{
for (int l = 0; l < t.Length; l++)
{
if (t[l] == ' ')
b = b + 1;
}
}
} while (t != null);
Console.WriteLine("Bai van tren co {0} tu", b);
Console.ReadLine();
myfile3.Close();
}
}
}

:021::021::021::021::021::021::021::021::021::021:
Có ai làm thao tác trên file có thể tính toán được không, vd trong file đó có chứa một dãy sô, rùi tính tổng các dãy số đó. :)) Làm rùi post lên dùm với nghe, thanks nhiều.

Ronaldo
23-12-2010, 03:27 PM
Bài đại ca làm nè Ku Phong:


using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication4
{
class Program
{
static void Main()
{
string[] a = new string[100];
string n;
Console.WriteLine("Nhap n =");
n = Console.ReadLine();
int m = Convert.ToInt32(n);
string filename = "input.txt";
StreamWriter file = File.CreateText(filename);
for (int i = 1; i <= m; i++)
{
Console.WriteLine("Nhap so thu " + i);
a[i] = Console.ReadLine();
file.WriteLine(a[i]);
}
file.Close();
int[] b = new int[100];
StreamReader f1 = File.OpenText("input.txt");
for(int i =1 ; i<=m;i++)
{
b[i] = Convert.ToInt32(f1.ReadLine());
}
for (int i = 1; i <= m; i++)
for(int j = i+1;j<=m;j++)
{
if (b[i] > b[j])
{
int tam;
tam = b[i];
b[i] = b[j];
b[j] = tam;
}
}
int tongtong=0;
for (int i = 1; i <= m; i++)
{
tongtong = tongtong + b[i];
}
Console.WriteLine("tong={0}", tongtong);

string filename2 = "output.txt";
StreamWriter f2 = File.CreateText(filename2);
for (int i = 1; i <= m; i++)
{
f2.WriteLine(b[i]);
}
f2.Close();
}
}
}




Bài này tương tự như bài chú làm, nhưng khác sau đây

-- Đầu tiên là nhập N số nguyên
-- Ghi vào 1 file có tên "input.txt"
-- Đọc file input.txt, sắp xếp nó, và out ra file output.txt
-- Tính tổng rồi, nhưng mà chưa tìm ra cách ÉP nó vào file output.txt sao cho k mất dữ liệu file output.txt có sẳn, ai biết REP luôn nha

AmGian
23-12-2010, 05:31 PM
Hehe, làm tuy nhiên chua import tong vào file nên, trong đó nó không có là đúng rùi!
Ta sửa lại cho mi roài nè!
Nhớ cảm ơn nha mi!
:ThumbsUp::ThumbsUp::ThumbsUp::ThumbsUp::ThumbsUp:

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication4
{
class Program
{
static void Main()
{
string[] a = new string[100];
string n;
Console.WriteLine("Nhap n =");
n = Console.ReadLine();
int m = Convert.ToInt32(n);
string filename = "input.txt";
StreamWriter file = File.CreateText(filename);
for (int i = 1; i <= m; i++)
{
Console.WriteLine("Nhap so thu " + i);
a[i] = Console.ReadLine();
file.WriteLine(a[i]);
}
file.Close();
int[] b = new int[100];
StreamReader f1 = File.OpenText("input.txt");
for (int i = 1; i <= m; i++)
{
b[i] = Convert.ToInt32(f1.ReadLine());
}
for (int i = 1; i <= m; i++)
for (int j = i + 1; j <= m; j++)
{
if (b[i] > b[j])
{
int tam;
tam = b[i];
b[i] = b[j];
b[j] = tam;
}
}
int tongtong = 0;
for (int i = 1; i <= m; i++)
{
tongtong = tongtong + b[i];
}
Console.WriteLine("tong={0}", tongtong);
string filename2 = "output.txt";
StreamWriter f2 = File.CreateText(filename2);
f2.WriteLine("Tong la: "+tongtong);
for (int i = 1; i <= m; i++)
{
f2.WriteLine(b[i]);
}
f2.Close();
}
}
}

Ronaldo
23-12-2010, 06:52 PM
UP cho anh em theo dỏi ^!^

cuibapvn
23-12-2010, 07:44 PM
chú nào cũng khá .. hehe :012:

DualCore
23-12-2010, 08:50 PM
Mấy chú làm thêm mấy bài cơ bản cho anh học với. Mai thi rùi mà chả có được mấy trong đầu hết.

minhtam
23-12-2010, 09:36 PM
Bài Tập Mảng về tìm Số Hoàn Hảo

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

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int a, i = 0, j = 0, s = 1, m = 0;
Console.WriteLine(" nhap so phan tu");
a = int.Parse(Console.ReadLine());
int[] mang = new int[a];
for (i = 0; i < a; i++)
{
Console.WriteLine(" phan tu thu {0} :", i);
mang[i] = int.Parse(Console.ReadLine());
}
for (i = 0; i < a; i++)
{
s = 1;
for (j = 2; j < mang[i]; j++)
{
if (mang[i] % j == 0)
{
s = s + j;
}
}
if (mang[i] == s&& mang[i] !=1)
{
Console.WriteLine(" {0} la so hoan hao", mang[i]);
m = m + 1;
}
}
Console.WriteLine(" so luong shh la {0}", m);
Console.ReadLine();
}
}
}

lt_forever
23-12-2010, 09:39 PM
Lớp : Rút gọn phân số

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

namespace phan_so
{
class phanso
{
int tuso;
int mauso;
public phanso()
{
tuso = 0;
mauso = 0;
}
public phanso(int ts, int ms)
{
tuso = ts;
mauso = ms;
}
public void Nhap ()
{
Console.WriteLine(" Chuong trinh rut gon phan so");
Console.Write("Nhap tu so :");
tuso = int.Parse(Console.ReadLine());
Console.Write("Nhap mau so :");
mauso = int.Parse(Console.ReadLine());
}
public void Xuat()
{
Console.WriteLine("Tu so la : {0}",tuso);
Console.WriteLine("Mau so la : {0}",mauso);
Console.ReadLine();
}
public int USC (int a, int b)
{
while(a!=b)
{
if (a > b)
{
a = a - b;
}
else
b = b - a;
}
return a;
}

public void rutgon()
{
int i = USC(tuso, mauso);
tuso = (int) tuso / i;
mauso = (int) mauso / i;
}

}
class Program
{
static void Main(string[] args)
{
phanso ps = new phanso();
ps.Nhap();
ps.Xuat();
ps.rutgon();
ps.Xuat();
Console.ReadLine();
}
}
}

cun ne
23-12-2010, 09:44 PM
hehe mình cũng mạng phép đưa lên một bài về lớp có gì xin bà con chỉ bảo :expect:


//thong tin cua n sinh vien
using System;
using System.Collections.Generic;
using System.Text;

namespace thong_tin_n_sinhvien
{
class Program
{
class sinhvien
{
public sinhvien()
{
}
private string hoten;
private string namsinh;
private float diemLTM, diemlinux, diemTB;
public void nhap()
{
int i = 0;
Console.WriteLine("nhap thong tin sinh vien thu:{0}", i ++);
Console.WriteLine("ho va ten {0}");
hoten = Console.ReadLine();
Console.WriteLine("nhap nam sinh");
namsinh = Console.ReadLine();
Console.WriteLine("nhap diem LTM:");
diemLTM = float.Parse(Console.ReadLine());
Console.WriteLine("nhap diem linux");
diemlinux = float.Parse(Console.ReadLine());
}
public void xuat()
{
Console.WriteLine("thong tin sinh vien vua nhap la");
Console.WriteLine("ho va ten: {0}", hoten);
Console.WriteLine("nam sinh{0}:", namsinh);
Console.WriteLine("diem lap trinh: {0}", diemLTM.ToString());
Console.WriteLine("diem linux: {0}", diemlinux.ToString());
Console.WriteLine("Diem trung binh: {0}", diemtb().ToString());//xuat diem cho nay luon
}
public float diemtb()//xuat thang cai nay tren ham xuat luon, ko can xuong duoi
{
return (diemlinux + diemLTM) / 2;
}
}
static void Main(string[] args)
{
sinhvien[] DS;
int i, n;
Console.WriteLine("nhap so luong thong tin can thuc hien:");
n = int.Parse(Console.ReadLine());
DS = new sinhvien[n];
for (i = 0; i < n; i++)
{
DS[i] = new sinhvien();
DS[i].nhap();
}
for (i = 0; i < n; i++)
{
//DS[i] = new sinhvien(); khong nen tao them mot doi tuong moi cho mang neu ko se ko co ket qua
DS[i].xuat();
}
}
}
}

cun ne
23-12-2010, 09:46 PM
đề nghị ku lùn đưa máy bài cơ bản đi hic. mấy bài này cao siêu quá e không hiểu :022:

sunboy
23-12-2010, 11:43 PM
Bài Tập Mảng về tìm Số Hoàn Hảo

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

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int a, i = 0, j = 0, s = 1, m = 0;
Console.WriteLine(" nhap so phan tu");
a = int.Parse(Console.ReadLine());
int[] mang = new int[a];
for (i = 0; i < a; i++)
{
Console.WriteLine(" phan tu thu {0} :", i);
mang[i] = int.Parse(Console.ReadLine());
}
for (i = 0; i < a; i++)
{
s = 1;
for (j = 2; j < mang[i]; j++)
{
if (mang[i] % j == 0)
{
s = s + j;
}
}
if (mang[i] == s&& mang[i] !=1)
{
Console.WriteLine(" {0} la so hoan hao", mang[i]);
m = m + 1;
}
}
Console.WriteLine(" so luong shh la {0}", m);
Console.ReadLine();
}
}
}


Bé Tâm đặt trong thẻ PHP nên lỗi một số chỗ. Mình edit xíu lại nè..


//Tim so hoan hao
for (i = 0; i < n; i++)
{
int s = 0;
for (int j = 1; j < mang1[i]; j++)
{
if (mang1[i] % j == 0)
s = s + j;
}
if (mang1[i] == s )
{
Console.WriteLine("{0} la so hoan hao!", mang1[i]);
m = m + 1;
}