将负数存储在 char 数组中(仍然没有所需/所需的解决方案)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这里的关键是利用输入值的有限范围。由于您的输入数字只是一位整数,因此请找到几个不可能的输入值并将它们保留给运算符。例如,您可以让 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.如果您确定整数范围在 -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.
为什么不使用 use
来代替呢?通过使用,
您只能读取一个 ASCII 字符,该字符可以是空格、减号或数字。
编辑:查阅ASCII表后,代码为0x2F的
/
字符将翻译为-1
iff 它的读取方式如下:why don't use use
instead? By using
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:您可以改用 Union 数组。
然后制作ABC数组。
You can use an Union array instead.
Then make array of ABC.