Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Działania matematyczne na int

Ostatnio zmodyfikowano 2014-03-01 15:30
Autor Wiadomość
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:
C/C++
/* Notes:

Jednostka ogólna = 1cm
*/
#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; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG Msg; /* A temporary location for all messages */


// Zmienne:
//HWND:
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 i dword
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;
//string char i podobne
LPCSTR je1;
LPCSTR je2;
LPCSTR je3;
LPCSTR je4;
LPCSTR je5;
LPSTR col1;
LPSTR col2;
LPSTR war1;
LPSTR war2;
string diag;
string diag2;
LPSTR diagL;
//HPEN i HBRUSH

//inne
HINSTANCE hNewInstance;
HFONT hNormalFont =( HFONT ) GetStockObject( DEFAULT_GUI_FONT );
/* This is where all the input to the window goes to */
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 ) {
       
        /* Upon destruction, tell the main thread to stop */
    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;
        }
       
        /* All other messages (a lot of them) are processed using default procedures */
        default:
        return DefWindowProc( hwnd, Message, wParam, lParam );
    }
    return 0;
}
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
    WNDCLASSEX wc; /* A properties struct of our window */
   
    /* zero out the struct and set the stuff we want to modify */
    memset( & wc, 0, sizeof( wc ) );
    wc.cbSize = sizeof( WNDCLASSEX );
    wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
   
    /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
    wc.hbrBackground =( HBRUSH )( COLOR_WINDOW + 1 );
    wc.lpszClassName = "WindowClass";
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); /* Load a standard icon */
    wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION ); /* use the name "A" to use the project icon */
   
    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, /* x */
    CW_USEDEFAULT, /* y */
    1000, /* width */
    600, /* height */
    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 );
    /*
    This is the heart of our program where all input is processed and
    sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
    this loop will not produce unreasonably high CPU usage
    */
    hNewInstance = hInstance;
    while( GetMessage( & Msg, NULL, 0, 0 ) > 0 ) { /* If no error is received... */
        TranslateMessage( & Msg ); /* Translate key codes to chars if present */
        DispatchMessage( & Msg ); /* Send it to WndProc */
    }
    return Msg.wParam;
}

Prosiłbym o szybką pomoc.
P-104946
Monika90
» 2014-02-18 22:49:06
A co innego ma się wyświetlić jak nie zero? Zmienna wynik ma wartość zero od początku swego istnienia i nigdzie nie przypisujesz do niej innej wartości.
P-104948
AssSoft
Temat założony przez niniejszego użytkownika
» 2014-02-18 23:18:37
Błąd po prostu... Głupi - Tak głupi że nawet nie przyszedł mi do głowy.
dodałem w odpowiednim miejscu
wynik = ob_prost();
 Ta linijka musiała się gdzieś zawieruszyć...

Problem jednak pozostał - przynajmniej częściowo. Podczas działań które wymagają mnożenia wszystko idzie dobrze, problem pojawia się przy dzieleniu. Np. jeśli zażądam od programu obliczenia w którym boki są w cm, a wynik jest w dm to suma jest równa 0.
Zmiana danych na float nie pomaga.
Prosiłbym o pomoc.
P-104950
DejaVu
» 2014-02-18 23:42:01
Napisz najpierw prosty program konsolowy skoro proste operacje matematyczne sprawiają Ci problem. A z kodu wynika, że większość Twojej pracy to CTRL+C/CTRL+V.

http://www.daniweb.com​/software-development/c/threads​/443001​/win32-application-using-dev-c
P-104951
AssSoft
Temat założony przez niniejszego użytkownika
» 2014-02-19 08:52:13
To co podałeś w linku jest kopią tego co Dev-C++ domyślnie wypisuje w przypadku wybrania projektu GUI :) Nigdy tamtej strony nie widziałem na oczy. Kilka lat temu napisałem podobny program który był częścią grupy programów konsolowych które wtedy tworzyłem. Stąd ten ma numerek 2.
Prawdą jest iż część mojej pracy to kopiowanie kodu, jednak zwykle jest to kod stworzony prze zemnie. Czasem po prostu nie warto ręcznie pisać czegoś co i tak będzie się różniło tylko kilkoma znakami od tego co zrobiłem 2 min wcześniej.


P-104956
Monika90
» 2014-02-19 09:17:35

Np. jeśli zażądam od programu obliczenia w którym boki są w cm, a wynik jest w dm to suma jest równa 0.
Zmiana danych na float nie pomaga.

Zastąp ten kod
C/C++
wynik = ob_prost();
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 );

poniższym, który jest dużo prostszy
C/C++
ostringstream s;
s << "a * b = " << ob_prost();
SetWindowText( herri1st, s.str().c_str() );
i nie tylko typy zmiennych zmień na float (albo double), ale też typ zwracany z funkcji ob_prost()
P-104957
alixir
» 2014-02-19 09:29:24
Zmiana na float/double pomaga. Problemem jest pewnie to, że robisz to w niewłaściwy sposób.
Nie wystarczy zmienić zmiennych z int na float! Musisz przeanalizować cały kod
P-104958
AssSoft
Temat założony przez niniejszego użytkownika
» 2014-03-01 15:30:26
Wielkie dzięki, zapomniałem o zmianie dwóch zmiennych na float, teraz wszystko działa.
Zamykam temat
P-105445
« 1 »
  Strona 1 z 1