Friday, April 4, 2008

Database

Finally, today I got the data from my database displayed in my window where I want it to be displayed. But. I can't make it display in the actual program, just in a simplest odbc application. When I create a simple database with one table and a simple table with corresponding controls, everything works just perfectly. But when I try to use my database, which contains multiple tables and all of my controls, it just doesn't want to display them. In fact, it doesn't even want to run or debug the application. It might be because of the structure of the database, or relationships between tables, or I don't know. Maybe I'll just start it all again from scratch.

Thursday, March 27, 2008

odbc

Ok so I decided to switch to ODBC instead of DAO, as it seems to perform all the same functions, and it actually works. (Well, at least it doesn't give me any errors yet). So I'm just going to try that, and then we'll see. I also can't do much at home, because I don't have microsoft access there, and open office version can't create the mdb files that I need. Anyways, I think I'm getting closer to what I have planned.

Friday, March 21, 2008

Ufff... all that software!

Finally I got to the most interesting part of my program - connecting the c++ program body with the database I have. I found a lot of tutorials on that topic online, especially on microsoft msdn website. Well, as I decided to use mfc in my program (I don't want to code everything by hand), I tried to link the database and my program statically through the mfc wizard. When I tried that I ran into "Unrecognized database format". At first I thought that was because I didn't have Microsoft Access on my computer. So I installed special dao dll libraries. That didn't really work. Then I tried to install free trial of Access, but that didn't help either. Finally, different online programming forums told me to update my MFC library from Jet 3.4 to Jet 4, which I couldn't find for free anywhere. So I found the Visual Studio Service Pack 5. It says in the description that they updated the mfc in there. It takes pretty long time to install this whole thing. So, the software problems took me way more time than actual coding. I'm filling my database with the units I have for now, while the pack is installing. I really hope it will work.

Monday, March 17, 2008

research

Ok, so now I am reading the "Access Database" book (Thanks, Richard!). The book's really good. It explains all the basics of databases and their structure and how they work. It's pretty understandable, although there is a lot of terms in there. I'm hoping to finish the book as soon as possible and start working on the project. I'm going to make some sample easy databases on the way, just to get it better, and I will probably do them in Access for now. I'll figure it out about the software later.

Thursday, March 13, 2008

The Database

So I started learning all the basics of bulding and transferring data from databases. I decided to use Microsoft Access to build the database, because it's going to be pretty simple. In the program I am going to use DAO library, because that's the one that can read data from Microsoft Access files.
Following the advice of online tutorials, at first I figured out how exactly I wanted the database to work. I split the whole list of units into categories, in each category would be a 2d table, where I would paste the numbers, needed to convert from any unit to any other unit. Basically I would have the same list of units on the horizontal menu and the vertical menu. On their intersections I would put the numbers, by which I need to multiply the horizontal unit to get the vertical one.
I don't have access to any programming or database software right now, so I started just researching and going through tutorials on databases. I found a very good website with different units. Oh my god! I never thought there were so many different crazy units in the world and someone actually wrote them down! This is unbelievable! And that is only from one source! There are only 272 different units - that's just for length! How would I do that without a database?
Another thing is, the same unit in different areas is different. Like the US mile would be totally different from the Scottish mile and so on. At first I thought just to write the region in parenthesis, but then I had an idea, what if I could categorize the units not only by the measurement, but by the area, too. I'm not sure how I'm going to do that yet, I guess I'll figure it out when I get to actually building the database.
Anyways, I found out, that my program might not be as small as I thought, I have many ideas of updating it. I need to build the basics at first :)

Thursday, February 21, 2008

My new program

Ok. So I started my project itself today. I decided to make a very cool program.
For that, I think I would need to learn about databases. As I need to store a lot of information in kind of a structured way, I think a database wouldn't be a bad idea. I found some online tutorials on those:
http://www.geekgirls.com/databasics_01.htm
I'm going to research that a little. I am still going to use the shaping of windows code, I think it's really cool. I don't know about control arrays, I guess I wouldn't need them as much. But even if I do, I found something interesting today. The member variables that I found once and didn't know what to do with them, could actually serve as a way to establish an array. They are kind of like pointers, but to the controls, and they can be represented as an array, so. That works for me.

Tuesday, February 12, 2008

Shaping the windows

I just found this very cool code! I found out that MFC is good of course, but an empty Win32 would be much better, there are so much more opportunities in it. Although I can't see the dialog itself and arrange all the control elements on the form, I can do it through the code.
Therefore, I found a code to actually change the shape of the window. You would only need an original bmp image with a shape. In bmp format, the compiler sees white as blank space, so I can actually draw the shape of my window. I would just need the following code:

BOOL CreateMainWnd(HINSTANCE hInstance)
{
HFONT buttonfont; /*create windows and set fonts*/
hwMain = CreateWindowEx(0,"Window Shaping","Window Shaping", WS_OVERLAPPED,50,50,450,350,0,0,hInstance,0);
hwStatica = CreateWindowEx(0,"Button","Sant Bani",WS_CHILD | WS_VISIBLE | SS_CENTER, 100,88,50,20,hwMain,0,hInstance,0);
hwStaticb = CreateWindowEx(0,"Button","Exit",WS_CHILD | WS_VISIBLE | SS_CENTER, 194,88,50,20,hwMain,0,hInstance,0);
buttonfont = CreateFont(16,0,FW_DONTCARE,FW_DONTCARE,FW_DONTCARE,
FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_DONTCARE,"Arial");

SendMessage(hwStatica,WM_SETFONT,reinterpret_cast(buttonfont),MAKELPARAM(1, 0));
SendMessage(hwStaticb,WM_SETFONT,reinterpret_cast(buttonfont),MAKELPARAM(1, 0));

SetWindowPos(hwMain,HWND_TOPMOST,50,50,626,374, NULL);

ShowWindow(hwMain,1);

if(!hwMain)
return FALSE;
return TRUE;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
switch(wMsg) {
case WM_DESTROY:
PostQuitMessage(0); /*destroy window*/
return 0;
break;
case WM_PAINT:
HDC hdcTemp, bpHDC;
PAINTSTRUCT sentinel;
BITMAP bm;
bpHDC = BeginPaint(hwMain, &sentinel);
if(work) /*if its our first time painting, shape the window*/
{
ShowWindow(hwMain,0); /* window not visible until shaped*/
Mold(bpHDC);
ShowWindow(hwMain,1);
}
hdcTemp = CreateCompatibleDC(bpHDC); /*load and bitblt image to our hdc*/
hwBmp = LoadBitmap(hInst,MAKEINTRESOURCE(102));
SelectObject(hdcTemp,static_cast(hwBmp));
GetObject(static_cast(hwBmp),sizeof(bm),&bm);
BitBlt(bpHDC,0,-30,bm.bmWidth,bm.bmHeight,hdcTemp,0,0,SRCCOPY);
DeleteDC(bpHDC); /*delete uneeded objects to free up memory*/
DeleteDC(hdcTemp);
EndPaint(hwMain,&sentinel);
return 0;
break;
case WM_COMMAND:
/*put a couple of buttons there, just for fun*/
if(wParam == BN_CLICKED)
{
if(reinterpret_cast(lParam) == hwStatica)
ShellExecute(NULL, "open", "http://www.santbani.org", NULL, NULL, SW_SHOWNORMAL);
else if(reinterpret_cast(lParam) == hwStaticb)
DestroyWindow(hwMain);
}
return 0;
break;
case WM_LBUTTONDOWN:
SendMessage(hwMain,WM_SYSCOMMAND,SC_MOVE,0); /*tell the window the user wants to move the window*/
break;
default:
return DefWindowProc(hWnd,wMsg,wParam,lParam);
}
return FALSE;
}


And all the other usual code routines.
I also found out I need to include "windows.h" in any windows application I do. There is also a huge code for just creating a new window and showing it on the screen, which I am trying to understand by lines now. It seems not so hard, but really cool!

Thursday, February 7, 2008

Input/Output

As the control arrays still remain unsolved, I have met another problem when converting the code. The very first thing is input and output format. As I want the user to enter the numbers into textboxes, I was trying to find a code to convert all those variables. The first thing is, when I use AfxMessageBox, I can't do the same things I could with "cout". For example the line
cout << "cell(" <<>
would just give me huge error in AfxMessageBox. It says me that the first parameter should be a string value for this function. So I decided to put everything into another string variable before calling the function:
outString="cell("+(i+1)+","...."\n"

But then I find that I also need to convert the number into a string before that:
_itoa(i+1,buffer,10);

And then I find out that it doesn't convert it exactly into the type of string I want it to be converted into. So I have to use this:
...+(Char::ToString(buffer))+...

This is what the tutorial told me. But. The tutorial says I would need using namespace System for that, but my compiler doesn't like it at all. So I'm kind of stuck here.
Actually I don't really need this, as I'm going to output everything in the same textboxes on the form, but anyways, how would I do that if I needed to?
I'm going to do more research on strings.

Sunday, February 3, 2008

Control arrays

So I'm now trying to convert my sudoku solver into a windows application. I created a new MFC and put a bunch of controls on the form and "explored" whatever I could do with them without touching the code itself.
Then I started converting the console code into a windosapp code. The first thing I ran into is that I can't create a control array as easily as in Visual Basic. The default naming of the controls doesn't give me such options, whatever I tried. As I need to get data from the cells the whole thing would look like.
CEdit *pEdit1 = reinterpret_cast(this->GetDlgItem(IDC_EDIT1));
CEdit *pEdit2 = reinterpret_cast(this->GetDlgItem(IDC_EDIT2));
...
pEdit1->SetWindowText(_T(""));
pEdit2->SetWindowText(_T(""));
...

That's what happens when put the event handler for my Clear button. (Haven't started with the Solve one yet) So what I run into is that I have to write 81*2=162(!) lines of code just for this little event! So I want to make a loop for that, but the problem is that I can't make an array of pointers, because the object the pointers point to is not an array. And I couldn't find the declaration of the CEdit class, as it is probably a default one and it's hiding somewhere I can't reach. I'm now trying to create a new array of the CEdit class, so that I can put them all in an array and maybe even use the same one pointer for each of them. The problem is I can't display my new class array objects on the form, I can't put them on the control panel and just "draw", because they are not default. So it's kind of complicated. I'll do some more research on that.
At least I got how the whole windowsapp thing works.

