Hi,

I tried to get a simple application running that does prime factorial decomposition. The algorithm works, i tested it without the calulator. I dont know how to properley get user input tho.


Code:

#include <fxcg/display.h>
#include <fxcg/keyboard.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
 
int* primeFactors(int n);
char* primeFactorsAsString(int n);

int main() {
   
    PrintXY(1, 1, "--Primfactorzerlegung", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);

    char *buffer = (char*) malloc(256);
    int start = 0;
    int cursor = 0;
    buffer[0] = '\0';

    int key;

    DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);


    while (1) {
        GetKey(&key);

        if (key == KEY_CTRL_EXE) {
            int input = atoi(buffer);
            char bufferTwo[102] = "--";
            strcat(bufferTwo, primeFactorsAsString(input));
            PrintXY(1, 3, bufferTwo, TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
            buffer = "";
        } else if (key && key < 30000) {
            cursor = EditMBStringChar((unsigned char*) buffer, 256, &cursor, &key, 1, 2);
            DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);
        } else {
            EditMBStringCtrl((unsigned char*) buffer, 256, &start, &cursor, &key, 1, 2);
        }
    }
 
    return 0;
}
}

I believe setting resetting the buffer causes the issue. The first input and displaying works, however, when I try to enter a number a second time, i get this error:


Code:

System ERROR
REBOOT   : [EXIT]
INITIALIZE: [EXE]
TLB ERROR
TARGET=0030202F
PC       =00301F30


Any help would be appreciated
A few notes, EditMBStringChar accepts an int, not int* for cursor and key (i.e. don't & it).
It also doesn't accept an x or y.
This should be:

Code:

cursor = EditMBStringChar((unsigned char*) buffer, 256, cursor, key);


Also, the assigning buffer to "" is creating a new, non-malloced string.
Instead, setting buffer[0] = '\0'; (as is done earlier), should effectively clear the string.

Hopefully these fix the error, but please feel free to post any other problems you have!
I have changed that mistake in my code, as well as resetting the buffer, curser, and start variable properley. I noticed that even though this if statement clearly gets triggered because the prime factorization appears on the calculators screen, however the input field is not being wiped with this code. The next time i press a key, i get the error.


Code:
#include <fxcg/display.h>
#include <fxcg/keyboard.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
 
int* primeFactors(int n);
char* primeFactorsAsString(int n);

int main() {
   
    PrintXY(1, 1, "--Primfactorzerlegung", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);

    char *buffer = (char*) malloc(256);
    int start = 0;
    int cursor = 0;
    buffer[0] = '\0';

    int key;

    DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);


    while (1) {
        GetKey(&key);

        if (key == KEY_CTRL_EXE) {
            int input = atoi(buffer);
            char bufferTwo[102] = "--";
            strcat(bufferTwo, primeFactorsAsString(input));
            PrintXY(1, 3, bufferTwo, TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
            buffer[0] = '\0';
            start = 0;
            cursor = 0;
            DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);
        } else if (key && key < 30000) {
            cursor = EditMBStringChar((unsigned char*) buffer, 256, cursor, key);
            DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);
        } else {
            EditMBStringCtrl((unsigned char*) buffer, 256, &start, &cursor, &key, 1, 2);
        }
    }
 
    return 0;
}


Btw, is there an emulator for this calculator so that i dont always have to use my physical one?
Lasslos wrote:
I have changed that mistake in my code, as well as resetting the buffer, curser, and start variable properley. I noticed that even though this if statement clearly gets triggered because the prime factorization appears on the calculators screen, however the input field is not being wiped with this code. The next time i press a key, i get the error.


Code:
#include <fxcg/display.h>
#include <fxcg/keyboard.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
 
int* primeFactors(int n);
char* primeFactorsAsString(int n);

int main() {
   
    PrintXY(1, 1, "--Primfactorzerlegung", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);

    char *buffer = (char*) malloc(256);
    int start = 0;
    int cursor = 0;
    buffer[0] = '\0';

    int key;

    DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);


    while (1) {
        GetKey(&key);

        if (key == KEY_CTRL_EXE) {
            int input = atoi(buffer);
            char bufferTwo[102] = "--";
            strcat(bufferTwo, primeFactorsAsString(input));
            PrintXY(1, 3, bufferTwo, TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
            buffer[0] = '\0';
            start = 0;
            cursor = 0;
            DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);
        } else if (key && key < 30000) {
            cursor = EditMBStringChar((unsigned char*) buffer, 256, cursor, key);
            DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);
        } else {
            EditMBStringCtrl((unsigned char*) buffer, 256, &start, &cursor, &key, 1, 2);
        }
    }
 
    return 0;
}


Btw, is there an emulator for this calculator so that i dont always have to use my physical one?


Yes, there is an official emulator - https://casioeducation.com.au/software-emulators/.

I tested your program by just printing the itoa of the string, and it works perfectly fine (as expected).
The problem with it seems to be with the primeFactorsAsString function, could you post this so that we can try and help you debug it?

Code:

#include <fxcg/display.h>
#include <fxcg/keyboard.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
 
