C Language Program Development Classic example of the four

Source: Internet
Author: User
Tags case statement printf

Program 31 "

Question: Please enter the first letter of the week to determine the day of the week, and if the first letter is the same, continue to judge the second letter.

1. Procedure analysis: Use the situation statement is better, if the first letter is the same, then judge with the case statement or if statement to judge the second letter.

2. Program Source code:

#include <stdio.h>
void main()
{
 char letter;
 printf("please input the first letter of someday\n");
 while ((letter=getch())!='Y')/*当所按字母为Y时才结束*/
 {
  switch (letter)
  {
   case 'S':printf("please input second letter\n");
    if((letter=getch())=='a')
     printf("saturday\n");
    else if ((letter=getch())=='u')
     printf("sunday\n");
    else printf("data error\n");
     break;
   case 'F':printf("friday\n");break;
   case 'M':printf("monday\n");break;
   case 'T':printf("please input second letter\n");
    if((letter=getch())=='u')
     printf("tuesday\n");
    else if ((letter=getch())=='h')
     printf("thursday\n");
    else printf("data error\n");
     break;
   case 'W':printf("wednesday\n");break;
   default: printf("data error\n");
  }
 }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.