C語言例題首字母判斷星期幾

一個小程序例題如下。

題目:

請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續判斷第二個字母。
1.程序分析:
用情況語句比較好,如果第一個字母一樣,則判斷用情況語句或if語句判斷第二個字母。
2.程序源代碼:
#include <stdio.h>
void main()
{
char letter;
printf( “C語言研究中心 www.dotcpp.com\n” );
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”);
}
}

}

本例題轉自C語言網,非原創。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章