AssSoft Temat założony przez niniejszego użytkownika |
Działania matematyczne na int » 2014-02-18 22:26:40 Coby nie przedłużać problem jest następujący: W programie który ma wyliczać pole prostokąta ( ostatecznie będzie obliczać dużo więcej ) w wybranych jednostkach i podawać wynik też w wybranych. Podstawowy problem najprawdopodobniej tkwi w konwersji. Zwracany wynik najczęściej wynosi 0 - cóż za program twierdzi że 2 * 2 = 0? :) Sądziłem że błąd polega na tym że obliczam to w int, ale przy float wynik jest taki sam. Oto kod:
#include <string> #include <windows.h> #include <sstream> #include <windowsx.h> #include <cstring> #include <cstring> #define prostokat 0 #define trojkat 1 #define romb 2 #define trapez 3 #define rownoleglobok 4
#define ID_hm2dp 1001 #define ID_hm3dp 1002 #define ID_hsave1p 1003 #define ID_hinfo1st 1004 #define ID_herri1st 1005 #define ID_hwymXtx 1006 #define ID_hwymYtx 1007 #define ID_htype1se 1008 #define ID_htype2se 1009 #define ID_hmtypese 10010 #define ID_hresp 10011 #define ID_hsend1p 10012
#define ID2_hsend1p 10112
using namespace std; HWND hwnd; MSG Msg;
HWND hmram;
HWND hm2dp; HWND hm3dp; HWND hsave1p; HWND hsend1p; HWND hsend2p; HWND hinfo1st; HWND herri1st; HWND hwymXtx; HWND hwymYtx; HWND hwymHtx; HWND htype1se; HWND htype2se; HWND htype3se; HWND htype4se; HWND hmtypese; HWND hresp; HWND hany1st; HWND hany2st; HWND hany3st; HWND hany4st; HWND hany5st; HWND hany6st;
int X; int Y; int Xpoz; int Ypoz; int scal; int acstan; int wynik;
int je1I; int je2I; int je3I; int je4I; int je5I;
int i; int ii;
LPCSTR je1; LPCSTR je2; LPCSTR je3; LPCSTR je4; LPCSTR je5; LPSTR col1; LPSTR col2; LPSTR war1; LPSTR war2; string diag; string diag2; LPSTR diagL;
HINSTANCE hNewInstance; HFONT hNormalFont =( HFONT ) GetStockObject( DEFAULT_GUI_FONT );
void DestroyAllWindows() { DestroyWindow( hsave1p ); DestroyWindow( hsend1p ); DestroyWindow( hsend2p ); DestroyWindow( hinfo1st ); DestroyWindow( herri1st ); DestroyWindow( hwymXtx ); DestroyWindow( hwymYtx ); DestroyWindow( hwymHtx ); DestroyWindow( htype1se ); DestroyWindow( htype2se ); DestroyWindow( htype3se ); DestroyWindow( htype4se ); DestroyWindow( hmtypese ); DestroyWindow( hm2dp ); DestroyWindow( hm3dp ); DestroyWindow( hresp ); DestroyWindow( hany1st ); DestroyWindow( hany2st ); DestroyWindow( hany3st ); DestroyWindow( hany4st ); DestroyWindow( hany5st ); DestroyWindow( hany6st ); } int ob_prost() { int jall1; int jall2; int wyn; war1 = new char[ GetWindowTextLength( hwymXtx ) + 1 ]; war2 = new char[ GetWindowTextLength( hwymYtx ) + 1 ]; GetWindowText( hwymXtx, war1, GetWindowTextLength( hwymXtx ) + 1 ); GetWindowText( hwymYtx, war2, GetWindowTextLength( hwymYtx ) + 1 ); jall1 = atoi( war1 ); jall2 = atoi( war2 ); je1I = ComboBox_GetCurSel( htype1se ); je2I = ComboBox_GetCurSel( htype2se ); je3I = ComboBox_GetCurSel( htype3se ); je4I = ComboBox_GetCurSel( htype4se ); je5I = ComboBox_GetCurSel( hmtypese ); switch( je1I ) { case 0: je1 = "mm"; jall1 = jall1 / 10; break; case 1: je1 = "cm"; jall1 = jall1; break; case 2: je1 = "dm"; jall1 = jall1 * 10; break; case 3: je1 = "m"; jall1 = jall1 * 100; break; case 4: je1 = "km"; jall1 = jall1 * 1000; break; } switch( je2I ) { case 0: je2 = "mm"; jall2 = jall2 / 10; break; case 1: je2 = "cm"; jall2 = jall2; break; case 2: je2 = "dm"; jall2 = jall2 * 10; break; case 3: je2 = "m"; jall2 = jall2 * 100; break; case 4: je2 = "km"; jall2 = jall2 * 1000; break; } wyn = jall1 * jall2; switch( je5I ) { case 0: je5 = "mm"; wyn = wyn * 100; break; case 1: je5 = "cm"; wyn = wyn; break; case 2: je5 = "dm"; wyn = wyn / 100; break; case 3: je5 = "m"; wyn = wyn / 1000; break; case 4: je5 = "km"; wyn = wyn / 1000000; break; } return wyn; } LRESULT CALLBACK WndProc( HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam ) { switch( Message ) { case WM_DESTROY: { PostQuitMessage( 0 ); break; } case WM_COMMAND: { switch( wParam ) { case ID_hm2dp: DestroyAllWindows(); hmtypese = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_DROPDOWNLIST, 102, 52, 100, 200, hwnd,( HMENU ) ID_hmtypese, hNewInstance, NULL ); hresp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Reset", WS_CHILD | WS_VISIBLE, 1, 1, 100, 50, hwnd,( HMENU ) ID_hresp, hNewInstance, NULL ); hsend1p = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Rozpocznij", WS_CHILD | WS_VISIBLE, 102, 1, 100, 50, hwnd,( HMENU ) ID_hsend1p, hNewInstance, NULL ); hany1st = CreateWindowEx( 0, "STATIC", "Wybierz figurę:", WS_CHILD | WS_VISIBLE | SS_LEFT, 1, 52, 100, 25, hwnd, NULL, hNewInstance, NULL ); SetWindowText( hany1st, "Wybierz figurę" ); SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "prostokąt" ); SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "trójkąt" ); SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "romb" ); SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "trapez" ); SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "równoległobok" ); break; case ID_hm3dp: DestroyAllWindows(); break; case ID_hsend1p: { acstan = ComboBox_GetCurSel( hmtypese ); if( acstan == CB_ERR ) { break; } DestroyAllWindows(); switch( acstan ) { case prostokat: { hsend1p = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Oblicz", WS_CHILD | WS_VISIBLE, 1, 1, 100, 50, hwnd,( HMENU ) ID2_hsend1p, hNewInstance, NULL ); hresp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Reset", WS_CHILD | WS_VISIBLE, 1, 51, 100, 25, hwnd,( HMENU ) ID_hresp, hNewInstance, NULL ); herri1st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "Oczekiwanie na dane...", WS_CHILD | WS_VISIBLE | SS_LEFT, 102, 1, 380, 50, hwnd, NULL, hNewInstance, NULL ); hany1st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "bok A", WS_CHILD | WS_VISIBLE | SS_LEFT, 1, 75, 100, 25, hwnd, NULL, hNewInstance, NULL ); hany2st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "bok B", WS_CHILD | WS_VISIBLE | SS_LEFT, 1, 100, 100, 25, hwnd, NULL, hNewInstance, NULL ); hany3st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "jednostka", WS_CHILD | WS_VISIBLE | SS_LEFT, 101, 51, 100, 25, hwnd, NULL, hNewInstance, NULL ); hany4st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "wartość", WS_CHILD | WS_VISIBLE | SS_LEFT, 201, 51, 100, 25, hwnd, NULL, hNewInstance, NULL ); htype1se = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_DROPDOWNLIST, 101, 76, 100, 200, hwnd, NULL, hNewInstance, NULL ); htype2se = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_DROPDOWNLIST, 101, 101, 100, 200, hwnd, NULL, hNewInstance, NULL ); hwymXtx = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 201, 76, 100, 25, hwnd, NULL, hNewInstance, NULL ); hwymYtx = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 201, 101, 100, 25, hwnd, NULL, hNewInstance, NULL ); hany5st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "wynik w", WS_CHILD | WS_VISIBLE | SS_LEFT, 1, 126, 100, 25, hwnd, NULL, hNewInstance, NULL ); hany6st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "kolor", WS_CHILD | WS_VISIBLE | SS_LEFT, 301, 51, 100, 25, hwnd, NULL, hNewInstance, NULL ); htype3se = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_DROPDOWNLIST, 301, 76, 100, 200, hwnd, NULL, hNewInstance, NULL ); htype4se = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_DROPDOWNLIST, 301, 101, 100, 200, hwnd, NULL, hNewInstance, NULL ); hmtypese = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_DROPDOWNLIST, 101, 126, 100, 200, hwnd, NULL, hNewInstance, NULL ); SendMessage( htype1se, CB_ADDSTRING, 0,( LPARAM ) "mm" ); SendMessage( htype1se, CB_ADDSTRING, 0,( LPARAM ) "cm" ); SendMessage( htype1se, CB_ADDSTRING, 0,( LPARAM ) "dm" ); SendMessage( htype1se, CB_ADDSTRING, 0,( LPARAM ) "m" ); SendMessage( htype1se, CB_ADDSTRING, 0,( LPARAM ) "km" ); SendMessage( htype2se, CB_ADDSTRING, 0,( LPARAM ) "mm" ); SendMessage( htype2se, CB_ADDSTRING, 0,( LPARAM ) "cm" ); SendMessage( htype2se, CB_ADDSTRING, 0,( LPARAM ) "dm" ); SendMessage( htype2se, CB_ADDSTRING, 0,( LPARAM ) "m" ); SendMessage( htype2se, CB_ADDSTRING, 0,( LPARAM ) "km" ); SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "mm2" ); SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "cm2" ); SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "dm2" ); SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "m2" ); SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "km2" ); SendMessage( htype3se, CB_ADDSTRING, 0,( LPARAM ) "czarny" ); SendMessage( htype3se, CB_ADDSTRING, 0,( LPARAM ) "niebieski" ); SendMessage( htype3se, CB_ADDSTRING, 0,( LPARAM ) "czerwony" ); SendMessage( htype3se, CB_ADDSTRING, 0,( LPARAM ) "zielony" ); SendMessage( htype4se, CB_ADDSTRING, 0,( LPARAM ) "czarny" ); SendMessage( htype4se, CB_ADDSTRING, 0,( LPARAM ) "niebieski" ); SendMessage( htype4se, CB_ADDSTRING, 0,( LPARAM ) "czerwony" ); SendMessage( htype4se, CB_ADDSTRING, 0,( LPARAM ) "zielony" ); break; } case trojkat: { } } break; } case ID2_hsend1p: { SetWindowText( herri1st, "Rozpoczęto wstępne obliczanie" ); if( ComboBox_GetCurSel( htype1se ) == CB_ERR || ComboBox_GetCurSel( htype2se ) == CB_ERR ) { SetWindowText( herri1st, "Błąd - brak któregoś argumentu" ); break; } if( ComboBox_GetCurSel( htype3se ) == CB_ERR || ComboBox_GetCurSel( htype4se ) == CB_ERR ) { SetWindowText( herri1st, "Błąd - brak któregoś argumentu" ); break; } if( ComboBox_GetCurSel( hmtypese ) == CB_ERR ) { SetWindowText( herri1st, "Błąd - brak któregoś argumentu" ); break; } SetWindowText( herri1st, "Rozpoczęto obliczanie" ); char c[ 10 ]; itoa( wynik, c, 10 ); stringstream diag2ss; diag2ss << c; diag2ss >> diag2; diag = "a * b = "; diag += diag2; diagL = const_cast < char *>( diag.c_str() ); SetWindowText( herri1st, diagL ); break; } case ID_hresp: { DestroyAllWindows(); hm2dp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Figury 2d", WS_CHILD | WS_VISIBLE, 1, 1, 250, 250, hwnd,( HMENU ) ID_hm2dp, hNewInstance, NULL ); hm3dp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Figury 3d", WS_CHILD | WS_VISIBLE, 1, 252, 250, 250, hwnd,( HMENU ) ID_hm3dp, hNewInstance, NULL ); hmram = CreateWindowEx( 0, "BUTTON", "czekam na dane...", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 488, 1, 500, 500, hwnd,( HMENU ) ID_hm3dp, hNewInstance, NULL ); } break; } break; } default: return DefWindowProc( hwnd, Message, wParam, lParam ); } return 0; }
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { WNDCLASSEX wc; memset( & wc, 0, sizeof( wc ) ); wc.cbSize = sizeof( WNDCLASSEX ); wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground =( HBRUSH )( COLOR_WINDOW + 1 ); wc.lpszClassName = "WindowClass"; wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION ); if( !RegisterClassEx( & wc ) ) { MessageBox( NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK ); return 0; } hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, "WindowClass", "VaLC II", WS_VISIBLE | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 1000, 600, NULL, NULL, hInstance, NULL ); if( hwnd == NULL ) { MessageBox( NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK ); return 0; } hm2dp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Figury 2d", WS_CHILD | WS_VISIBLE, 1, 1, 250, 250, hwnd,( HMENU ) ID_hm2dp, hInstance, NULL ); hm3dp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Figury 3d", WS_CHILD | WS_VISIBLE, 1, 252, 250, 250, hwnd,( HMENU ) ID_hm3dp, hInstance, NULL ); hmram = CreateWindowEx( 0, "BUTTON", "czekam na dane...", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 488, 1, 500, 500, hwnd,( HMENU ) ID_hm3dp, hInstance, NULL ); SendMessage( hm2dp, WM_SETFONT,( WPARAM ) hNormalFont, 0 ); SendMessage( hm3dp, WM_SETFONT,( WPARAM ) hNormalFont, 0 ); hNewInstance = hInstance; while( GetMessage( & Msg, NULL, 0, 0 ) > 0 ) { TranslateMessage( & Msg ); DispatchMessage( & Msg ); } return Msg.wParam; }
Prosiłbym o szybką pomoc. |