Microsoft Visual C++

I just switched from Dev C++ to Microsoft Visual C++. This one is actually visual. So if you want to have more controls in front of your eyes, but not just in the code itself, you should use this one. It doesn't give as much freedom as Visual Basic, but at least I can see the forms and their controls. I also found out that for a beginner it's better to start with an MFC application: it writes the header and resource files automatically, so I don't have to write the codes I don't know yet. MFC is enough for the first applications, it helps to adjust.

Tuesday, January 29, 2008

Sudoku

I finally got my sudoku-solving program to work. For easy problems. I'm not really familiar with "advanced techniques" of solving sudokus, so my program can just do what I can do.
Here's the full cpp code:

#include
#include
#include
#include
#include
#include

using namespace std;

/*global variables*/
int cell[9][9];int i,j,k,x,y,a,b,c,d,first,second;
set CellSet;
set::iterator it;bool changed, assigned;

/*exit the program function*/
void exitme()
{
system ("PAUSE");
}

/*finding combinations in the squares*/
void squareMe(int &b, int &c)
{
if ((cell[c][b]!=0)&&(CellSet.count(cell[c][b])>0)) {
CellSet.erase(cell[c][b]);
changed=true;
}
}

void squareIt(int &b){
for (d=0;d<3;d++) {
if (i==0+3*d) {
for (c=i;c<(i+3);c++) squareMe(b,c);
}
else if (i==1+3*d) {
for (c=i-1;c<(i+2);c++) squareMe(b,c);
}
else if (i==2+3*d) {
for (c=i-2;c<=i;c++) squareMe(b,c);
}
}
}

