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

如何从右到左填充变量Char?,c,arrays,right-to-left,C,Arrays,Right To Left,通常,TPV是用集成软件制造的,您可以开发它与服务器之间的交互。现在我的工作是进行基础开发,换句话说,我开发整个项目 我的问题是下一个: 我已经在TPV中设置了一个钥匙,用于启动销售。这将打印一个标签“键入价格”和一个名为“价格”的char,初始化为“00,00€” 这很简单。现在我想从右到左填写价格字符,即: 按“1”:将使价格达到:00,01欧元 下一个数字(“2”):00,12欧元 下一个(“3”):01,23欧元 (……):123,45欧元 (……):123456,78欧元 等等 此

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

我的问题是下一个:

我已经在TPV中设置了一个钥匙,用于启动销售。这将打印一个标签“键入价格”和一个名为“价格”的
char
,初始化为“00,00€”

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

  • 按“1”:将使价格达到:00,01欧元
  • 下一个数字(“2”):00,12欧元
  • 下一个(“3”):01,23欧元
  • (……):123,45欧元
  • (……):123456,78欧元
  • 等等
此price char与动态内存一起使用,使用的函数包括
calloc
realloc
,为每个按下的新键保留内存

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

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 '\0' and '€'

        precio = (char *) calloc(4, sizeof(char));
        precio = (char *) realloc(precio, sizeof(char) + sizeof('\0'));

        (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] = '\0';
                    }
                } //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('\0'));

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

        } //End While
     } //End if for precio  
}//End While
char*precio=“00,00€”;
int j;
while(1){//等待销售初始化的循环
UpdateStatusbar(fontClock);
如果(!XuiHasKey()){
继续;
}
key=XuiGetKey();
if(key==XUI\u KEYUP){
j=sizeof(precio)-2;
//precio(-2)的最后一个元素的位置,以避免字符“\0”和“€”
precio=(char*)calloc(4,sizeof(char));
precio=(char*)realloc(precio,sizeof(char)+sizeof('\0');
(用于打印“键入价格”的无关代码)
while(key!=XUI\u键输入和key!=XUI\u键取消){
UpdateStatusbar(fontClock);
如果(!XuiHasKey()){
继续;
}
key=XuiGetKey();
if(key==XUI\u KEYCLEAR){
如果(j>0){
j--;
precio[j]='\0';
}
}//作为键盘中的退格删除的函数
否则如果(键!=“e”){
如果(strcmp(精度,,“0000”)==0){
precio[j]=KeyToChar(键);
}否则{
对于(int i=1;i
如您所见,我使用TPV制造商提供的一些特殊库(XUI)

如果需要更多信息,请索取,我会还给你

和一个名为“price”的字符,初始化为“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); 
  }
}
#包括
#包括
#包括
#包括
#包括
#包括
/**
*这是一个字符串的价格。
*@param使用每个数字更新get的状态变量
*@param应为新输入的字符或清除时为0
*TODO:重构为2个函数
*@返回已分配的指针或返回空值(出现错误)
*TODO:传递指向该指针的指针,并使用realloc保存空闲调用
*或者最好只在堆栈上分配一个足够大的缓冲区
*比如'char buf[log10(ULLONG_MAX)+sizeof(“,€”)`,实际上应该没有那么大
*/
字符*价格\更新(无符号长*状态,整型新字符){
如果(新字符==0){
*状态=0;
}否则{
如果(!('0'*状态)转到错误\u溢出;
*状态+=待添加;
}
const int count=snprintf(NULL,0,“%02lld,%02lld€”,*状态/100,*状态%100);
char*price=malloc(计数+1);
如果(价格==NULL)转到错误\u MALLOC;
snprintf(价格,计数+1,“%02 lld,%02 lld€”,*州/100,*州%100);
退货价格;
//TODO:实现您最喜欢的错误处理解决方案
错误\u溢出:
错误消息:
错误\u MALLOC:
返回NULL;
}
/**
*结算价格
*/
字符*price_clear(无符号长*状态){
返回价格(状态为0);
}
//用法
无符号长状态=0;
int main(){
而(1){
一些无关的代码();
int key=XuiGetKey();
char*price=key==key\u CLEAR?price\u CLEAR(&state):price\u get(&state,key);
如果(price==NULL){/*句柄错误*/abort();}
用价格做某事;
免费(价格);
}
}

我会保留一个
int
而不是
char*
,每次按键时,你都会将它乘以10,然后将你按下的数字相加。然后当你需要显示它时,你将它除以100,然后将它转换成
char*
。似乎更容易处理。
sizeof(char)+sizeof('\0')
sizeof(char sizeof(char)+sizeof(int)
。既然
sizeof(char)
被指定为总是等于
1
,而
int
的大小通常是
4
,那么你就有
1+4
等于
5
。你只分配了五个字节。总是这样。当前代码有什么问题吗?我忘了说:我知道