Шустрый

Профиль
Группа: Участник
Сообщений: 117
Регистрация: 4.6.2009
Репутация: нет Всего: 0
|
Добрый день! Вот программа у меня должна рисовать путь кратчайший, но что то не работает... Хотя должна бы... Замучилась... У меня VS 2008. Если есть какие либо предложение, по исправлению, буду рада)) Вот код: | Код | #include <stdafx.h> #include <windows.h> // Win32API Header File #include <cmath> #include <cstring> #include <iostream>
#define word unsigned int
using namespace std; #define Red RGB (255,0,0) #define Lime RGB (206,255,0) #define Blue RGB (0,0,255) static HWND hConWnd; int BCX_Line (HWND,int,int,int,int,int=0,HDC=0); int BCX_Circle (HWND,int,int,int,int=0,int=0,HDC=0); HWND GetConsoleWndHandle (void); int flag[11] ; word c[11][11], l[11];
int min2(int n) { int i, result; for(i=0;i<n;i++) if(!(flag[i])) result=i; for(i=0;i<n;i++) if((l[result]>l[i])&&(!flag[i])) result=i; return result; }
word minim(word x, word y) { if(x<y) return x; return y; }
int main() { int i, j, n, p, xn, xk; char s[80], path[80][11]; POINT pt[11] ; const double PI = 4. * atan( 1. ) ;
cout<<"напишите число точек: "; cin >> n; for(i=0;i<n;i++) for(j=0;j<n;j++) c[i][j]=0; for(i=0;i<n;i++) for(j=i+1;j<n;j++) { cout<<" задайте длины рёбер x"<<i+1<<" do x"<<j+1<<": "; cin>>c[i][j]; } cout<<" "; for(i=0;i<n;i++) cout<<" X"<<i+1; cout<<endl<<endl; for(i=0;i<n;i++) { printf("X%d",i+1); for(j=0;j<n;j++) { printf("%6d",c[i][j]); c[j][i]=c[i][j]; } printf("\n\n"); } for(i=0;i<n;i++) for(j=0;j<n;j++) if(c[i][j]==0) c[i][j] = 65535; //nekonecno cout<<" задайте начальную точку: "; cin>>xn; cout<<" задайте конечную точку: "; cin>>xk; xk--; xn--; if(xn==xk) { cout<<"Начальная и конечные точки совпадают."<<endl; cin.get() ; return 1; }
for(i=0;i<n;i++) { flag[i]=0; l[i]=65535; } l[xn]=0; flag[xn]=1; p=xn; itoa(xn+1,s,10); for(i=1;i<=n;i++) { strcpy(path[i],"X"); strcat(path[i],s); } do { for(i=0;i<n;i++) if((c[p][i]!=65535)&&(!flag[i])&&(i!=p)) { if(l[i]>l[p]+c[p][i]) { itoa(i+1,s,10); strcpy(path[i+1],path[p+1]); strcat(path[i+1],"-X"); strcat(path[i+1],s); } l[i]=minim(l[i],l[p]+c[p][i]); } p = min2(n) ; flag[p]=1; } while(p!=xk); if ( l[p] != 65535) { cout<<"Put: "<<path[p+1]<<endl; cout<<"Dlina puti: "<<l[p]<<endl; } else cout<<"Путь не существует!"<<endl;
// Отрисовка. Верщины графа располагаются вдоль окружности с параметрами: const int x0 = 480, y0 = 150, r0 = 130 ; double deA = 2* PI / double( n ) , al = PI / 2. ; int x, y ;
hConWnd = GetConsoleWndHandle() ; if ( hConWnd ) { for ( i = 0; i < n; i++ ) { pt[i].x = x0 + r0 * cos( al ) ; pt[i].y = y0 - r0 * sin( al ) ; BCX_Circle( hConWnd, pt[i].x, pt[i].y, 3, Lime ) ; al += deA ; } for ( i = 0; i < n; i++ ) { for ( j = i+1; j < n ; j++ ) { if ( c[i][j] != 65535 ) BCX_Line( hConWnd, pt[i].x, pt[i].y, pt[j].x, pt[j].y, Blue ) ; } } cin.get() ; }
return 0 ; } int BCX_Line (HWND Wnd,int x1,int y1,int x2,int y2,int Pen,HDC DrawHDC) { int a,b=0; HPEN hOPen; // penstyle, width, color HPEN hNPen = CreatePen(PS_SOLID, 2, Pen); if (!DrawHDC) DrawHDC = GetDC(Wnd), b = 1; hOPen = (HPEN)SelectObject(DrawHDC, hNPen); // starting point of line MoveToEx(DrawHDC, x1, y1, NULL); // ending point of line a = LineTo(DrawHDC, x2, y2); DeleteObject(SelectObject(DrawHDC, hOPen)); if (b) ReleaseDC(Wnd, DrawHDC); return a; } // converts circle(centerX,centerY,radius,pen) to WinApi function // ellipse inside box with upper left and lower right coordinates int BCX_Circle(HWND Wnd,int X,int Y,int R,int Pen,int Fill,HDC DrawHDC) { int a, b = 0; if (!DrawHDC) DrawHDC = GetDC(Wnd), b = 1; // penstyle, width, color HPEN hNPen = CreatePen(PS_SOLID, 2, Pen); HPEN hOPen = (HPEN)SelectObject(DrawHDC, hNPen); HBRUSH hOldBrush; HBRUSH hNewBrush; // if true will fill circle with pencolor if (Fill) { hNewBrush = CreateSolidBrush(Pen); hOldBrush = (HBRUSH)SelectObject(DrawHDC, hNewBrush); } else { hNewBrush = (HBRUSH)GetStockObject(NULL_BRUSH); hOldBrush = (HBRUSH)SelectObject(DrawHDC, hNewBrush); } a = Ellipse(DrawHDC, X-R, Y+R, X+R, Y-R); DeleteObject(SelectObject(DrawHDC, hOPen)); DeleteObject(SelectObject(DrawHDC, hOldBrush)); if (b) ReleaseDC(Wnd, DrawHDC); return a; } // the hoop ... HWND GetConsoleWndHandle(void) { HWND hConWnd; OSVERSIONINFO os; char szTempTitle[64], szClassName[128], szOriginalTitle[1024]; os.dwOSVersionInfoSize = sizeof( OSVERSIONINFO ); GetVersionEx( &os ); // may not work on WIN9x if ( os.dwPlatformId == VER_PLATFORM_WIN32s ) return 0; GetConsoleTitle( szOriginalTitle, sizeof( szOriginalTitle ) ); sprintf( szTempTitle,"%u - %u", GetTickCount(), GetCurrentProcessId() ); SetConsoleTitle( szTempTitle ); Sleep( 40 ); // handle for NT hConWnd = FindWindow( NULL, szTempTitle ); SetConsoleTitle( szOriginalTitle ); // may not work on WIN9x if ( os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) { hConWnd = GetWindow( hConWnd, GW_CHILD ); if ( hConWnd == NULL ) return 0; GetClassName( hConWnd, szClassName, sizeof ( szClassName ) ); while ( strcmp( szClassName, "ttyGrab" ) != 0 ) { hConWnd = GetNextWindow( hConWnd, GW_HWNDNEXT ); if ( hConWnd == NULL ) return 0; GetClassName( hConWnd, szClassName, sizeof( szClassName ) ); } } return hConWnd; }
|
|