0

I haven't found any good tutorials so I started experimenting. I wanted to make calculations with two BigNr by adding them in to editable text boxes. I got their input and tried to get a result from them in a variable called C. The only problem is that the program isn't adding the values to the variable or refuses to show it in the application. The functions used

#include <bits/stdc++.h>
using namespace std;
ofstream fout("txt.out");
typedef int BigNr[1001];
void Citire(BigNr A){
    for(int i=A[0];i>=1;i--)
        fout<<A[i];
}
void Convertire_string(BigNr A,string s){
    for(int i = 1; i <=A[0]; i++)
        s[i-1]=A[i];
}
void Initializare(BigNr A)
{
    A[0] = 0;
}

void InitInt(BigNr A, unsigned int X)
{
    A[0] = 0;
    while (X)
    {
        A[++A[0]] = X % 10;
        X /= 10;
    }
}
void IntInit(BigNr A, unsigned int x){
    int i=1;
    for(;i<=A[0];i++)
        x=x*10+A[i];
}
void InitBigNr(BigNr  A, BigNr B)
{
    for (int i = 0; i <= A[0]; i++)
        B[i] = A[i];
}
void InitBigNr_String(BigNr A, string s)
{
    int k = 0;
    A[0] = s.size();
    for(int i = s.size() - 1; i >= 0; i--)
        A[++k] = s[i]-'0';
}
int Comparare(BigNr A, BigNr  B)
{
    while (A[0] && !A[A[0]]) A[0]--;
    while (B[0] && !B[B[0]]) B[0]--;//se elimina eventualele 0 nesemnificatine (din fata primei cifre)
    if (A[0] != B[0])
        return A[0]<B[0] ? -1 : 1;
    int i = A[0];
    while (i && A[i] == B[i]) i--;
    return A[i] < B[i] ? -1 : A[i] == B[i] ? 0 : 1;
}
void InmultireBigNr_Int(BigNr A, unsigned int X) /* A <- AX*/
{
    int i;
    unsigned int T = 0;
    for (i = 1; i <= A[0]; ++i)
    {
        A[i] = A[i] * X + T;
        T = A[i] / 10;
        A[i] = A[i] % 10;
    }
    while (T) /* Cat timp exista transport */
    {
        A[++A[0]] = T%10;
        T /= 10;
    }
}
void Adunare(BigNr a, BigNr b, BigNr rez)
{
    int t=0, maxi; // t = cifra de transport *
    if(a[0]<b[0])
    {
        maxi=b[0];
        for(int i = a[0]+1; i<=maxi; i++) //adaugam 0-uri pana cand cele 2 numere au acelasi nr de cifre
            a[i]=0;
    }
    else
    {
        maxi=a[0];
        for(int i = b[0] + 1 ; i<=maxi; i++)
            b[i]=0;
    }
    int i;
    for(i=1;i<=maxi;i++)
    {
        int cifra = a[i] + b[i] + t;
        rez[i]=cifra%10;
        t=cifra/10; // cifra t o "tinem in minte"
    }
    if(t)
        rez[i]=t;
    else
        i--;
    rez[0]=i;
}
void Scadere(BigNr a, BigNr b, BigNr rez)
{
    if(a[0]<b[0])
        Scadere(b, a, rez);
    else
    {
        int i, t=0;
        for(i=1;i<=a[0];i++)
        {
          rez[i]=a[i]-b[i]+t;
          if(rez[i]<0)
          {
              rez[i]=10+rez[i]; /* prin cifra t "imprumutam o zece" de la unitatea mai mare */
              t=-1;    // t se face -1 pentru a demonstra faptul ca am "imprumutat o zece"
          }
          else
            t=0; //daca avem o scadere de genul 9-4, nu e nevoie sa imprumutam 10, deci t ramane 0
        }
        i--;
        while(i && !rez[i]) // eliminam 0-urile ramase
            i--;
        rez[0]=i; //adaugam nr de cifre
}
}
void InmultireBigNr_BigNr(BigNr A,BigNr B,BigNr C){
    int T=0;
    C[0]=A[0]+B[0]-1;
    for(int i=1;i<=A[0]+B[0];++i)
        C[i]=0;
    for(int i=1;i<=A[0];++i)
        for(int j=1;j<=B[0];++j)
            C[i + j - 1] += A[i] * B[j];
    for(int i=1;i<=C[0];++i){
        C[i]+=T;
        T=C[i]/10;
        C[i]%=10;
    }
    if(T)
        C[++C[0]]=T;
}
int ImpartireBigNr_Scalar(BigNr A,int X){
    int R=0;
    for(int i=A[0];i;--i){
        R=10*R+A[i];
        A[i]=R/X;
        R%=X;
    }
    while(!A[A[0]]&&A[0] > 1)
        A[0]--;
    return R;
}
int RestBigNr_Scalar(BigNr A,int X){
    int R=0;
    for(int i=A[0];i;--i)
        R=(10*R+A[i])%X;
    return R;
}

