Tutorial C# Sharp - Learn to program with performing exercises C# Sharp

Tutorial C# Sharp

Learn to program with performing exercises C# Sharp

Pascal to C# translator - Tutorial C# Sharp
Tutorial C# Sharp4,85581245

Pascal to C# translator - Tutorial C# Sharp


Lesson 8:

File management


Exercise 8.15:

Pascal to C# translator


Objetive:

Create a basic Pascal to C# translator. It will accept program such as:

example program;

var
i: integer;
max: integer;

begin
writeLn('How many times?');
readLn(max);
for i := 1 to max do
writeLn('Hola');
end.

The steps you must follow are:
Read from beginning to end a text file, whose name will be entered by the user in command line or in an interactive way: up to 2 points.

Dump the contents to another text file, whose name will be the same, but with ".cs" extension instead of ".pas": up to 4 points.

Replace "WriteLn" with "Console.WriteLine", " = "with "==", " := " with "=", simple quotes with double quotes, "begin" with "{" and "end;", "end.", "end" (in that order) with "}", : up to 6 points.

Replace "program x;" with "class x {" followed with "Main", Replace "readln(x)" with "x=Convert.ToInt32(Console.RadLine())" ("x" must be any other identifier): up to 8 points.

Eliminate "var" and replace "x: integer" with "int x" (but "x" must be any other identifier): up to 9 points. Give a proper format to "for": up to 10 points.

Create a compilable C# source from the previous Pascal source and similar ones: up to 11 points.


Source Code:


using System;
using System.IO;
namespace PascalToCSharp
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter name file: ");
            string fileName = Console.ReadLine();


            if (File.Exists(fileName))
            {
                StreamReader filePascal = File.OpenText(fileName);
                StreamWriter fileCSharp = File.CreateText(fileName.Substring(0, fileName.Length - 3) + "cs");
                string line;
                do
                {
                    line = filePascal.ReadLine();
                    if (line != null)
                    {
                        line = line.Replace("writeLn", "Console.WriteLine");
                        line = line.Replace(" = ", "==");
                        line = line.Replace(" :=", "=");
                        line = line.Replace("'", "\"");
                        line = line.Replace("begin", "{");
                        line = line.Replace("end;", "}");
                        line = line.Replace("end.", "}");
                        line = line.Replace("end", "}");

                        if ((line.Contains("program ")) &&
                             (line.Substring(line.Length - 1) == ";"))
                        {
                            line = line.Replace("program ", "class ");
                            line = line.Replace(";", "\n{\n static void Main()\n{");
                        }

                        if (line.Contains("readLn("))
                        {
                            line = line.Replace("readLn(", "");
                            line = line.Replace(");", "");
                            line += " = Convert.ToInt32(Console.RadLine());";
                        }

                        line = line.Replace("var", "");
                        if (line.Contains(": integer;"))
                        {
                            line = line.Replace(": integer;", "");
                            line = "int " + line.Trim() + ";";
                        }

                        /* if ((line.Contains("for ")) &&
                            (line.Contains(" to ")) && 
                            (line.Contains(" do ")))
                         {
                             line = line.Replace("for ", "for (");
                             line = line.Replace(" to", "");
                             line = "int " + line.Trim() + ";";
                         }
                 */

                        fileCSharp.WriteLine(line);
                    }
                }
                while (line != null);
                filePascal.Close();
                fileCSharp.Close();
            }
        }
    }
}
Exercisey 8.15






Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.