将负数存储在 char 数组中(仍然没有所需/所需的解决方案)

发布于 2024-10-08 06:20:54 字数 1169 浏览 3 评论 0原文

EDIT:
  platform unix 
  type :  ansi c

我有表格数据;

1 2 3 -1 2 -9 1 3 + 
-1 2 -3 -4 -
*

范围在 -9 到 9

'+ - *' 运算符之间的整数,并表明您应该获取位于下一行的数据 数据

是 char 双指针

当我获取数据并存储时,每行必须存储在双指针 char 数组中

example :   data[0] :=>  1 2 3 -1 2 -9 1 3 + 
            more precisely :  data[0][3] must store -1 

,我可以未实现在 data[i][j] 中存储 -3(负整数) 因为 '-' 是一个字符,所以 data[i][j] 不接受 3

我应该怎么处理这个问题?

编辑:我的代码;

size_t datalen = sizeof( char ) ;

data = ( char ** ) malloc( sizeof (char * ) ) 
for ( i = 0 ;   ; ++i ) 
   data[i] = (char * ) malloc ( datalen )
   for ( j = 0 ;  ; ++ j )
         signed char ch;
         if j !=  0 
            datalen += 1
            data[i] = ( char * ) realloc ( begin[i], datalen )
         scanf ("%c ", &ch ) 
         begin[i][j] = ch 
         if ch == OP ( op = + , - , * , / )
             break
   if strlen ( begin[i] ) == 1 
           break

编辑 : 如果你查看 ascii 表,你就会明白为什么我不使用 scanf("%d",&ch) http://www.asciitable.com/

EDIT:
  platform unix 
  type :  ansi c

I have data in form ;

1 2 3 -1 2 -9 1 3 + 
-1 2 -3 -4 -
*

integer in range between -9 and 9

'+ - *' operator and shows that you should take data which lies at following line

data is char double pointer

each line must be stored in double pointer char array

example :   data[0] :=>  1 2 3 -1 2 -9 1 3 + 
            more precisely :  data[0][3] must store -1 

when I take data and store, I could not achieve store -3 ( negative integer ) in data[i][j]
because '-' is a character so 3 is not accepted by data[i][j]

What should I do to handle this problem?

EDIT: MY code ;

size_t datalen = sizeof( char ) ;

data = ( char ** ) malloc( sizeof (char * ) ) 
for ( i = 0 ;   ; ++i ) 
   data[i] = (char * ) malloc ( datalen )
   for ( j = 0 ;  ; ++ j )
         signed char ch;
         if j !=  0 
            datalen += 1
            data[i] = ( char * ) realloc ( begin[i], datalen )
         scanf ("%c ", &ch ) 
         begin[i][j] = ch 
         if ch == OP ( op = + , - , * , / )
             break
   if strlen ( begin[i] ) == 1 
           break

EDIT :
if you look at ascii table you will be understand why I am not using scanf("%d",&ch)
http://www.asciitable.com/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等待我真够勒 2024-10-15 06:20:54

我认为这里的关键是利用输入值的有限范围。由于您的输入数字只是一位整数,因此请找到几个不可能的输入值并将它们保留给运算符。例如,您可以让 64 为“+”,65 可以为“-”等。使用 strtol() 一次读入一个数字,然后验证它们并确保它们落在在您指定的范围内并将它们转换为字符。如果您看到一个没有附加数字的运算符,请将其转换为适当的保留值(为更清晰的代码创建“encode_operator”和“decode_operator”函数)并存储它。

这种方法的缺点是你不能盲目地将存储的值用作数字。当您从数组中提取数据时,您必须检查每个值以查看它是运算符还是数字。对于您的情况,一个简单的 #IS_OPERATOR(x) ((x < -9) || (x > 9)) 宏应该能够为您完成此操作。

I believe the key here is to take advantage of the limited range of your input values. Since your input numbers will only be single-digit integers, find several impossible input values and reserve them for operators. For example, you can let 64 be '+', 65 can be '-', etc. Use strtol() to read in the numbers one at a time, then validate them and make sure they fall in your specified range and cast them down to a char. If you see an operator without a number attached to it, convert it to the appropriate reserved value (make 'encode_operator' and 'decode_operator' functions for cleaner code) and store it.

The downside to this method is that you cannot blindly use the stored value as a number. When you extract the data from the array, you will have to check each value to see if it is an operator or a number. In your case, a simple #IS_OPERATOR(x) ((x < -9) || (x > 9)) macro should be able to do this for you.

—━☆沉默づ 2024-10-15 06:20:54

如果您确定整数范围在 -9 到 9 之间,并且希望将它们单独存储在 char 中,那么有一个技巧,尽管包括更多的数字计算。

通过对每个数字减去 -9,将 {-9, -8, ... , 8, 9} 映射到 {0, 1, ..., 17, 18}。这时候你已经有了所有的非负整数,所以做你想做的事吧。在运行时,您应该通过添加 -9 将存储的数据转换为其原始值。

If you are sure that integers range between -9 and 9, and want to store them in a char individually, there is a trick, though more number calculation included.

Map {-9, -8, ... , 8, 9} to {0, 1, ..., 17, 18}, by minusing -9 for every number. At this time, you have all non-negative integers, so do what you want. At runtime, you should convert the data stoed to its original value by adding -9.

ㄟ。诗瑗 2024-10-15 06:20:54
char '2' != int 2

为什么不使用 use

int tmp; 
scanf("%d", &tmp);
data[i][j] = tmp;

来代替呢?通过使用,

scanf("%c", &ch);

您只能读取一个 ASCII 字符,该字符可以是空格、减号或数字。

编辑:查阅ASCII表后,代码为0x2F的/字符将翻译为 -1 iff 它的读取方式如下:

char c; 
scanf("%c", &c);
data[i][j] = c - '0'; // 0x2F - 0x30 == -1
char '2' != int 2

why don't use use

int tmp; 
scanf("%d", &tmp);
data[i][j] = tmp;

instead? By using

scanf("%c", &ch);

you read just one ASCII character, that could be space, minus sign or digit.

EDIT: after consulting ASCII table, the / character with code 0x2F would translate to -1 iff it's read in this way:

char c; 
scanf("%c", &c);
data[i][j] = c - '0'; // 0x2F - 0x30 == -1
凉栀 2024-10-15 06:20:54

您可以改用 Union 数组。

typedef union
{
  int num;
  char op;
} ABC;

然后制作ABC数组。

You can use an Union array instead.

typedef union
{
  int num;
  char op;
} ABC;

Then make array of ABC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文