Sample PMT Code

 

using System;

using System.IO;

 

namespace SamplePMT

{

       /// <summary>  // XML tag for documentation – not covered

       / Summary description for BinaryOperations.

       /// </summary>

 

       public class BinaryOperations

       {

              public BinaryOperations()

              {

              }

 

              static void Main()

              {

                     StreamReader FileReader;

                     FileReader = new StreamReader("input.txt");

                        / StreamReader is a stream class for

                     / text input from a file – p.760/2

 

                     string sLine;

                     string [] split;     // string array declared

                     string b1, b2;

                     char delm = ' ';    

                        // Delimiter delm declared and initialized to ‘ ’

 

                     int inputLines = Int32.Parse(FileReader.ReadLine());

                        // Declare inputLines and initialize it to the read

                        // value FileReader.ReadLine(), that is, to the value

                        // read from the first line of the file (‘5’ in the

                        // sample input.

                        // For Int32.Parse see p.75/-2.

 

                     for (int i=0;  i<inputLines; i++)

                           //go over all remaining lines of text input file

                     {

                           sLine = FileReader.ReadLine(); // Read line of text

                           split = sLine.Split(delm);

                                  // Split sLine using delimiter; this gives:

                           // & or | in split[0],

                           // 1st binary number in split[1], and

                                  // 2nd binary in split[2];

 

                           char [] result= new char[split[1].Length];

                                  // Declare a character array and initialize

                                  // it to the length of the split array

                                  // (e.g., length 2 for ‘10’ in the 2nd and

                                  // 3rd lines of the sample input)

 

                           b1 = split[1];   // 1st binary number

                           b2 = split[2];   // 2nd binary number

                          

                           if (split[0] == "&")  // apply bit-wise ‘&’ operation

                           {

                                  for(int j =0; j<split[1].Length; j++)

                                      // for each position j of b1 and b2

                                  {

                                      // perform bitwise ‘&’ operation

                                         if ((b1[j] == '1') & (b2[j] =='1'))

                                                result[j] = '1';

                                         else

                                                result[j] ='0';

                                  }

                           }

                           else   // apply bit-wise ‘|’ operation

                           {

                                  for(int j =0; j<split[1].Length; j++)

                                      // for each position j of b1 and b2

                                  {

                                      // perform bitwise ‘&’ operation

                                         if ((b1[j] == '0') & (b2[j] =='0'))

                                                result[j] = '0';

                                         else

                                                result[j] ='1';

                                  }

 

                           }

                           Console.Write(b1 + " " +

                                          split[0] + " " +

                                          b2 + " = ");

                               // Output:

                               // 1st binary number and a space,

                               // the operator (in split[0]) and a space,

                               // the 2nd binary number and ' = ' (includes 2

                               // spaces).

                               // The decimal equivalent of the result still

                               // not displayed.

                                                      

                           //convert binary result ‘result’ to decimal

 

                           double decValue=0;

 

                           for(int j =0; j<result.Length; j++)

                           {

                                  int k = Int32.Parse(result[j].ToString());

                                      // Array element result[j] stores (j+1)-st

                                      // digit of binary result (e.g., result[0]

                                      // stores 1st digit of binary result).

                                      // Convert result[j] to string, then

                                      // convert string to integer.

                                      // E.g., a binary char “1” becomes

                                      // a string “1”, and then an integer “1”.

 

                                  double p = Math.Pow(2.0,(result.Length)-j-1);

                                      // Calculate what is the power to which 2

                                      // should be raised.

                                      // Follow step-by-step to understand this.

 

                                  decValue += k * p;

                           }

                           Console.WriteLine(decValue);

                               // Now the decimal equivalent of the result (not

                               // dispalayed above) is displayed.

                     } // end for

              } // end of Main

       } // of public class BinaryOperations

} // end of namespace SamplePMT