有人可以帮我解决这个问题....谢谢!! [英] Could Someone Help me on this CODE.... THANKS!!

查看:64
本文介绍了有人可以帮我解决这个问题....谢谢!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止...用户输入2个整数。一位数一位数b / b
。之后我必须打印出用户输入的内容,然后将
加在一起。例如:


输入第一个整​​数(一次一个数字)// - 1结束它

1

2

3

-1

输入第二个Intger(一次一个数字)// - 1结束它

2

3

4

5

6

-1

你输入了:

123

&

23456

这些数字的总和是23579


//我总是试图加入两个......


这是我的代码..感谢您的帮助!! !


#include< iostream>

using namespace std;

void display(int x [],int n) ;

void main(){


int arr [100],num;

int i = 0,cnt;

cout<< 输入第一个整​​数的数字(一次一个)"

<< endl;

while(1){

cin>> num;

if(num!= - 1){

arr [i] = num;

i ++;

}

else {

休息;

}

}


cnt = i;


int arr1 [100],num2;

int j = 0,cnt1;

cout<< 输入第二个整数的数字(一个在

时间内)"<< endl;

while(1){

cin>> num2;

if(num2!= - 1){

arr1 [j] = num2;

j ++;

}

else {

休息;

}

}

cnt1 = j;


//显示结果

cout<< 您输入了数字:"<< endl;

display(arr,cnt);

cout<<"和"<< endl;

显示(arr1,cnt1);


}


void display(int x [],对于(i = 0; i< n; i ++){

cout<< x [i];

}

cout<< " \ n";

}

Here is what I have so far... User is to enter 2 integers. One digit
at a time. After that I have to print out what the user entered, and
then add the two together. Example:

Enter First Integer (one digit at a time) //-1 ends it
1
2
3
-1
Enter Second Intger (one digit at a time) //-1 ends it
2
3
4
5
6
-1
You entered:
123
&
23456
The sum of these numbers is 23579

//I AM LOST TRYING TO ADD THE TWO TOGETHER....

HERE IS MY CODE.. THANKS FOR HELPING!!!

#include <iostream>
using namespace std;
void display(int x[], int n);
void main() {

int arr[100],num;
int i=0, cnt;
cout << "enter the digits of your first integer (one at a time)"
<<endl;
while(1) {
cin >> num;
if (num !=-1) {
arr[i]=num;
i++;
}
else {
break;
}
}

cnt=i;

int arr1[100],num2;
int j=0, cnt1;
cout << "enter the digits of your second integer (one at a
time)"<<endl;
while(1) {
cin >> num2;
if (num2 !=-1) {
arr1[j]=num2;
j++;
}
else {
break;
}
}
cnt1=j;

//display the results
cout << "you entered the numbers: "<<endl;
display(arr, cnt);
cout<<"and"<<endl;
display(arr1, cnt1);

}

void display(int x[], int n) {
int i;
for (i=0; i<n; i++) {
cout << x[i];
}
cout << "\n";
}

推荐答案

" 1111111111" <诶*** @ hotmail.com>在留言中写道

新闻:3f ************************** @ posting.google.c om ...
"1111111111" <eh***@hotmail.com> wrote in message
news:3f**************************@posting.google.c om...
这是我到目前为止...用户输入2个整数。一次一位数。之后我必须打印出用户输入的内容,然后将两者一起添加。例如:

输入第一个整​​数(一次一个数字)// - 1结束它
1
2
3
-1 >输入第二个Intger(一次一个数字)// - 1个结束它
2
3
4
5
-1


有些狡辩。你没有输入一个整数,你输入了三个整数。

输入1然后是2然后3将要求你缩放你有的整数

并将它们加在一起得到一个整数。 (1 * 100 + 2 * 10 + 3 = 123)。

你需要一个算法才能得到你的

"一位数的最终整数-at-A-时间"进入方式。 (从0开始,对于每个数字

输入乘以你的整数10并添加新数字。)

当然,为什么不允许用户输入整数123作为单个

条目?目前尚不清楚该任务是什么,所以它可能已经要求

一次一位数。进入,但我们不知道。

您输入了:
123
&
23456
这些数字的总和是23579

//我总是试图加入两个......

这是我的代码..感谢您的帮助!!!

#包括< iostream>
使用命名空间std;
void display(int x [],int n);

