#include <iostream>
int main()
{
//局部对象
int base,exponent;
long result=1;
//读入底数、指数
std::cout<<"Place enter base and exponent :"<<std::endl;
std::cin>>base>>exponent;
if (exponent<0)
{
std::cout<<"Exponent con not smaller than 0"<<std::endl;
return -1;
}
if (exponent>0)
{
//计算
for (int i = 1; i <=exponent; i++)
{
result*=base;
}
}
std::cout<<base<<"raised to the power of"<<exponent<<"= "<<result<<std::endl;
system("pause");
return 0;
}
1.命名空间的 using 声明
a.使用 using 声明可以在不需要加前缀 namespace_name:: 的情况下访问命名空间中的名字。using 声明的形式如下:
using namespace::name
b.一个 using 声明一次只能作用于一个命名空间成员。using 声明可用来明确指定在程序中用到的命名空间中的名字,如果希望使用 std(或其他的命名空间)中的几个名字,则必须为要用到的每个名字都提供一个 using 声明
c.有一种情况下,必须总是使用完全限定的标准库名字:在头文件中。理由是头文件的内容会被预处理器复制到程序中。用 #include 包含文件时,相当于头文件中的文本将成为我们编写的文件的一部分。如果在头文件中放置 using 声明,就相当于在包含该头文件 using 的每个程序中都放置了同一 using,不论该程序是否需要 using 声明。
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
//局部对象
int base,exponent;
long result=1;
//读入底数、指数
cout<<"Place enter base and exponent :"<<endl;
cin>>base>>exponent;
if (exponent<0)
{
cout<<"Exponent con not smaller than 0"<<endl;
return -1;
}
if (exponent>0)
{
//计算
for (int i = 1; i <=exponent; i++)
{
result*=base;
}
}
cout<<base<<"raised to the power of"<<exponent<<"= "<<result<<endl;
system("pause");
return 0;
}