void ImpartireBigNr_BigNr(BigNr A,BigNr B,BigNr C,BigNr R){
    C[0]=0;
    int i;
    while(Comparare(A,B)>=0){
        Scadere(A,B,A);
        if(C[0]==0)
            C[0]=1;
        C[1]++;
        if(C[1]==10){
            C[1]=0;
            for(i=2;C[i]==9;i++)
                C[i]=0;
            C[i]++;
            if(C[C[0]+1]!=0)
                C[0]++;
        }
    }
    InitBigNr(A,R);
} 

And the program that runs the functions and the interface

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#include <windows.h>
#include "bignr.h"
#define GET_INPUTA 0
#define ID_FILE_EXIT 1
#define GET_INPUTB 2
#define ADUNARE 3
#define SCADERE 4
#define INMULTIRE_BIGNR 5
#define INMULTIRE_SCALAR 6
#define IMPARTIRE_BIGNR 7
#define IMPARTIRE_SCALAR 8
#define RIDICARE_LA_PUTERE 9
#define AFISARE_APLICATIE 10
#define AFISARE_FISIER 11

typedef int BigNr[1001];
BigNr A,B,C,R;
int x,ok;

HWND hEdit,hEditb,hOut;
void CreateMainMenu(HWND hwnd){
    HMENU hMenu= CreateMenu();
    HMENU hFMItem = CreateMenu();
    AppendMenu(hFMItem,MF_STRING,NULL,"New");
    AppendMenu(hFMItem,MF_STRING,NULL,"Open");
    AppendMenu(hFMItem,MF_STRING,ID_FILE_EXIT,"Exit");
    AppendMenu(hMenu,MF_POPUP,(UINT_PTR)hFMItem,"File");
    AppendMenu(hMenu,MF_STRING,NULL,"Edit");
    AppendMenu(hMenu,MF_STRING,NULL,"Tools");
    AppendMenu(hMenu,MF_STRING,NULL,"Help");
    SetMenu(hwnd,hMenu);
}
void AddControls(HWND hwnd){
    hEdit=CreateWindowW(L"Edit",L"",WS_VISIBLE | WS_CHILD | ES_MULTILINE | ES_AUTOVSCROLL, 200,152,100,50,hwnd,NULL,NULL,NULL);
    hEditb=CreateWindowW(L"Edit",L"",WS_VISIBLE | WS_CHILD | ES_MULTILINE | ES_AUTOVSCROLL,400,152,100,50,hwnd,NULL,NULL,NULL);
    hOut=CreateWindowW(L"Edit",L"",WS_VISIBLE | WS_CHILD | ES_MULTILINE | ES_AUTOVSCROLL, 200,300,300,200,hwnd,NULL,NULL,NULL);
    CreateWindowW(L"Button",L"+",WS_VISIBLE | WS_CHILD, 310,120,30,30,hwnd,(HMENU)ADUNARE,NULL,NULL);
    CreateWindowW(L"Button",L"-",WS_VISIBLE | WS_CHILD, 360,120,30,30,hwnd,(HMENU)SCADERE,NULL,NULL);
    CreateWindowW(L"Button",L"X.BigNr",WS_VISIBLE | WS_CHILD, 310,160,30,30,hwnd,(HMENU)INMULTIRE_BIGNR,NULL,NULL);
    CreateWindowW(L"Button",L"X.S",WS_VISIBLE | WS_CHILD, 360,160,30,30,hwnd,(HMENU)INMULTIRE_SCALAR,NULL,NULL);
    CreateWindowW(L"Button",L"/.BigNr",WS_VISIBLE | WS_CHILD, 310,200,30,30,hwnd,(HMENU)IMPARTIRE_BIGNR,NULL,NULL);
    CreateWindowW(L"Button",L"/.S",WS_VISIBLE | WS_CHILD, 360,200,30,30,hwnd,(HMENU)IMPARTIRE_SCALAR,NULL,NULL);
    CreateWindowW(L"Button",L"x^y",WS_VISIBLE | WS_CHILD, 335,80,30,30,hwnd,(HMENU)RIDICARE_LA_PUTERE,NULL,NULL);
    CreateWindowW(L"Button",L"afisare in aplicatie",WS_VISIBLE | WS_CHILD,550,137,140,30,hwnd,(HMENU)AFISARE_APLICATIE,NULL,NULL);
    CreateWindowW(L"Button",L"afisare in fisier",WS_VISIBLE | WS_CHILD,550,177,140,30,hwnd,(HMENU)AFISARE_FISIER,NULL,NULL);
}

