C/Language Basics/Switch
Содержание
- 1 a simple 4 function calculator
- 2 Console menu: switch with char case
- 3 Get three input at the same time: scanf
- 4 How to use switch: char
- 5 How to use switch: number
- 6 Switch: char and nested if
- 7 Switch demo
- 8 Switch inside for loop
- 9 Switch with char case
- 10 Switch with default
- 11 Switch with int case
a simple 4 function calculator
#include <stdio.h>
int main()
{
char line[100];
int result =0;
char operator;
int value;
while (1) {
printf("Result: %d\n", result);
printf("Enter operator and number: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%c %d", &operator, &value);
if ((operator == "q") || (operator == "Q"))
break;
switch (operator) {
case "+":
result += value;
break;
case "-":
result -= value;
break;
case "*":
result *= value;
break;
case "/":
if (value == 0) {
printf("Error:Divide by zero\n");
printf(" operation ignored\n");
} else
result /= value;
break;
default:
printf("Unknown operator %c\n", operator);
break;
}
}
return (0);
}
#include <stdio.h>
#include <conio.h>
int main(void)
{
char ch;
do {
printf("\n Enter a character, q to quit: ");
ch = getche();
printf("\n");
switch(ch) {
case "a":
printf("a ");
case "b":
printf("b ");
case "c":
printf("c");
break;
case "d":
printf("d ");
case "e":
printf("e ");
}
} while(ch != "q");
return 0;
}
Get three input at the same time: scanf
#include <stdio.h>
int main(void)
{
int i, j;
char op;
printf("Enter operation(+ - * /): ");
scanf("%d%c%d", &i, &op, &j);
switch(op) {
case "+": printf("%d", i + j);
break;
case "-": printf("%d", i - j);
break;
case "/": if(j) printf("%d", i / j);
break;
case "*": printf("%d", i * j);
}
return 0;
}
How to use switch: char
#include <stdio.h>
void main()
{
char answer = 0; /* Stores an input character */
printf("Enter Y or N: ");
scanf(" %c", &answer);
switch (answer)
{
case "y": case "Y":
printf("\n affirmative.");
break;
case "n": case "N":
printf("\n negative.");
break;
default:
printf("\n not respond correctly...");
break;
}
}
How to use switch: number
#include <stdio.h>
void main()
{
int choice = 0; /* The number chosen */
/* Get the choice input */
printf("\n Pick a number between 1 and 10: ");
scanf("%d",&choice);
/* Check for an invalid selection */
if((choice>10) || (choice <1))
choice = 11; /* Selects invalid choice message */
switch(choice)
{
case 7:
printf("\nCongratulations!");
printf("\nYou win.");
break; /* Jumps to the end of the block */
case 2:
printf("\nYou win.");
break; /* Jumps to the end of the block */
case 8:
printf("\nYou win.");
break; /* Jumps to the end of the block */
case 11:
printf("\nTry between 1 and 10.");
/* No break ?so continue with the next statement */
default:
printf("\nSorry, you lose.\n");
break; /* Defensive break - in case of new cases */
}
}
Switch: char and nested if
/*Example 3.11 A calculator*/
#include <stdio.h>
void main()
{
double number1 = 0.0; /* First operand: a decimal number */
double number2 = 0.0; /* Second operand: a decimal number */
char operation = 0; /* Operation - must be +, -, *, /, or % */
printf("\nEnter the calculation\n");
scanf("%lf %c %lf", &number1, &operation, &number2);
/* Code to check the input goes here */
switch(operation)
{
case "+": /* No checks necessary for add */
printf("= %lf\n", number1 + number2);
break;
case "-": /* No checks necessary for subtract */
printf("= %lf\n", number1 - number2);
break;
case "*": /* No checks necessary for multiply */
printf("= %lf\n", number1 * number2);
break;
case "/":
if(number2 == 0) /* second operand cannot be zero */
printf("\n\n\aDivision by zero error!\n");
else
printf("= %lf\n", number1 / number2);
break;
case "%": /* second operand cannot be zero */
if((long)number2 == 0)
printf("\n\n\aDivision by zero error!\n");
else
printf("= %ld\n", (long)number1 % (long)number2);
break;
default: /* Operation is invalid*/
printf("\n\n\aIllegal operation!\n");
}
}
Switch demo
#include <stdio.h>
#include <stdlib.h>
int main()
{
char line[10];
while (1) {
printf("Enter add(a), delete(d), quit(q): ");
fgets(line, sizeof(line), stdin);
switch (line[0]) {
case "a":
printf("Add\n");
break;
case "d":
printf("Delete\n");
break;
case "q":
printf("Quit\n");
exit(0);
defualt:
printf("Error:Bad command %c\n", line[0]);
break;
}
}
}
Switch inside for loop
#include <stdio.h>
main() {
int i,n;
scanf("%d",&n);
for(i = 1; i<n; i= i+1) {
switch(i%2) {
case 0 : printf("the number %d is even \n",i);
break;
case 1 : printf("the number %d is odd \n",i);
break;
}
}
}
Switch with char case
#include <stdio.h>
int main(void) {
int a, b;
char ch;
printf("Do you want to (Enter first letter):\n");
printf("(A)dd, (S)ubtract, (M)ultiply, or (D)ivide?\n");
do {
printf("Your choice: ");
ch = getchar();
} while(ch!="A" && ch!="S" && ch!="M" && ch!="D");
printf("\n");
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
switch(ch) {
case "A": printf("%d", a + b);
break;
case "S": printf("%d", a - b);
break;
case "M": printf("%d", a * b);
break;
case "D": if(b!=0) printf("%d", a / b);
}
return 0;
}
Switch with default
#include <stdio.h>
#include <conio.h>
int main(void)
{
char ch;
printf("Enter the letter: ");
ch = getche();
switch(ch) {
case "a":
case "e":
case "i":
case "o":
case "u":
case "y":
printf(" is a vowel\n");
break;
default:
printf(" is a consonant");
}
return 0;
}
Switch with int case
#include <stdio.h>
int main(void)
{
int i;
printf("Enter a number between 1 and 4: ");
scanf("%d", &i);
switch(i) {
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
case 4:
printf("four");
break;
default:
printf("Unrecognized Number");
}
return 0;
}