先验算法java实现代码 先验算法java实现代码转换
Java如何实现验证码验证功能
Java如何实现验证码验证功能呢?日常生活中,验证码随处可见,他可以在一定程度上保护账号安全,那么他是怎么实现的呢?
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站制作、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的昆明网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
Java实现验证码验证功能其实非常简单:用到了一个Graphics类在画板上绘制字母,随机选取一定数量的字母随机生成,然后在画板上随机生成几条干扰线。
首先,写一个验证码生成帮助类,用来绘制随机字母:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public final class GraphicHelper {
/**
* 以字符串形式返回生成的验证码,同时输出一个图片
*
* @param width
* 图片的宽度
* @param height
* 图片的高度
* @param imgType
* 图片的类型
* @param output
* 图片的输出流(图片将输出到这个流中)
* @return 返回所生成的验证码(字符串)
*/
public static String create(final int width, final int height, final String imgType, OutputStream output) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphic = image.getGraphics();
graphic.setColor(Color.getColor("F8F8F8"));
graphic.fillRect(0, 0, width, height);
Color[] colors = new Color[] { Color.BLUE, Color.GRAY, Color.GREEN, Color.RED, Color.BLACK, Color.ORANGE,
Color.CYAN };
// 在 "画板"上生成干扰线条 ( 50 是线条个数)
for (int i = 0; i 50; i++) {
graphic.setColor(colors[random.nextInt(colors.length)]);
final int x = random.nextInt(width);
final int y = random.nextInt(height);
final int w = random.nextInt(20);
final int h = random.nextInt(20);
final int signA = random.nextBoolean() ? 1 : -1;
final int signB = random.nextBoolean() ? 1 : -1;
graphic.drawLine(x, y, x + w * signA, y + h * signB);
}
// 在 "画板"上绘制字母
graphic.setFont(new Font("Comic Sans MS", Font.BOLD, 30));
for (int i = 0; i 6; i++) {
final int temp = random.nextInt(26) + 97;
String s = String.valueOf((char) temp);
sb.append(s);
graphic.setColor(colors[random.nextInt(colors.length)]);
graphic.drawString(s, i * (width / 6), height - (height / 3));
}
graphic.dispose();
try {
ImageIO.write(image, imgType, output);
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
接着,创建一个servlet,用来固定图片大小,以及处理验证码的使用场景,以及捕获页面生成的验证码(捕获到的二维码与用户输入的验证码一致才能通过)。
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(urlPatterns = "/verify/regist.do" )
public class VerifyCodeServlet extends HttpServlet {
private static final long serialVersionUID = 3398560501558431737L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获得 当前请求 对应的 会话对象
HttpSession session = request.getSession();
// 从请求中获得 URI ( 统一资源标识符 )
String uri = request.getRequestURI();
System.out.println("hello : " + uri);
final int width = 180; // 图片宽度
final int height = 40; // 图片高度
final String imgType = "jpeg"; // 指定图片格式 (不是指MIME类型)
final OutputStream output = response.getOutputStream(); // 获得可以向客户端返回图片的输出流
// (字节流)
// 创建验证码图片并返回图片上的字符串
String code = GraphicHelper.create(width, height, imgType, output);
System.out.println("验证码内容: " + code);
// 建立 uri 和 相应的 验证码 的关联 ( 存储到当前会话对象的属性中 )
session.setAttribute(uri, code);
System.out.println(session.getAttribute(uri));
}
}
接着写一个HTML注册页面用来检验一下:
html
head
meta charset="UTF-8"
title注册/title
link rel="stylesheet" href="styles/general.css"
link rel="stylesheet" href="styles/cell.css"
link rel="stylesheet" href="styles/form.css"
script type="text/javascript" src="js/ref.js"/script
style type="text/css"
.logo-container {
margin-top: 50px ;
}
.logo-container img {
width: 100px ;
}
.message-container {
height: 80px ;
}
.link-container {
height: 40px ;
line-height: 40px ;
}
.link-container a {
text-decoration: none ;
}
/style
/head
body
div class="container form-container"
form action="/wendao/regist.do" method="post"
div class="form" !-- 注册表单开始 --
div class="form-row"
span class="cell-1"
i class="fa fa-user"/i
/span
span class="cell-11" style="text-align: left;"
input type="text" name="username" placeholder="请输入用户名"
/span
/div
div class="form-row"
span class="cell-1"
i class="fa fa-key"/i
/span
span class="cell-11" style="text-align: left;"
input type="password" name="password" placeholder="请输入密码"
/span
/div
div class="form-row"
span class="cell-1"
i class="fa fa-keyboard-o"/i
/span
span class="cell-11" style="text-align: left;"
input type="password" name="confirm" placeholder="请确认密码"
/span
/div
div class="form-row"
span class="cell-7"
input type="text" name="verifyCode" placeholder="请输入验证码"
/span
span class="cell-5" style="text-align: center;"
img src="/demo/verify/regist.do" onclick="myRefersh(this)"
/span
/div
div class="form-row" style="border: none;"
span class="cell-6" style="text-align: left"
input type="reset" value="重置"
/span
span class="cell-6" style="text-align:right;"
input type="submit" value="注册"
/span
/div
/div !-- 注册表单结束 --
/form
/div
/body
/html
效果如下图:
在控制台接收到的图片中验证码的变化如下:
当点击刷新页面的时候,验证码也会随着变化,但我们看不清验证码时,只要点击验证码就会刷新,这样局部的刷新可以用JavaScript来实现。
在img
src="/demo/verify/regist.do"中,添加一个问号和一串后缀数字,当刷新时让后缀数字不断改变,那么形成的验证码也会不断变化,我们可以采用的一种办法是后缀数字用date代替,date获取本机时间,时间是随时变的,这样就保证了刷新验证码可以随时变化。
代码如下:
function myRefersh( e ) {
const source = e.src ; // 获得原来的 src 中的内容
//console.log( "source : " + source ) ;
var index = source.indexOf( "?" ) ; // 从 source 中寻找 ? 第一次出现的位置 (如果不存在则返回 -1 )
//console.log( "index : " + index ) ;
if( index -1 ) { // 如果找到了 ? 就进入内部
var s = source.substring( 0 , index ) ; // 从 source 中截取 index 之前的内容 ( index 以及 index 之后的内容都被舍弃 )
//console.log( "s : " + s ) ;
var date = new Date(); // 创建一个 Date 对象的 一个 实例
var time = date.getTime() ; // 从 新创建的 Date 对象的实例中获得该时间对应毫秒值
e.src = s + "?time=" + time ; // 将 加了 尾巴 的 地址 重新放入到 src 上
//console.log( e.src ) ;
} else {
var date = new Date();
e.src = source + "?time=" + date.getTime();
}
}
如回答不详细可追问
如何用70行Java代码实现神经网络算法
如何用70行Java代码实现神经网络算法
import java.util.Random;
public class BpDeep{
public double[][] layer;//神经网络各层节点
public double[][] layerErr;//神经网络各节点误差
public double[][][] layer_weight;//各层节点权重
public double[][][] layer_weight_delta;//各层节点权重动量
public double mobp;//动量系数
public double rate;//学习系数
public BpDeep(int[] layernum, double rate, double mobp){
this.mobp = mobp;
this.rate = rate;
layer = new double[layernum.length][];
layerErr = new double[layernum.length][];
layer_weight = new double[layernum.length][][];
layer_weight_delta = new double[layernum.length][][];
Random random = new Random();
for(int l=0;llayernum.length;l++){
layer[l]=new double[layernum[l]];
layerErr[l]=new double[layernum[l]];
if(l+1layernum.length){
layer_weight[l]=new double[layernum[l]+1][layernum[l+1]];
layer_weight_delta[l]=new double[layernum[l]+1][layernum[l+1]];
for(int j=0;jlayernum[l]+1;j++)
for(int i=0;ilayernum[l+1];i++)
layer_weight[l][j][i]=random.nextDouble();//随机初始化权重
}
}
}
//逐层向前计算输出
public double[] computeOut(double[] in){
for(int l=1;llayer.length;l++){
for(int j=0;jlayer[l].length;j++){
double z=layer_weight[l-1][layer[l-1].length][j];
for(int i=0;ilayer[l-1].length;i++){
layer[l-1][i]=l==1?in[i]:layer[l-1][i];
z+=layer_weight[l-1][i][j]*layer[l-1][i];
}
layer[l][j]=1/(1+Math.exp(-z));
}
}
return layer[layer.length-1];
}
//逐层反向计算误差并修改权重
public void updateWeight(double[] tar){
int l=layer.length-1;
for(int j=0;jlayerErr[l].length;j++)
layerErr[l][j]=layer[l][j]*(1-layer[l][j])*(tar[j]-layer[l][j]);
while(l--0){
for(int j=0;jlayerErr[l].length;j++){
double z = 0.0;
for(int i=0;ilayerErr[l+1].length;i++){
z=z+l0?layerErr[l+1][i]*layer_weight[l][j][i]:0;
layer_weight_delta[l][j][i]= mobp*layer_weight_delta[l][j][i]+rate*layerErr[l+1][i]*layer[l][j];//隐含层动量调整
layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隐含层权重调整
if(j==layerErr[l].length-1){
layer_weight_delta[l][j+1][i]= mobp*layer_weight_delta[l][j+1][i]+rate*layerErr[l+1][i];//截距动量调整
layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距权重调整
}
}
layerErr[l][j]=z*layer[l][j]*(1-layer[l][j]);//记录误差
}
}
}
public void train(double[] in, double[] tar){
double[] out = computeOut(in);
updateWeight(tar);
}
}
题目1:一个简单的算法演示程序(JAVA语言实现)
1. 选择一个算法(提供选择见下),利用各种方法(图形、动画等)演示算法的演示过程。
2. 可以进行手动演示,也可以自动步进式演示。
3. 允许用户设置算法的各个输入参数,以及自动步进式演示中的时间间隔。
4. 不同的算法输入要求见下。
界面要求:
1. 尽量使用图形界面实现,要符合日常软件使用规范来设计菜单和界面。
2. 如果无法实现图形界面,则在命令行方式下也需要提供菜单,方便用户操作。
其他要求:
1. 标识符命名遵循Windows命名规范。
2. 能够注意各种异常处理,注重提高程序运行效率。
提交内容:
1. 全部源代码。
2. 软件设计和使用说明书(UML类图;实现的功能、主要技术;使用帮助文档)
参考算法:
1. 最小生成树算法:Prim算法、Kruskal算法。允许以下方式输入一个图形:绘制图形、输入邻接矩阵、输入边及其关联的顶点。要求在图形方式下进行演示算法执行步骤。
2. 单源最短路算法:Dijkstra算法。允许以下方式输入一个图形:绘制图形、输入邻接矩阵、输入边及其关联的顶点。要求在图形方式下进行演示算法执行步骤。
3. 最优编码算法:Huffman编码算法。允许用户输入一段英文文字,或者打开一个txt文档(英文内容),据此文档内容进行编码。要求动态列出每个字符的出现概率统计结果以及对应编码。
4. 其他可供演示的具有一定难度的算法,如关键路径问题、有向图的极大连通分支等。
优先级调度算法如何用JAVA实现
没java的 发段源代码给你 有兴趣自己慢慢理解
#include time.h
#include dos.h
#include math.h
#include conio.h
#include stdio.h
#include stdlib.h
#include time.h
#include graphics.h
#define ESC 0x1b
#define ENTER 0x0d
#define TRUE 1
#define FALSE 0
/*每隔TIME秒就变换一次优先级*/
#define TIME 5
/*数据结构*/
/****************************************************************/
enum _Status/*进程状态枚举*/
{
READY =0,/*就绪*/
RUN,/*执行中*/
SUSPEND,/*挂起*/
};
typedef enum _Status Status;
/****************************************************************/
struct _Pcb/*进程结构*/
{
int PID;/*进程ID,ID为负数的进程为系统后备队列的作业*/
int Time;/*进程运行需要的时间*/
int Prior;/*进程的优先级,越大越优先*/
Status Sts;/*状态*/
struct _Pcb *Next;/*指向本进程队列中下个进程的PCB*/
};
typedef struct _Pcb PCB;
/****************************************************************/
struct _Batch/*多道处理中的道结构*/
{
PCB *pcb;/*该道当前正在处理的进程*/
struct _Batch *Next;/*下一道*/
};
typedef struct _Batch Batch;
/****************************************************************/
/*多道系统相关全局变量*/
PCB *ProcQueue = NULL;/*进程链表,按优先级从大到小排列*/
Batch *BatchQueue = NULL;/*系统多道链表*/
/****************************************************************/
/*动态优先权抢占式调度算法及相关函数声明*/
/****************************************************************/
int InitBatchs(int n);/*初始化多道系统,n为道数*/
int InsertProc(int prior, int time);/*向进程链表中按优先级大小插入一个新进程*/
int InsertIDLE();/*向进程队列中加入后备进程,当系统空闲时将被调入*/
int SortProcQueue();/*将进程链表按优先级从大到小排列*/
int AddBatch();/*增加系统道数*/
int DeleteBatch();/*减少系统道数*/
int UnSuspendProc(int id);/*解除ID为id的进程的挂起状态*/
int UpdateBatchs();/*多道系统根据进程链表进行更新,并将执行完毕的进程删除*/
int PassSeconds(int n);/*系统经过n秒后计算数据并进行优先级调度*/
/****************************************************************/
/*各函数的定义*/
/****************************************************************/
int InitBatchs(int n)
{
int i;
for (i=0; in; ++i)
{
AddBatch();
}
return (UpdateBatchs());
}
int InsertProc(int prior, int time)
{
static int sysid = 0;/*该系统已经加入过多少进程,此值将是新进程的ID*/
PCB *last,*now,*pcb;
pcb = (PCB*)malloc(sizeof(PCB));
if (pcb == NULL) return FALSE;
pcb-Prior = prior;
pcb-Time = time;
pcb-PID = (++sysid);
pcb-Sts = READY;
if (ProcQueue == NULL)/*如果进程队列为空*/
{
ProcQueue = pcb;
pcb-Next = NULL;
return TRUE;
}
last = ProcQueue;
now = last-Next;
if (pcb-Prior last-Prior)/*pcb将排在队头*/
{
pcb-Next = ProcQueue;
ProcQueue = pcb;
return TRUE;
}
while ((now != NULL) (pcb-Prior now-Prior))/*寻找插入位置*/
{
last = now;
now = last-Next;
}
last-Next = pcb;
pcb-Next = now;
return TRUE;
}
int InsertIDLE()
{
PCB *now = ProcQueue;
PCB *idle = (PCB*)malloc(sizeof(PCB));
if (idle == NULL) return FALSE;
idle-PID = -1;
idle-Prior = -1;
idle-Sts = SUSPEND;
idle-Time = -1;
idle-Next = NULL;
if (ProcQueue == NULL)
{
ProcQueue = idle;
return TRUE;
}
while(now-Next != NULL)
{
now = now-Next;
}
now-Next = idle;
return TRUE;
}
int SortProcQueue()
{ /*冒泡排序*/
PCB *last, *now;
int b = FALSE;/*上次遍历是否无交换产生*/
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果链表中无进程或只有一个进程*/
return FALSE;
while (!b)
{
b = TRUE;
last=ProcQueue;
now=last-Next;
if (last-Prior now-Prior)
{
ProcQueue = now;
last-Next = now-Next;
now-Next = last;
b = FALSE;
last = ProcQueue;
now = last-Next;
}
while (now-Next!=NULL)
{
if ((now-Prior)(now-Next-Prior))
{
last-Next = now-Next;
now-Next = now-Next-Next;
last-Next-Next = now;
b = FALSE;
}
else
last = last-Next;
now = last-Next;
}
}
return TRUE;
}
int AddBatch()
{
Batch *bt = (Batch*)malloc(sizeof(Batch));
if (bt==NULL) return FALSE;
bt-Next = BatchQueue;
BatchQueue = bt;
bt-pcb = NULL;
return (InsertIDLE());
}
int DeleteBatch()
{
Batch *bt = BatchQueue;
PCB *last, *now;
if (BatchQueue==NULL || BatchQueue-Next==NULL)/*如果只剩最后一道则不删除*/
return FALSE;
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果只有最后一个后备空闲进程*/
return FALSE;/**/
last = ProcQueue;
now = last-Next;
while (now-Next != NULL)/*查找到最后一个进程,该进程必定是后备空闲进程*/
{
last = now;
now = last-Next;
}
if (now==NULL || now-PID=0)/*未查找到后备进程*/
return FALSE;/**/
free(now);
last-Next = NULL;
BatchQueue = BatchQueue-Next;
free(bt);
return TRUE;
}
int UnSuspendProc(int id)
{
PCB *now = ProcQueue;
if (ProcQueue==NULL) return FALSE;
while (now != NULL)
{
if (now-PID == id)
{
now-Sts = READY;
return TRUE;
}
}
return FALSE;
}
int UpdateBatchs()
{
Batch *bt = BatchQueue;
PCB *last = ProcQueue, *now;
while (bt != NULL)
{
bt-pcb = NULL;
bt = bt-Next;
}
if (ProcQueue == NULL) return TRUE;
while (last-Sts==RUN last-PID=0 last-Time=0)
{
ProcQueue = ProcQueue-Next;
free(last);
last = ProcQueue;
}
now = last-Next;
while (now != NULL)
{
if (now-Sts==RUN now-PID=0 now-Time=0)/*如果该进程是运行中的一般进程并已执行完毕*/
{
last-Next = now-Next;
free(now);
}
else
last = last-Next;
now = last-Next;
}
bt = BatchQueue;
now = ProcQueue;
while (bt != NULL now != NULL)
{
bt-pcb = now;
now-Sts = RUN;
bt = bt-Next;
now = now-Next;
}
while (now != NULL)
{
if (now-Sts == RUN)
{
now-Sts = SUSPEND;
}
now = now-Next;
}
return TRUE;
}
int PassSeconds(int n)
{
static int time = 0;
int i=0, ProcEnd = FALSE;
PCB *pcb = ProcQueue;
Batch *bt = BatchQueue;
if (bt == NULL) return FALSE;
time += n;
if (time=TIME)
{
i = time/TIME;/*经过多少时间段*/
time = time%TIME;
}
while (bt != NULL)/*更新进程运行时间*/
{
if (bt-pcb-PID=0)
{
bt-pcb-Time -= n;
if (bt-pcb-Time = 0)/*进程结束*/
{
ProcEnd = TRUE;
}
}
bt = bt-Next;
}
if (i 0)
{
while (pcb != NULL)/*更新进程优先权(动态优先权)*/
{
if (pcb-Sts == RUN pcb-PID=0)/*运行的进程优先权降低*/
{
pcb-Prior -= i;
if (pcb-Prior 0)
pcb-Prior = 0;
}
else if (pcb-Sts == SUSPEND pcb-PID=0)/*挂起的进程优先权升高*/
pcb-Prior += i;
pcb = pcb-Next;
}
}
if (i0)
SortProcQueue();/*如果优先级有变动则重新排序*/
if (ProcEnd || i0)
{
UpdateBatchs();/*更新多道进程*/
}
return TRUE;
}
/****************************************************************/
/*图形界面相关函数*/
/****************************************************************/
/*表格的单位宽度和高度*/
#define WIDTH 64
#define HEIGHT 12
void *black=NULL;/*背景色方格,使用它擦出表格中的图形*/
int InitGraph()/*初始化图形界面*/
{
int GraphDriver; /* The Graphics device driver */
int GraphMode; /* The Graphics mode value */
int ErrorCode;
GraphDriver = DETECT; /* Request auto-detection */
initgraph( GraphDriver, GraphMode, "" );
ErrorCode = graphresult(); /* Read result of initialization*/
if( ErrorCode != grOk )
{ /* Error occured during init */
printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
getch();
return FALSE;
}
cleardevice();
black = (void*)malloc(imagesize(1,1,WIDTH-1,HEIGHT-1));
getimage(1,1,WIDTH-1,HEIGHT-1,black);
DrawFrame();
DrawData();
return TRUE;
}
int DrawFrame()/*画边框和表头*/
{
settextjustify(CENTER_TEXT, CENTER_TEXT);
gprintf(320, HEIGHT/2-1, "Multi-Batch System Emulation");
settextjustify(LEFT_TEXT, TOP_TEXT);
moveto(0,HEIGHT);
lineto(0,479);
lineto(639,479);
lineto(639,HEIGHT);
lineto(0,HEIGHT);
line(WIDTH*4,HEIGHT,WIDTH*4,479);
line(WIDTH*7,HEIGHT,WIDTH*7,479);
line(0,HEIGHT*2,639,HEIGHT*2);
line(0,HEIGHT*3,639,HEIGHT*3);
gprintf(HEIGHT*0+2,HEIGHT*1+2,"System Batchs");/*系统多道列表头*/
gprintf(HEIGHT*0+2,HEIGHT*2+2,"Batch");
gprintf(WIDTH*1+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*2+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*3+2,HEIGHT*2+2,"Prior");
gprintf(WIDTH*4+2,HEIGHT*1+2,"Suspended Processes");/*挂起队列列表头*/
gprintf(WIDTH*4+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*5+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*6+2,HEIGHT*2+2,"Prior");
gprintf(WIDTH*7+2,HEIGHT*1+2,"Ready Processes");/*就绪队列列表头*/
gprintf(WIDTH*7+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*8+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*9+2,HEIGHT*2+2,"Prior");
}
int DrawData()/*绘制系统数据*/
{
int numRun=0, numSus=0, numRed=0;/*运行挂起和就绪的进程各有多少*/
PCB* now = ProcQueue;
int x=0, y=0;
while (now != NULL)
{
switch(now-Sts)
{
case RUN:
x = WIDTH*1;
y = HEIGHT*(3+(numRun++));
break;
case SUSPEND:
x = WIDTH*4;
y = HEIGHT*(3+(numSus++));
break;
case READY:
x = WIDTH*7;
y = HEIGHT*(3+(numRed++));
break;
}
if (now-Sts==RUN)/*该进程为正在运行的进程*/
{
putimage(x-WIDTH+1,y+1,black,COPY_PUT);
gprintf(x-WIDTH+2,y+2,"%d",numRun);
}
if (now-PID=0)/*该进程不是后备进程*/
{
putimage(x+1,y+1,black,COPY_PUT);
gprintf(x+2,y+2,"%d",now-PID);
putimage(x+1+WIDTH,y+1,black,COPY_PUT);
gprintf(x+WIDTH+2,y+2,"%d",now-Time);
putimage(x+1+WIDTH*2,y+1,black,COPY_PUT);
gprintf(x+WIDTH*2+2,y+2,"%d",now-Prior);
}
else
{
putimage(x+1,y+1,black,COPY_PUT);
putimage(x+1+WIDTH,y+1,black,COPY_PUT);
putimage(x+1+WIDTH*2,y+1,black,COPY_PUT);
gprintf(x+2,y+2,"system idle process");
}
now = now-Next;
}
}
int DlgGetNum(char *buf,int l,int t,int r,int b,int gettime)
{
char ch;
int pos=0;
bar(l,t,r,b);
gprintf(l+10,t+5,"Add new Process");
if (gettime)
gprintf(l+10,t+20,"input the time:");
else
gprintf(l+10,t+20,"input the priority:");
while (1)
{
ch = getch();
if (ch == ENTER)/*如果输入了回车键*/
{
if(pos!=0)/*如果位置不在第一位(回车键不准第一个输入)*/
{
buf[pos]='\0';
break;
}
}
else if (ch='0' ch='9')
{
buf[pos++]=ch;
buf[pos]='\0';
}
else if (ch == ESC)
{
return FALSE;
}
else/*其他按键均按BackSpace处理*/
{
--pos;
buf[pos]='\0';
}
if (pos0)
{ pos=0; buf[pos]='\0';}
else if (pos4)/*最多输入4位数*/
{ pos=4; buf[pos]='\0';}
bar(l,t+35,r,t+47);
gprintf(l+50,t+35,buf);
}
return TRUE;
}
int NewProcDlg()
{
int l=200,t=150,r=380,b=200,pos=0,prior=0,time=0;
char buf[5],ch;
PCB *pcb;
void* bg = (void*)malloc(imagesize(l,t,r,b));
getimage(l,t,r,b,bg);
setfillstyle(1,2);
flushall();
/*******输入优先级**********/
if (!DlgGetNum(buf,l,t,r,b,FALSE))
goto exit;
prior = atoi(buf);
/*******输入时间**********/
pos=0;
buf[pos]='\0';
if (!DlgGetNum(buf,l,t,r,b,TRUE))
goto exit;
time = atoi(buf);
InsertProc(prior, time);
exit:
putimage(l,t,bg,COPY_PUT);
free(bg);
return TRUE;
}
int gprintf( int xloc, int yloc, char *fmt, ... )/*图形系统中的格式输出*/
{
va_list argptr; /* Argument list pointer */
char str[140]; /* Buffer to build sting into */
int cnt; /* Result of SPRINTF for return */
va_start( argptr, format ); /* Initialize va_ functions */
cnt = vsprintf( str, fmt, argptr ); /* prints string to buffer */
outtextxy( xloc, yloc, str ); /* Send string in graphics mode */
va_end( argptr ); /* Close va_ functions */
return( cnt ); /* Return the conversion count */
}
/****************************************************************/
/*main函数*/
int main()
{
clock_t start=0, end=0;
char kb;
InitBatchs(3);/*初始化为3道系统*/
InitGraph();
while (1)
{
start = end = clock();
while (!kbhit())
{
start = clock();
if ((start-end)/18.2 = 1)/*时间过了一秒*/
{
start = end = clock();
PassSeconds(1);
cleardevice();
DrawFrame();
DrawData();
}
}
kb = getch();
switch (kb)
{
case ESC:
closegraph();
return 0;
case 'w':
AddBatch();
UpdateBatchs();
cleardevice();
DrawFrame();
DrawData();
break;
case 's':
DeleteBatch();
UpdateBatchs();
cleardevice();
DrawFrame();
DrawData();
break;
case 'i':
NewProcDlg();
UpdateBatchs();
DrawFrame();
DrawData();
break;
}
}
return 0;
}
这两道题代码怎么写java?
创建一个名字为“ReportCard”的类,然后用下边的内容全部替换掉,你会成为全班最亮的仔。
import java.util.HashMap;
/**
* 学生成绩单
*/
public class ReportCard {
public static void main(String[] args) {
ReportCard reportCard = new ReportCard("张三", "070602213");
reportCard.set("语文", 80.0);
reportCard.set("数学", 59.5);
reportCard.set("英语", 66.0);
reportCard.set("java", 80, 99.0);
reportCard.set("数据库", 80, 66.0);
reportCard.set("毛概", null);
System.out.println(reportCard.getStudentName() + "语文分数:" + reportCard.get("语文"));
System.out.println(reportCard.getStudentName() + "数学考核结果:" + (reportCard.isPassed("数学") ? "合格" : "不合格"));
System.out.println(reportCard.getStudentName() + "期末是否挂科:" + (reportCard.isAllPassed() ? "否" : "是"));
}
// 学生姓名
private String studentName;
// 学生学号
private String studentNumber;
// 成绩单
private HashMapString, CourseResult cards = new HashMap();
public ReportCard() {
}
public ReportCard(String studentName, String studentNumber) {
this.studentName = studentName;
this.studentNumber = studentNumber;
}
public Double get(String courseName){
CourseResult courseResult = cards.get(courseName);
return courseResult == null ? Double.NaN : courseResult.getStudentScore();
}
public void set(String courseName, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, studentScore);
cards.put(courseName, courseResult);
}
public void set(String courseName, double passMark, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, passMark, studentScore);
cards.put(courseName, courseResult);
}
public boolean isPassed(String courseName){
return cards.get(courseName).isPassed();
}
public boolean isAllPassed(){
for(CourseResult cr : cards.values()){
if ( ! cr.isPassed()) {
return false;
}
}
return true;
}
public String getStudentName() {
return studentName;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
/**
* 课程
*/
class Course{
// 课程名称
protected String courseName;
// 及格分
protected double passMark = 60;
public Course(String courseName, Double passMark) {
this.courseName = courseName;
if ( passMark != null) {
this.passMark = passMark;
}
}
}
/**
* 课程成绩
*/
class CourseResult extends Course{
// 学生成绩
private Double studentScore;
public CourseResult(String courseName, Double studentScore) {
this(courseName, null, studentScore);
}
public CourseResult(String courseName, Double passMark, Double studentScore) {
super(courseName, passMark);
this.studentScore = studentScore == null ? Double.NaN : studentScore;
}
public boolean isPassed(){
return studentScore = passMark;
}
public String getCourseName() {
return courseName;
}
public double getPassMark() {
return passMark;
}
public Double getStudentScore() {
return studentScore;
}
}
分享名称:先验算法java实现代码 先验算法java实现代码转换
本文链接:http://ybzwz.com/article/dodohie.html