Java如何實(shí)現(xiàn)驗(yàn)證碼驗(yàn)證功能呢?日常生活中,驗(yàn)證碼隨處可見,他可以在一定程度上保護(hù)賬號(hào)安全,那么他是怎么實(shí)現(xiàn)的呢?
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的昆明網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
Java實(shí)現(xiàn)驗(yàn)證碼驗(yàn)證功能其實(shí)非常簡(jiǎn)單:用到了一個(gè)Graphics類在畫板上繪制字母,隨機(jī)選取一定數(shù)量的字母隨機(jī)生成,然后在畫板上隨機(jī)生成幾條干擾線。
首先,寫一個(gè)驗(yàn)證碼生成幫助類,用來繪制隨機(jī)字母:
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?{
/**
*?以字符串形式返回生成的驗(yàn)證碼,同時(shí)輸出一個(gè)圖片
*
*?@param?width
*????????????圖片的寬度
*?@param?height
*????????????圖片的高度
*?@param?imgType
*????????????圖片的類型
*?@param?output
*????????????圖片的輸出流(圖片將輸出到這個(gè)流中)
*?@return?返回所生成的驗(yàn)證碼(字符串)
*/
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?是線條個(gè)數(shù))
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();
}
}?
接著,創(chuàng)建一個(gè)servlet,用來固定圖片大小,以及處理驗(yàn)證碼的使用場(chǎng)景,以及捕獲頁(yè)面生成的驗(yàn)證碼(捕獲到的二維碼與用戶輸入的驗(yàn)證碼一致才能通過)。
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?{
//?獲得?當(dāng)前請(qǐng)求?對(duì)應(yīng)的?會(huì)話對(duì)象
HttpSession?session?=?request.getSession();
//?從請(qǐng)求中獲得?URI?(?統(tǒng)一資源標(biāo)識(shí)符?)
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();?//?獲得可以向客戶端返回圖片的輸出流
//?(字節(jié)流)
//?創(chuàng)建驗(yàn)證碼圖片并返回圖片上的字符串
String?code?=?GraphicHelper.create(width,?height,?imgType,?output);
System.out.println("驗(yàn)證碼內(nèi)容:?"?+?code);
//?建立?uri?和?相應(yīng)的?驗(yàn)證碼?的關(guān)聯(lián)?(?存儲(chǔ)到當(dāng)前會(huì)話對(duì)象的屬性中?)
session.setAttribute(uri,?code);
System.out.println(session.getAttribute(uri));
}
}?
接著寫一個(gè)HTML注冊(cè)頁(yè)面用來檢驗(yàn)一下:
html
head
meta?charset="UTF-8"
title注冊(cè)/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"?!--?注冊(cè)表單開始?--
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="請(qǐng)輸入用戶名"
/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="請(qǐng)輸入密碼"
/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="請(qǐng)確認(rèn)密碼"
/span
/div
div?class="form-row"
span?class="cell-7"
input?type="text"?name="verifyCode"?placeholder="請(qǐng)輸入驗(yàn)證碼"
/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="注冊(cè)"
/span
/div
/div?!--?注冊(cè)表單結(jié)束?--
/form
/div
/body
/html
效果如下圖:
在控制臺(tái)接收到的圖片中驗(yàn)證碼的變化如下:
當(dāng)點(diǎn)擊刷新頁(yè)面的時(shí)候,驗(yàn)證碼也會(huì)隨著變化,但我們看不清驗(yàn)證碼時(shí),只要點(diǎn)擊驗(yàn)證碼就會(huì)刷新,這樣局部的刷新可以用JavaScript來實(shí)現(xiàn)。
在img
src="/demo/verify/regist.do"中,添加一個(gè)問號(hào)和一串后綴數(shù)字,當(dāng)刷新時(shí)讓后綴數(shù)字不斷改變,那么形成的驗(yàn)證碼也會(huì)不斷變化,我們可以采用的一種辦法是后綴數(shù)字用date代替,date獲取本機(jī)時(shí)間,時(shí)間是隨時(shí)變的,這樣就保證了刷新驗(yàn)證碼可以隨時(shí)變化。
代碼如下:
function?myRefersh(?e?)?{
const?source?=?e.src?;?//?獲得原來的?src?中的內(nèi)容
//console.log(?"source?:?"?+?source??)?;
var?index?=?source.indexOf(?"?"?)?;??//?從?source?中尋找???第一次出現(xiàn)的位置?(如果不存在則返回?-1?)
//console.log(?"index?:?"?+?index??)?;
if(?index??-1?)?{?//?如果找到了????就進(jìn)入內(nèi)部
var?s?=?source.substring(?0?,?index?)?;?//?從?source?中截取?index?之前的內(nèi)容?(?index?以及?index?之后的內(nèi)容都被舍棄?)
//console.log(?"s?:?"?+?s??)?;
var?date?=?new?Date();?//?創(chuàng)建一個(gè)?Date?對(duì)象的?一個(gè)?實(shí)例
var?time?=?date.getTime()?;?//?從?新創(chuàng)建的?Date?對(duì)象的實(shí)例中獲得該時(shí)間對(duì)應(yīng)毫秒值
e.src?=?s?+?"?time="?+?time?;?//?將?加了?尾巴?的?地址?重新放入到?src?上
//console.log(?e.src?)?;
}?else?{
var?date?=?new?Date();
e.src?=?source?+?"?time="?+?date.getTime();
}
}
如回答不詳細(xì)可追問
如何用70行Java代碼實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)算法
import java.util.Random;
public class BpDeep{
public double[][] layer;//神經(jīng)網(wǎng)絡(luò)各層節(jié)點(diǎn)
public double[][] layerErr;//神經(jīng)網(wǎng)絡(luò)各節(jié)點(diǎn)誤差
public double[][][] layer_weight;//各層節(jié)點(diǎn)權(quán)重
public double[][][] layer_weight_delta;//各層節(jié)點(diǎn)權(quán)重動(dòng)量
public double mobp;//動(dòng)量系數(shù)
public double rate;//學(xué)習(xí)系數(shù)
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();//隨機(jī)初始化權(quán)重
}
}
}
//逐層向前計(jì)算輸出
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];
}
//逐層反向計(jì)算誤差并修改權(quán)重
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];//隱含層動(dòng)量調(diào)整
layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隱含層權(quán)重調(diào)整
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];//截距動(dòng)量調(diào)整
layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距權(quán)重調(diào)整
}
}
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. 選擇一個(gè)算法(提供選擇見下),利用各種方法(圖形、動(dòng)畫等)演示算法的演示過程。
2. 可以進(jìn)行手動(dòng)演示,也可以自動(dòng)步進(jìn)式演示。
3. 允許用戶設(shè)置算法的各個(gè)輸入?yún)?shù),以及自動(dòng)步進(jìn)式演示中的時(shí)間間隔。
4. 不同的算法輸入要求見下。
界面要求:
1. 盡量使用圖形界面實(shí)現(xiàn),要符合日常軟件使用規(guī)范來設(shè)計(jì)菜單和界面。
2. 如果無法實(shí)現(xiàn)圖形界面,則在命令行方式下也需要提供菜單,方便用戶操作。
其他要求:
1. 標(biāo)識(shí)符命名遵循Windows命名規(guī)范。
2. 能夠注意各種異常處理,注重提高程序運(yùn)行效率。
提交內(nèi)容:
1. 全部源代碼。
2. 軟件設(shè)計(jì)和使用說明書(UML類圖;實(shí)現(xiàn)的功能、主要技術(shù);使用幫助文檔)
參考算法:
1. 最小生成樹算法:Prim算法、Kruskal算法。允許以下方式輸入一個(gè)圖形:繪制圖形、輸入鄰接矩陣、輸入邊及其關(guān)聯(lián)的頂點(diǎn)。要求在圖形方式下進(jìn)行演示算法執(zhí)行步驟。
2. 單源最短路算法:Dijkstra算法。允許以下方式輸入一個(gè)圖形:繪制圖形、輸入鄰接矩陣、輸入邊及其關(guān)聯(lián)的頂點(diǎn)。要求在圖形方式下進(jìn)行演示算法執(zhí)行步驟。
3. 最優(yōu)編碼算法:Huffman編碼算法。允許用戶輸入一段英文文字,或者打開一個(gè)txt文檔(英文內(nèi)容),據(jù)此文檔內(nèi)容進(jìn)行編碼。要求動(dòng)態(tài)列出每個(gè)字符的出現(xiàn)概率統(tǒng)計(jì)結(jié)果以及對(duì)應(yīng)編碼。
4. 其他可供演示的具有一定難度的算法,如關(guān)鍵路徑問題、有向圖的極大連通分支等。
沒java的 發(fā)段源代碼給你 有興趣自己慢慢理解
#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秒就變換一次優(yōu)先級(jí)*/
#define TIME 5
/*數(shù)據(jù)結(jié)構(gòu)*/
/****************************************************************/
enum _Status/*進(jìn)程狀態(tài)枚舉*/
{
READY =0,/*就緒*/
RUN,/*執(zhí)行中*/
SUSPEND,/*掛起*/
};
typedef enum _Status Status;
/****************************************************************/
struct _Pcb/*進(jìn)程結(jié)構(gòu)*/
{
int PID;/*進(jìn)程ID,ID為負(fù)數(shù)的進(jìn)程為系統(tǒng)后備隊(duì)列的作業(yè)*/
int Time;/*進(jìn)程運(yùn)行需要的時(shí)間*/
int Prior;/*進(jìn)程的優(yōu)先級(jí),越大越優(yōu)先*/
Status Sts;/*狀態(tài)*/
struct _Pcb *Next;/*指向本進(jìn)程隊(duì)列中下個(gè)進(jìn)程的PCB*/
};
typedef struct _Pcb PCB;
/****************************************************************/
struct _Batch/*多道處理中的道結(jié)構(gòu)*/
{
PCB *pcb;/*該道當(dāng)前正在處理的進(jìn)程*/
struct _Batch *Next;/*下一道*/
};
typedef struct _Batch Batch;
/****************************************************************/
/*多道系統(tǒng)相關(guān)全局變量*/
PCB *ProcQueue = NULL;/*進(jìn)程鏈表,按優(yōu)先級(jí)從大到小排列*/
Batch *BatchQueue = NULL;/*系統(tǒng)多道鏈表*/
/****************************************************************/
/*動(dòng)態(tài)優(yōu)先權(quán)搶占式調(diào)度算法及相關(guān)函數(shù)聲明*/
/****************************************************************/
int InitBatchs(int n);/*初始化多道系統(tǒng),n為道數(shù)*/
int InsertProc(int prior, int time);/*向進(jìn)程鏈表中按優(yōu)先級(jí)大小插入一個(gè)新進(jìn)程*/
int InsertIDLE();/*向進(jìn)程隊(duì)列中加入后備進(jìn)程,當(dāng)系統(tǒng)空閑時(shí)將被調(diào)入*/
int SortProcQueue();/*將進(jìn)程鏈表按優(yōu)先級(jí)從大到小排列*/
int AddBatch();/*增加系統(tǒng)道數(shù)*/
int DeleteBatch();/*減少系統(tǒng)道數(shù)*/
int UnSuspendProc(int id);/*解除ID為id的進(jìn)程的掛起狀態(tài)*/
int UpdateBatchs();/*多道系統(tǒng)根據(jù)進(jìn)程鏈表進(jìn)行更新,并將執(zhí)行完畢的進(jìn)程刪除*/
int PassSeconds(int n);/*系統(tǒng)經(jīng)過n秒后計(jì)算數(shù)據(jù)并進(jìn)行優(yōu)先級(jí)調(diào)度*/
/****************************************************************/
/*各函數(shù)的定義*/
/****************************************************************/
int InitBatchs(int n)
{
int i;
for (i=0; in; ++i)
{
AddBatch();
}
return (UpdateBatchs());
}
int InsertProc(int prior, int time)
{
static int sysid = 0;/*該系統(tǒng)已經(jīng)加入過多少進(jìn)程,此值將是新進(jìn)程的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)/*如果進(jìn)程隊(duì)列為空*/
{
ProcQueue = pcb;
pcb-Next = NULL;
return TRUE;
}
last = ProcQueue;
now = last-Next;
if (pcb-Prior last-Prior)/*pcb將排在隊(duì)頭*/
{
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;/*上次遍歷是否無交換產(chǎn)生*/
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果鏈表中無進(jìn)程或只有一個(gè)進(jìn)程*/
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)/*如果只有最后一個(gè)后備空閑進(jìn)程*/
return FALSE;/**/
last = ProcQueue;
now = last-Next;
while (now-Next != NULL)/*查找到最后一個(gè)進(jìn)程,該進(jìn)程必定是后備空閑進(jìn)程*/
{
last = now;
now = last-Next;
}
if (now==NULL || now-PID=0)/*未查找到后備進(jìn)程*/
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)/*如果該進(jìn)程是運(yùn)行中的一般進(jìn)程并已執(zhí)行完畢*/
{
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;/*經(jīng)過多少時(shí)間段*/
time = time%TIME;
}
while (bt != NULL)/*更新進(jìn)程運(yùn)行時(shí)間*/
{
if (bt-pcb-PID=0)
{
bt-pcb-Time -= n;
if (bt-pcb-Time = 0)/*進(jìn)程結(jié)束*/
{
ProcEnd = TRUE;
}
}
bt = bt-Next;
}
if (i 0)
{
while (pcb != NULL)/*更新進(jìn)程優(yōu)先權(quán)(動(dòng)態(tài)優(yōu)先權(quán))*/
{
if (pcb-Sts == RUN pcb-PID=0)/*運(yùn)行的進(jìn)程優(yōu)先權(quán)降低*/
{
pcb-Prior -= i;
if (pcb-Prior 0)
pcb-Prior = 0;
}
else if (pcb-Sts == SUSPEND pcb-PID=0)/*掛起的進(jìn)程優(yōu)先權(quán)升高*/
pcb-Prior += i;
pcb = pcb-Next;
}
}
if (i0)
SortProcQueue();/*如果優(yōu)先級(jí)有變動(dòng)則重新排序*/
if (ProcEnd || i0)
{
UpdateBatchs();/*更新多道進(jìn)程*/
}
return TRUE;
}
/****************************************************************/
/*圖形界面相關(guān)函數(shù)*/
/****************************************************************/
/*表格的單位寬度和高度*/
#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");/*系統(tǒng)多道列表頭*/
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");/*掛起隊(duì)列列表頭*/
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");/*就緒隊(duì)列列表頭*/
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()/*繪制系統(tǒng)數(shù)據(jù)*/
{
int numRun=0, numSus=0, numRed=0;/*運(yùn)行掛起和就緒的進(jìn)程各有多少*/
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)/*該進(jìn)程為正在運(yùn)行的進(jìn)程*/
{
putimage(x-WIDTH+1,y+1,black,COPY_PUT);
gprintf(x-WIDTH+2,y+2,"%d",numRun);
}
if (now-PID=0)/*該進(jìn)程不是后備進(jìn)程*/
{
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)/*如果位置不在第一位(回車鍵不準(zhǔn)第一個(gè)輸入)*/
{
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位數(shù)*/
{ 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();
/*******輸入優(yōu)先級(jí)**********/
if (!DlgGetNum(buf,l,t,r,b,FALSE))
goto exit;
prior = atoi(buf);
/*******輸入時(shí)間**********/
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, ... )/*圖形系統(tǒng)中的格式輸出*/
{
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函數(shù)*/
int main()
{
clock_t start=0, end=0;
char kb;
InitBatchs(3);/*初始化為3道系統(tǒng)*/
InitGraph();
while (1)
{
start = end = clock();
while (!kbhit())
{
start = clock();
if ((start-end)/18.2 = 1)/*時(shí)間過了一秒*/
{
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;
}
創(chuàng)建一個(gè)名字為“ReportCard”的類,然后用下邊的內(nèi)容全部替換掉,你會(huì)成為全班最亮的仔。
import java.util.HashMap;
/**
* 學(xué)生成績(jī)單
*/
public class ReportCard {
public static void main(String[] args) {
ReportCard reportCard = new ReportCard("張三", "070602213");
reportCard.set("語(yǔ)文", 80.0);
reportCard.set("數(shù)學(xué)", 59.5);
reportCard.set("英語(yǔ)", 66.0);
reportCard.set("java", 80, 99.0);
reportCard.set("數(shù)據(jù)庫(kù)", 80, 66.0);
reportCard.set("毛概", null);
System.out.println(reportCard.getStudentName() + "語(yǔ)文分?jǐn)?shù):" + reportCard.get("語(yǔ)文"));
System.out.println(reportCard.getStudentName() + "數(shù)學(xué)考核結(jié)果:" + (reportCard.isPassed("數(shù)學(xué)") ? "合格" : "不合格"));
System.out.println(reportCard.getStudentName() + "期末是否掛科:" + (reportCard.isAllPassed() ? "否" : "是"));
}
// 學(xué)生姓名
private String studentName;
// 學(xué)生學(xué)號(hào)
private String studentNumber;
// 成績(jī)單
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;
}
}
}
/**
* 課程成績(jī)
*/
class CourseResult extends Course{
// 學(xué)生成績(jī)
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;
}
}
網(wǎng)站欄目:先驗(yàn)算法java實(shí)現(xiàn)代碼 先驗(yàn)算法java實(shí)現(xiàn)代碼轉(zhuǎn)換
瀏覽地址:http://jinyejixie.com/article4/dodohie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、做網(wǎng)站、建站公司、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容