| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 816 人关注过本帖
标题:本人写的一元多项式加法(里面仍有写不足,希望大家多多支持)
取消只看楼主 加入收藏
wu1011
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2007-11-18
结帖率:80%
收藏
 问题点数:0 回复次数:0 
本人写的一元多项式加法(里面仍有写不足,希望大家多多支持)
import javax.swing.*;
import *;
class Exepriment{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  Item item1[];
  Item item2[];
  Item item3[];
  int item_len1,item_len2,n;
  Exepriment()throws IOException{
      start();     
  }
  void start()throws IOException{
     String temp = "1.建立一元多项式(1)."+"\n"+
                   "2.建立一元多项式(2)."+"\n"+
                   "3.多项式相加."+"\n"+
                   "4.退出.";
     System.out.println(temp);
     temp = "请选择你要进行的操作:";
     System.out.print(temp);
     String input = in.readLine();
     char c_input = input.charAt(0);
     switch(c_input){
         case '1':
            Create_Item1();
            out_Item1();
              if(judge())
               start();
           else
              System.exit(-1);
           break;
        
        case '2':
            Create_Item2();
            out_Item2();
              if(judge())
               start();
           else
              System.exit(-1);
           break;
        
        case '3':
              add_Item();
              out_Item3();
              if(judge())
               start();
           else
              System.exit(-1);
      
            break;
         
         case '4':
            //弹出一个询问对话框
            int result = JOptionPane.showConfirmDialog(null,"你是否想离开?","系统提示",JOptionPane.YES_NO_OPTION);
            if(result == 1)
               start();
            else if(result == 0)
               System.exit(-1);
            break;
         
         default:
           //提示要求做出正确的选择
           JOptionPane.showMessageDialog(null,"输入有误,你正确选择你所要进行的操作!","系统提示",JOptionPane.ERROR_MESSAGE);   
           start();
       }   
   }  
  
   //判断是否还要进行操作
     public boolean judge()throws IOException{
        String temp = "你是否还要进行操作?'y' or 'n':";
        System.out.print(temp);
        String input = in.readLine();
        if(input.equals("y"))
           return true;
        else
           return false;      
    }

   //拆分字符串
   private String[] split_str(String str){
      String temp[] = str.split(",");
      return temp;
   }
   //创建一元多项式(1)
   private void Create_Item1()throws IOException{
      String Num[];
      String temp = "请输入你要创建的一元多项式的项数:";
      System.out.print(temp);
      String input = in.readLine();
      //input = Correct(input);
      item_len1 = Integer.parseInt(input);
      item1 = new Item[item_len1];
      for(int i=0;i<item_len1;i++){
          temp = "请输入第"+(i+1)+"项未知数(x)的系数和指数(中间用逗号隔开):";
          System.out.print(temp);
          input = in.readLine();
          Num = split_str(input);
          int input_Coe_num = Integer.parseInt(Num[0]);
          int input_Index_num = Integer.parseInt(Num[1]);
          item1[i] = new Item();
          item1[i].Coefficient = input_Coe_num;
          item1[i].Index_number = input_Index_num;
      }
   }
   
  //输出医院多项式(1)
   private void out_Item1(){
      for(int i=0;i<item_len1;i++){
         for(int j=0;j<item_len1-i-1;j++){
             if(item1[j].Index_number>item1[j+1].Index_number){
                Item temp = new Item();
                temp = item1[j];
                item1[j] = item1[j+1];
                item1[j+1] = temp;
             }
         }
      }
     System.out.println("你所创的一元多项式(1)为:");
     String str="";
     
     for(int i=0;i<item_len1;i++){
        if(item1[i].Index_number == 0){
           if(item1[i].Coefficient > 0 && i !=0)
              str = str + " + " + item1[i].Coefficient;
           else
              str = str + item1[i].Coefficient;
        }
        else{
             if(item1[i].Coefficient > 0 && i !=0)
              str = str + " + " + item1[i].Coefficient + "x^" + item1[i].Index_number;
           else
              str = str + " " + item1[i].Coefficient + "x^" + item1[i].Index_number;
        }
      
     }
      System.out.println(str);
   }
    //创建一元多项式(2)
    private void Create_Item2()throws IOException{
       String Num[];
       String temp = "请输入你要创建的一元多项式的项数:";
       System.out.print(temp);
       String input = in.readLine();
       item_len2 = Integer.parseInt(input);
       item2 = new Item[item_len2];
       for(int i=0;i<item_len2;i++){
          temp = "请输入第"+(i+1)+"项未知数(x)的系数和指数(中间用逗号隔开):";
          System.out.print(temp);
          input = in.readLine();
          Num = split_str(input);
          int input_Coe_num = Integer.parseInt(Num[0]);
          int input_Index_num = Integer.parseInt(Num[1]);
          item2[i] = new Item();
          item2[i].Coefficient = input_Coe_num;
          item2[i].Index_number = input_Index_num;
       }
    }
   
