Commit d44797a4 by tonis2

Tundub töötavat

parent 2e861166
No preview for this file type
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#define SUMMA 100 #define SUMMA 100
struct Bills
struct resource { {
int amount; int amount;
int size; int size;
}; };
// struct bank_resources struct Result
// { {
// int five; int total;
// int twenty_five; struct Bills content[100];
// int fifty; };
// int hundred;
// };
// Kontrollib kas kasutaja vastas soovitud vastusega // Kontrollib kas kasutaja vastas soovitud vastusega
int is_correct_choice(char user_answer[8]) int is_correct_choice(char user_answer[8])
...@@ -26,67 +25,109 @@ int is_correct_choice(char user_answer[8]) ...@@ -26,67 +25,109 @@ int is_correct_choice(char user_answer[8])
return (strcmp(new, "no") == 0) || (strcmp(new, "yes") == 0); return (strcmp(new, "no") == 0) || (strcmp(new, "yes") == 0);
} }
// int withdraw_from_lowest(struct resource data) {
// int return_amount = 0;
// if (data.five > 0) {
// data.five -= 1;
// return_amount = 5;
// }
// else if (data.twenty_five > 0) {
// data.twenty_five -= 1;
// return_amount = 25;
// }
// else if (data.fifty > 0) {
// data.fifty -= 1;
// return_amount = 50;
// }
// else if (data.hundred > 0) {
// data.hundred -= 1;
// return_amount = 100;
// }
// }
int main(void) int main(void)
{ {
struct resource bank[4]; struct Bills bank[4];
struct Result result;
result.total = 0;
char user_answer[8];
//Paneme raha kogustes kupüüre
bank[0].amount = rand() % 2; bank[0].amount = 5;
bank[0].size = 5; bank[0].size = 5;
bank[1].amount = rand() % 3;
bank[1].amount = 3;
bank[1].size = 25; bank[1].size = 25;
bank[2].amount = rand() % 2;
bank[2].amount = 1;
bank[2].size = 50; bank[2].size = 50;
bank[3].amount = rand() % 2;
bank[3].size = 100;
char user_answer[8]; //Küsime kasutajalt sisendit
int return_total = 0;
printf("Kas soovite väikseid kupüüre? ( yes / no ) \n"); printf("Kas soovite väikseid kupüüre? ( yes / no ) \n");
scanf("%s", user_answer); scanf("%s", user_answer);
int user_choice = is_correct_choice(user_answer); int user_choice = is_correct_choice(user_answer);
//Kui kasutaja valesti vastab, laseme tal uuesti vastata
while (user_choice != 1) while (user_choice != 1)
{ {
//Laseme kasutajal uuesti vastata
printf("Vale vastus ! \n"); printf("Vale vastus ! \n");
printf("Kas soovite väikseid kupüüre? ( yes / no ) \n"); printf("Kas soovite väikseid kupüüre? ( yes / no ) \n");
scanf("%s", user_answer); scanf("%s", user_answer);
user_choice = is_correct_choice(user_answer); user_choice = is_correct_choice(user_answer);
} }
//Anname suuri kupüüre int low_bills = strcmp(user_answer, "yes") == 0;
if (strcmp(user_answer, "no") == 0) int bill_index = 0, result_index = 0;
bool wrong_amount_of_bills = false;
// Kui kasutaja ei taha väikseid kupüüre alustame suurtest esimesena.
if (!low_bills)
{
bill_index = 2;
}
// Üritame pangast võtta vajalikku summa kupüüre.
while (result.total < SUMMA && !wrong_amount_of_bills)
{ {
if (low_bills && bank[bill_index].amount == 0)
{
if ((bill_index + 1) <= 2)
{
bill_index += 1;
}
}
if (!low_bills && bank[bill_index].amount == 0)
{
if (bill_index - 1 >= 0)
{
bill_index -= 1;
}
}
//Kui vastavat kupüüri on pangas, üritame selle kogu kasutajale tagasi antavasse rahasse lisada.
if (bank[bill_index].amount > 0)
{
result.content[result_index].amount = 1;
result.content[result_index].size = bank[bill_index].size;
result_index += 1;
result.total += bank[bill_index].size;
if (result.total > SUMMA)
{
wrong_amount_of_bills = true;
break;
}
bank[bill_index].amount -= 1;
}
} }
//Anname väikseid kupüüre if (wrong_amount_of_bills)
if (strcmp(user_answer, "yes") == 0)
{ {
printf("Vastus %d \n", user_choice); printf("Masinal pole piisavalt vajatud kupüüre, et summat vahetada \n");
}
else
{
int i = 0;
for (i; i < sizeof result.content / sizeof *result.content; i++)
{
if (result.content[i].amount == 1 && result.content[i].size > 0)
{
printf("Masin tagastab raha %d \n", result.content[i].size);
}
}
printf("Tagastatud summa kokku %d \n", result.total);
} }
return 0; return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment