⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fitinput.java

📁 用Java开发的实用数学建模程序 简单易懂 初学者可以用来学习java知识
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			}
			//统计应该使用哪些数据
			int comboBoxIndex=comboBox.getSelectedIndex();
			
			if(comboBoxIndex==0)
				JOptionPane.showMessageDialog(parentComponent,"请先选择数据处理类型","选择类型",JOptionPane.INFORMATION_MESSAGE);
			
			if(comboBoxIndex==1)
			//一元线性回归
			{
				n=1;//防止多项式回归时n值被更改
				yMatrix=new double[m];
				xMatrix=new double[m][n+1];//由于常数项的存在,xMatrix的第1列恒为1
				xData=new double[m];
				int index=0;
				
				if(m<n+1)
				{
					JOptionPane.showMessageDialog(parentComponent,"所选的数据的组数必须不小于"+(n+1),"数据不足",JOptionPane.WARNING_MESSAGE);
					return;
				}
				
				for(i=0;i<textFieldCount;i++)
				{
					if(selected[i])
					{
						for(j=0;j<n+1;j++)
						{
							try
							{
								if(j==0)
								{
									yMatrix[index]=Double.parseDouble(((JTextField[])textFieldVector.get(i))[j].getText());
									xMatrix[index][0]=1;
								}
								else
								{
									xMatrix[index][j]=Double.parseDouble(((JTextField[])textFieldVector.get(i))[j].getText());
									xData[index]=xMatrix[index][j];
								}
							}
							catch(NumberFormatException nException)
							{
								if(j==0)
									tmp="第"+(i+1)+"组数据的y值不是有效的数字!\n如果不需要这组数据,请把复选框中的勾去掉。";
								else
									tmp="第"+(i+1)+"组数据的x的值不是有效的数字!\n如果不需要这组数据,请把复选框中的勾去掉。";
								JOptionPane.showMessageDialog(parentComponent,tmp,"输入错误",JOptionPane.WARNING_MESSAGE);
								return;
							}
						}
						index++;//index和i是很不相同的
					}
				}
				//完成值的传递
				
				try
				{
					Fit linearFit=new Fit(xMatrix,yMatrix);
					double[] result=linearFit.multiFit();
					StringBuffer buffer=new StringBuffer("相关系数为"+(float)result[result.length-1]+"\n");
					buffer.append("回归方程为:\n");
					buffer.append("y = "+(float)result[0]);
					if(result[1]>0)
						buffer.append("+"+(float)result[1]+" * x");
					else if(result[1]<0)
						buffer.append((float)result[1]+" * x");
					//处理正负号
					
					ViewPlotFrame view=new ViewPlotFrame(xData,yMatrix,new LinearFunction(result),buffer.toString());
					view.showPlot();
					
				}//把double在显示时转换为float显示效果会好一点
				catch(NullPointerException nullE)
				{
					JOptionPane.showMessageDialog(parentComponent,
							"数据有误。\n适合数据的回归方程有无穷多个。\n请重新检查数据!","数据错误",JOptionPane.WARNING_MESSAGE);
				}
				//显示求解结果
			}
			
			else if(comboBoxIndex==2)
			//多项式回归
			{
				pass=false;//用于标记是否通过合法性检验
				secondTime=false;
				
				while(!pass)
				{
					if(secondTime==true)
						tmp=JOptionPane.showInputDialog(parentComponent,
								"请输入一个不少于2的整数!\n请重新输入:","输入多项式次数",JOptionPane.WARNING_MESSAGE);
					else
						tmp=JOptionPane.showInputDialog(parentComponent,
								"请输入回归多项式的次数:","输入多项式次数",JOptionPane.QUESTION_MESSAGE);
					try
					{
						if(tmp==null)
							return;
						n=-1;
						n=Integer.parseInt(tmp);
					}
					catch(NumberFormatException nume)
					{
						secondTime=true;
					}
					if(n>=2)
						pass=true;
					else
						secondTime=true;
				}
				//输入多项式次数
				
				yMatrix=new double[m];
				xMatrix=new double[m][n+1];//由于常数项的存在,xMatrix的第1列恒为1
				xData=new double[m];
				int index=0;
				
				if(m<n+1)
				{
					JOptionPane.showMessageDialog(parentComponent,"所选的数据的组数必须不小于"+(n+1),"数据不足",JOptionPane.WARNING_MESSAGE);
					return;
				}
				
				for(i=0;i<textFieldCount;i++)
				{
					if(selected[i])
					{
						for(j=0;j<n+1;j++)
						{
							try
							{
								if(j==0)
								{
									yMatrix[index]=Double.parseDouble(((JTextField[])textFieldVector.get(i))[j].getText());
									xMatrix[index][0]=1;
								}
								else
								{
									xMatrix[index][j]=Math.pow(Double.parseDouble(((JTextField[])textFieldVector.get(i))[1].getText()),j);
									xData[index]=xMatrix[index][1];
								}
							}
							catch(NumberFormatException nException)
							{
								if(j==0)
									tmp="第"+(i+1)+"组数据的y值不是有效的数字!\n如果不需要这组数据,请把复选框中的勾去掉。";
								else
									tmp="第"+(i+1)+"组数据的x的值不是有效的数字!\n如果不需要这组数据,请把复选框中的勾去掉。";
								JOptionPane.showMessageDialog(parentComponent,tmp,"输入错误",JOptionPane.WARNING_MESSAGE);
								return;
							}
						}
						index++;//index和i是很不相同的
					}
				}
				//完成值的传递
			
				try
				{
					Fit polyFit=new Fit(xMatrix,yMatrix);
					double[] result=polyFit.multiFit();
					StringBuffer buffer=new StringBuffer("相关系数为"+(float)result[result.length-1]+"\n");
					buffer.append("回归方程为:\n");
					buffer.append("y = "+(float)result[0]);
					if(result[1]>=0)
						buffer.append("+"+(float)result[1]+" * x");
					else if(result[1]<0)
						buffer.append((float)result[1]+" * x");
					//处理正负号
					for(i=2;i<result.length-1;i++)
					{
						if(result[i]>=0)
							buffer.append("+"+(float)result[i]+" * x^"+i);
						else if(result[i]<0)
							buffer.append((float)result[i]+" * x^"+i);
						//处理正负号
						if(i%3==0)
							buffer.append("\n");
					}
					
					double[] polynomialCoefficients=new double[result.length-1];
					for(i=0;i<result.length-1;i++)
						polynomialCoefficients[i]=result[i];
					ViewPlotFrame view=new ViewPlotFrame(xData,yMatrix,new PolynomialFunction(polynomialCoefficients),buffer.toString());
					view.showPlot();
				}//把double在显示时转换为float显示效果会好一点
				catch(NullPointerException nullE)
				{
					JOptionPane.showMessageDialog(parentComponent,
							"数据有误。\n适合数据的回归方程有无穷多个。\n请重新检查数据!","数据错误",JOptionPane.WARNING_MESSAGE);
				}
				//显示求解结果
			}
		}
		
		if(e.getSource()==oneHelpButton)
		{
			ImageIcon helpMessage=new ImageIcon("resource\\OneFitHelp.GIF");
			JOptionPane.showMessageDialog(parentComponent,helpMessage,"输入帮助",JOptionPane.INFORMATION_MESSAGE,new ImageIcon(Library.helpIcon_Scaled));
		}
		
		if(e.getSource()==saveButton)
		//保存数据
		{
			JFileChooser saveFile=new JFileChooser();
			saveFile.setFont(Library.font);
			saveFile.setFileFilter(new DataFileFilter("txt","带Tab分隔符的数据文件(*.txt)"));
			int choice=saveFile.showSaveDialog(parentComponent);
			if(choice==JFileChooser.ERROR_OPTION)
			{
				JOptionPane.showMessageDialog(parentComponent,"文件读取错误!","错误",JOptionPane.ERROR_MESSAGE);
				return;
			}
			else if(choice==JFileChooser.APPROVE_OPTION)
			//保存文件
			{
				File file=saveFile.getSelectedFile();
				String fileName=file.getAbsolutePath();
				int lastIndex=fileName.toLowerCase().lastIndexOf(".txt",fileName.length()-4);
				if(file.exists() && lastIndex!=-1)
				{
					int overWrite=JOptionPane.showConfirmDialog(parentComponent,"文件已经存在,是否覆盖?",
							"覆盖文件",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
					if(overWrite==JOptionPane.NO_OPTION)
						return;
				}
				try
				{
					if(lastIndex==-1)
						fileName=fileName+".txt";
					//保证以.txt为后缀
					
					BufferedWriter fileWriter=new BufferedWriter(new FileWriter(fileName,false));
					textFieldCount=textFieldVector.size();
					for(i=0;i<textFieldCount;i++)
					{
						tempTextField=(JTextField[])textFieldVector.get(i);
						fileWriter.write(tempTextField[0].getText()+"\t"+tempTextField[1].getText());
						fileWriter.newLine();
					}
					fileWriter.close();
				}
				catch(IOException ioE)
				{
					JOptionPane.showMessageDialog(parentComponent,"文件读写错误!\n请重试!",
							"读写错误",JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		
		if(e.getSource()==openButton)
		//导入数据
		{
			JFileChooser openFile=new JFileChooser();
			openFile.setFont(Library.font);
			openFile.setFileFilter(new DataFileFilter("txt","带Tab分隔符的数据文件 (*.txt)"));
			int choice=openFile.showOpenDialog(parentComponent);
			if(choice==JFileChooser.ERROR_OPTION)
			{
				JOptionPane.showMessageDialog(parentComponent,"文件读取错误!","错误",JOptionPane.ERROR_MESSAGE);
				return;
			}
			else if(choice==JFileChooser.APPROVE_OPTION)
			//读取文件
			{
				File file=openFile.getSelectedFile();
				try
				{
					BufferedReader fileReader=new BufferedReader(new FileReader(file));
					String currentString;
					String[] subString=new String[2];
					int firstIndex,lastIndex;
					textFieldVector=new Vector();
					checkBoxVector=new Vector();
					textFieldCount=0;
					n=1;
					inputPanel.removeAll();
					multiInput=false;
					
					JLabel[] variableLabel=new JLabel[n+2];
					variableLabel[0]=new JLabel("y",JLabel.CENTER);
					variableLabel[0].setFont(Library.font);
					inputPanel.add(variableLabel[0]);
					variableLabel[n]=new JLabel("x",JLabel.CENTER);
					variableLabel[n].setFont(Library.font);
					inputPanel.add(variableLabel[n]);
					variableLabel[n+1]=new JLabel("使用数据",JLabel.CENTER);
					variableLabel[n+1].setFont(Library.font);
					inputPanel.add(variableLabel[n+1]);
					//往输入面板中添加标签
					
					while((currentString=fileReader.readLine())!=null)
					//读取文件当前行并作处理
					{
						firstIndex=currentString.indexOf("\t");
						lastIndex=currentString.lastIndexOf("\t");
						if(firstIndex==-1 || firstIndex!=lastIndex)
						//如果文件同一行中不包含制表符或者包含多于一个的制表符,则说明文件格式错误
							throw new InvalidFileFormatException();
						subString[0]=currentString.substring(0,firstIndex);
						subString[1]=currentString.substring(firstIndex+1,currentString.length());
						
						tempTextField=new JTextField[n+1];
						textFieldCount++;
						tempTextField[0]=new JTextField(subString[0],Library.TEXTFIELD_LENGTH);
						tempTextField[0].setToolTipText("第"+textFieldCount+"组:y");
						inputPanel.add(tempTextField[0]);
						tempTextField[n]=new JTextField(subString[1],Library.TEXTFIELD_LENGTH);
						tempTextField[n].setToolTipText("第"+textFieldCount+"组:x");
						inputPanel.add(tempTextField[n]);
						textFieldVector.add(tempTextField);
						tempCheckBox=new JCheckBox("使用",true);
						tempCheckBox.setFont(Library.font);
						inputPanel.add(tempCheckBox);
						checkBoxVector.add(tempCheckBox);
						//往输入面板中添加输入域
					}
					
					tempTextField[tempTextField.length-1].addFocusListener(this);
					//最后一行才需要加这个动作
					fileReader.close();
					
					parentComponent.setSize(parentComponent.getPreferredSize());
					parentComponent.setVisible(true);
				}
				catch(FileNotFoundException notFount)
				{
					showOne();
					JOptionPane.showMessageDialog(parentComponent,"文件不存在,请重新选择!",
							"文件不存在",JOptionPane.ERROR_MESSAGE);
				}
				catch(InvalidFileFormatException invalid)
				{
					showOne();
					JOptionPane.showMessageDialog(parentComponent,"文件格式错误,请重试!",
							"文件格式错误",JOptionPane.ERROR_MESSAGE);
				}
				catch(IOException ioE)
				{
					showOne();
					JOptionPane.showMessageDialog(parentComponent,"文件读写错误!\n请重试!",
							"读写错误",JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}
	
	/**
	 *由FocusListener所指定的方法,当光标移动到最后一组数据输入域的最后一个输入框时,生成下一组数据输入域
	 *@param e		光标停留在输入框时所产生的事件
	 */
	public void focusGained(FocusEvent e)
	{
		tempTextField[tempTextField.length-1].removeFocusListener(this);
		//添加输入域的动作只对最后一组数据有用
		tempCheckBox.setSelected(true);
		tempTextField=new JTextField[n+1];
		textFieldCount++;
		tempTextField[0]=new JTextField(Library.TEXTFIELD_LENGTH);
		tempTextField[0].setToolTipText("第"+textFieldCount+"组:y");
		inputPanel.add(tempTextField[0]);
		if(multiInput)
		{
			for(i=1;i<tempTextField.length;i++)//自变量的下标从1开始
			{
				tempTextField[i]=new JTextField(Library.TEXTFIELD_LENGTH);
				tempTextField[i].setToolTipText("第"+textFieldCount+"组:x"+i);
				inputPanel.add(tempTextField[i]);
			}
		}
		else
		{
			tempTextField[n]=new JTextField(Library.TEXTFIELD_LENGTH);
			tempTextField[n].setToolTipText("第"+textFieldCount+"组:x");
			inputPanel.add(tempTextField[n]);
		}
		//多元输入和一元输入就只有JTextField的提示不同
		textFieldVector.add(tempTextField);
		tempCheckBox=new JCheckBox("使用",false);
		tempCheckBox.setFont(Library.font);
		inputPanel.add(tempCheckBox);
		checkBoxVector.add(tempCheckBox);
		//往输入面板中添加输入域
		tempTextField[tempTextField.length-1].addFocusListener(this);
		
		parentComponent.setSize(parentComponent.getPreferredSize());
		parentComponent.setVisible(true);
	}
	
	/**
	 *由FocusListener所指定的方法,这是空方法。
	 *@param e		光标离开输入框时所产生的事件
	 */
	public void focusLost(FocusEvent e){}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -