练习题---三角运算

好久没有做练习题了。

编写一个小的计算程序,用来进行三角运算(
Sin Cos tan… ),该程序通过交互接收用户输入,例如:
系统刚启动的时候处于提示状态:
Function>
这时用户可以输入函数名称,输入sin 表示想进行sin 运算,此时再提醒用户输入角度:
Angel>
用户可以输入角度,
计算完毕后,以Result< 方式输出结果,并且重新回到Function> 的状态下。
在任何时候用户输入非法,则显示Error< ,在其后描述具体的错误原因。然后重新回到 错误输入前状态。
1 )语言不限
2 )支持很方便的扩展
(3)变量的命名和使用要符合学习的内容

代码如下:
/** */ /**---------------------------------------------
 *   Class Name   : YW2_Test01.java
 *   Purpose      : 编写一个小的计算程序,用来进行三角运算(Sin, Cos,tan…),该程序通过交互接收用户输入
 *
 *   
@author realsmy
 *   
@since 2007/10/16
 *
 *   Copyright realsmy. All rights reserved.
 *---------------------------------------------
 
*/

package  com.neusoft.test;

import  java.io.BufferedReader;
import  java.io.IOException;
import  java.io.InputStreamReader;

//  三角函数名的枚举类型
enum  FuncName {
    SIN,
    COS,
    TAN
}


public   class  YW5_Test01 {
    
    
// 三角函数名
    private FuncName function;
    
    
// 表示角度
    private double angel;
    
    
// 圆周率常量
    private static double PAI = 3.14159265;

    
/** *//**
     * ---------------------------------------------
     * Method Name : YW5_Test01 
     * Exposition : 构造函数,执行运算过程
     * ---------------------------------------------
     
*/

    
public YW5_Test01(){
        
// 是指三角函数名
        setFunction();
        
// 设置角度
        setAngel();
        
// 计算出结果
        getResult();
    }

    
    
/** *//**
     * ---------------------------------------------
     * Method Name : setFuncName 
     * Exposition : 设置三角函数名字
     * ---------------------------------------------
     
*/

    
private void setFuncName(FuncName func) {
        
this.function = func;
    }

    
/** *//**
     * ---------------------------------------------
     * Method Name : setFunction 
     * Exposition : 设置三角函数名字
     * ---------------------------------------------
     
*/

    
private void setFunction(){
        System.out.print(
"Function> ");     
        
if ( !checkFunction(getFunction())) {
            System.out.println(
"error: worng function name, please input again:");
            setFunction();
        }

    }

    
/** *//**
     * ---------------------------------------------
     * Method Name : getFunction 
     * Exposition : 取得三角函数名字
     * ---------------------------------------------
     
*/

    
private String getFunction(){
        String func 
= null;
        
try {
            BufferedReader in 
= new BufferedReader(new InputStreamReader(System.in));
            func 
= in.readLine().toUpperCase();
        }
 catch (IOException e) {
        }

        
return func;
    }

    
/** *//**
     * ---------------------------------------------
     * Method Name : checkFunction 
     * Exposition : 检查三角函数名字
     * ---------------------------------------------
     
*/

    
private Boolean checkFunction(String func){
        
for ( FuncName funcName : FuncName.values()) {
            
if( funcName.toString().equals(func)) {
                setFuncName(funcName);
                
return true;
            }

        }

        
return false;
    }

    
/** *//**
     * ---------------------------------------------
     * Method Name : setAngel 
     * Exposition : 设置角度
     * ---------------------------------------------
     
*/

    
private void setAngel(){
        System.out.print(
"Angel> ");     
        getAngel();
    }

    
/** *//**
     * ---------------------------------------------
     * Method Name : getAngel 
     * Exposition : 取得角度
     * ---------------------------------------------
     
*/

    
private double getAngel(){
        
try {
            BufferedReader in 
= new BufferedReader(new InputStreamReader(System.in));
            angel 
= Double.parseDouble(in.readLine());
        }
 catch(NumberFormatException ne){
            System.out.println(
"The input is not a number, please input again:");
            setAngel();
        }
 catch (IOException e) {
        }

        
return angel;
    }

    
/** *//**
     * ---------------------------------------------
     * Method Name : getResult 
     * Exposition : 取得结果
     * ---------------------------------------------
     
*/

    
private void getResult(){
        
double result = 0;
        
switch (function){
        
case SIN:
            result 
= Math.sin(angel*PAI/180);
            
break;
        
case COS:
            result 
= Math.cos(angel*PAI/180);
            
break;
        
case TAN:
            result 
= Math.tan(angel*PAI/180);
            
break;
        }

        System.out.println(
"Result< "+ function + " " + angel + " = " + result);
    }

    
    
/** *//**
     * ---------------------------------------------
     * Method Name : main 
     * Exposition : 测试用主函数
     * ---------------------------------------------
     
*/

    
public static void main(String[] args){
        
new YW5_Test01();
    }

   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值