Stroustrup 5.9,练习4(第105页) [英] Stroustrup 5.9, exercise 4 (page 105)

查看:68
本文介绍了Stroustrup 5.9,练习4(第105页)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个程序编译但遇到了一些奇怪的结果,一个语义 -

BUG就在那里。我甚至放了一些std :: cout要知道出了什么问题,但它确实是b $ b做了一些我没有命令它做的事情:


------------ ---计划-------------------

/ * Stroustrup,5.9运动4

声明:

编写一个交换2个整数的程序。解决它

使用1st指针然后引用。

方法:

1.)要求用户输入2个整数

2.)使用指针交换价值,

打印2个语句以显示它正在做什么

3.)打印SWAPPED值

4.)使用参考资料交换谷物评价

5.)所以在交换2次之后我们得到原始输入值。

* /

#include< iostream>


void swap_p(int * x,int * y);

void swap_r(int& x,int& y );


int main()

{

使用std :: cout;

使用std :: cin;

使用std :: endl;


int i;

int j;


cout<< 输入第一个整​​数:;

cin> i;

cout<< 输入第二个整数:;

cin> j;


cout<< i = << i<< ''\t''

<< j = << j<< endl;


int * pi =& i;

int * pj =& j;

swap_p(pi, pj);


cout<< " \ nvalues使用Poiners \ n交换

<< i = << i<< ''\t''

<< j = << j<< endl;

swap_r(i,j);


cout<< " \ nswappe dusing Refrences\\\
"

<< i = << i<< ''\t''

<< j = << j<< endl;


返回0;

}


void swap_p(int * x,int * y)

{

int temp_val = * x;

x = y;

std :: cout<< " ------------------------------ \ n";

std :: cout< ;< x = << * x<< " \ty =" << * y<<''\ n'';


* y = temp_val;

std :: cout<< x = << * x<< " \ty =" << * y<<''\ n'';

std :: cout<< ------------------------------ \ n;

}


void swap_r(int& x,int& y)

{

int temp_val = x;

x = y;

y = temp_val;

}

--------------输出----- -------------

[arch @ voodo tc ++ pl] $ g ++ -ansi -pedantic -Wall -Wextra 5.9_ex-04.cpp

[arch @ voodo tc ++ pl] $ ./a.out

输入第一个整​​数:3

输入第二个整数:-2

i = 3 j = -2

------------------------------

x = -2 y = -2

x = 3 y = 3

---------------- --------------
使用Poiners交换


i = 3 j = 3

swappe dusing refrences

i = 3 j = 3

[arch @ voodo tc ++ pl] $

--- ---------------------------

x = -2 y = -2

x = 3 y = 3

------------------------------

第一行很好,但为什么它会改变x的值?在第二行。我确实

没有对x做任何事情




this programme compiles but runs into some strange results, a semantic-
BUG is there. i even put some "std::cout" to know what is wrong but it
does something that i did NOT order it to do:

--------------- PROGRAMME -------------------
/* Stroustrup, 5.9 exercise 4

STATEMENT:
write a programme that swaps 2 integers. solve it
using 1st with pointers and then references.
METHOD:
1.) asks the user to input 2 integers
2.) swaps the value using pointers,
prints 2 statements to show what it is doing
3.) prints the SWAPPED values
4.) swaps the vale sagain using References
5.) so after swapping 2 times we get the original input values.
*/
#include<iostream>

void swap_p(int* x, int* y);
void swap_r(int& x, int& y);

int main()
{
using std::cout;
using std::cin;
using std::endl;

int i;
int j;

cout << "Enter 1st integer: ";
cin >i;
cout << "enter 2nd integer: ";
cin >j;

cout << "i = " << i << ''\t''
<< "j = " << j << endl;

int* pi = &i;
int* pj = &j;
swap_p(pi, pj);

cout << "\nvalues swapped using Poiners\n"
<< "i = " << i << ''\t''
<< "j = " << j << endl;
swap_r(i, j);

cout << "\nswappe dusing Refrences\n"
<< "i = " << i << ''\t''
<< "j = " << j << endl;

return 0;
}

void swap_p(int* x, int* y)
{
int temp_val = *x;
x = y;
std::cout << "------------------------------\n";
std::cout << "x = " << *x << "\ty = " << *y <<''\n'';

*y = temp_val;
std::cout << "x = " << *x << "\ty = " << *y <<''\n'';
std::cout << "------------------------------\n";
}

void swap_r(int& x, int& y)
{
int temp_val = x;
x = y;
y = temp_val;
}
-------------- OUTPUT ------------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra 5.9_ex-04.cpp
[arch@voodo tc++pl]$ ./a.out
Enter 1st integer: 3
enter 2nd integer: -2
i = 3 j = -2
------------------------------
x = -2 y = -2
x = 3 y = 3
------------------------------

values swapped using Poiners
i = 3 j = 3

swappe dusing Refrences
i = 3 j = 3
[arch@voodo tc++pl]$
------------------------------
x = -2 y = -2
x = 3 y = 3
------------------------------

1st line is fine but why does it change value of "x" in 2nd line.i did
not do anything to "x"

?

推荐答案

g ++ -ansi -pedantic -Wall -Wextra 5.9_ex-04.cpp

[arch @ voodo tc ++ pl]
g++ -ansi -pedantic -Wall -Wextra 5.9_ex-04.cpp
[arch@voodo tc++pl]


./ a.out

输入第一个整​​数:3

输入第二个整数:-2

i = 3 j = -2

------------------------------

x = -2 y = -2

x = 3 y = 3

--------------------------- ---

使用Poiners交换


i = 3 j = 3

swappe dusing refrences

i = 3 j = 3

[arch @ voodo tc ++ pl]
./a.out
Enter 1st integer: 3
enter 2nd integer: -2
i = 3 j = -2
------------------------------
x = -2 y = -2
x = 3 y = 3
------------------------------

values swapped using Poiners
i = 3 j = 3

swappe dusing Refrences
i = 3 j = 3
[arch@voodo tc++pl]




---- --------------------------

x = -2 y = -2

x = 3 y = 3

------------------------------


第一行很好,但为什么它会改变x的值。在第二行。我做了什么

没有对x做任何事情





------------------------------
x = -2 y = -2
x = 3 y = 3
------------------------------

1st line is fine but why does it change value of "x" in 2nd line.i did
not do anything to "x"

?

这篇关于Stroustrup 5.9,练习4(第105页)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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