void main(){


int main(){

please。

int arr [100],num;
int i = 0,cnt;
cout<< 输入第一个整​​数的数字(一次一个)"
<< endl;
while(1){
cin>> num;
if(num!= - 1){
arr [i] = num;
i ++;
}
else {
break;

}
}
cnt = i;

int arr1 [100],num2;
int j = 0, cnt1;
cout<< 输入第二个整数的数字(一个在
时间)"<< endl;
while(1){
cin>> num2;
if(num2!= - 1){
arr1 [j] = num2;
j ++;
}
else {
break;
}
}
cnt1 = j;

//显示结果
cout<< 你输入了数字:"<< endl;
显示(arr,cnt);
cout<<"和"<< endl;
显示( arr1,cnt1);

}
void display(int x [],int n){
int i;
for(i = 0 ; i< n; i ++){
cout<< x [i];
}
cout<< " \ n";
}
Here is what I have so far... User is to enter 2 integers. One digit
at a time. After that I have to print out what the user entered, and
then add the two together. Example:

Enter First Integer (one digit at a time) //-1 ends it
1
2
3
-1
Enter Second Intger (one digit at a time) //-1 ends it
2
3
4
5
6
-1
Some quibbles. You didn''t enter an integer, you entered three integers.
Entering 1 then 2 then 3 would require you to scale the integers you have
and add them together to get a single integer. (1 * 100 + 2 * 10 + 3 = 123).
You would need an algorithm to do that to get the final integer with your
"one-digit-at-a-time" entry method. (Start with a 0, and for each digit
entered multiply your integer by 10 and add the new digit.)
Of course, why not allow the user to enter the integer 123 as a single
entry? It''s not clear what the assignment is, so it may have called for the
"one-digit-at-a-time" entry, but we don''t know.
You entered:
123
&
23456
The sum of these numbers is 23579

//I AM LOST TRYING TO ADD THE TWO TOGETHER....

HERE IS MY CODE.. THANKS FOR HELPING!!!

#include <iostream>
using namespace std;
void display(int x[], int n);

void main() {
int main( ) {
please.

int arr[100],num;
int i=0, cnt;
cout << "enter the digits of your first integer (one at a time)"
<<endl;
while(1) {
cin >> num;
if (num !=-1) {
arr[i]=num;
i++;
}
else {
break;
}
}

cnt=i;

int arr1[100],num2;
int j=0, cnt1;
cout << "enter the digits of your second integer (one at a
time)"<<endl;
while(1) {
cin >> num2;
if (num2 !=-1) {
arr1[j]=num2;
j++;
}
else {
break;
}
}
cnt1=j;

//display the results
cout << "you entered the numbers: "<<endl;
display(arr, cnt);
cout<<"and"<<endl;
display(arr1, cnt1);

}

void display(int x[], int n) {
int i;
for (i=0; i<n; i++) {
cout << x[i];
}
cout << "\n";
}




嗯,你把数字放到数组中并打印出整个阵列一个
$ b $一次数字b。它使它看起来像一个整数,但它不是。并且

这就是为什么你在将数字加在一起时遇到问题 - 你还没有

两个数字。

我担心整个设计在这一点上的任务也很麻烦(虽然它仍然可以通过乘以和加上

数字来完成)。在这一点上最好的选择是重新思考整个整数如何使用cin读取
。既然你可以阅读一个数字,为​​什么你不能阅读

在用户点击Enter之前输入的几位数?

仔细想想再试一次。但你做了一个有趣的工作,使用数组

这个。

-

加里



Well, you got the digits into arrays and printed out the whole array one
digit at a time. It makes it look like a single integer, but it isn''t. And
that''s why you have a problem adding the numbers together -- you havn''t got
two numbers yet.
I''m afraid the whole design at this point is making the assignment too
cumbersome (although it still can be done by multiplying and adding the
digits). The best bet at this point is to rethink how an entire integer can
be read in using cin. Since you can read a single digit, why can''t you read
several digits that are entered before the user hits Enter?
Think it over and try again. But you did an interesting job using arrays for
this.
--
Gary


* 1111111111:
* 1111111111:
这是我到目前为止的...用户输入2个整数。一次一位数。之后我必须打印出用户输入的内容,然后将两者一起添加。例如:

输入第一个整​​数(一次一个数字)// - 1结束它
1
2
3
-1 >输入第二个Intger(一次一个数字)// - 1结束它
2
3
4
5
-1
你进入了:
123
&
23456
这些数字的总和是23579

//我很难尝试添加两个一起......

这是我的代码..感谢帮助!!!

#include< iostream>
使用命名空间std;
void display(int x [],int n);

void main(){


''main''必须有返回类型''int ''。


如果您的编译器没有发出警告或错误,它就会被破坏。


Visual C ++编译器是一个例子。

int arr [100],num;
int i = 0,cnt;
cout<< 输入第一个整​​数的数字(一次一个)"


你的意思是,每行一个。


<< endl;
while(1){


更好地使用''(;;)''或者你冒着编译警告的风险。

另外,如果你绝对必须使用''while''' ,然后''while(true)''。


cin>> num;
if(num!= - 1){
arr [i] = num;
i ++;


使用++ i,而不是i ++。


}
其他{
休息;


为什么不简单''if(num == -1){break; }''?


另外,更严重的是,如果

用户输入的数字超过一百位,那么你冒着_buffer溢出的风险。


您应该检查一下,并且必须使用符号

常数作为数组大小。

}
}
cnt = i;

int arr1 [100],num2;
int j = 0,cnt1;
cout<< 输入第二个整数的数字(一个在
时间)"<< endl;
while(1){
cin>> num2;
if(num2!= - 1){
arr1 [j] = num2;
j ++;
}
else {
break;
}
}
cnt1 = j;


请注意,这几乎与以前的代码相同。


所以这应该是一个函数。


此外,不是那个数组加上一个元素数基本上是什么

a std :: vector给你,所以请改用std :: vector。


//显示结果
cout<< 你输入了数字:"<< endl;
显示(arr,cnt);
cout<<"和"<< endl;
显示( arr1,cnt1);


现在,如何加在一起数字取决于你是否可以

假设一个有限的范围(所以你可以转换为整数并使用

内置的补充)或不。


如果没有,使用从最低有效数字开始的循环,

添加那些,并从结果计算0 ... 9范围数字

和随身携带(如果有的话)。循环的下一次迭代,添加进位和

下一个最低有效数字。等等。


}

void display(int x [],int n){
int i;
for( i = 0; i< n; i ++){
cout<< x [i];
}
cout<< " \ n";
}
Here is what I have so far... User is to enter 2 integers. One digit
at a time. After that I have to print out what the user entered, and
then add the two together. Example:

Enter First Integer (one digit at a time) //-1 ends it
1
2
3
-1
Enter Second Intger (one digit at a time) //-1 ends it
2
3
4
5
6
-1
You entered:
123
&
23456
The sum of these numbers is 23579

//I AM LOST TRYING TO ADD THE TWO TOGETHER....

HERE IS MY CODE.. THANKS FOR HELPING!!!

#include <iostream>
using namespace std;
void display(int x[], int n);
void main() {
''main'' must have return type ''int''.

If your compiler doesn''t issue a warning or error it''s broken.

The Visual C++ compiler is one example.
int arr[100],num;
int i=0, cnt;
cout << "enter the digits of your first integer (one at a time)"
You mean, "one per line".

<<endl;
while(1) {
Better use ''for( ;; )'' or else you risk a compilation warning.
Also, if you absolutely must use ''while'', then ''while( true )''.

cin >> num;
if (num !=-1) {
arr[i]=num;
i++;
Use ++i, not i++.

}
else {
break;
Why not simply ''if( num == -1 ) { break; }''?

Also, more serious, here you risk a _buffer overflow_ if the
user enters more than one hundred digits.

You should check for that, and that necessitates using a symbolic
constant for the array size.

}
}

cnt=i;

int arr1[100],num2;
int j=0, cnt1;
cout << "enter the digits of your second integer (one at a
time)"<<endl;
while(1) {
cin >> num2;
if (num2 !=-1) {
arr1[j]=num2;
j++;
}
else {
break;
}
}
cnt1=j;
Note that this is virtually the same code as before.

So this should be made into a function.

Also, not that array plus a count of elements is essentially what
a std::vector gives you, so do use a std::vector instead.

//display the results
cout << "you entered the numbers: "<<endl;
display(arr, cnt);
cout<<"and"<<endl;
display(arr1, cnt1);
Now, how to "add together" the numbers depends on whether you can
assume a limited range (so you can convert to integer and use the
built-in addition) or not.

If not, the use a loop that starts at the least significant digit,
adds those, and from the result computes the 0...9 range digit
and carry (if any). Next iteration of the loop, add the carry and
the next least significant digits. And so on.

}

void display(int x[], int n) {
int i;
for (i=0; i<n; i++) {
cout << x[i];
}
cout << "\n";
}




我会以稍微不同的方式构建一个函数返回

a字符串,其中没有换行字符。


-

答:因为它弄乱了人们通常阅读文本的顺序。 />
问:为什么这么糟糕?

A:热门发布。

问:usenet上最烦人的事情是什么?电子邮件?



I''d structure that a little bit differently, as a function returning
a string, and with no newline char in it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


>
现在,如何加在一起数字取决于你是否可以假定有限的范围(所以你可以转换为整数并使用
内置的补充)或不。
Now, how to "add together" the numbers depends on whether you can
assume a limited range (so you can convert to integer and use the
built-in addition) or not.




我该怎么做呢?????



How would I go about doing this?????


这篇关于有人可以帮我解决这个问题....谢谢!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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