重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
#include stdio.h
专业领域包括成都网站制作、网站设计、商城网站建设、微信营销、系统平台开发, 与其他网站设计及系统开发公司不同,创新互联公司的整合解决方案结合了帮做网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,为客户提供全网互联网整合方案。
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)//如果算数中没有+-
{
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("输入算数:");
scanf("%s",s);
n = strlen(s);
printf("=%d\r\n",fun(s,n));
}
用的递归,这样省去很多麻烦
//注意,没有考虑*/和+-的优先级。
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);
}
}
用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器的方法:
1、打开visual C++ 6.0-文件-新建-文件-C++ Source File;
2、输入预处理命令和主函数:
#includestdio.h /*函数头:输入输出头文件*/
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;/*进行加法运算*/
case'-':d=a-b;break;/*进行减法运算*/
case'*':d=a*b;break;/*进行乘法运算*/
case'/':d=a/b;break; /*进行除法运算*/
}
6、输出结果:
printf("%d%c%d=%d\n",a,c,b,d);/*输出结果*/
完整的源代码:
#includestdio.h /*函数头:输入输出头文件*/
void main()/*空类型:主函数*/
{
int a,b,d;/*定义变量的数据类型为整型*/
char c;/*定义变量的数据类型为字符型*/
printf("输入如“3*4”或“5+2”的四则运算式:");/*输出文字提示*/
scanf("%d%c%d",a,c,b);/*输入四则运算式*/
switch(c)/*判断运算符号*/
{
case'+':d=a+b;break;/*进行加法运算*/
case'-':d=a-b;break;/*进行减法运算*/
case'*':d=a*b;break;/*进行乘法运算*/
case'/':d=a/b;break;/*进行除法运算*/
}
printf("%d%c%d=%d\n",a,c,b,d);/*输出结果*/
}