如题
可以用for循环
int x,n;
int result = x;
for(int i = 0; i<n;i++){
result = result * x;
}
写一个for循环
#include<bits/stdc++.h>
using namespace std;
int main(){
int x,n,s=1;
cin>>x>>n;
for(int i=1;i<=n;i++)
s=s*x;
cout<<s;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:
*计算保留两位小数
a--加法 b--减法
c--乘法 d--除法
e--开放 f--平方
g--立方 q--退出
运行示例:
&欢迎使用简易计算机&
请输入以下字母:
a:加法 b:减法 c:乘方 d:除法
e:开方 f:平方 g:立方 q:退出
请输入你的选项:e
请输入数值:5
2.00的开方等于2.24
代码示例:
代码:
#include<stdio.h>
#include <math.h>
float num1,num2;
float *p1=&num1,*p2=&num2;
void input1(){
printf("请输入数值:");
scanf("%f",p1);
}
void input2(){
printf("请输入两个数值,中间用空格隔开:");
scanf("%f %f",p1,p2);
}
void add(){
input2();
printf("%.2f加%.2f等于%.2f",*p1,*p2,*p1+*p2);
}
void sub(){
input2();
printf("%.2f减%.2f%等于%.2f",*p1,*p2,*p1-*p2);
}
void mul(){
input2();
printf("%.2f乘与%.2f等于%.2f",*p1,*p2,*p1*(*p2));
}
void count_d(){
input2();
printf("%.2f除于%.2f等于%.2f",*p1,*p2,*p1/(*p2));
}
void count_e(){
input1();
printf("%.2f的开方等于%.2f",*p1,sqrt(*p1));
}
void count_f(){
input1();
printf("%.2f的平方等于%.2f",*p1,*p1*(*p1));
}
void count_g(){
input1();
printf("%.2f的立方等于%.2f",*p1,pow(*p1,3));
}
int main()
{
char in;
printf("***欢迎使用简易计算机***\n");
printf("--------------------------------\n");
printf("a. 加法\t\t b.减法 \n");
printf("c. 乘法 d. 除法 \n");
printf("e. 开方\t\t f.平方 \n");
printf("g. 立方\t\t q.退出 \n");
printf("--------------------------------\n");
printf("请输入你的选项:");
scanf("%c",&in);
switch(in){
case 'a' :add();break;
case 'b' :sub();break;
case 'c' :mul();break;
case 'd' :count_d();break;
case 'e' :count_e();break;
case 'f' :count_f();break;
case 'g' :count_g();break;
case 'q' :break;}
}
代码分析:在运行代码是必须先要输入特定字母,接下来进行此次算法;此外对加减乘除只限于两个数之间的运算,此代码没有实用性(微笑)。