当前位置:编程学习 > C/C++ >>

c++编程改错~~

1. Before you enter the following program in the computer, please point out the errors of it, then analyze and correct the errors.

#include <iostream.h>

int n;

void main()
{
double m=3.8;
cout<<"n="<<n<<endl;
fn();
}

void fn()
{
cout<<"m="<<m<<endl;
cout<<"n="<<n<<endl;
}


2. Before you enter the following program in the computer, please read it and write down the output results of the program. Then, you can test the results in the computer by compiling and running it in VC++6.0.

#include <iostream.h>

void func();

int n=1;

int main()
{
static int x=5;
int y;
y=n;
cout<<"x="<<x++<<", y="<<++y<<", n="<<n<<endl;
func();
cout<<"x="<<x++<<", y="<<++y<<", n="<<n<<endl;
func();
}

void func()
{
static int x=4;
int y=10;
x+=3;
n+=10;
y+=n;
cout<<"x="<<x<<", y="<<y<<", n="<<n<<endl;
}

3. Before you enter the following program in the computer, please give the results of the following program of Macros. Then, you can test the results in the computer by compiling and running it in VC++6.0.

#include <iostream.h>

#define f(x) x*x

void main()
{
cout<<f(4+4)<<endl;
cout<<f(2+2)<<endl;
cout<<f(4+4)/f(2+2)<<endl;
}


4. Based on the following namespace and what we have learned about namespace, please use two or three different ways to call the namespace in the main function. The program should be designed by modular programming and tell the role of the namespace.

namespace My {
int Add(int, int) ;
int Mul(int, int) ;
}

int My::Add(int a, int b)
{
return a+b;
}

int My::Mul(int a, int b)
{
return a*b;
}

namespace Your {
int Add(int, int) ;
int Mul(int, int) ;}

int Your::Add(int a, int b)
{
return a-b;
}

int Your::Mul(int a, int b)
{
return a/b;
}
追问:第4个帮帮忙,,,高手。。
答案:#include <iostream.h>

int n;
double m=3.8;

void fn()
{
cout<<"m="<<m<<endl;
cout<<"n="<<n<<endl;
}
void main()
{
cout<<"n="<<n<<endl;
fn();
}

这四个题目,只有第一个是改错题,楼上已经回答过了;

第二个是写出程序的输出结果:

x=5, y=2, n=1
x=7, y=21, n=11
x=6, y=3, n=11
x=10, y=31, n=21

 

第三题也是输出结果:

24
8
28

 

第四题是让你在main函数中写出使用namespace的两到三个例子:

例如:

void main(){
  cout<<"2+3="<<My::Add(2,3)<<endl;
  cout<<"2-3="<<Your::Add(2,3)<<endl;
}

上一个:C++编程问题
下一个:C++编程问题

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,