栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从网页输入Java或C程序?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从网页输入Java或C程序?

对于Java,我使用了Jsp和Servlet。

Index.jsp

<body><form action="sample" method="post">    <h1>Travelling Salesman Problem</h1>    <input placeholder="Number of Cities" type="text" name="cities" required="">    <input placeholder="Matrix" type="text" name="matrix" required="">    <button>Submit</button></form></body>

Servlet Sample.java

import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;import java.util.InputMismatchException;import java.util.Scanner;import java.util.Stack;import static java.lang.System.out;public class sample extends HttpServlet {    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        String city = request.getParameter("cities");        String numbers = request.getParameter("matrix");        int[] abc=new int[100];        String[] args = new String[2];        args[0] = city;        args[1] = numbers;        abc = TSPNearestNeighbour.main(args);  //passing the two arguments to my java class        PrintWriter out = response.getWriter();        for (int i=0;i<=abc.length;i++) { out.println(abc[i]);        }    }}

TSPNearestNeighbour.Java(Java类)

import java.util.InputMismatchException;import java.util.Stack;public class TSPNearestNeighbour{    private int numberOfNodes;    private Stack<Integer> stack;    public TSPNearestNeighbour()    {        stack = new Stack<Integer>();    }    public int[] tsp(int[][] adjacencyMatrix)    {        int[] arr= new int[100];        numberOfNodes = adjacencyMatrix[1].length - 1;        int[] visited = new     int[numberOfNodes + 1];        visited[1] = 1;        stack.push(1);        int element, dst = 0, i;        int min = Integer.MAX_VALUE;        boolean minFlag = false;        System.out.print(1 + "t");        while (!stack.isEmpty())        { element = stack.peek(); i = 1; min = Integer.MAX_VALUE; while (i <= numberOfNodes) {     if  (adjacencyMatrix[element][i] > 1 && visited[i] == 0)     {         if (min > adjacencyMatrix[element][i])         {  min = adjacencyMatrix[element][i];  dst = i;  minFlag = true;         }     }     i++; } if (minFlag) {     visited[dst] = 1;     stack.push(dst);     System.out.print(dst +"t");     minFlag = false;     continue; }stack.pop();        }        return arr;// ignore this line,it's regarding my logic    }    public static int[] main (String[] args) {        if (args.length < 2) { System.out.println("Two arguments required <city> <numbers>"); System.exit(-1);        }        int number_of_nodes;        number_of_nodes = Integer.parseInt(args[0]);        String[] splitText = args[1].split(" +");        int[] mat = new int[splitText.length];        for (int i = 0; i < splitText.length; i++) { mat[i] = Integer.parseInt(splitText[i]); // since we are passing the parameters to matrix,we convert every value from string type to integer        }        int[] abc = new int[100];        try { int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1]; int count = 0; for (int i = 1; i <= number_of_nodes; i++) {     for (int j = 1; j <= number_of_nodes; j++) {         if (count == mat.length)  break;         adjacency_matrix[i][j] = mat[(i - 1) * number_of_nodes + (j - 1)];//matrix input         count++;     } } for (int i = 1; i <= number_of_nodes; i++) {     for (int j = 1; j <= number_of_nodes; j++) {         if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0) {  adjacency_matrix[j][i] = 1;         }     } } System.out.println("the citys are visited as follows"); TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour(); abc = tspNearestNeighbour.tsp(adjacency_matrix);        } catch (InputMismatchException inputMismatch) { System.out.println("Wrong Input format");        }        return abc;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/435697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号