can you help me to convert this code from Java to C import java.util.*; import j
ID: 3714214 • Letter: C
Question
can you help me to convert this code from Java to C
import java.util.*;
import java.io.*;
public class Puzzle {
public static void main(String[] args) throws FileNotFoundException ,IOException {
int choice , row , col , k=0 ;
String num ;
String puzzle [][]=load_board(k);
Scanner scan = new Scanner (System.in);
do {
drow_board(load_board (k),k);
System.out.println("1.Insert");
System.out.println("2.Delete");
System.out.println("3.Reset");
System.out.println("4.Check");
System.out.println("5.Exit");
System.out.print("Please select your choice: ");
choice = scan.nextInt();
FileOutputStream outfile = new FileOutputStream ("puzzle.txt");
PrintWriter pw = new PrintWriter(outfile);
switch (choice) {
case 1 :
System.out.print("Enter the row and colum of the cell [0-5]: ");
row= scan.nextInt();
col = scan.nextInt();
if (puzzle[row][col].contains("0")) {
System.out.print("Enter a number [0-9]: ");
num = scan.next();
puzzle[row][col]= num ;
for(int i = 0 ; i<puzzle.length ; i++) {
for(int j = 0 ; j<puzzle[i].length ; j++) {
pw.print(puzzle[i][j]+" ");
}
pw.println();
}
pw.close();
}else {
System.out.println("Error");
System.exit(1);
}
break;
case 2 :
System.out.print("Enter the row and colum of the cell [0-5]: ");
row= scan.nextInt();
col = scan.nextInt();
puzzle[row][col]= "0" ;
for(int i = 0 ; i<puzzle.length ; i++) {
for(int j = 0 ; j<puzzle[i].length ; j++) {
pw.print(puzzle[i][j]+" ");
}
pw.println();
}
pw.close();
break ;
case 3 :
k =-1;
puzzle = load_board(k);
break ;
case 4 :
if (puzzle[0][0].contains("8") && puzzle[0][2].contains("9") && puzzle[0][4].contains("4")&&
puzzle[2][0].contains("6") && puzzle[2][2].contains("7") && puzzle[2][4].contains("3")
&&puzzle[4][0].contains("2") && puzzle[4][2].contains("5") && puzzle[4][4].contains("1"))
System.out.println("Good");
else
System.out.println("Fail");
System.exit(1);
break ;
case 5 :
System.exit(1);
}
k++;
}while(choice != 0);
}
public static String [][] load_board (int k) throws FileNotFoundException {
String [][] puzzle = new String [6][6];
if (k==0 || k==-1) {
FileInputStream infile = new FileInputStream ("data.txt");
Scanner scan = new Scanner(infile);
for (int i = 0 ; i<puzzle.length ; i++) {
for(int j = 0 ; j<puzzle[i].length ; j++) {
puzzle[i][j]= scan.next();
}
}
scan.close();
}
else {
FileInputStream infile = new FileInputStream ("puzzle.txt");
Scanner scan = new Scanner(infile);
for (int i = 0 ; i<puzzle.length ; i++) {
for(int j = 0 ; j<puzzle[i].length ; j++) {
}
}
scan.close();
}
return puzzle;
}
public static void drow_board (String puzzle [][] , int k) throws FileNotFoundException {
load_board (k);
System.out.println("---------------------------------------------");
for(int i = 0 ; i<puzzle.length ; i++) {
for (int j = 0 ; j<puzzle[i].length ; j++) {
if(puzzle[i][j].contains("0"))
System.out.print("| ");
else if(j==puzzle[i].length-1)
System.out.println("| "+puzzle[i][j]+"|");
else
System.out.print("| "+puzzle[i][j]+" ");
}
System.out.println("--------------------------------------------");
}
}
}
?
Explanation / Answer
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
int choice , row , col , k=0 ;
String num ;
String puzzle [][]=load_board(k);
Scanner scan = new Scanner (System.in);
do {
drow_board(load_board (k),k);
Console.WriteLine("1.Insert");
Console.WriteLine("2.Delete");
Console.WriteLine("3.Reset");
Console.WriteLine("4.Check");
Console.WriteLine("5.Exit");
Console.WriteLine("Please select your choice: ");
choice = scan.nextInt();
FileOutputStream outfile = new FileOutputStream ("puzzle.txt");
PrintWriter pw = new PrintWriter(outfile);
switch (choice) {
case 1 :
Console.WriteLine("Enter the row and colum of the cell [0-5]: ");
row= scan.nextInt();
col = scan.nextInt();
if (puzzle[row][col].contains("0")) {
Console.WriteLine("Enter a number [0-9]: ");
num = scan.next();
puzzle[row][col]= num ;
for(int i = 0 ; i<puzzle.length ; i++) {
for(int j = 0 ; j<puzzle[i].length ; j++) {
pw.print(puzzle[i][j]+" ");
}
pw.println();
}
pw.close();
}else {
Console.WriteLine("Error");
System.exit(1);
}
break;
case 2 :
Console.WriteLine("Enter the row and colum of the cell [0-5]: ");
row= scan.nextInt();
col = scan.nextInt();
puzzle[row][col]= "0" ;
for(int i = 0 ; i<puzzle.length ; i++) {
for(int j = 0 ; j<puzzle[i].length ; j++) {
pw.print(puzzle[i][j]+" ");
}
pw.println();
}
pw.close();
break ;
case 3 :
k =-1;
puzzle = load_board(k);
break ;
case 4 :
if (puzzle[0][0].contains("8") && puzzle[0][2].contains("9") && puzzle[0][4].contains("4")&&
puzzle[2][0].contains("6") && puzzle[2][2].contains("7") && puzzle[2][4].contains("3")
&&puzzle[4][0].contains("2") && puzzle[4][2].contains("5") && puzzle[4][4].contains("1"))
Console.WriteLine("Good");
else
Console.WriteLine("Fail");
System.exit(1);
break ;
case 5 :
System.exit(1);
}
k++;
}while(choice != 0);
}
}
public static String [][] load_board (int k) throws FileNotFoundException {
String [][] puzzle = new String [6][6];
if (k==0 || k==-1) {
using (StreamReader reader = new StreamReader("not-there.txt"))
{
reader.ReadToEnd();
}
Scanner scan = new Scanner(infile);
for (int i = 0 ; i<puzzle.length ; i++) {
for(int j = 0 ; j<puzzle[i].length ; j++) {
puzzle[i][j]= scan.next();
}
}
scan.close();
}
else {
FileInputStream infile = new FileInputStream ("puzzle.txt");
Scanner scan = new Scanner(infile);
for (int i = 0 ; i<puzzle.length ; i++) {
for(int j = 0 ; j<puzzle[i].length ; j++) {
}
}
scan.close();
}
return puzzle;
}
public static void drow_board (String puzzle [][] , int k) throws FileNotFoundException {
load_board (k);
Console.WriteLine("---------------------------------------------");
for(int i = 0 ; i<puzzle.length ; i++) {
for (int j = 0 ; j<puzzle[i].length ; j++) {
if(puzzle[i][j].contains("0"))
Console.WriteLine("| ");
else if(j==puzzle[i].length-1)
Console.WriteLine("| "+puzzle[i][j]+"|");
else
Console.WriteLine("| "+puzzle[i][j]+" ");
}
Console.WriteLine("--------------------------------------------");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.