void ShowResult(){

}
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Code::Blocks Template Windows App"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           900,                 /* The programs width */
           600,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
    CreateMainMenu(hwnd);
    AddControls(hwnd);
    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_COMMAND:
            {
                switch(LOWORD(wParam))
                {
                case ID_FILE_EXIT:{
                    PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                    }break;
                case GET_INPUTA:{
                    wchar_t text[1002];
                    GetWindowTextW(hEdit, text, 1002);
                    wstring ws(text);
                    string str(ws.begin(), ws.end());
                    InitBigNr_String(A,str);
                    }break;
                case GET_INPUTB:{
                    wchar_t txt[1002];
                    GetWindowTextW(hEditb, txt, 1002);
                    wstring ws(txt);
                    string strA(ws.begin(), ws.end());
                    InitBigNr_String(B,strA);
                    }break;
                case ADUNARE:
                    Adunare(A,B,C);
                    break;
                case SCADERE:
                    Scadere(A,B,C);
                    break;
                case INMULTIRE_BIGNR:
                    InmultireBigNr_BigNr(A,B,C);
                    break;
                case INMULTIRE_SCALAR:
                    IntInit(A,x);
                    InmultireBigNr_Int(A,x);
                    InitBigNr(A,C);
                    break;
                case IMPARTIRE_BIGNR:
                    ImpartireBigNr_BigNr(A,B,C,R);
                    ok=1;
                    break;
                case IMPARTIRE_SCALAR:
                    IntInit(A,x);
                    ImpartireBigNr_Scalar(A,x);
                    InitBigNr(A,C);
                    x=RestBigNr_Scalar(A,x);
                    ok=1;
                    break;
                case AFISARE_APLICATIE:{
                    string s;
                    Convertire_string(C,s);
                    SetWindowText(hOut,s.c_str());
                    }break;
                case AFISARE_FISIER:{
                    Citire(C);
                    if(ok==1)
                        fout<<endl<<"Rest= ";
                    Citire(R);
                    system("txt.out");
                    }break;
                }

            }break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

And here is the header which is not really important

#ifndef BIGNR_H_INCLUDED
#define BIGNR_H_INCLUDED
#include <bits/stdc++.h>
#include "bignr.cpp"

#endif // BIGNR_H_INCLUDED
  • 2
    Unrelated: [Avoid `#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – user4581301 May 16 '22 at 18:40
  • 2
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver May 16 '22 at 18:41
  • Unrelated: Prefer `constexpr int GET_INPUTA = 0;` to `#define GET_INPUTA 0`. When you `#define`, you ask for a very simple text substitution before the really compiler gets started compiling. It will do very stupid things that a variable can check for and reject, so prefer a variable where possible. – user4581301 May 16 '22 at 18:44
  • *I haven't found any good tutorials...* you probably need a [good C++ book](https://stackoverflow.com/a/388282/4641116). – Eljay May 16 '22 at 18:47
  • 2
    Unrelated: [Don't include cpp files](https://stackoverflow.com/questions/1686204/why-should-i-not-include-cpp-files-and-instead-use-a-header). Compile and link the cpp file. – user4581301 May 16 '22 at 18:47
  • Until you know a programming language well enough to know bad code, be cautious of tutorials. If you can't recognize bad advice as bad, you'll follow and regret it. Definitely start with good books. – user4581301 May 16 '22 at 18:49
  • 1
    If you want an interface, at some point you have to declare a class. I'm also pretty sure you're expected to write your class declaration in the header file. I just wrapped up finals in my Intro class, I'm always amazed at how much debt someone will take on to do nothing. – sweenish May 16 '22 at 20:12
  • I think you should write a console version of the program first, to make sure everything works. – Sergio Aug 12 '22 at 10:54
  • what do you mean by bignr? How big must the numbers you enter be? – Sergio Aug 12 '22 at 17:04

0 Answers0