c-如何从右到左填充变量Char



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

我的问题是下一个:

我已经在TPV中编程了一把钥匙,用来初始化销售。这会打印一个标签"键入价格"和一个名为"价格"的char,初始化为"00,00€">

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

  • 按"1":将使价格为:00,01€
  • 下一个数字("2"(:00.12€
  • 下一个("3"(:01,23€
  • (…(:123,45欧元
  • (…(:123456,78欧元
  • 等等

此价格字符与动态内存一起使用,使用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 '' and '€'
precio = (char *) calloc(4, sizeof(char));
precio = (char *) realloc(precio, sizeof(char) + sizeof(''));
(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] = '';
}
} //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(''));
XuiCanvasDrawText(XuiRootCanvas(), 10, 180, 20, font,
XUI_TEXT_NORMAL, colorMsgFg, precio);
} //End While
} //End if for precio  
}//End While

正如你所看到的,我使用了一些由TPV 制造商提供的特殊库(XUI(

如果需要更多的信息,询问它,我会把它还给

和一个名为"价格;其被初始化为"0";00,00欧元;

按压";1〃:将使价格达到:00,01€

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

我在大约15分钟内写下了下面的代码,但没有运行。错误和拼写错误是意料之中的事。我使用unsigned long long允许输入至少9223372036854775806美分。我使用snprintf来计算所需的字符数,然后用malloc分配内存,然后用snprintf再次输出字符串。我假设字符没有什么特别之处,并且由编译器/环境正确处理。

#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); 
}
}

最新更新