PHP :: Aufgabe #246

2 Lösungen Lösungen öffentlich

Preise im Copy-Shop für Kopien

Anfänger - PHP von DragStar - 20.03.2020 um 11:53 Uhr
In einem Copy-Shop gilt folgende Preisliste:

01 - 49 Seiten kostet 0,10 Euro pro Seite
50 - 99 Seiten kostet 0,09 Euro pro Seite
100 - 199 Seiten kostet 0,08 Euro pro Seite
ab 200 Seiten kostet 0,06 Euro pro Seite


Erstellen Sie ein Programm, welches nach Eingabe der Anzahl der Kopien den Gesamtpreis ausgibt.

Lösungen:

vote_ok
von Exception (7090 Punkte) - 05.05.2020 um 18:13 Uhr
Quellcode ausblenden PHP-Code
<?php

array_shift($argv); // remove script name

if (sizeof($argv) !== 1) {
  die ('Error: 1 argument expected, ' . sizeof($argv) . ' provided.' . PHP_EOL);
}

$copies = intval($argv[0]);

if ($copies < 1) {
  die ('Error: Invalid amount (' . $copies . ') provided.' . PHP_EOL);
}

$factor = 0.0;

if ($copies < 50) {
  $factor = 0.1;
} elseif ($copies < 100) {
  $factor = 0.09;
} elseif ($copies < 200) {
  $factor = 0.08;
} else {
  $factor = 0.06;
}

$price = $copies * $factor;

echo "$copies copies x $factor € = $price €" , PHP_EOL;

Konsolenausgabe:

php main.php
Error: 1 argument expected, 0 provided.

php main.php 0
Error: Invalid amount (0) provided.

php main.php 1
1 copies x 0.1 € = 0.1 €

php main.php 50
50 copies x 0.09 € = 4.5 €

php main.php 49
49 copies x 0.1 € = 4.9 €

php main.php 100
100 copies x 0.08 € = 8 €

php main.php 199
199 copies x 0.08 € = 15.92 €

php main.php 200
200 copies x 0.06 € = 12 €
vote_ok
von Mario2606 (470 Punkte) - 28.10.2020 um 09:23 Uhr
Quellcode ausblenden PHP-Code
<?php
        $geld = 0;
      if($_POST["eingabe"] < 50){
        $geld = 0.10;
      } else
      if($_POST["eingabe"] < 100 && $_POST["eingabe"] > 49){
        $geld = 0.09;
      } else
      if($_POST["eingabe"] < 200 && $_POST["eingabe"] > 99){
        $geld = 0.08;
      } else
      if($_POST["eingabe"] > 199){
        $geld = 0.06;
      }
      echo $ergebnis = $_POST["eingabe"]*$geld;
      ?>





Teil 2:

Quellcode ausblenden HTML-Code
<html>
    <head>
        <meta charset="windows-1252">
        <title></title>
    </head>
    <body>
        <form action="uebung6.1.php" method="post">
            <input type="number" name="eingabe" placeholder="Kopienmenge">
        <input type="submit" value="ausrechnen">
        </form>
    </body>
</html>