导航:首页 > 源码编译 > 写一个提供各种算法的小程序

写一个提供各种算法的小程序

发布时间: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)

源代码(注意源代码的缩进)

阅读全文

与写一个提供各种算法的小程序相关的资料

热点内容
免费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
XP禁止新建文件夹 浏览:394
程序员的悲惨生活 浏览:207
什么找房app比较好用 浏览:202