Hallo zusammen,
Ich habe ein Java Programm geschrieben, dass mithilfe einer Array List Kontakte speichern, löschen, verschieben, ... usw. kann. Nun will ich das gleiche Programm in C umsetzen. Bisher habe ich nur in Java Programmiert also fällt mir dies nicht gerade leicht.
Hier zumal das soweit fertige Java Programm (bin natürlich auch hier offen für verbesserungen):
Hier habe ich es so gemacht, dass ich verschiedene Methoden mithilfe von switch/case ansteuern konnte durch ein simples Menü.
Ich habe dies auch in C Probiert und bin kläglich daran gescheitert.
Kann mir da jemand Tipps geben die mir vielleicht weiterhelfen könnten?
Hier mein bisheriger C Code (ist bisher noch nicht viel):
Vielen Dank vorab
Ich habe ein Java Programm geschrieben, dass mithilfe einer Array List Kontakte speichern, löschen, verschieben, ... usw. kann. Nun will ich das gleiche Programm in C umsetzen. Bisher habe ich nur in Java Programmiert also fällt mir dies nicht gerade leicht.
Hier zumal das soweit fertige Java Programm (bin natürlich auch hier offen für verbesserungen):
Java:
import java.io.Console;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Basics {
static Scanner sc = new Scanner(System.in);
static List<String> ContactList = new ArrayList<String>();
static String ContactName;
static String ContactNameInsert;
static boolean beenden = false;
//***********************************************************************************************************************************************
public static void main(String args[]) { // main Methode
ausgabeMenu();
defineCase();
}
//***********************************************************************************************************************************************
public static void ausgabeMenu() { // Ausgabe des Menüs
System.out.println();
System.out.println();
System.out.println("****************************");
System.out.println("Contactlist app");
System.out.println("Commands:");
System.out.println("l - list contacts");
System.out.println("a - add contact");
System.out.println("c - clear list");
System.out.println("i - insert contact");
System.out.println("d - delete contact");
System.out.println("q - quit program");
System.out.println("h - print this help");
System.out.println("****************************");
System.out.println();
System.out.println();
}
//************************************************************************************************************************************************
public static void defineCase() {
while (beenden != true) {
System.out.println("Enter command: -h- for help.");
String command = sc.nextLine();
switch (command) {
case "a":
addContact();
break;
case "c":
clearList();
break;
case "h":
ausgabeMenu();
break;
case "d":
deleteContact();
break;
case "l":
listContacts();
break;
case "i":
insertContact();
break;
case "q":
quitProgramm();
default:
System.err.println("Command not available. Please enter -h- for help.");
}
}
//************************************************************************************************************************************************
}
public static void addContact() { // ein Kontakt wird erstellt und in der ArrayListe gespeichert.
System.out.println();
System.out.println();
System.out.println("********ADD CONTACT*************");
System.out.print("Please enter Contact Name:");
ContactName = sc.nextLine();
ContactList.add(ContactName);
System.out.println("Contact created successfully");
System.out.println("********ADD CONTACT*************");
System.out.println();
System.out.println();
}
// ************************************************************************************************************************************************
public static void clearList() {
if (!ContactList.isEmpty()) {
System.out.println();
System.out.println();
System.out.println("********CLEAR LIST*************");
ContactList.clear();
System.out.println("Contact list successfully cleared.");
System.out.println("********CLEAR LIST*************");
System.out.println();
System.out.println();
} else
System.err.print("No contacts have been saved yet. Please enter -a- to create a contact");
}
// ************************************************************************************************************************************************
public static void deleteContact() {// Ein Kontakt wird an beliebiger stelle entfernt.
try {
System.out.println();
System.out.println();
System.out.println("********DELETE CONTACT*************");
System.out.println("Which contact should be deleted? Please type in the Number.");
listContacts();
String ContactNumberStr = sc.next();
int ContactNumber = Integer.parseInt(ContactNumberStr);
ContactList.remove(ContactNumber);
System.out.println("Contact successfully deleted");
System.out.println("********DELETE CONTACT*************");
System.out.println();
System.out.println();
} catch (IndexOutOfBoundsException e) {
System.err.println(" ERROR: Contact does not exist.");
} catch (NumberFormatException e) {
System.err.println("ERROR: Please enter a valid number");
}
}
// ************************************************************************************************************************************************
public static void listContacts() { // Der Inhalt der ArrayList wird ausgegeben.
System.out.println();
System.out.println();
System.out.println("********CONTACT LIST*************");
if (!ContactList.isEmpty()) {
for (int i = 0; i < ContactList.size(); i++) {
System.out.println(i + " " + ContactList.get(i));
}
} else
System.err.println("No contacts have been saved yet. Please enter -a- to create a contact");
System.out.println("********CONTACT LIST*************");
System.out.println();
System.out.println();
}
// ************************************************************************************************************************************************
public static void quitProgramm() { // Das Programm wird beendet und die ArrayListe wird geleert.
System.out.println();
System.out.println();
System.out.println("********************************");
System.out.println("The program was terminated");
System.out.println("********************************");
System.out.println();
System.out.println();
ContactList.clear();
beenden = true;
System.exit(1);
}
// ************************************************************************************************************************************************
public static void insertContact() { // Kontakte werden auf einer bestimmten Position gespeichert.
if (ContactList.size() != 0) {
try {
System.out.println();
System.out.println();
System.out.println("********INSERT CONTACT*************");
System.out.print("Please enter Contact Name:");
ContactNameInsert = sc.nextLine();
System.out.println("Where do you want to insert the contact?");
listContacts();
String ContactOrderNumberStr = sc.next();
int ContactOrderNumber = Integer.parseInt(ContactOrderNumberStr);
ContactList.add(ContactOrderNumber, ContactNameInsert);
System.out.println("Contact successfully added.");
System.out.println("********INSERT CONTACT*************");
System.out.println();
System.out.println();
} catch (IndexOutOfBoundsException e) {
System.err.println(" ERROR: Contact does not exist.");
} catch (NumberFormatException e) {
System.err.println("ERROR: Please enter a valid number");
}
} else
System.err.println("No contacts have been saved yet. Please add contacts first");
}
// ************************************************************************************************************************************************
}
Hier habe ich es so gemacht, dass ich verschiedene Methoden mithilfe von switch/case ansteuern konnte durch ein simples Menü.
Ich habe dies auch in C Probiert und bin kläglich daran gescheitert.
Kann mir da jemand Tipps geben die mir vielleicht weiterhelfen könnten?
Hier mein bisheriger C Code (ist bisher noch nicht viel):
C:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
//float ContactList[];
//int beenden = 1;
//char command[2];
//***********************************************************************************************************************************************
void defineCase() {
}
//***********************************************************************************************************************************************
void ausgabeMenu() {
printf("*************************\n");
printf("Contactlist app\n");
printf("Commands:\n");
printf("l - list contacts\n");
printf("a - add contact\n");
printf("c - clear list\n");
printf("i - insert contact\n");
printf("d - delete contact\n");
printf("q - quit programm\n");
printf("h - print this help\n");
printf("**************************\n");
}
//***********************************************************************************************************************************************
void listContacts() {
}
//***********************************************************************************************************************************************
void addContact() {
}
//***********************************************************************************************************************************************
void clearList() {
}
//***********************************************************************************************************************************************
int main(int argc, char *argv[]) {
ausgabeMenu();
defineCase();
}
//***********************************************************************************************************************************************
Vielen Dank vorab