如何连接两个整数值 [英] How to concatenate two integer values

查看:60
本文介绍了如何连接两个整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何连接两个整数值。

示例程序:

#include" Port.h"

#include" BinaryConversion.h"

#include" iostream.h"

Port p;

long binary(很久);


void BinaryConversion :: Fire()

{

长号,r;

number = p.GetToken_DecimalNo();

printf(" Inside user code%d",number);

r = binary(number);

p.Send(r);

}

长二进制(长号)

{

长余数;

长的重新;

而(数字> 1)

{

余数=数字%2;

number = number / 2;

re = re&剩余; //连接这两个整数值

}


返回(重新);


}


如果能够解决上述问题,那将是一个很大的帮助。谢谢

很多!

解决方案

priya写道:


如何连接两个整数值。


连接是什么意思?你想把两个数字打包成

相同的变量,一个在高位,一个在低位吗?


示例程序:

#include" Port.h"
#include" BinaryConversion.h"
#include" iostream.h"


如果您的教科书告诉您这样做,那么您需要一本新的

教科书。使用#include< iostream> ;.


< snip>

长二进制(长号)
{
长余数;
long re;
while(number> 1)
{
余数=数字%2;
数字=数字/ 2;
re = re &安培;剩余; //连接这两个整数值
}

返回(重新);

}




你为什么不告诉我们你想要二元()做什么?你的代码很难告诉

。我之前关于将两个数字打包到相同的

变量中的猜测一定是错的,因为你只有一个数字作为输入。


J.


感谢您的回复


binary()方法用于查找给定小数的二进制值

数。


我想说:

字符串连接:

string s =" hi";

string s1 =" hello";

string s3 = s1 + s2;

结果:hihello


同样的我想要连接两个整数:

int n = 10;

int n1 = 90;

我需要的结果:1090


priya写道:

感谢您的回复

binary()方法用于查找给定的二进制值十进制
数字。

我想说:
字符串连接:
字符串s =" hi";
字符串s1 =" hello" ;
字符串s3 = s 1 + s2;
结果:hihello

同样我想连接两个整数:
int n = 10;
int n1 = 90;
结果我需要:1090




嗯,简单的方法是使用字符串。

int n1 = 10,n2 = 90;

stringstream ss;

ss<< n1<< n2;

long int n3 = strtol(ss.str()。c_str(),NULL,10);


如果必须这样做艰难的方式(例如,对于家庭作业)然后

你需要弄清楚n2中有多少位数,例如通过

log10(n2)然后存储类似的东西(10 * log10(n2)* n1 + n2)。

所以对于你的原始例子,你想要的东西是有效的out to

(10 * 2 * n1 + n2)其中n1 = 10且n2 = 90.


雅克。


Hi
How to concatenate two integer Values.
Example Program :
#include "Port.h"
#include "BinaryConversion.h"
# include "iostream.h"
Port p;
long binary(long);

void BinaryConversion::Fire()
{
long number,r;
number=p.GetToken_DecimalNo();
printf("Inside user code %d",number);
r=binary(number);
p.Send(r);
}
long binary(long number)
{
long remainder;
long re;
while(number >1)
{
remainder = number%2;
number = number / 2;
re = re & remainder; //concatenate these two integer values
}

return(re);

}

It would be a great help if the above issues can be resolved. Thanks a
lot!

解决方案

priya wrote:

Hi
How to concatenate two integer Values.
What do you mean by concatenation? Do you want to pack two numbers into
the same variable, with one in the high bits and one in the low bits?


Example Program :
#include "Port.h"
#include "BinaryConversion.h"
# include "iostream.h"
If your textbook told you to do it this way then you need a new
textbook. Use #include <iostream>.

<snip>
long binary(long number)
{
long remainder;
long re;
while(number >1)
{
remainder = number%2;
number = number / 2;
re = re & remainder; //concatenate these two integer values
}

return(re);

}



Why don''t you tell us what you want binary() to do? It''s hard to tell
from your code. My earlier guess about packing two numbers into the same
variable must be wrong, because you only have one number as input.

J.


Thanks for your reply

binary() method is for finding the binary value of given decimal
number.

I meant to say :
String concatenation :
string s="hi";
string s1="hello";
string s3=s1+s2;
Result :hihello

Similarly i want to concatenate two integer :
int n=10;
int n1=90;
The result i need : 1090


priya wrote:

Thanks for your reply

binary() method is for finding the binary value of given decimal
number.

I meant to say :
String concatenation :
string s="hi";
string s1="hello";
string s3=s1+s2;
Result :hihello

Similarly i want to concatenate two integer :
int n=10;
int n1=90;
The result i need : 1090



Well, the easy way is to use strings.
int n1 = 10, n2 = 90;
stringstream ss;
ss << n1 << n2;
long int n3 = strtol(ss.str().c_str(), NULL, 10);

If you have to do it the hard way (e.g. for a homework assignment) then
you need to figure out how many digits there are in n2, e.g. by taking
log10(n2) and then store something like (10*log10(n2)*n1 + n2).
So for your original example you want something that works out to
(10*2*n1 + n2) where n1=10 and n2=90.

Jacques.


这篇关于如何连接两个整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