【程序31】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续
判断第二个字母。
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 char c; 6 cin>>c; 7 switch(c) 8 { 9 case ‘M‘: 10 cout<<"Monday"<<endl; 11 break; 12 case ‘T‘: 13 cout<<"input another letter: "; 14 cin>>c; 15 if(c==‘u‘) cout<<"Tuesday"<<endl; 16 else if(c==‘h‘) cout<<"Thursday"<<endl; 17 break; 18 case ‘W‘: 19 cout<<"Wendesday"<<endl; 20 break; 21 case ‘F‘: 22 cout<<"Friday"<<endl; 23 break; 24 case ‘S‘: 25 cout<<"input another letter: "; 26 cin>>c; 27 if( c==‘a‘) cout<<"Saturday"<<endl; 28 else if( c==‘u‘) cout<<"Sunday"<<endl; 29 break; 30 default: 31 cout<<"input error"<<endl; 32 break; 33 } 34 system("pause"); 35 return 0; 36 }
==============================================================
【程序32】(TC中可以添加头文件“conio.h”,但VC中没有,只能手动添加textbackground())
题目:Press any key to change color, do you want to try it. Please hurry up!
1 #include<iostream> 2 #include "windows.h" 3 using namespace std; 4 int textbackground(short iColor) 5 { 6 HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE); 7 CONSOLE_SCREEN_BUFFER_INFO csbInfo; 8 GetConsoleScreenBufferInfo(hd, &csbInfo); 9 return SetConsoleTextAttribute(hd, (iColor<<4)|(csbInfo.wAttributes&~0xF0)); 10 } 11 int main() 12 { 13 int color; 14 char c; 15 for( color=0; color<8; color++) 16 { 17 textbackground(color); 18 cout<<"This is color "<<color<<endl; 19 cout<<"Please press any key to continue."<<endl; 20 cin>>c; 21 } 22 system("pause"); 23 return 0; 24 }
==============================================================
【程序33】(略)
题目:学习gotoxy()与clrscr()函数
==============================================================
【程序34】
题目:练习函数调用
1 #include<iostream> 2 using namespace std; 3 void hello_world(); 4 void triple_hello_world(); 5 int main() 6 { 7 triple_hello_world(); 8 system("pause"); 9 return 0; 10 } 11 void hello_world() 12 { 13 cout<<"Hello World!"<<endl; 14 } 15 void triple_hello_world() 16 { 17 for(int i=0; i<3; i++) 18 hello_world(); 19 }
时间: 2024-10-23 20:43:08