当前位置:首页 » 编程语言 » c语言制作计算器乘方
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言制作计算器乘方

发布时间: 2022-11-29 20:51:19

A. 使用c语言编程,用函数实现一个计算器,在主函数中调用函数,包括加减乘除,乘方,绝对值和sin函数。

#include<stdio.h>
#include<stdlib.h>
double jia(double a,double b)
{
return a+b;
}
double jian(double a,double b)
{
return a-b;
}
double cheng(double a,double b)
{
return a*b;
}
double chu(double a,double b)
{
return a/b;
}
double juei(double a)
{
return a>0 ? a : -a;
}
double chengfang(double a,double b)
{
return pow(a,b);
}
double sinx(double a)
{
return sin(a);
}
int main()
{
int m;
double a,b;

while(1)
{
printf("请输入第一个操作数:");
scanf("%lf",&a);
printf("0、退出\n1、加\n2、减\n3、乘\n4、除\n5、绝对值\n6、乘方\n7sin、\n请选择一个:");
scanf("%d",&m);
if(1==m || 2==m || 3==m || 4==m || 6==m)
{
printf("请输入第二个操作数:");
scanf("%lf",&b);
}
switch(m)
{
case 0:
exit(0);
break;
case 1:
printf("%lf+%lf=%lf\n",a,b,jia(a,b));
break;
case 2:
printf("%lf-%lf=%lf\n",a,b,jian(a,b));
break;
case 3:
printf("%lf*%lf=%lf\n",a,b,cheng(a,b));
break;
case 4:
if(0.0==b)
{
printf("除数不能为0。\n");
}
else
{
printf("%lf/%lf=%lf\n",a,b,chu(a,b));
}
break;
case 5:
printf("|%lf|=%lf\n",a,juei(a));
break;
case 6:
printf("%lf的%lf方=%lf\n",a,b,chengfang(a,b));
break;
case 7:
printf("sin(%lf)=%lf\n",a,sinx(a));
break;
default:
printf("无法处理的命令。\n");
break;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}

B. 用c语言编写计算器

#include"stdio.h"
/*预处理命令*/

void main()
/*主函数*/

{

double a,b;
/*双精度实型变量说明*/

char c,d;
/*变量说明*/

do
/*循环体*/

{

printf("input a (-*/)b\n");
/*输入提示*/

scanf("%lf%c%lf",&a,&c,&b);
/*输入算术表达式*/

if(c==' ')
/*判断 */

printf("=%0.2f",a b);
/*输出a b的值*/

else if(c=='-')
/*判断-*/

printf("=%0.2f",a-b);
/*输出a-b的值*/

else if(c=='*')
/*判断**/

printf("=%0.2f",a*b);
/*输出a*b的值*/

else if(c=='/')
/*判断/*/

printf("=%0.3f",a/b);
/*输出a/b*/

else
/*不满足以上条件*/

printf("error");
/*输出错误*/

printf("\n\ninput\n");
/*输入\n*/

scanf("%c",&d);
/*输入符号给d*/

}
/*循环体结束*/

while(d=='\n');
/*循环条件语句*/

}

C. C语言乘方运算

C语言的乘方运算可以利用库函数pow。

pow函数原型:double pow( double x, double y );

头文件:math.h/cmath(C++中)

功能:计算x的y次幂。

参考代码:

#include<stdio.h>
#include<math.h>
intmain()
{
inta=3,b=2;
doublet=pow(a,b);//计算3的平方并输出
printf("%.0lf ",t);
return0;
}
/*
输出:
9
*/

D. 如何编写一个可以计算任意两个实数的四则运算,求余,乘方的C语言计算器

/*

36/7

36 / 7 = 5.14286

12^2

12 ^ 2 = 144

6+9

6 + 9 = 15

8-7

8 - 7 = 1

q

Down

Press any key to continue

*/

#include<stdio.h>

intmain(){
intopna,opnb,i,res;
charop;
while(scanf("%d%c%d",&opna,&op,&opnb)==3){
switch(op){
case'+':printf("%d+%d=%d ",opna,opnb,opna+opnb);break;
case'-':printf("%d-%d=%d ",opna,opnb,opna-opnb);break;
case'*':printf("%d*%d=%d ",opna,opnb,opna*opnb);break;
case'/':
if(opnb)printf("%d/%d=%g ",opna,opnb,1.0*opna/opnb);
elseprintf("致命错误,除数为0。 ");
break;
case'%':
if(opnb)printf("%d%%%d=%d ",opna,opnb,opna%opnb);
elseprintf("致命错误,除数为0。 ");
break;
case'^':
if(opna==0)printf("致命错误,底数为0。 ");
else{
res=1;
for(i=0;i<opnb;++i)res*=opna;
printf("%d^%d=%d ",opna,opnb,res);
}
break;
default:printf("不能识别的运算符:%c ",op);break;
}
}
puts("Down");
return0;
}

E. c语言中乘方要怎么写

设求x的y次方,且y为int型,如果你是想通过调用库函数实现,则可如下调用
#include "math.h"
double a = pow(x, y);
若你想自己设计一个函数来求乘方,则可如下实现
double pow(double x, int y) {
int i;
double proct = 1.0;
for(i = y; i > 0; i--)
proct *= x;
return proct;
}

F. C语言简单计算器,支持加减乘除乘方运算,每步要有注释,求助C语言高手解决,谢谢!

//注意,没有考虑*/和+-的优先级。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator3 extends JFrame implements ActionListener {
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字
// 构造者

public Calculator3() {
setTitle("Calculator");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i < 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}

// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if (btn == clear) {
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
} else if (btn == equal) {
operate('=');
equaled = true;
} else if (btn == plus) {
operate('+');
equaled = false;
} else if (btn == minus) {
operate('-');
equaled = false;
} else if (btn == multi) {
operate('*');
equaled = false;
} else if (btn == div) {
operate('/');
equaled = false;
} else if (btn == changePN) {
operate('p');
operate('=');
equaled = true;
} else if (btn == sqrt) {
operate('s');
operate('=');
equaled = true;
} else {
if (equaled)
storedNumber = 0;
for (int i = 0; i < 10; i++)
if (btn == numbers[i]) {
if (operation.getText().equals("0"))
operation.setText("" + i);
else if (!operated)
operation.setText(operation.getText() + i);
else {
operation.setText("" + i);
operated = false;
}
}
if (btn == dot && !dotExist) {
operation.setText(operation.getText() + ".");
dotExist = true;
}
}
}

// 进行运算的方法
private void operate(char operator) {
double currentNumber = Double.valueOf(operation.getText())
.doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' && equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}

// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component,
int row, int col, int width, int height) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton) component).addActionListener(this);
getContentPane().add(component);
}

// 主方法初始化并显示窗口
public static void main(String[] args) {
Calculator3 calc = new Calculator3();
calc.setSize(290, 400);
calc.setVisible(true);
}
}

G. 用c语言做一个加减乘除还有乘方的计算器,做出来界面要好看好看好看。一定要c语言c++。

给你个程序:刚写的,我也是初学者,只可以计算小数,括号和四种基本运算,单不能乘方,我用的是VC++6.0:代码如下:
#include "stdio.h"
#include "string.h"
#include "math.h"

const int N=30;//定义数组长度,可以修改更大;计算能力更强
char unnum[N];//模拟栈符号数组,用来存放运算符
int i=-1,len;
int numarrow=0;//模拟栈数字数组指针,用来控制模拟栈数字数组数的存取位置
int chararrow=0;//模拟栈符号数组指针,用来控制模拟栈符号数组运算符的存取位置
char ch[N],*p=ch;//原算式
double num[N];////模拟栈符号数组,用来存放运算符

void count(char *p);//从左往右读取元算式元素
void resultCount();//对模拟栈数组进行相应的计算
void choose(char *p);//判断字符的类型
void judge(char c);//判断'+','-'运算符,并进行相应的计算
void input_num(double n);//将数字存入模拟栈数字数组
void input_char(char c);//将运算符存入模拟栈符号数组
void nummul(char c);//判断运算符'*',‘/’并进行相应的计算
void chtoin();//读取字符串中当前的double数字;可为小数
/*主函数*/
int main()
{
//freopen("四则运算.in","r",stdin);
gets(ch);//输入原算式
count(p);
if(numarrow!=0) resultCount();//如果模拟栈数字数组指针不指向0;对模拟栈数字数组进行清算;
printf("%s=%.1lf\n",ch,num[0]);//输出结果
return 0;
}
void count(char *p)
{ i++;
len=strlen(p);
for(i;i<len;i++)
{ if(*(p+i)==')')
{ resultCount();//括号结束,对括号内的算式进行计算
return;
}
else choose(p+i);
}
}
void resultCount()
{ numarrow-=2;
chararrow--;
while(unnum[chararrow]!='(' && chararrow>=0)
{ judge(unnum[chararrow]);
chararrow--;
}
numarrow++;
}
void choose(char *p)
{ char a;
if(*p<='9' && *p>='0') chtoin();
if(*p=='+' || *p=='-') input_char(*p);
if(*p=='*' || *p=='/')
{ if(*(p+1) != '(')
{ i++;
chtoin();
i++;
nummul(*p);
}
if(*(p+1)=='(')
{ a=*p;
input_char(*(p+1));//将括号存入模拟栈符号数组,作为括号算式结束标志
i++;
count(ch);//运算符‘*’,‘/’后遇到括号时递归运算并计算括号内的结果存入模拟栈数字数组
numarrow+=1;
nummul(a); //将当前值与括号内的值相乘并存入模拟栈数字数组
}
}
if(*p=='(')
{ a=*p;
input_char(*p);//将括号存入模拟栈符号数组,作为括号算式结束标志
count(ch); //‘+’‘-’后或开头遇到括号递归计算
numarrow++;
}
}
void judge(char c)
{ switch(c)
{ case '+' : num[numarrow]+=num[numarrow+1]; break;//选择相应的计算
case '-' : num[numarrow]-=num[numarrow+1]; break;
default : break;
}
num[numarrow+1]=0; //归零,也可不写
numarrow--;
}
void input_num(double n)//对模拟栈数字数组赋值
{ num[numarrow++]=n;
}
void input_char(char c)//模拟栈符号数组赋值
{ unnum[chararrow++]=c;
}
void nummul(char c)//判断并进行相应的计算
{ numarrow-=2;
if(c=='*') num[numarrow]*=num[numarrow+1];
if(c== '/') num[numarrow]/=num[numarrow+1];
num[numarrow+1]=0;
numarrow++;
}
void chtoin()//读取但前数字,double类型
{ char a[N];
int j=0;
while((ch[i] <='9' && ch[i] >='0' ) || ch[i]=='.')
{ a[j++]=ch[i];
i++;
}
i--;
a[j]='\0';
input_num(atof(a));
}

H. c语言乘方函数

在C语言的头文件 math.h中定义了pow(x,y),返回结果是x的y次方。其中,x、y及函数值都是double型;具体使用时要先添加#include<math.h>。

在C++以及其他高级编程语言中都定义了此操作函数。C++中,乘方函数被定义在了头文cmath头文件下。具体使用时,需先引用头文件#include <cmath>。

对于64位长整型数据进行乘方计算,pow函数已无法满足其精度需要,这里需要通过长整型数的四则运算来实现。

乘方函数名称:pow(double,double), 具体参数中至少一方为float、double、long double类型。如计算5³;时, 直接使用 pow(5,3);返回结果即记为125。

I. 用c语言设计一个简单的加减乘除计算器 具体需要这样做

1、打开visual C++ 6.0-文件-新建-文件-C++ Source File。

2、输入预处理命令和主函数:#include /*函数头:输入输出头文件*/,void main()/*空类型:主函数*/。

3、定义变量:int a,b,d; /*定义变量的数据类型为整型*/,char c;/*定义变量的数据类型为字符型*/。

4、输入四则运算式:printf(输入如“3*4”或“5+2”的四则运算式:);/*输出文字提示*/scanf(%d%c%d,&a,&c,&b);/*输入四则运算式*/。

5、判断运算符号:switch(c) /*判断运算符号*/{case'+':d=a+b;break;/*进行加法6、运算*/case'-':d=a-b;break;/*进行减法运算*/case'*':d=a*b;break;/*进行乘法运算*/case'/':d=a/b;break; /*进行除法运算*/}。

7、输出结果:printf(%d%c%d=%d\n,a,c,b,d);/*输出结果*/。

J. 怎样用编辑C语言计算乘方

#include<stdio.h>
intChengFang(intx,intn)
{
inttmp;
if(n==1)
returnx;
if(n==0)
return1;
if(n%2==0){
tmp=ChengFang(x,n/2);
returntmp*tmp;
}
tmp=ChengFang(x,(n-1)/2);
returntmp*tmp*x;
}
intmain()
{
intx,y;
x=ChengFang(3,4);
y=ChengFang(2,5);
printf("3^4=%d2^5=%d ",x,y);
}