C++语言程序题

来源:百度知道 编辑:UC知道 时间:2024/05/18 09:20:40
1.文件main1.cpp 内容如下:
#include<iostream.h>
void exchange(int x,int y)
{
int t;
t=x;x=y;y=t;
}
void main()
{
int a=15,b=10;
cout<<"first :a="<<a<<"b="<<b<<endl;
exchange(a,b);
cout<<"second: a="<<a<<"b="<<b<<endl;
}
(1) 请写出程序main1.cpp的运行结果;
(2) 如果对上面程序作如下修改,
函数exchange的形参改为int&x和int& y;
请写出修改后程序的运行结果。

2.文件main2.cpp 内容如下:
#include<iostream.h>
void main( )
{
int f1, f2, f;
int i, n;
f1=1, f2=1;
cout<<"input n=";
cin>>n;
cout<<f1<<'\t'<<f2<<'\t';
for(i=3; i<=n ;i++)
{
f = f1+f2;
f1 = f2;
f2 = f;
cout<<f<<'\t';
if(i%5 == 0)
cout<<"\n";

1(1)
first:a=15 b=10
second:a=15 b=10
它们只是交换了形参,没有交换实参。
(2)
first:a=15 b=10
second:a=10 b=15
它们交换的是地址,故实参发生变化。
2
答案是
1 1 2
3
答案是
a=11b=18c=20
x1=-0.818182+1.0718
x2=-0.818182-1.0718
另外你定义float a, b, c,delta, x1, x2,real, image; 的时候有错,c和x2后面的逗号应该是英文输入状态下的逗号,你输入的是中文状态下的逗号,请注意。多看看书,不要老是这样问,对你的vc++不会有太大提高的,同学。

找个环境,自己编译运行一下就知道答案了。
你有时间在网上问别人,自己试试就知道了啊。

1 运行后的结果是 a=15b=15,调用函数后还是一样的。如果你要实现交换的话应该这样写,函数应该这样写。
void exchange(int &x,int &y)
{
int t;
t=x;x=y;y=t;
}

2
f1 1f2 1

f 2f 2f 2 f 2f 2f 2f 2f 2 f 2f 2f 2f 2f 2f 2f 2f 2f 2

第三个你就自己去输吧! 麻烦啦

同楼上的,就算做出来是错误的那也是你你自己做的。
你怎么不叫我帮你花钱。