从网页输入Java或C程序? [英] Taking input to a Java or C program from a web page?

查看:55
本文介绍了从网页输入Java或C程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施旅行商优化问题,并且使用Java和C程序对该问题进行了编程.该程序将矩阵作为输入并显示最佳路径.

I was implementing traveling salesman optimisation problem and i programmed the same using Java and C program. The program takes matrix as a input and displays the optimal path.

import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;

public class TSPNearestNeighbour {
    private int numberOfNodes;
    private Stack<Integer> stack;

    public TSPNearestNeighbour() {
        stack = new Stack<Integer>();
    }

    public void tsp(int adjacencyMatrix[][]) {
        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();
        }
    }

    public static void main(String... arg) {
        int number_of_nodes;
        Scanner scanner = null;
        try {
            System.out.println("Enter the number of nodes in the graph");
            scanner = new Scanner(System.in);
            number_of_nodes = scanner.nextInt();
            int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];
            System.out.println("Enter the adjacency matrix");


            for (int i = 1; i <= number_of_nodes; i++) {
                for (int j = 1; j <= number_of_nodes; j++) {
                    adjacency_matrix[i][j] = scanner.nextInt();
                }
            }
            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();
            tspNearestNeighbour.tsp(adjacency_matrix);
        } catch (InputMismatchException inputMismatch) {
            System.out.println("Wrong Input format");
        }
        scanner.close();
    }
}

C代码

#include<stdio.h>
#include<conio.h>
int a[10][10],visited[10],n,cost=0;

void get()
{
    int i,j;
    printf("Enter No. of Cities: ");
    scanf("%d",&n);
    printf("\nEnter Cost Matrix\n");
    for(i=0;i < n;i++)
    {
        printf("\nEnter Elements of Row # : %d\n",i+1);
        for( j=0;j < n;j++)
            scanf("%d",&a[i][j]);
        visited[i]=0;
    }
    printf("\n\nThe cost list is:\n\n");
    for( i=0;i < n;i++)
    {
        printf("\n\n");
        for(j=0;j < n;j++)
            printf("\t%d",a[i][j]);
    }
}

void mincost(int city)
{
    int i,ncity;
    visited[city]=1;    
    printf("%d -->",city+1);
    ncity=least(city);
    if(ncity==999)
    {
        ncity=0;
        printf("%d",ncity+1);
        cost+=a[city][ncity];
        return;
    }
    mincost(ncity);
}

int least(int c)
{
    int i,nc=999;
    int min=999,kmin;
    for(i=0;i < n;i++)
    {
        if((a[c][i]!=0)&&(visited[i]==0))
            if(a[c][i] < min)
            {
                min=a[i][c]+a[c][i];
                kmin=a[c][i];
                nc=i;
            }
    }
    if(min!=999)
        cost+=kmin;
    return nc;
}

void put()
{
    printf("\n\nMinimum cost:");
    printf("%d",cost);
}

void main()
{
    clrscr();
    get();
    printf("\n\nThe Path is:\n\n");
    mincost(0);
    put();
    getch();
}

这两个程序也可以正常工作.但是我想为此进行案例研究,我需要从网页上为这些矩阵输入信息.那么有没有办法做到这一点?对于Java或c,两者之一都可以.我也知道JSP和Web编程.我只想了解如何获取这些矩阵的输入并将其发送到服务器端.

The two programs are also working fine. But i want to implement a case study for it and i need to take input for those matrix from a webpage. So is there any way to do it so? For java or for c, either of one is fine. I know JSP and Web Programming too. I just want to how to take the input for those matrix and send it to server side.

推荐答案

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

For Java i used Jsp and Servlets.

Index.jsp

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

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类)

TSPNearestNeighbour.Java (Java Class)

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;
    }


}

这篇关于从网页输入Java或C程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