问题
i have to do a order system for a fastfood restaurant (its just a school project). I have finished the tool already.
But the teacher told us to use getch instead of scanf.
But i dont understand, how to implent it.. Can i just replace my scanf in the code with getch? I dont need the Echo from Scanf and dont need confirmation with the ENTER-Key.
The task was:
Create a CMD-Tool in the Programming Language C. NO GUI! The Solution must include the commands „malloc“, „realloc“, „getch“, and free. The CMD Tool is a order process for a imaginary fast food restaurant. The menu looks like this:
A „Big Burger“ 4,90€
B „Little Burger“ 1,90€
C „Wrap“ 4,90€
D „Salat“ 2,90€
E „Frites“ 1,90€
F „Softdrink“ 1,90€
G „Watter“ 1,20€
H „Ketchup“ 0,30€
The cashier can type in the order with the keyboard.. If someone order Meal A and Meal C. He types the Letter a or A and C or c. Lower and Uppercase letters doesnt matter.
Every Order should be displayed in a single line.
After the Order is complete, the tools should show a summary of the order. All the meals that was ordered and a summary of the price. The order should be completed by pressing the letter X.
With the letter Z you can undone the order.
My Tool with Scanf:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
int main()
{
char *order;
char *Total_Order;
int end = 1;
start: //Festlegung goto Label, bei Bestellabbruch.
order=(char*)malloc(1*sizeof(char));
Total_Order = (char*)calloc(1, sizeof(char));
// float für Kommazahl, Variable entspricht Preis vom Essen.
float A=4.90 ,B=1.90 ,C=4.90 ,D=2.90 ,E=1.90 ,F=1.90 ,G=1.20 , H=0.30;
printf("Order:\n");
printf(" A Big Burger 4.90EUR \n B Little Burger 1.90EUR \n C Wrap 4.90EUR \n D Salat 2.90EUR \n E Frites 1.90EUR \n F Softdrink 1.90EUR \n G Water 1.20EUR \n H Ketchup 0.30EUR \n");
int n=1;
do {
scanf(" %c",order);
//printf("%c\n",*order);
if(n==1){
printf("Enter Order:\n");
}
char temp=*order;
temp=tolower(temp);
switch (temp) {
case 'a':
printf("A Big Burge 4.90EUR \n");
Total_Order[n-1]='a';
break;
case 'b':
printf("B Little Burger 1.90EUR \n");
Total_Order[n-1]='b';
break;
case 'c':
printf("C Wrap 4.90EUR \n");
Total_Order[n-1]='c';
break;
case 'd':
printf("D Salat 2.90EUR \n");
Total_Order[n-1]='d';
break;
case 'e':
printf("E Frites 1.90EUR \n");
Total_Order[n-1]='e';
break;
case 'f':
printf("F Softdrink 1.90EUR \n");
Total_Order[n-1]='f';
break;
case 'g':
Total_Order[n-1]='g';
printf("G Watter 1.20EUR \n");
break;
case 'h':
Total_Order[n-1]='h';
printf("H Ketchup 0.30EUR \n");
break;
default:
printf(" ");
break;
}
if(*order == 'x' || *order == 'X'){
end=0;
}
if(*order == 'z' || *order == 'Z'){
system ("CLS");
free(Total_Order);
free(order);
goto start; // Sprung zu start:
}
n++;
Total_Order = (char*)realloc(Total_Order, n * sizeof(char));
}while (end== 1);
float t=0.0;
int i=0;
printf("Finished Order:\n");
for (i = 0; i < n; ++i) {
switch (Total_Order[i]) {
case 'a':
printf("A ,,Big Burge 4.90EUR \n");
Total_Order[n-1]='A';
t = t + A;
break;
case 'b':
printf("B ,,Little Burger 1.90EUR \n");
Total_Order[n-1]='B';
t = t + B;
break;
case 'c':
printf("C Wrap 4.90EUR \n");
Total_Order[n-1]='C';
t = t + C;
break;
case 'd':
printf("D Salat 2.90EUR \n");
Total_Order[n-1]='D';
t = t + D;
break;
case 'e':
printf("E Frites 1.90EUR \n");
Total_Order[n-1]='E';
t = t + E;
break;
case 'f':
printf("F Softdrink 1.90EUR \n");
Total_Order[n-1]='F';
t = t + F;
break;
case 'g':
Total_Order[n-1]='G';
printf("G Watter 1.20EUR \n");
t = t + G;
break;
case 'h':
Total_Order[n-1]='H';
printf("H Ketchup 0.30EUR \n");
t = t + H;
break;
default:
printf(" ");
t = t + 0.0;
break;
}
}
printf("Summe %f",t);
free(Total_Order);
free(order);
return 0;
}
#include <stdio.h> // Standard Input / Output
#include <stdlib.h> //Zur Umwandlung von Zahlen, für Speicherverwaltung und ähnliche Aufgaben
#include <ctype.h> //Einzelne Zeichen überprüfen und umwandeln
#include <conio.h> // Konsole Input / Output getch
int main()
{
char *order; //Zeiger-Deklaration Bestellungen
char *Total_Order; //Zeiger-Deklaration Bestellungen Gesamt
int end = 1;
// goto = schlecht für Performance!!! Ersatz dafür?
start: //Festlegung goto Label, bei Bestellabbruch.
order=(char*)malloc(1*sizeof(char));
Total_Order = (char*)calloc(1, sizeof(char));
// float für Kommazahl, Variable entspricht Preis vom Essen.
float A=4.90 ,B=1.90 ,C=4.90 ,D=2.90 ,E=1.90 ,F=1.90 ,G=1.20 , H=0.30;
//Fragen: Euro-Zeichen als € Symbol
printf("Kundenbestellung:\n");
printf(" A Big Burger 4.90EUR \n B Little Burger 1.90EUR \n C Wrap 4.90EUR \n D Salat 2.90EUR \n E Frites 1.90EUR \n F Softdrink 1.90EUR \n G Water 1.20EUR \n H Ketchup 0.30EUR \n");
int n=1;
do {
scanf(" %c",order);
//printf("%c\n",*order);
if(n==1){
printf("Bestelleingabe:\n");
}
char temp=*order;
temp=tolower(temp);
switch (temp) {
case 'a':
printf("A Big Burge 4.90EUR \n");
Total_Order[n-1]='a';
break;
case 'b':
printf("B Little Burger 1.90EUR \n");
Total_Order[n-1]='b';
break;
case 'c':
printf("C Wrap 4.90EUR \n");
Total_Order[n-1]='c';
break;
case 'd':
printf("D Salat 2.90EUR \n");
Total_Order[n-1]='d';
break;
case 'e':
printf("E Frites 1.90EUR \n");
Total_Order[n-1]='e';
break;
case 'f':
printf("F Softdrink 1.90EUR \n");
Total_Order[n-1]='f';
break;
case 'g':
Total_Order[n-1]='g';
printf("G Watter 1.20EUR \n");
break;
case 'h':
Total_Order[n-1]='h';
printf("H Ketchup 0.30EUR \n");
break;
default:
printf(" ");
break;
}
if(*order == 'x' || *order == 'X'){
end=0;
}
if(*order == 'z' || *order == 'Z'){
system ("CLS");
free(Total_Order);
free(order);
goto start; // Sprung zu start:
}
n++;
Total_Order = (char*)realloc(Total_Order, n * sizeof(char));
}while (end== 1);
float t=0.0;
int i=0;
printf("Abgeschlossene Bestellung:\n");
for (i = 0; i < n; ++i) {
switch (Total_Order[i]) {
case 'a':
printf("A ,,Big Burge 4.90EUR \n");
Total_Order[n-1]='A';
t = t + A;
break;
case 'b':
printf("B ,,Little Burger 1.90EUR \n");
Total_Order[n-1]='B';
t = t + B;
break;
case 'c':
printf("C Wrap 4.90EUR \n");
Total_Order[n-1]='C';
t = t + C;
break;
case 'd':
printf("D Salat 2.90EUR \n");
Total_Order[n-1]='D';
t = t + D;
break;
case 'e':
printf("E Frites 1.90EUR \n");
Total_Order[n-1]='E';
t = t + E;
break;
case 'f':
printf("F Softdrink 1.90EUR \n");
Total_Order[n-1]='F';
t = t + F;
break;
case 'g':
Total_Order[n-1]='G';
printf("G Watter 1.20EUR \n");
t = t + G;
break;
case 'h':
Total_Order[n-1]='H';
printf("H Ketchup 0.30EUR \n");
t = t + H;
break;
default:
printf(" ");
t = t + 0.0;
break;
}
}
printf("Summe %f",t);
free(Total_Order);
free(order);
return 0;
}
I hope you have some tips for me. Thank you.
**Would there be a easier and cleaner way for my tool? **How could i make the foodname and price easier changeable?****
回答1:
You can use an array of struct
to describe the food.
I've added a sample for getch
[using fgetc
]. Compile with -DUSEGET=1
for that, or without to use scanf
Also, never use goto
No need for order
to be char *order
and do malloc
. A simple int order
is sufficient.
Having n
with origin 0 [instead of 1] simplifies things a bit.
I've refactored your program considerably:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//#include <conio.h>
typedef struct {
float price; // food price
char *name; // type of food
} food_t;
food_t foodlist[] = {
{ .price = 4.90, .name = "Big Burger" },
{ .price = 1.90, .name = "Little Burger" },
{ .price = 4.90, .name = "Wrap" },
{ .price = 2.90, .name = "Salat" },
{ .price = 1.90, .name = "Frites" },
{ .price = 1.90, .name = "Softdrink" },
{ .price = 1.20, .name = "Water" },
{ .price = 0.30, .name = "Ketchup" },
};
#define FOOD_COUNT (sizeof(foodlist) / sizeof(foodlist[0]))
#define ALLFOOD(_food,_list) \
_food = &_list[0]; _food < &_list[FOOD_COUNT]; ++_food
int
scanchar(void)
{
int temp;
while (1) {
temp = fgetc(stdin);
if (temp == EOF)
break;
if (temp == ' ')
continue;
if (temp != '\n')
break;
}
return temp;
}
int
main(void)
{
food_t *food;
int order;
char *Total_Order = NULL;
int end;
int temp;
int start = 1;
int n;
// Festlegung goto Label, bei Bestellabbruch.
while (start) {
start = 0;
n = 0;
end = 1;
printf("Order:\n");
for (order = 0, ALLFOOD(food,foodlist), order += 1)
printf(" %c %s %.2fEUR\n",order + 'A',food->name,food->price);
while (end == 1) {
if (n == 0) {
printf("Enter Order:\n");
}
#if USEGET
order = scanchar();
if (order == EOF)
break;
#else
scanf(" %c", &order);
#endif
temp = order;
temp = toupper(temp);
temp -= 'A';
if ((temp >= 0) && (temp < FOOD_COUNT)) {
Total_Order = realloc(Total_Order,n + 1);
Total_Order[n] = temp + 'A';
++n;
continue;
}
switch (order) {
case 'x':
case 'X':
end = 0;
break;
case 'z':
case 'Z':
system("CLS");
free(Total_Order);
Total_Order = NULL;
start = 1;
end = 0;
break;
}
}
}
float t = 0.0;
printf("Finished Order:\n");
for (int i = 0; i < n; ++i) {
order = Total_Order[i];
food = &foodlist[order - 'A'];
printf("%c ,,%s %.2fEUR\n",order,food->name,food->price);
t += food->price;
}
printf("Summe %f\n", t);
free(Total_Order);
Total_Order = NULL;
return 0;
}
来源:https://stackoverflow.com/questions/61031364/c-ordersystem-for-fastfood-project