
04-Mar-2005, 13:08
|
|
New Member
|
|
Join Date: Mar 2005
Posts: 10
|
|
|
Convert Java to C++???
I have a program that I wrote in Java and need it converted to C++. It is a airplane seating assignment that uses arrays. Im so clueless when it comes to C++ arrays. Can someone help me by recommending a better converter then Microsoft's JCLA...its just not cutting it.
JAVA Code:
//Chapter 9: Programming Exercise 12
import java.io.*;
public class Ch9_PrExercise12
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException
{
char[][] seatPlan = new char[13][6];
char resp;
initiailzeSeatPlan(seatPlan);
showMenu(seatPlan);
System.out.println("To reserve a seat enter Y/y(Yes), N/n(No): ");
resp = keyboard.readLine().charAt(0);
System.out.println();
while(resp == 'y' || resp == 'Y')
{
assignSeat(seatPlan);
showMenu(seatPlan);
System.out.println("Reserve another seat Y/y(Yes), N/n(No): ");
resp = keyboard.readLine().charAt(0);
System.out.println();
}
}
public static void initiailzeSeatPlan(char[][] sPlan)
{
int i, j;
for(i = 0; i < sPlan.length; i++)
for(j = 0; j < sPlan[0].length; j++)
sPlan[i][j] = '*';
}
public static void showSeatAssignments(char[][] sPlan)
{
int i, j;
System.out.println(" A B C D E F");
for(i = 0; i < sPlan.length; i++)
{
if( i < 9)
System.out.print("Row " + (i+1) + " ");
else
System.out.print("Row " + (i+1) + " ");
for(j = 0; j < sPlan[0].length; j++)
{
System.out.print(sPlan[i][j] + " ");
if(j == 2)
System.out.print(" ");
}
System.out.println();
}
System.out.println("* -- available seat");
System.out.println("X -- occupied seat");
System.out.println();
}
public static void assignSeat(char[][] sPlan) throws IOException
{
char ticketType;
char resp;
System.out.print("Enter ticket type: F/f (first class) "
+ " E/e (Economy class): ");
ticketType = keyboard.readLine().charAt(0);
System.out.println();
switch(ticketType)
{
case 'f':
case 'F': if(!isFirstClassFull(sPlan))
assignFirstClassSeat(sPlan);
else
{
System.out.println("Sorry!!! First Class is Full");
System.out.print("Press Y/y to continue: ");
resp = keyboard.readLine().charAt(0);
System.out.println();
}
break;
case 'e':
case 'E': if(!isNonSmokingEconomicFull(sPlan) ||
!isSmokingEconomicFull(sPlan))
assignEconomicSeat(sPlan);
else
{
System.out.println("Sorry!!! Economic Class is Full");
System.out.print("Press Y/y to continue: ");
resp = keyboard.readLine().charAt(0);
System.out.println();
}
}
showSeatAssignments(sPlan);
}
public static void showMenu(char[][] sPlan)
{
System.out.println("This program assigns seats for a commercial airplane.");
System.out.println("The current seat assignments is as follows.");
showSeatAssignments(sPlan);
System.out.println("Rows 1 and 2 are for first class passengers");
System.out.println("Rows 1 through 7 are nonsmoking");
}
public static boolean isFirstClassFull(char[][] sPlan)
{
int i, j;
int assignedSeats = 0;
for(i = 0; i < 2; i++)
for(j = 0; j < sPlan[0].length; j++)
if(sPlan[i][j] == 'X')
assignedSeats++;
return (assignedSeats == 2 * sPlan[0].length);
}
public static boolean isNonSmokingEconomicFull(char[][] sPlan)
{
int i, j;
int assignedSeats = 0;
for(i = 2; i < 7; i++)
for(j = 0; j < sPlan[0].length; j++)
if(sPlan[i][j] == 'X')
assignedSeats++;
return (assignedSeats == 5 * sPlan[0].length);
}
public static boolean isSmokingEconomicFull(char[][] sPlan)
{
int i, j;
int assignedSeats = 0;
for(i = 7; i < 13; i++)
for(j = 0; j < sPlan[0].length; j++)
if(sPlan[i][j] == 'X')
assignedSeats++;
return (assignedSeats == 6 * sPlan[0].length);
}
public static void selectSeatNumber(int startRows, int endRows,
IntClass rowNo, CharClass columnNo) throws IOException
{
System.out.print("Enter Row number " + startRows + " - "
+ endRows + ": ");
rowNo.setNum(Integer.parseInt(keyboard.readLine()));
System.out.println();
while(rowNo.getNum() < startRows || rowNo.getNum() > endRows)
{
System.out.print("Enter Row number " + startRows + " - "
+ endRows + ": ");
rowNo.setNum(Integer.parseInt(keyboard.readLine()));
System.out.println();
}
System.out.print("Enter seat number (A - F): ");
columnNo.setChar(keyboard.readLine().charAt(0));
System.out.println();
while(columnNo.getChar() < 'A' || columnNo.getChar() > 'F')
{
System.out.print("Enter seat number (A - F): ");
columnNo.setChar(keyboard.readLine().charAt(0));
System.out.println();
}
}
public static void assignFirstClassSeat(char[][] sPlan) throws IOException
{
IntClass rowNum = new IntClass();
CharClass columnPos = new CharClass();
if(!isFirstClassFull(sPlan))
{
selectSeatNumber(1, 2, rowNum, columnPos);
while(sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] != '*')
{
System.out.println("*#*#*#*# This seat is occupied *#*#*#*#");
System.out.println("Make another selection");
showSeatAssignments(sPlan);
selectSeatNumber(1, 2, rowNum, columnPos);
}
sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] = 'X';
System.out.println("This seat is reserved for you");
}
else
System.out.println("Sorry!!! First Class is Full");
}
public static void assignEconomicSeat(char[][] sPlan) throws IOException
{
char seatType;
if(!isNonSmokingEconomicFull(sPlan) &&
isSmokingEconomicFull(sPlan))
assignSeatNonSmokingEconomic(sPlan);
else
if(isNonSmokingEconomicFull(sPlan) &&
!isSmokingEconomicFull(sPlan))
assignSeatSmokingEconomic(sPlan);
else
if(!isNonSmokingEconomicFull(sPlan) &&
!isSmokingEconomicFull(sPlan))
{
System.out.print("Enter seat type: smoking (S/s), nonsmoking (N/n): ");
seatType = keyboard.readLine().charAt(0);
System.out.println();
while(seatType != 'S' && seatType != 's' &&
seatType != 'N' && seatType != 'n')
{
System.out.println("Invalid seat type.");
System.out.print("Enter smoking (S/s), nonsmoking (N/n): ");
seatType = keyboard.readLine().charAt(0);
System.out.println();
}
if(seatType == 'N' || seatType == 'n')
assignSeatNonSmokingEconomic(sPlan);
else
assignSeatSmokingEconomic(sPlan);
}
else
System.out.println("Economic Class is Full");
}
public static void assignSeatNonSmokingEconomic(char[][] sPlan) throws IOException
{
IntClass rowNum = new IntClass();
CharClass columnPos = new CharClass();
if(!isNonSmokingEconomicFull(sPlan))
{
selectSeatNumber(3, 7, rowNum, columnPos);
while(sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] != '*')
{
System.out.println("*#*#*#*# This seat is occupied *#*#*#*#");
System.out.println("Make another selection");
showSeatAssignments(sPlan);
selectSeatNumber(3, 7, rowNum, columnPos);
}
sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] = 'X';
System.out.println("This seat is reserved for you");
}
else
System.out.println("Non smoking section of economic class is Full");
}
public static void assignSeatSmokingEconomic(char[][] sPlan) throws IOException
{
IntClass rowNum = new IntClass();
CharClass columnPos = new CharClass();
if(!isSmokingEconomicFull(sPlan))
{
selectSeatNumber(8, 13, rowNum, columnPos);
while(sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] != '*')
{
System.out.println("*#*#*#*# This seat is occupied *#*#*#*#");
System.out.println("Make another selection");
showSeatAssignments(sPlan);
selectSeatNumber(8, 13, rowNum, columnPos);
}
sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] = 'X';
System.out.println("This seat is reserved for you");
}
else
System.out.println("Smoking section of economic class is Full");
}
}
Last edited by LuciWiz : 26-Mar-2006 at 10:14.
Reason: Please insert your Java code between [java] & [/java] tags
|