帮助将其更改为面向功能的prog并向其添加数组 [英] help changing this to function oriented prog and add array to it

查看:60
本文介绍了帮助将其更改为面向功能的prog并向其添加数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hellow guys我不要求太多,但我不知道如何在此添加数组并将printf更改为面向函数如果你们都能给我一些关于数组和函数的好例子,接近我的订购系统我真的很感谢谢谢。



hellow guys im not asking too much but i dont have any idea how to add an array in this and change the printf to function oriented if you all can give me some good examples on both array and function oriented with close to my ordering system i would really appreciate it thanks.

 #include<stdio.h>
 #include<conio.h>

void main()
{
int code;
float price;
float totalf;
float payment;
float change;
int qty;
float mul;
int exceed;
float sub;
float subt;
int can;
int qty1;
int cel;
int yn;

totalf = 0;
qty1 = 0;

printf("\n\n\n\n\n");
printf("Code			Item				Price");
printf("\n");
printf("\n 1			Burger				10.50");
printf("\n");
printf("\n 2			Footlong			36.75");
printf("\n");
printf("\n 3			Shawarma			62.80");
printf("\n");
printf("\n 4			Fries				25.60");
printf("\n");
printf("\n 5			Juice				10.00");
printf("\n");
printf("\n 6			Shake				15.00");
printf("\n");
printf("\n Press . to exit after you order or just want to exit");


do
{
printf("\n\n");
printf("\nEnter Code: ");
scanf("%i", &code);
printf("Quantity: ");
scanf("%i", &qty);

qty1 = qty1+qty;
if(qty1<= 20)
{
switch(code)
{
case 1:
	printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Burger");
   printf("\nPrice per Item: 10.50");
  price = 10.50;
  mul = qty * price;
  totalf = totalf + mul;
  printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;

case 2:
	printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Footlong");
   printf("\nPrice per Item: 36.75");
	price = 36.75;
   mul = qty * price;
   printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  totalf = totalf + mul;
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;

case 3:
	printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Shawarma");
   printf("\nPrice per Item: 62.80");
	price = 62.80;
   mul = qty * price;
   printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  totalf = totalf + mul;
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;

case 4:
	printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Fries");
   printf("\nPrice per Item: 25.60");
	price = 25.60;
   mul = qty * price;
   printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  totalf = totalf + mul;
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;

case 5:
	printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Juice");
   printf("\nPrice per Item: 10.00");
	price = 10.00;
   mul = qty * price;
   printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  totalf = totalf + mul;
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;

case 6:
printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Shake");
   printf("\nPrice per Item: 15.00");
	price = 15.00;
   mul = qty * price;
   printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  totalf = totalf + mul;
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;

default:
printf("Invalid Input!");

}
}
else
{
switch(code)
{
case 1:
	printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Burger");
   printf("\nPrice per Item: 10.50");
  price = 10.50;
  mul = qty * price;
  totalf = totalf + mul;
  printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;

case 2:
	printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Footlong");
   printf("\nPrice per Item: 36.75");
	price = 36.75;
   mul = qty * price;
   printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  totalf = totalf + mul;
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;

case 3:
	printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Shawarma");
   printf("\nPrice per Item: 62.80");
	price = 62.80;
   mul = qty * price;
   printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  totalf = totalf + mul;
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;

case 4:
	printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Fries");
   printf("\nPrice per Item: 25.60");
	price = 25.60;
   mul = qty * price;
   printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  totalf = totalf + mul;
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;

case 5:
	printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Juice");
   printf("\nPrice per Item: 10.00");
	price = 10.00;
   mul = qty * price;
   printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  totalf = totalf + mul;
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;

case 6:
printf("Current Item Order: ");
	printf("%i", qty);
   printf(" Shake");
   printf("\nPrice per Item: 15.00");
	price = 15.00;
   mul = qty * price;
   printf("\nCurrent Item price: ");
  printf("%.2f", mul);
  totalf = totalf + mul;
  printf("\nCurrent Order Total: ");
  printf("%.2f", totalf);
  break;
default:
printf("Invalid Input!");
}
printf("\nExceeded Maximum Order");
}
}while(totalf < 1500.00 && qty1 < 20 );

if(totalf == 1500.00)
{
printf("\n\n");
printf("\nThis is the maximum amount to be ordered!");
}
if(qty1 == 20 || totalf == 1500.00)
{
printf("\n");
printf("\nThis will be your last order!");
}

if (totalf > 1500.00 || qty1 > 20)
{
printf("\n\n");
printf("\nYour Order Exceeded the Maximum Amount Allowed");
printf("\nPress 0 to cancel your order or Press 9 to deduct your order: ");
scanf("%i", &exceed);

switch(exceed)
{
case 9:
qty1;
do
{
printf("\nHow Much Amount  will be cancelled? ");
printf("\nEnter Order Item Code to subtract: ");
scanf("%i", &cel);
switch(cel)
{
case 1:
price = 10.50;
printf("\nHow many burger to remove: ");
scanf("%i", &qty);
sub = price * qty;
break;

case 2:
price = 36.75;
printf("\nHow many footlong to remove: ");
scanf("%i", &qty);
sub = price * qty;
break;

case 3:
price = 62.80;
printf("\nHow many shawarma to remove: ");
scanf("%i", &qty);
sub = price * qty;
break;

case 4:
price = 25.60;
printf("\nHow many fries to remove: ");
scanf("%i", &qty);
sub = price * qty;
break;

case 5:
price = 10.00;
printf("\nHow many juice to remove: ");
scanf("%i", &qty);
sub = price * qty;
break;

case 6:
price = 15.00;
printf("\nHow many burger to remove: ");
scanf("%i", &qty);
sub = price * qty;
break;
}
subt = totalf - sub;
totalf = subt;
qty1 = qty1 - qty;
}while(totalf > 1500.00 || qty1 > 20);
break;

case 0:
can = 1;
break;
}
}
else
{
totalf;                                             
}

if(can == 1)
{
printf("\nOrder Cancelled!");
}

else
{
printf("\n");
printf("\nYour Total Order is: ");
printf("%.2f", totalf);

do
{
printf("\n");
printf("\nCustomer's Money: ");
scanf("%f", &payment);

if(payment < totalf)
{
printf("\nSorry you don't have sufficient cash!");
printf("\nDo you want to cancel your order? Press 1 to pay or press 0 to cancel: ");
scanf("%i", &yn);
}
else
{
change = payment - totalf;
printf("\nYour Change is: ");
printf("%.2f", change);
printf("\n\n THANK YOU!");
}
}while(payment<totalf yn="=" mode="hold" />}
printf("\n Canceled Order!");
getch();
}

推荐答案

我举一个简单的例子:



I give you a simple example:

#include <stdio.h>

typedef struct _Item
{
  const char * desc;
  float price;
} Item;


void show_menu(Item it[], int items)
{
  int n;
  printf("%-20s %s\n", "Item", "Price");
  for (n=0; n<items; ++n)
  {
    printf("%-20s %5.2f\n", it[n].desc, it[n].price);
  }
}


int main()
{
  Item item[] = { { "Burger", 10.5}, {"FootLong", 36.75 } /* more items here */ };

  const int ITEMS = sizeof(item)/sizeof(item[0]);

  show_menu(item, ITEMS);
  return 0;
}


这篇关于帮助将其更改为面向功能的prog并向其添加数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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