#include?stdio.h
專業(yè)領(lǐng)域包括成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、商城網(wǎng)站建設(shè)、微信營銷、系統(tǒng)平臺(tái)開發(fā), 與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開發(fā)公司不同,創(chuàng)新互聯(lián)公司的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。
int?jisuan(int?a,int?b,char?fu)
{
if(fu=='+')?return?a+b;
if(fu=='-')?return?a-b;
if(fu=='*')?return?a*b;
if(fu=='/')?return?a/b;
}
int?fun(char?*ss,int?n)
{
int?i,flag=0;
if(n==1)?return?ss[0]-'0';
for(i=0;in;i++)
{
if((ss[i]=='+')||(ss[i]=='-'))//掃描加減號
{
flag?=?1;
return?jisuan(fun(ss,i),fun(ss+i+1,n-i-1),ss[i]);
}
}
if(flag==0)//如果算數(shù)中沒有+-
{
for(i=0;in;i++)
if((ss[i]=='*')||(ss[i]=='/'))
{
return?jisuan(fun(ss,i),fun(ss+i+1,n-i-1),ss[i]);
}
}
}
void?main(void)
{
char?s[50];
int?n;
printf("輸入算數(shù):");
scanf("%s",s);
n?=?strlen(s);
printf("=%d\r\n",fun(s,n));
}
用的遞歸,這樣省去很多麻煩
//注意,沒有考慮*/和+-的優(yōu)先級。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator3 extends JFrame implements ActionListener {
private boolean dotExist, operated, equaled; // 幫助運(yùn)算的布爾變量
private double storedNumber; // 目前的結(jié)果
private char lastOperator; // 表示上一運(yùn)算符
private JTextField operation; // 結(jié)果欄
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 運(yùn)算符
private JButton[] numbers; // 數(shù)字
// 構(gòu)造者
public Calculator3() {
setTitle("Calculator");
// 初始化變量
dotExist = false; // 表示當(dāng)前的數(shù)是否有小數(shù)點(diǎn)
operated = false; // 表示任意運(yùn)算符是否被按下
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);
}
// 對按鈕進(jìn)行反應(yīng)的方法
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;
}
}
}
// 進(jìn)行運(yùn)算的方法
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);
}
}
用C語言編寫一個(gè)簡單的可以進(jìn)行加減乘除運(yùn)算混合運(yùn)算的計(jì)算器的方法:
1、打開visual C++ 6.0-文件-新建-文件-C++ Source File;
2、輸入預(yù)處理命令和主函數(shù):
#includestdio.h /*函數(shù)頭:輸入輸出頭文件*/
void main()/*空類型:主函數(shù)*/
3、定義變量:
int a,b,d; /*定義變量的數(shù)據(jù)類型為整型*/
char c;/*定義變量的數(shù)據(jù)類型為字符型*/
4、輸入四則運(yùn)算式:
printf("輸入如“3*4”或“5+2”的四則運(yùn)算式:");/*輸出文字提示*/
scanf("%d%c%d",a,c,b);/*輸入四則運(yùn)算式*/
5、判斷運(yùn)算符號:
switch(c) /*判斷運(yùn)算符號*/
{
case'+':d=a+b;break;/*進(jìn)行加法運(yùn)算*/
case'-':d=a-b;break;/*進(jìn)行減法運(yùn)算*/
case'*':d=a*b;break;/*進(jìn)行乘法運(yùn)算*/
case'/':d=a/b;break; /*進(jìn)行除法運(yùn)算*/
}
6、輸出結(jié)果:
printf("%d%c%d=%d\n",a,c,b,d);/*輸出結(jié)果*/
完整的源代碼:
#includestdio.h /*函數(shù)頭:輸入輸出頭文件*/
void main()/*空類型:主函數(shù)*/
{
int a,b,d;/*定義變量的數(shù)據(jù)類型為整型*/
char c;/*定義變量的數(shù)據(jù)類型為字符型*/
printf("輸入如“3*4”或“5+2”的四則運(yùn)算式:");/*輸出文字提示*/
scanf("%d%c%d",a,c,b);/*輸入四則運(yùn)算式*/
switch(c)/*判斷運(yùn)算符號*/
{
case'+':d=a+b;break;/*進(jìn)行加法運(yùn)算*/
case'-':d=a-b;break;/*進(jìn)行減法運(yùn)算*/
case'*':d=a*b;break;/*進(jìn)行乘法運(yùn)算*/
case'/':d=a/b;break;/*進(jìn)行除法運(yùn)算*/
}
printf("%d%c%d=%d\n",a,c,b,d);/*輸出結(jié)果*/
}