Exemple #1
0
    static void Type()
    {
        //Type = "int" | "void" | "bool" | "char" .
        IntSet allowedTypes = new IntSet();

        allowedTypes.Incl(intSym);
        allowedTypes.Incl(voidSym);
        allowedTypes.Incl(boolSym);
        allowedTypes.Incl(charSym);

        Accept(allowedTypes, "valid data type expected");
    }
Exemple #2
0
    public static void Main(string[] args)
    {
        //                                        check that arguments have been supplied
        if (args.Length != 2)
        {
            Console.WriteLine("missing args");
            System.Environment.Exit(1);
        }
        //                                        attempt to open data file
        InFile data = new InFile(args[0]);

        if (data.OpenError())
        {
            Console.WriteLine("cannot open " + args[0]);
            System.Environment.Exit(1);
        }
        //                                        attempt to open results file
        OutFile results = new OutFile(args[1]);

        if (results.OpenError())
        {
            Console.WriteLine("cannot open " + args[1]);
            System.Environment.Exit(1);
        }
        //                                        various initializations
        int    total       = 0;
        IntSet mySet       = new IntSet();
        IntSet smallSet    = new IntSet(1, 2, 3, 4, 5);
        string smallSetStr = smallSet.ToString();
        //                                        read and process data file
        int item = data.ReadInt();

        while (!data.NoMoreData())
        {
            total = total + item;
            if (item > 0)
            {
                mySet.Incl(item);
            }
            item = data.ReadInt();
        }
        //                                        write various results to output file
        results.Write("total = ");
        results.WriteLine(total, 5);
        results.WriteLine("unique positive numbers " + mySet.ToString());
        results.WriteLine("union with " + smallSetStr
                          + " = " + mySet.Union(smallSet).ToString());
        results.WriteLine("intersection with " + smallSetStr
                          + " = " + mySet.Intersection(smallSet).ToString());

        /* or simply
         * results.WriteLine("union with " + smallSetStr + " = " + mySet.Union(smallSet));
         * results.WriteLine("intersection with " + smallSetStr + " = " + mySet.Intersection(smallSet));
         */

        results.Close();
    } // Main
Exemple #3
0
    public static void Main(string [] args)
    {
        const int Max = 32000;
        IntSet    uncrossed = new IntSet(Max);         // the sieve //bool[] uncrossed = new bool[Max];
        int       i, n, k, it, iterations, primes = 0; // counters

        IO.Write("How many iterations? ");
        iterations = IO.ReadInt();
        bool display = iterations == 1;

        IO.Write("Supply largest number to be tested ");
        n = IO.ReadInt();
        if (n > Max)
        {
            IO.Write("n too large, sorry");
            System.Environment.Exit(1);
        }
        IO.WriteLine("Prime numbers between 2 and " + n);
        IO.WriteLine("-----------------------------------");
        for (it = 1; it <= iterations; it++)
        {
            primes = 0;
            for (i = 2; i <= n; i++)                // clear sieve
            {
                uncrossed.Incl(i);                  //uncrossed[k] = true;
            }
            for (i = 2; i <= n; i++)                // the passes over the sieve
            {
                if (uncrossed.Contains(i))          //uncrossed[i]
                {
                    if (display && primes % 8 == 0) // ensure line not too long
                    {
                        IO.WriteLine();
                    }
                    primes++;
                    if (display)
                    {
                        IO.Write(i, 6);
                    }
                    k = i;                     // now cross out multiples of i
                    do
                    {
                        uncrossed.Excl(k); //uncrossed[k] = false;
                        k += i;
                    } while (k <= n);
                }
            }
            if (display)
            {
                IO.WriteLine();
            }
        }
        IO.Write(primes + " primes");
    } // main
Exemple #4
0
        static IntSet getCol(int col)
        {
            IntSet newInset = new IntSet();

            for (int i = 0; i < 9; i++)
            {
                if (board[i][col] != 0)
                {
                    newInset.Incl(board[i][col]);
                }
            }
            return(newInset);
        }
Exemple #5
0
        //converts a column in the assignedBoard into a set
        static IntSet getColSet(string[,] assignedBoard, int col)
        {
            IntSet retSet = new IntSet();

            for (int row = 0; row < 9; row++)
            {
                if (assignedBoard[row, col] != "..") //if it's number
                {
                    int num = Convert.ToInt32(assignedBoard[row, col].Trim());
                    retSet.Incl(num);
                }
            }
            return(retSet);
        }
Exemple #6
0
        //converts a row in the assingedBoard into a set
        static IntSet getRowSet(string[,] assignedBoard, int row)
        {
            IntSet retSet = new IntSet();

            for (int col = 0; col < 9; col++)
            {
                if (assignedBoard[row, col] != "..")
                {
                    int num = Convert.ToInt32(assignedBoard[row, col].Trim());
                    retSet.Incl(num);
                }
            }
            return(retSet);
        }
Exemple #7
0
        static IntSet getBlock(int row, int col)
        {
            int blockRow = -1;
            int blockCol = -1;

            if (row >= 0 && row <= 2)
            {
                blockRow = 0;
            }
            else if (row > 2 && row < 6)
            {
                blockRow = 1;
            }
            else
            {
                blockRow = 2;
            }

            if (col >= 0 && col <= 2)
            {
                blockCol = 0;
            }
            else if (col > 2 && col < 6)
            {
                blockCol = 1;
            }
            else
            {
                blockCol = 2;
            }

            IntSet blockNums = new IntSet();

            for (int i = 3 * blockRow; i < (3 * blockRow) + 3; i++)
            {
                for (int j = 3 * blockCol; j < (3 * blockCol) + 3; j++)
                {
                    if (board[i][j] != 0)
                    {
                        blockNums.Incl(board[i][j]);
                    }
                }
            }
            return(blockNums);
        }
