06-06-2013, 02:41 AM
Calculator with subtraction/addition/voting! :3
Edited to use booleans for loops:
Not fully edited (I need to set the --loop to loop = false instead) but it works.
Old one:
Edited to use booleans for loops:
Code:
#include <stdio.h>
#include <stdbool.h>
main()
{
bool loop = true;
int op;
printf("Type '0' for a list of operations\ncurrently available.\n");
while (loop == 1) {
scanf("%d", &op);
if (op == 0) {
printf("Type 1 for Addition,\n2 for Subtraction,\n3 for a voting system.\nPlease note that only integers are supported!\n");
};
if (op == 1) {
--loop;
add();
};
if (op == 2) {
--loop;
sub();
};
if (op == 3) {
--loop;
vote();
};
};
};
add()
{
int inputA;
int inputB;
printf("Addition selected\nPlease give input for input A\n");
scanf("%d", &inputA);
printf("Thank you.\nNow please provide input B.\n");
scanf("%d", &inputB);
printf("Now calculating\n");
while (inputA != 0) {
++inputB;
--inputA;
};
printf("Calculation complete.\nResult: %d\n", inputB);
main();
};
sub()
{
int inputA;
int inputB;
printf("Subtraction selected\nPlease give input for input A\n");
scanf("%d", &inputA);
printf("Thank you.\nNow please provide input B.\n");
scanf("%d", &inputB);
printf("Now calculating\n");
while (inputB != 0) {
--inputB;
--inputA;
};
printf("Calculation complete.\nResult: %d\n", inputA);
main();
};
vote()
{
int variA;
int variB;
int buffer;
int loop = 1;
printf("Usage: Have everyone come up to the\ncomputer and type either 1 or 2 to\nrepresent their choice. When voting\nis done, type 3.\n");
while(loop == 1) {
scanf("%d", &buffer);
if (buffer == 1) {
++variA;
buffer = 0;
};
if (buffer == 2) {
++variB;
buffer = 0;
};
if (buffer == 3) {
--loop;
buffer = 0;
};
if (buffer > 3) {
printf("Invalid operation!\n");
buffer = 0;
};
};
if (variA > variB) {
printf("Option 1 wins!\n");
};
if (variA < variB) {
printf("Option 2 wins!\n");
};
if (variA == variB) {
printf("Tie!\n");
};
main();
};
Not fully edited (I need to set the --loop to loop = false instead) but it works.
Old one:
Code:
#include <stdio.h>
main()
{
int loop = 1;
int op;
printf("Type '0' for a list of operations\ncurrently available.\n");
while (loop == 1) {
scanf("%d", &op);
if (op == 0) {
printf("Type 1 for Addition,\n2 for Subtraction,\n3 for a voting system.\nPlease note that only integers are supported!\n");
};
if (op == 1) {
--loop;
add();
};
if (op == 2) {
--loop;
sub();
};
if (op == 3) {
--loop;
vote();
};
};
};
add()
{
int inputA;
int inputB;
printf("Addition selected\nPlease give input for input A\n");
scanf("%d", &inputA);
printf("Thank you.\nNow please provide input B.\n");
scanf("%d", &inputB);
printf("Now calculating\n");
while (inputA != 0) {
++inputB;
--inputA;
};
printf("Calculation complete.\nResult: %d\n", inputB);
main();
};
sub()
{
int inputA;
int inputB;
printf("Subtraction selected\nPlease give input for input A\n");
scanf("%d", &inputA);
printf("Thank you.\nNow please provide input B.\n");
scanf("%d", &inputB);
printf("Now calculating\n");
while (inputB != 0) {
--inputB;
--inputA;
};
printf("Calculation complete.\nResult: %d\n", inputA);
main();
};
vote()
{
int variA;
int variB;
int buffer;
int loop = 1;
printf("Usage: Have everyone come up to the\ncomputer and type either 1 or 2 to\nrepresent their choice. When voting\nis done, type 3.\n");
while(loop == 1) {
scanf("%d", &buffer);
if (buffer == 1) {
++variA;
buffer = 0;
};
if (buffer == 2) {
++variB;
buffer = 0;
};
if (buffer == 3) {
--loop;
buffer = 0;
};
if (buffer > 3) {
printf("Invalid operation!\n");
buffer = 0;
};
};
if (variA > variB) {
printf("Option 1 wins!\n");
};
if (variA < variB) {
printf("Option 2 wins!\n");
};
if (variA == variB) {
printf("Tie!\n");
};
main();
};