菜鸟求助C#高手
- USE2初学C#,求教个初级问题
假设有一个文本 1.txt,内容为一批电话号码从上到下逐行排列
如:
13307105572
13307105623
13307105675
请教应该如何用C#实现以颠倒的顺序生成一个新文本?
13307105675
13307105623
13307105572
谢谢啦~!! - sivalei参照堆栈算法,后进先出吧。。。。
我是一点codeing能力都没有了
#define MAX_SIZE 100
typedef int DATA_TYPE;
struct stack
{
DATA_TYPE data[MAX_SIZE];
int top;
};
[本帖最后由 sivalei 于 2006-9-19 17:17 编辑] - sivalei你到csdn上发问,保准5分钟就有答案。。。。在tg。。。只有喷子:D
- USE2呵呵 谢谢LS啦~!:D
- kiler很简单,给楼主现写一个
using System;
using System.Collections;
using System.IO;
namespace ReadFile
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
ArrayList al = new ArrayList();
//把文件里面的内容按行读出
try
{
using (StreamReader sr = new StreamReader("1.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
al.Add(line);
}
}
}
catch (Exception ex)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(ex.Message);
}
//反转Arraylist
al.Reverse();
for(int i=0;i<al.Count;i++)
{
System.Console.WriteLine(al.ToString());
}
System.Console.ReadLine();
}
}
} - USE2楼上强啊.......简单明了
比我用数组写的这个高级多了= =|||
[本帖最后由 use2 于 2006-9-19 20:00 编辑] - kiler呵呵,靠这个吃饭的。
建议楼主多看看Msdn,上面有很多写的很规范的代码。 - helinusing System;
using System.Collections;
using System.IO;
namespace ReadFile
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
ArrayList al = new ArrayList();
//把文件里面的内容按行读出
try
{
using (StreamReader sr = new StreamReader("1.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
al.Add(line);
}
}
}
catch (Exception ex)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(ex.Message);
}
//反转Arraylist
//al.Reverse(); 反转也是排序, 生成出来的次序有问题的吧
for(int i=al.Count - 1;i>=0;i--)
{
System.Console.WriteLine(al.ToString());
}
System.Console.ReadLine();
}
}
} - kiler我刚才查了一下Reverse()方法是将整个 ArrayList 中元素的顺序反转。
也试了几个例子,没有问题。 - helinKiler, 查了SDK, 是我弄错了
- helin如果读进来的时候, 调用ArrayList.Insert(0, line)应该也可以吧, 省去Reverse()这一步了