導航:首頁 > 源碼編譯 > 寫一個提供各種演算法的小程序

寫一個提供各種演算法的小程序

發布時間:2022-12-28 03:00:10

『壹』 一個簡單的小程序 C語言 BF演算法

引用沒問題,就是BF函數錯了。

#include<stdio.h>
#include<string.h>
#include<iostream>//.h去掉
usingnamespacestd;//命名空間

intBF(charS[],charT[])
{
inti,j,start;
i=0;
j=0;
start=0;
while(S[i]!=''&&T[j]!='')//T[i]改為T[j]
{
if(S[i]==T[j])
{
i++;j++;
}
else{
start++;
i=start;
j=0;
}
}
if(T[j]=='')
returnstart+1;//start+1,因為數組從0開始計數
else
return0;
}

intmain()
{
charS[1000],T[1000];
intstart;
printf("輸入主串: ");
scanf("%s",S);
printf("%s ",S);
printf("輸入子串: ");
scanf("%s",T);
printf("%s ",T);
start=BF(S,T);
cout<<"主串與子串在主串的第"<<start<<"個字元匹配"<<endl;

}

『貳』 java:編寫一個計算器小程序,要求可以做加減乘除運算

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
private static final long serialVersionUID = 8199443193151152362L;
private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),
bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),
bto_6=new JButton("6"),bto_cheng=new JButton("*"),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),
bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton("."),bto_deng=new JButton("="),bto_jia=new JButton("+");
JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,
bto_0,bto_dian,bto_deng,bto_jia};
private JTextField text_double;// = new JTextField("0");
private String operator = "="; //當前運算的運算符
private boolean firstDigit = true; // 標志用戶按的是否是整個表達式的第一個數字,或者是運算符後的第一個數字
private double resultNum = 0.0; // 計算的中間結果
private boolean operateValidFlag = true; //判斷操作是否合法
public Calculator()
{
super("Calculator");
this.setBounds(300, 300, 300, 300);
this.setResizable(false);
this.setBackground(Color.orange);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());//設置布局
text_double=new JTextField("0",20);//設置文本區
text_double.setHorizontalAlignment(JTextField.RIGHT);//設置水平對齊方式未右對齊
this.getContentPane().add(text_double,BorderLayout.NORTH);//將文本區添加到Content北部
JPanel panel=new JPanel(new GridLayout(5,4));//在內容窗口添加一個網格布局
this.getContentPane().add(panel);//添加panel面板
for(int i=0;i<button.length;i++)//在面板上添加按鈕
panel.add(button[i]);

for(int i=0;i<button.length;i++)
button[i].addActionListener(this);//為按鈕注冊
text_double.setEditable(false);//文本框不可編輯
text_double.addActionListener(this);//

this.setVisible(true);
}
public void actionPerformed(ActionEvent e)//
{
String c= e.getActionCommand();//返回與此動作相關的命令字元串。
System.out.println("##########command is "+c);
if(c.equals("C")){
handleC(); //用戶按了「C」鍵
}
else if (c.equals("CE")) // 用戶按了"CE"鍵
{
text_double.setText("0");
}
else if ("0123456789.".indexOf(c) >= 0) // 用戶按了數字鍵或者小數點鍵
{
handleNumber(c); // handlezero(zero);
} else //用戶按了運算符鍵
{
handleOperator(c);
}
}
private void handleC() // 初始化計算器的各種值
{
text_double.setText("0");
firstDigit = true;
operator = "=";
}
private void handleNumber(String button) {
if (firstDigit)//輸入的第一個數字
{
text_double.setText(button);
} else if ((button.equals(".")) && (text_double.getText().indexOf(".") < 0))//輸入的是小數點,並且之前沒有小數點,則將小數點附在結果文本框的後面
//如果字元串參數作為一個子字元串在此對象中出現,則返回第一個這種子字元串的第一個字元的索引;如果它不作為一個子字元串出現,則返回 -1
{
text_double.setText(text_double.getText() + ".");
} else if (!button.equals("."))// 如果輸入的不是小數點,則將數字附在結果文本框的後面
{
text_double.setText(text_double.getText() + button);
}
// 以後輸入的肯定不是第一個數字了
firstDigit = false;
}
private void handleOperator(String button) {

if (operator.equals("/")) {
// 除法運算
// 如果當前結果文本框中的值等於0
if (getNumberFromText() == 0.0){
// 操作不合法
operateValidFlag = false;
text_double.setText("除數不能為零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("+")){
// 加法運算
resultNum += getNumberFromText();
} else if (operator.equals("-")){
// 減法運算
resultNum -= getNumberFromText();
} else if (operator.equals("*")){
// 乘法運算
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
// 平方根運算
if(getNumberFromText()<0){
operateValidFlag = false;
text_double.setText("被開方數不能為負數");}
else
resultNum = Math.sqrt(resultNum);
}
else if (operator.equals("+/-")){
// 正數負數運算
resultNum = resultNum * (-1);
} else if (operator.equals("=")){
// 賦值運算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
// 雙精度浮點數的運算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
text_double.setText(String.valueOf(t1));
} else {
text_double.setText(String.valueOf(resultNum));
}
}
operator = button; //運算符等於用戶按的按鈕
firstDigit = true;
operateValidFlag = true;
}
private double getNumberFromText() //從結果的文本框獲取數字
{
double result = 0;
try {
result = Double.valueOf(text_double.getText()).doubleValue(); // ValueOf()返回表示指定的 double 值的 Double 實例
} catch (NumberFormatException e){
}
return result;
}
public static void main(final String[] args) {
new Calculator();
}
}

『叄』 用python編寫一個小程序,輸出所有滿足條件的素數

按照你的要求編寫的Python程序如下

importmath

foriinrange(100,1000):

forjinrange(2,int(math.sqrt(i))+1):

ifi%j==0:

break;

else:

if(i%10+i//10%10)%10==i//100:

print(i)

源代碼(注意源代碼的縮進)

閱讀全文

與寫一個提供各種演算法的小程序相關的資料

熱點內容
好興動app還款怎麼登錄不上去了 瀏覽:665
鄭州雲伺服器託管 瀏覽:722
伺服器地址跟蹤 瀏覽:980
免費google雲伺服器 瀏覽:516
摘譯和編譯的英文 瀏覽:359
熱泵壓縮機選型 瀏覽:121
op手機微信加密如何解除 瀏覽:386
如何在王牌戰爭找到高爆率伺服器 瀏覽:13
江浙小學語文輔導課用什麼APP 瀏覽:99
新夢幻大陸伺服器地址 瀏覽:241
網吧伺服器怎麼更換壁紙 瀏覽:530
linux命令方法 瀏覽:332
linux下載freetype 瀏覽:123
程序員入駐平台 瀏覽:327
程序員大戰外掛 瀏覽:745
html實例教程pdf 瀏覽:157
linux命令開放所有許可權 瀏覽:575
30歲能學會編程 瀏覽:737
小火箭的伺服器是什麼 瀏覽:967
cad查信息命令 瀏覽:402