Prime C++ Exercise 2.11 编写程序,要求用户输入两个数——底数(base)和指数 (exponent),输出底数的指数次方的结果。

#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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值