如何从右到左填充变量 Char?

How can I fill a variable Char from right to left?

通常 TPV 是使用集成软件制造的,您可以开发它与服务器之间的交互。现在我的工作是做基础开发,换句话说,我开发整个东西。

我的问题是下一个:

我在 TPV 中编写了一个用于启动销售的密钥。这会打印一个标签 "Type the Price" 和一个名为 "price" 的 char,初始化为“00,00€”

很简单。现在我想从右到左填充那个价格字符,即:

此价格字符与使用 callocrealloc 等函数的动态内存一起使用,为每个按下的新键保留内存。

这是到目前为止的代码,目前我只想实现该功能。

char * precio = "00,00€";
int j;

while(1){      //Loop that waits for the sale to be initialized
    UpdateStatusbar(fontClock);
    if (!XuiHasKey()) {
            continue;
    }
    key = XuiGetKey();
    if (key == XUI_KEYUP) {
        j = sizeof(precio) - 2;

        //Position of the last element of precio (-2) to avoid the characters '[=10=]' and '€'

        precio = (char *) calloc(4, sizeof(char));
        precio = (char *) realloc(precio, sizeof(char) + sizeof('[=10=]'));

        (Irrelevant code for printing the: "Type the price")
        while (key != XUI_KEYENTER && key != XUI_KEYCANCEL) {

                UpdateStatusbar(fontClock);
                if (!XuiHasKey()) {
                    continue;
                }
                key = XuiGetKey();

                if (key == XUI_KEYCLEAR) {
                    if (j > 0) {
                        j--;
                        precio[j] = '[=10=]';
                    }
                } //Function to delete as the backspace in your keyboard

                else if (key != 'e') {
                    if(strcmp(precio, "0000") == 0){
                        precio[j] = KeyToChar(key);
                    }else {
                        for (int i = 1; i < j; ++i) {
                            precio[i-1] = precio[i];
                            precio[j] = KeyToChar(key);
                        }
                    }
//This key !='e' comes for the function KeyToChar(). 'e' means Key is not a number  

                }
                precio = (char *) realloc(precio,
                        (j + 1) * sizeof(char) + sizeof('[=10=]'));

                XuiCanvasDrawText(XuiRootCanvas(), 10, 180, 20, font,
                        XUI_TEXT_NORMAL, colorMsgFg, precio);

        } //End While
     } //End if for precio  
}//End While

如您所见,我使用了 TPV 制造商提供的一些特殊库 (XUI)

如果需要更多信息,请索取,我会回复

and a char named "price" that is initialized as "00,00€"

Pressing "1": would make the price to: 00,01€

只需使用整数并将其转换为每个输入的新字符的字符串。

我在大约 15 分钟内写出了下面的代码,不是吗 运行。预计会出现错误和拼写错误。我使用 unsigned long long 允许至少输入 9,223,372,036,854,775,806 美分。我用snprintf计算需要的字符数,然后用malloc分配内存,然后用snprintf再次输出字符串。我假设 字符没有什么特别之处,并且由您的 compiler/environment.

正确处理
#include <limits.h>
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>

/**
 * Get's the price as a string.
 * @param the state variable that get's updated with each number
 * @param Should be the newly inputted character or 0 on clear
 *        TODO: refactor to 2 functions
 * @return an allocated pointer or NULL on error
 *         TODO: pass a pointer to that pointer and use realloc to save free call
 *           or preferably just allocate a buffer big enough on stack 
 *           like `char buf[log10(ULLONG_MAX) + sizeof(",€")]`, which shouldn't be that big actually
 */
char *price_update(unsigned long long *state, int new_char) {
   if (new_char == 0) {
      *state = 0;
   } else {
      if (!('0' <= new_char && new_char <= '9')) goto ERROR_EINVAL;
      if (ULLONG_MAX / 10 > *state)  goto ERROR_OVERFLOW;
      *state *= 10;
      const int to_add = new_char - '0';
      if (ULLONG_MAX - to_add > *state) goto ERROR_OVERFLOW;
      *state += to_add;
   }
   const int count = snprintf(NULL, 0, "%02lld,%02lld€", *state / 100, *state % 100);
   char *price = malloc(count + 1);
   if (price == NULL) goto ERROR_MALLOC;
   snprintf(price, count + 1, "%02lld,%02lld€", *state / 100, *state % 100);
   return price;
// TODO: implement your favorite solution to error handling
ERROR_OVERFLOW:
ERROR_EINVAL:
ERROR_MALLOC:
    return NULL;
}

/**
 * Clears the price
 */
char *price_clear(unsigned long long *state) {
    return price_get(state, 0);
}

// usage
unsigned long long state = 0;
int main(){
 while (1) {
     some_unrelated_code();
     int key = XuiGetKey();
     char *price = key == KEY_CLEAR ? price_clear(&state) : price_get(&state, key);
     if (price == NULL) { /* handle error */ abort(); }
     do_something_with_price(price);
     free(price); 
  }
}