int* primeFactors(int n);
char* primeFactorsAsString(int n);

int main() {
   
    PrintXY(1, 1, "--Primfactorzerlegung", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
    char *buffer = (char*) malloc(256);
    int start = 0;
    int cursor = 0;
    buffer[0] = '\0';

    int key;

    DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);


    while (1) {
        GetKey(&key);

        if (key == KEY_CTRL_EXE) {
            int input = atoi(buffer);
            char bufferTwo[102] = "--";
            strcat(bufferTwo, primeFactorsAsString(input));
            PrintXY(1, 3, bufferTwo, TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
            buffer[0] = '\0';
            start = 0;
            cursor = 0;
            DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);
        } else if (key && key < 30000) {
            cursor = EditMBStringChar((unsigned char*) buffer, 256, cursor, key);
            DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);
        } else {
            EditMBStringCtrl((unsigned char*) buffer, 256, &start, &cursor, &key, 1, 2);
        }
    }
 
    return 0;
}

int* primeFactors(int n)
{
    if (n < 2) {
        static int result[2];
        result[0] = 1;
        result[1] = n;
        return result;
    }

    int size = 0;
    static int factors[33];
    //Max int has 32 factors, plus first spot in array for length of array.

    while (n%2 == 0)
    {
        size++;
        factors[size] = 2;
        n = n/2;
    }
 
    for (int i = 3; i <= n; i = i+2)
    {
        while (n%i == 0)
        {
            size++;
            factors[size] = i;
            n = n/i;
        }
    }
    if (n > 2)
    {
        size++;
        factors[size] = n;
    }

    factors[0] = size;
    return factors;
}

char* primeFactorsAsString(int n) {
    int *factors;

    factors = primeFactors(n);

    if (factors[0] > 25) {
        return "TO BIG";
    }
    static char result[100];
    char buffer[100];


    for (int i = 1; i < factors[0]; i++)
    {
        sprintf(buffer, "%d * ", factors[i]);
        strcat(result, buffer);
       

    }
    sprintf(buffer, "%d", factors[factors[0]]);
    strcat(result, buffer);

    return result;
}
Lasslos wrote:

Code:

#include <fxcg/display.h>
#include <fxcg/keyboard.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
 
int* primeFactors(int n);
char* primeFactorsAsString(int n);

int main() {
   
    PrintXY(1, 1, "--Primfactorzerlegung", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
    char *buffer = (char*) malloc(256);
    int start = 0;
    int cursor = 0;
    buffer[0] = '\0';

    int key;

    DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);


    while (1) {
        GetKey(&key);

        if (key == KEY_CTRL_EXE) {
            int input = atoi(buffer);
            char bufferTwo[102] = "--";
            strcat(bufferTwo, primeFactorsAsString(input));
            PrintXY(1, 3, bufferTwo, TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
            buffer[0] = '\0';
            start = 0;
            cursor = 0;
            DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);
        } else if (key && key < 30000) {
            cursor = EditMBStringChar((unsigned char*) buffer, 256, cursor, key);
            DisplayMBString((unsigned char*) buffer, start, cursor, 1, 2);
        } else {
            EditMBStringCtrl((unsigned char*) buffer, 256, &start, &cursor, &key, 1, 2);
        }
    }
 
    return 0;
}

int* primeFactors(int n)
{
    if (n < 2) {
        static int result[2];
        result[0] = 1;
        result[1] = n;
        return result;
    }

    int size = 0;
    static int factors[33];
    //Max int has 32 factors, plus first spot in array for length of array.

    while (n%2 == 0)
    {
        size++;
        factors[size] = 2;
        n = n/2;
    }
 
    for (int i = 3; i <= n; i = i+2)
    {
        while (n%i == 0)
        {
            size++;
            factors[size] = i;
            n = n/i;
        }
    }
    if (n > 2)
    {
        size++;
        factors[size] = n;
    }

    factors[0] = size;
    return factors;
}

char* primeFactorsAsString(int n) {
    int *factors;

    factors = primeFactors(n);

    if (factors[0] > 25) {
        return "TO BIG";
    }
    static char result[100];
    char buffer[100];


    for (int i = 1; i < factors[0]; i++)
    {
        sprintf(buffer, "%d * ", factors[i]);
        strcat(result, buffer);
       

    }
    sprintf(buffer, "%d", factors[factors[0]]);
    strcat(result, buffer);

    return result;
}


It works for me.
You probably want to fill the screen with white under the area you are printing each time, but apart from that it works - see the examples section of PrizmSDK.

You said that it throws an error when you press a key, what OS version are you running (System->VERSION)?
Weird. I recompiled just be sure and it works now. I can also see why i wanna clear the space below it Very Happy . Thanks so much for helping. Pre Calc 11 is very boring right now so I need a way to get arround it and this is one step closer.
Lasslos wrote:
Weird. I recompiled just be sure and it works now. I can also see why i wanna clear the space below it Very Happy . Thanks so much for helping. Pre Calc 11 is very boring right now so I need a way to get arround it and this is one step closer.

You're welcome!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement