當前位置:首頁 » 編程語言 » 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);
}