PDA

View Full Version : sử lý thông tin trên 2 file têp



bientho88
10-06-2009, 19:57
em đang làm đề tài quản lý điện cua một thôn xã có một chỗ em chưa làm được. khi bắt tay vào làm thấy không đúng ý mình cho lắm, em có hai file file 1(STT,ho va ten,ma, tien),file 2 luu thông tin các các hộ còn nợ(STT,ho va ten,ma, tien no).em muốn công thêm tiền nợ của file vào tiền trong file 1 nhưng không biết tại sao không làm được.(chu ý:nếu không có nợ thì hộ gia đình trong file 1 không xuất hiện trong file 2).căn cứ vào mã ở 2 file để cộng số số tiền cùng số nợ.(từ file 1 ta có hộ gia đình 1 có mã M1 bây giờ ta làm một việc là tìm trong file 2 xem có hộ gia đình 1 không nếu có thì cộng thêm số nợ đó vào với số tiền,không thì trả về giá trị số tiền trong file 1) và code em làm như sau(chắc sai):


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

namespace duyet2file
{
class Node
{
public int n;
public string Hovaten, ma;
public int tien;
public Node Next;
}
class List
{
public static Node dau, cuoi;
public static void Nhap1()
{
using(StreamWriter f=File.CreateText(@"dl1.txt"))
{
int t;
do{
Console.Write("Nhap so thu tu: ");
t=int.Parse(Console.ReadLine());
if(t!=0)
{
Node P=new Node();
P.Next=null;
P.n=t;
f.Write(P.n.ToString()+'#');
Console.Write("Nhap Ho ve ten: ");
P.Hovaten=Console.ReadLine();
f.Write(P.Hovaten.ToString()+'#');
Console.Write("Nhap ma: ");
P.ma = Console.ReadLine();
f.Write(P.ma.ToString()+'#');
Console.Write("Nhap so tien: ");
P.tien=int.Parse(Console.ReadLine());
f.WriteLine(P.tien.ToString());
if(dau==null)
dau=cuoi=P;
else
{
cuoi.Next=P;
cuoi=P;
}
}
}while(t!=0);
}
}
public static void Nhap2()
{
using (StreamWriter f = File.CreateText(@"dl2.txt"))
{
int t;
do
{
Console.Write("Nhap so tru tu2: ");
t = int.Parse(Console.ReadLine());
if (t != 0)
{
Node P = new Node();
P.Next = null;
P.n = t;
f.Write(P.n.ToString() + '#');
Console.Write("Nhap Ho ve ten: ");
P.Hovaten = Console.ReadLine();
f.Write(P.Hovaten.ToString() + '#');
Console.Write("Nhap ma: ");
P.ma = Console.ReadLine();
f.Write(P.ma.ToString()+'#');
Console.Write("Nhap so tien: ");
P.tien = int.Parse(Console.ReadLine());
f.WriteLine(P.tien.ToString());
if (dau == null)
dau = cuoi = P;
else
{
cuoi.Next = P;
cuoi = P;
}
}
} while (t != 0);
}
}
public static void Tongtien()
{
FileStream f1 = new FileStream(@"dl1.txt", FileMode.Open, FileAccess.Read);
StreamReader w1 = new StreamReader(f1);
Node H1 = dau;
FileStream f2 = new FileStream(@"dl2.txt", FileMode.Open, FileAccess.Read);
StreamReader w2 = new StreamReader(f2);
Node H2 = dau;
while(!w1.EndOfStream)
{
string s = w1.ReadLine();
string[] tam1 = s.Split('#');
H1.n = int.Parse(tam1 [0]);
H1.Hovaten=tam1[1];
H1.ma = tam1[2];
H1.tien = int.Parse(tam1[3]);
Console.WriteLine("so tien cac ho no trong thang nay: {0}", H1.tien);
}
while (!w2.EndOfStream)
{
string m = w2.ReadLine();
string[] tam2 = m.Split('#');
H2.n = int.Parse(tam2[0]);
H2.Hovaten = tam2[1];
H2.ma = tam2[2];
H2.tien = int.Parse(tam2[3]);
Console.WriteLine("So tien no cac ho trong thang truoc: {0}", H2.tien);
}
while (H1 != null)
{
H2 = H1.Next;

while (H2 != null)
{
if (H1.ma == H2.ma)
{
//int tien1= H1.tien + H2.tien;
Console.WriteLine("tong so ca tien no:{0}", H1.tien+H2.tien);

}
else
{
// int tien2 = H1.tien;
Console.WriteLine("cac ho khong no: {0}", H1.tien);
}
H2 = H2.Next;
} H1 = H1.Next;
}

w1.Close();
f1.Close();
w2.Close();
f2.Close();


}
static void Main(string[] args)
{
Nhap1();
Nhap2();
Tongtien();
Console.ReadKey();
}
}
}

[=========> Bổ sung bài viết <=========]

không có bác nào giúp em ah.