练习题---三角运算

Posted on 2007-10-16 14:09 久城 阅读(802) 评论(0)  编辑  收藏 所属分类: JavaTest
好久没有做练习题了。

编写一个小的计算程序,用来进行三角运算(
Sin Costan…),该程序通过交互接收用户输入,例如:
系统刚启动的时候处于提示状态:
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();
    }

    
}


 



欢迎来访!^.^!
本BLOG仅用于个人学习交流!
目的在于记录个人成长.
所有文字均属于个人理解.
如有错误,望多多指教!不胜感激!


只有注册用户登录后才能发表评论。


网站导航:
 

Copyright © 久城