locked
Im new and i need help RRS feed

  • Question

  • i have a problem... i have to do a program that recives 2 txt files... the first look like this:

    --------------------------------
    1
    2
    1
    3                                note: i dont know how many line it got or which is the biggest number
    2
    1
    1
    1
    3
    2
    --------------------------------

    the second is like this:

    --------------------------------
    1 %
    2 $
    3 (                           note: i also dont know how many lines it got or wich is the biggest number
    4 )
    5 &
    -------------------------------

    and i am suposed to generate a 3rd table like this:

    -------------------------------
    %: 5 times
    $: 7 times
    (: 0 times
    ): 1 times
    ------------------------------

    so... can u guys help me by giving me some ideas like "do this and that"... im trying to learn... theres only 1 thing... it has to be a console app... tks alot
    Tuesday, October 21, 2008 1:59 PM

Answers

  • Code Snippet

    class Program
    {
    static void escreve(int[] codigo, string[] simbol)
    {
    Console.Write("Escreva o nome que deseja dar ao arquivo final: ");
    string path = Console.ReadLine();
    using (StreamWriter z = File.CreateText(path))
    {
    for (int n = 0; n < 105; n++)
    {
    string s = simbol[n] + ':' + codigo[n];
    z.WriteLine(s);
    }
    }
    }

    static void Main(string[] args)
    {
    int[] codigo = new int[105];
    string[] simbol = new string[105];
    using (StreamReader r = File.OpenText(@"c:\teste1.txt"))
    {
    while (!r.EndOfStream)
    {
    int n = int.Parse(r.ReadLine());
    codigo[n] = codigo[n] + 1;
    //Console.WriteLine(codigo[n]);
    }
    }
    using (StreamReader w = File.OpenText(@"c:\cod2.txt"))
    {
    int i = 0;
    while (!w.EndOfStream)
    {
    simbol[i] = w.ReadLine();
    i = i + 1;
    }
    }
    escreve(codigo, simbol);
    }
    }


    after alot of pain i came up with that... and it works...
    Tuesday, October 21, 2008 6:05 PM

All replies

  •  LeonardoIndex wrote:
    i have a problem... i have to do a program that recives 2 txt files... the first look like this:

    --------------------------------
    1
    2
    1
    3                                note: i dont know how many line it got or which is the biggest number
    2
    1
    1
    1
    3
    2
    --------------------------------

    the second is like this:

    --------------------------------
    1 %
    2 $
    3 (                           note: i also dont know how many lines it got or wich is the biggest number
    4 )
    5 &
    -------------------------------

    and i am suposed to generate a 3rd table like this:

    -------------------------------
    %: 5 times
    $: 7 times
    (: 0 times
    ): 1 times
    ------------------------------

    so... can u guys help me by giving me some ideas like "do this and that"... im trying to learn... theres only 1 thing... it has to be a console app... tks alot

    1. Create an Dictionary that will hold the numbers from the first file as keys and the number of apperences as values. As you read trough the first file, increment the value if you already have the key or add the key with the value 1 if the key is not in the Dictionary.

    2. Read the second file in another dictionary and have the numbers as keys and the signs as values.

    3 Go trough every entry in the secound dictionary and print the value. Look for the key corresponding to this value in the first dictionary's keys and print the corresponding value for this key near the value printed earlier.

     

    You'll get what you want.

    Tuesday, October 21, 2008 3:15 PM
  • isnt there a less complicated way to do this!?!? im used to program in Pascal... so i'm thinking about using pointers and creating a list or a pile... but that is too advance in C# language for me... so what about an matrix (is it how its spelled?)? each collum would be a code...line 1 would be like the count and line 2 the correspondent "value"... get it?
    Tuesday, October 21, 2008 3:23 PM
  •  LeonardoIndex wrote:
    isnt there a less complicated way to do this!?!? im used to program in Pascal... so i'm thinking about using pointers and creating a list or a pile... but that is too advance in C# language for me... so what about an matrix (is it how its spelled?)? each collum would be a code...line 1 would be like the count and line 2 the correspondent "value"... get it?
    The matrix thing will work, but you'll have to read the file multiple times, to see what every index means and this is time consuming.

    Do not use pointers in C#. They are supported (trough usafe declaration) but they will not make your work easier. There could be a betters solution then the one posted by me, but I can't think at it right now, as it's late and I have to go home. Anyways, my solution is a valid one. I don't know if it's the best, but it's valid 

    Tuesday, October 21, 2008 3:30 PM
  • i need to read a file like this:
    -------------------------
    1 (
    2 )
    3 *
    4 &
    ------------------------
    and i want to put the numbers in my "num" variable and the symbols in my "sym" variable... how can i do it!?!?
    Tuesday, October 21, 2008 4:10 PM
  • Code Snippet

    class Program
    {
    static void escreve(int[] codigo, string[] simbol)
    {
    Console.Write("Escreva o nome que deseja dar ao arquivo final: ");
    string path = Console.ReadLine();
    using (StreamWriter z = File.CreateText(path))
    {
    for (int n = 0; n < 105; n++)
    {
    string s = simbol[n] + ':' + codigo[n];
    z.WriteLine(s);
    }
    }
    }

    static void Main(string[] args)
    {
    int[] codigo = new int[105];
    string[] simbol = new string[105];
    using (StreamReader r = File.OpenText(@"c:\teste1.txt"))
    {
    while (!r.EndOfStream)
    {
    int n = int.Parse(r.ReadLine());
    codigo[n] = codigo[n] + 1;
    //Console.WriteLine(codigo[n]);
    }
    }
    using (StreamReader w = File.OpenText(@"c:\cod2.txt"))
    {
    int i = 0;
    while (!w.EndOfStream)
    {
    simbol[i] = w.ReadLine();
    i = i + 1;
    }
    }
    escreve(codigo, simbol);
    }
    }


    after alot of pain i came up with that... and it works...
    Tuesday, October 21, 2008 6:05 PM
  • Why is your solution worst then mine:
    1. it uses fixed size arrays (aka it allocates more - or less? - memory then it actualy uses)
    2. it is more time consuming to search a fixed size array then a dictionary.

    Please take a look at the Dictionary class I've pointed you to earlier.

    Regards,
    Tuesday, October 21, 2008 9:21 PM