Exemple #8
0
        static IntSet getCurrentSet(int [,] block) //gets all the possible suggestions for a block
        {
            IntSet retSet = new IntSet();

            if (block == null) //if the block is null it means it is already assinged and has no set of suggested values
            {
                return(retSet);
            }
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (block[i, j] != 0)
                    {
                        retSet.Incl(block[i, j]);
                    }
                }
            }
            return(retSet);
        }
Exemple #9
0
        //converts a block in the assingedBoard into a set
        static IntSet getBlockSet(string[,] assignedBoard, int row, int col)
        {
            IntSet retSet = new IntSet();

            int startRow = row - (row % 3);
            int startCol = col - (col % 3);

            for (int r = startRow; r < startRow + 3; r++)
            {
                for (int c = startCol; c < startCol + 3; c++)
                {
                    if (assignedBoard[r, c] != "..") //if it's number
                    {
                        int num = Convert.ToInt32(assignedBoard[r, c].Trim());
                        retSet.Incl(num);
                    }
                }
            }

            return(retSet);
        }
    public static void Main(string[] args)
    {
        const int Max = 32000;
        IntSet    uncrossed = new IntSet(Max);
        int       i, limit, k, it, iterations, primes = 0; // counters

        IO.Write("How many iterations? ");
        iterations = IO.ReadInt();
        bool display = true;

        IO.Write("Supply largest number to be tested ");
        limit = IO.ReadInt();
        if (limit > Max)
        {
            IO.Write("limit too large, sorry");
            System.Environment.Exit(1);
        }
        for (it = 1; it <= iterations; it++)
        {
            primes = 0;
            for (i = 2; i <= limit; i++)
            {
                // clear sieve
                uncrossed.Incl(i);
            }
            for (i = 2; i <= limit; i++)
            {               // the passes over the sieve
                if (uncrossed.Contains(i) == true)
                {
                    primes++;
                    k = i;                               // now cross out multiples of i
                    do
                    {
                        uncrossed.Excl(k);
                        k += i;
                    } while (k <= limit);
                }
            }
        }
        uncrossed.Incl(1);
        IntSet primeSet = new IntSet(uncrossed);

        IO.WriteLine("here");

        for (i = 2; i <= limit; i++)                 // clear sieve
        {
            uncrossed.Incl(i);
        }
        for (i = 3; i <= limit; i += 2)              // exclude reference to odd integers
        {
            uncrossed.Excl(i);
        }
        for (i = 4; i <= limit; i += 2)
        {               // the passes over the sieve in even numbers greater than 4
            if (uncrossed.Contains(i) == true)
            {
                for (int a = 1; a <= limit; a++)
                {
                    for (int b = 1; b <= limit; b++)
                    {
                        if (primeSet.Contains(a) && primeSet.Contains(b))
                        {
                            IO.WriteLine(a);
                            IO.WriteLine(b);
                            IO.WriteLine(i);
                            if (a + b == i)
                            {
                                IO.WriteLine("N = A + B: -> " + i.ToString() + " = " + a.ToString() + " + " + b.ToString());
                            }
                        }
                    }
                }
            }
            if (display)
            {
                IO.WriteLine();
            }
        } // main
    }
Exemple #11
0
    public static void Main(string[] args)
    {
        //List<string> temp = new List<string> { };
        //Screen MainScreen = new Screen();
        Screen.ClrScr();
        Screen.SetWindowSize(Console.WindowWidth, Console.WindowHeight);
        //Screen.DefaultColor();
        Screen.TextColor(0);
        Screen.BackgroundColor(3);
        InFile data = new InFile(args[0]);

        if (data.OpenError())
        {
            Console.WriteLine("cannot open " + args[0]);
            System.Environment.Exit(1);
        }
        int    i       = 0;
        int    tempInt = 9 * 9;
        IntSet mySet   = new IntSet(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List <IntSet>         mySets = new List <IntSet>();
        List <List <IntSet> > board  = new List <List <IntSet> >();

        //bool SolutionStarts = false;
        while (i < tempInt)
        {
            IntSet tempSet = new IntSet();
            int    temp    = data.ReadInt();
            tempSet.Incl(temp);
            if (temp != 0)
            {
                mySets.Add(tempSet);
            }
            if (temp == 0)
            {
                mySets.Add(mySet);
            }
            IO.Write(temp);
            i = i + 1;
            if (i != 0 && i % 9 == 0)
            {
                IO.Write('\n');
                board.Add(mySets);
                List <IntSet> tempSet2 = new List <IntSet>();
                mySets = tempSet2;
            }
        }
        IO.Write('\n');
        //IO.WriteLine(mySets.Count);
        //IO.WriteLine(board[1][0]);
        //IO.WriteLine(board[2][0]);
        IO.WriteLine("Please may you about to begin a new Game of Sudoku.");
        writeBoard();
        IO.WriteLine("Please may you you either type in (S)tart or (Q)uite");
        char T = IO.ReadChar();

        if (T == 'S' || T == 's')
        {
            bool GameNotFinished = true;
            while (GameNotFinished)
            {
                // Get input from user
                string [] userInput = getInputFromUser();
                while (!validateInput(userInput))
                {
                    IO.WriteLine("Invalid Input.");
                    userInput = getInputFromUser();
                }
                // List of valid input
                List <int> userMove = convertToIntList(userInput);
            }
        }
        if (T == 'q' || T == 'Q')
        {
            Screen.ClrScr(); System.Environment.Exit(1);
        }
    }