    //输出医院多项式(2)
    private void out_Item2(){
      for(int i=0;i<item_len2;i++){
         for(int j=0;j<item_len2-i-1;j++){
             if(item2[j].Index_number>item2[j+1].Index_number){
                Item temp = new Item();
                temp = item2[j];
                item2[j] = item2[j+1];
                item2[j+1] = temp;
             }
         }
      }
     System.out.println("你所创的一元多项式(2)为:");
     String str="";
     
     for(int i=0;i<item_len2;i++){
        if(item2[i].Index_number == 0){
           if(item2[i].Coefficient > 0 && i !=0)
              str = str + " + " + item2[i].Coefficient;
           else
              str = str + item2[i].Coefficient;
        }
        else{
             if(item2[i].Coefficient > 0 && i !=0)
              str = str + " + " + item2[i].Coefficient + "x^" + item2[i].Index_number;
           else
              str = str + " " + item2[i].Coefficient + "x^" + item2[i].Index_number;
        }
      
     }
      System.out.println(str);
   }
    //两个多项式相加
    private void add_Item()throws IOException{
       int item_len3=0;
       //获得和储存多项式(2)与多项式(1)指数相同的个数和项数
       int store_j[]= new int[item_len2];
       int count=0;
       for(int i=0;i<item_len1;i++){
          for(int j=0;j<item_len2;j++){
              if(item1[i].Index_number == item2[j].Index_number){
                   store_j[count] = j;
                   count++;
              }
          }
       }
       //相加后的长度
       item_len3 = item_len1+item_len2-count;
       item3 = new Item[item_len3];
      
       //一多项式(1)为基准
       inner:
       for(int i=0;i<item_len1;i++){
          for(int j=0;j<item_len2;j++){
              if(item1[i].Index_number == item2[j].Index_number){
                  item3[i] = new Item();
                  item3[i].Coefficient = item1[i].Coefficient + item2[j].Coefficient;
                  item3[i].Index_number = item1[i].Index_number;
                  continue inner;
              }
          }
          item3[i] = new Item();
          item3[i].Coefficient = item1[i].Coefficient;
          item3[i].Index_number = item1[i].Index_number;
       }
      
       //添加上多项式(2)剩余的项
       n = item_len1;
       if(count != 0){
           outer:
           for(int i=0;i<item_len2;i++){
              for(int j=0;j<count;j++){
                 if(i==store_j[j])
                    continue outer;
              }
              item3[n] = new Item();
              item3[n].Coefficient = item2[i].Coefficient;
              item3[n].Index_number = item2[i].Index_number;
              n++;
           }
       }
   }
   
   //输出一元多项式相加后(冒泡法)
   private void out_Item3(){
      for(int i=0;i<n;i++){
         for(int j=0;j<n-i-1;j++){
             if(item3[j].Index_number>item3[j+1].Index_number){
                Item temp = new Item();
                temp = item3[j];
                item3[j] = item3[j+1];
                item3[j+1] = temp;
             }
         }
      }
     System.out.println("相加后的结果为:");
     String str="";
     for(int i=0;i<n;i++){
        if(item3[i].Index_number == 0){
           if(item3[i].Coefficient > 0 && i !=0)
              str = str + " + " + item3[i].Coefficient;
           else
              str = str + item3[i].Coefficient;
        }
        else{
             if(item3[i].Coefficient > 0 && i !=0)
              str = str + " + " + item3[i].Coefficient + "x^" + item3[i].Index_number;
           else
              str = str + " " + item3[i].Coefficient + "x^" + item3[i].Index_number;
        }
      
     }
      System.out.println(str);
   }
  /*
  //对输入的多项式(1)和多项式(2)的长度进行错误纠正
   private String Correct(String input)throws IOException{
       String temp = input;
       char c_input[] = temp.toCharArray();
       for(int i=0;i<c_input.length;i++){
           if(!Character.isDigit(c_input[i])){
              System.out.print("请输入整数!");
              Correct_len();
           }
       }
       return temp;
     
   }
  private void Correct_len()throws IOException{
     String temp = "请输入你要创建的一元多项式的项数:";
     System.out.print(temp);
     String input = in.readLine();
     Correct(input);
  }
  */
   //入口方法
   public static void main(String args[])throws IOException{
      new Exepriment();
   }
  //未知数的项类
  class Item{
     //系数
     int Coefficient;
     //指数
     int Index_number;
   
  }
}
搜索更多相关主题的帖子: 多项式 加法 
2007-12-07 21:12
快速回复:本人写的一元多项式加法(里面仍有写不足,希望大家多多支持)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044974 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved