Task: sudokuSolver

Finally, after so many days I am building something. It is a Sudoku Solver. We are team of two, Nancy and me.

So far, we have successfully executed the C++ code, now the task is to make a GUI of the solver. After taking some suggestion from around, umm.. one person, we are working on Qt for the GUI.
We both have downloaded qtcreator on our favorite OS Ubuntu with the command: sudo apt-get intsall qtcreator.
It is working fine with a good speed. Now , we are learning some basics of Qt from this youtube channel(The new boston), then we would be developing the main application.

The code would always be available here.

-NR

Advertisements

new, No Memory Allocation

via Dynamic memory – C++ Tutorials.

When new throws exception

#include<iostream>
#include<new>

using namespace std;

int main(void)
{
     try
     {
          int * ptr = new int[100000000];
     }
     catch(bad_alloc &err)
     {
          cerr<<"Error: "<<err.what();
     }
}

When memory cannot be allocated further, then returning NULL from new expression

...
int *ptr = new (nothrow) int;
if(ptr == NULL) {cerr<<"Memory cannot be allocated.";}

If in the first case no try-catch would be used then program will crash. The first one is better than the second, because it is not required to check explicitly for the ptr to be NULL.

#define and typedef

#define is a macro in C++ used as:
#define MACRO_NAME VALUE

For example, `#define SIZE 100`,
Macro makes the replacements at the time of preprocessing, before compilation. All the values are put in expanded code before usual compilation starts.

typedef gives another name to a type and it is used as:
typedef TYPE NEW_TYPE;

Unlike #define it is interpreted by compiler and works as a new name for `TYPE`

See they are looking similiar, if I write #define TYPE int or typedef int INT what’s the difference here.

Difference: #define CHAR_PTR char* and typedef char* CHAR_PTR, lets take them one by one:

#define CHAR_PTR char*
...
CHAR_PTR a, b; //typeid(a).name = char* and typeid(b).name = char
               //because it is simply a replacement
_________________________________________________________________

typedef char* CHAR_PTR;
...
CHAR_PTR a,c; //typeid(a).name() = typeid(b).name = char*

Sources: StackOverflow, Let Us C, The C Programming Language -K&R

Toy C++ program for custom string conversion

I am working on lexer part of compilers right now, the task I was doing is to putting up keyword definitions in cool.flex file. Keywords were given in another file in this format:

//for example keyword is class
#define CLASS 234

and I am supposed to be writing it in cool.flex like this:

//keywords are case-insensitive
[cC] + [lL] + [aA] + [sS] + [sS]        {return (CLASS);}

Instead of doing all this by hand I wrote this C++ program and it worked fine.

#include &lt;iostream&gt;
#include &lt;fstream&gt;

using namespace std;

int main()
{
 string str, str1;
 fstream myfile("test.file");
 ofstream file("temp.file");
 while(getline(myfile, str))
 {
 str1="";
 for(int i=0;i&lt;str.length();i++)
 if(isupper(str[i]) && str[i+1]!=' ')
 {
 char ch=putchar(tolower(str[i]));
 file&lt;&lt;"["&lt;&lt;ch&lt;&lt;str[i]&lt;&lt;"]+";
 str1.push_back(str[i]);
 }
 else if(isupper(str[i]) && str[i+1]==' ')
 {
 char ch=putchar(tolower(str[i]));
 file&lt;&lt;"["&lt;&lt;ch&lt;&lt;str[i]&lt;&lt;"]";
 str1.push_back(str[i]);
 }
 file&lt;&lt;" { return("&lt;&lt;str1&lt;&lt;");} \n";
 }
 return 0;
}