1 / 6

Create a Shopping Weight Program Using If-Else and Switch Statements in C

This guide demonstrates how to create a C program that simulates item weight calculations in a store. It utilizes both `if-else` and `switch` statement structures to handle user input for item weight and type. The program allows up to five product choices, calculates the total price based on the chosen item and its weight, and returns an error message for unknown items or zero total cost. Key elements include variable declaration, user input handling, and conditional logic for displaying results.

quon-barnes
Télécharger la présentation

Create a Shopping Weight Program Using If-Else and Switch Statements in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Valiklaused

  2. Valiklaused • Osades olukordades võib muutuda tavapärase if-else konstruktsiooni kasutamine kohmakaks • Sõltuvalt olukorrast on selle lahendamiseks 2 täiendavat keelekonstruktsiooni • switch () • If / else if / else

  3. Valiklaused UMLis

  4. Valiklaused koodis switch () if / else if / else if(tingimus) { laused } else if (tingimus) { laused } else { laused } switch(avaldis) { case konstant või avaldis: laused break; case konstant või avaldis: laused break; default: laused break; }

  5. Luua algoritm programmile … • … mis matkib tüüpilist kauba kaalu poes, st • Loetakse sisse kauba kaal • Loetakse sisse kaup (st millega tegu) • Lähtudes kaubast ja kogusest arvutatakse välja hind • Saadud hind ja kauba nimetus väljastatakse • Tundmatu kauba numbri puhul väljastatakse veateade • Kui saadud maksumus on 0, siis väljastatakse veateade

  6. #include <stdio.h> intmain(void) { float kogus; intvalik; printf("Sisestagepaluntootekogus\n> "); scanf("%f", kogus); switch (valik) { case 0: printf(„Toode %d – Banaan\n", valik); hind = 0.38; break; default: hind = 0; printf("Eisaanudaru\n"); break; } maksumus= hind * kogus; printf("Kokku: %f", maksumus); return 0; } • Loo programm, mis emuleeriks toidupoes olevat kauba kaalu • Kaalul peab olema kokku 5 valikut (4 loo ise) • Kuva kasutajale pakutavate toodete loetelu • Kasutajalt loetakse sisse kogus ja valitud toode • Veendu, et kõik vajalikud muutujad oleksid deklareeritud • Loo eraldi vastus olukorras, kui summa on 0

More Related