int main ()
{
/*declare local variables*/
bool solved=true;
char InCell[]="";
/*clear variables*/
i=0;
j=0;
k=0;
x=0;
y=0;
a=0;
b=0;
c=0;
changed=false;
/*read the data*/
cout << "SUDOKU\n" << "Enter the problem:\n";
for (i=0;i<9;++i) {
for (j=0;j<9;++j) {
cout << "cell(" << i+1 << "," << j+1 << ") = ";
cin >> InCell[0];
if (isdigit(InCell[0])) {
cell[i][j]=atoi(InCell);
if (cell[i][j]==0) solved=false;
}
else {
cout << "Wrong data!\n";
exitme();
}
}
}
/*is there anything to solve?*/
if (solved==true) {
cout << "The problem is solved already!\n";
exitme();
}
/*general calcualtions*/
for (k=1;k<10;k++) CellSet.insert(k);
k=0;
assigned=true;
while ((solved==false)&&(assigned==true)) {
assigned=false;
for (i=0;i<9;i++) {
for (j=0;j<9;j++) {
for (k=1;k<10;k++) CellSet.insert(k);
k=0;
again:
if (cell[i][j]==0) {
changed=false;
for (k=0;k<9;k++) {
if ((cell[i][k]!=0)&&(CellSet.count(cell[i][k])>0)) CellSet.erase(cell[i][k]);
if ((cell[k][j]!=0)&&(CellSet.count(cell[k][j])>0)) CellSet.erase(cell[k][j]);
}
if (CellSet.size()==0) {
cout << "Wrong data. Impossible to solve.";
exitme();
}
if (CellSet.size()==1) {
it=CellSet.begin();
cell[i][j]=*it;
changed=false;
assigned=true;
}
for (a=0;a<3;a++) {
if (j==(0+3*a)) {
for (b=j;b<=(j+2);b++) squareIt(b);
}
if (j==(1+3*a)) {
for (b=(j-1);b<=(j+1);b++) squareIt(b);
}
if (j==(2+3*a)) {
for (b=(j-2);b<=j;b++) squareIt(b);
}
}
if (changed==true) goto again;
}
}
}
solved=true;
for (x=0;x<9;x++) {
for (y=0;y<9;y++) {
if (cell[x][y]==0) solved=false;
}
}
}

/*last step-output*/
cout << "SOLUTION:\n";
for (i=0;i<9;i++) {
for (j=0;j<9;j++) {
cout << "cell(" << i+1 << "," << j+1 << ")=" << cell[i][j] << "\n";
}
}
exitme();
}

So the program basically works, it just doesn't know things I don't know.
I am now working on creating windows applications in c++. Just exploring. We'll see where it leads.

Thursday, January 24, 2008

Start

I have started working on the project!