一个简单的计算器(图)



#include "stdAfx.h"
#include "ctype.h"
#include "stdlib.h"
void add();
void sub();
void mul();
void div();
int main(void)
{
	int ch;
	printf("Please enter your calculated\nserial number to the press CTRL + Z exit\n");
	printf("***************************\n");
	printf("a:add                 b:sub\n");
	printf("c:mul                 d:div\n");
	printf("***************************\n");
	while((ch=getchar())!=EOF)
	{
		system("cls");
		switch(ch)
		{
		case 'a':add();
			printf("NEXT?ELSE(CTRL+Z):\n");
			getchar();
			break;
		case 'b':sub();
			printf("NEXT?ELSE(CTRL+Z):\n");
			getchar();
			break;
		case 'c':mul();
			printf("NEXT? ELSE (CTRL+Z):\n");
			getchar();
			break;
		case 'd':div();
			printf("NEXT? ELSE (CTRL+Z):\n");
			getchar();
			break;
		default:break;
		}
	}
	printf("Done!\n");
	system("pause");
	return 0;
}
void add()
{
	float a,b;
	printf("You have into the addition\n");
	printf("please enter the first number:\n");
	scanf("%f",&a);
	printf("Please enter the second number:\n");
	scanf("%f",&b);
	printf("%f + %f = %0.2f\n",a,b,a+b);
}
void sub()
{
	float a,b;
	printf("You have into the subtraction\n");
	printf("please enter the first number:\n");
	scanf("%f",&a);
	printf("Please enter the second number:\n");
	scanf("%f",&b);
	printf("%f - %f = %0.2f\n",a,b,a-b);
}
void mul()
{
	float a,b;
	printf("You have into the multiplication\n");
	printf("please enter the first number:\n");
	scanf("%f",&a);
	printf("Please enter the second number:\n");
	scanf("%f",&b);
	printf("%f * %f = %0.2f\n",a,b,a*b);	
}
void div()
{
	float a,b;
	printf("You have into the division\n");
	printf("please enter the first number:\n");
	scanf("%f",&a);
	printf("Please enter the second number:\n");
A:	scanf("%f",&b);
	if(b==0)
	{
		printf("division second number can't input '0'\nagain:\n");
		goto A;
	}
	else
		printf("%f / %f = %0.2f\n",a,b,a/b);	
}


由于我是AI语言模型,无法直接提供形界面,但是可以提供一个简单的Java代码示例,来展示如何设计一个简易计算器的界面。 ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { private JTextField textField; private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonPlus, buttonMinus, buttonMultiply, buttonDivide, buttonClear, buttonEquals; private JPanel panel; // 构造方法 public Calculator() { super("Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 300); // 创建文本框 textField = new JTextField(20); textField.setEditable(false); // 禁止手动输入 // 创建按钮 button0 = new JButton("0"); button1 = new JButton("1"); button2 = new JButton("2"); button3 = new JButton("3"); button4 = new JButton("4"); button5 = new JButton("5"); button6 = new JButton("6"); button7 = new JButton("7"); button8 = new JButton("8"); button9 = new JButton("9"); buttonPlus = new JButton("+"); buttonMinus = new JButton("-"); buttonMultiply = new JButton("*"); buttonDivide = new JButton("/"); buttonClear = new JButton("C"); buttonEquals = new JButton("="); // 设置监听器 button0.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); button5.addActionListener(this); button6.addActionListener(this); button7.addActionListener(this); button8.addActionListener(this); button9.addActionListener(this); buttonPlus.addActionListener(this); buttonMinus.addActionListener(this); buttonMultiply.addActionListener(this); buttonDivide.addActionListener(this); buttonClear.addActionListener(this); buttonEquals.addActionListener(this); // 创建面板 panel = new JPanel(new GridLayout(4, 4)); panel.add(button7); panel.add(button8); panel.add(button9); panel.add(buttonPlus); panel.add(button4); panel.add(button5); panel.add(button6); panel.add(buttonMinus); panel.add(button1); panel.add(button2); panel.add(button3); panel.add(buttonMultiply); panel.add(buttonClear); panel.add(button0); panel.add(buttonEquals); panel.add(buttonDivide); // 添加组件 add(textField, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); setVisible(true); } // 监听器方法 public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); String text = textField.getText(); if (command.equals("C")) { textField.setText(""); } else if (command.equals("=")) { try { int result = evaluate(text); textField.setText(String.valueOf(result)); } catch (Exception ex) { textField.setText("Error"); } } else { textField.setText(text + command); } } // 计算表达式 private int evaluate(String text) throws Exception { String[] tokens = text.split(" "); if (tokens.length != 3) { throw new Exception(); } int operand1 = Integer.parseInt(tokens[0]); int operand2 = Integer.parseInt(tokens[2]); if (tokens[1].equals("+")) { return operand1 + operand2; } else if (tokens[1].equals("-")) { return operand1 - operand2; } else if (tokens[1].equals("*")) { return operand1 * operand2; } else if (tokens[1].equals("/")) { return operand1 / operand2; } else { throw new Exception(); } } public static void main(String[] args) { new Calculator(); } } ``` 这个Java代码示例实现了一个简单计算器界面,包含数字按钮、操作符按钮和清除、等号按钮。用户可以通过点击按钮来输入数字和操作符,最后按下等号按钮计算结果并显示在文本框中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值