// test14.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<cctype>
#include <vector>
#include<cstring>
//#include<stdexcept>
#include<exception>
using namespace std;

class Solution {
public:
	double Power(double base, int exponent) {
		if (base == 0&&exponent<=0)//base为负,指针也为负
		{
			cout << "无效的输入"<<endl;
			return 0;
		}
		double flag = 1;
		if (exponent > 0)//指数为正
		{
			for (int i = 0; i < exponent; i++)
				flag *= base;
		}
		else if (exponent < 0)//指数为负
		{
			for (int i = 0; i < -exponent; i++)
				flag *= (1/base);

		}
		else { //指数为0
			return flag;
		}
		return flag;
	}
};
int main()
{
	Solution so;
	double base;
	int exponent;
	while (true)
	{
		cout << "请输入一个浮点数: ";
		cin >> base;
		cout << "请输入一个整数: ";
		cin >> exponent;

		cout << base<<"的"<<exponent<<"次方是"<< so.Power(base,exponent)<< endl;
		cout << endl;
	}
	
	
}

相关文章:

  • 2022-12-23
  • 2021-12-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2021-07-29
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
相关资源
相似解决方案