C/C++相關練習

1.不用臨時變量進行變量a,b值互換:
設a = 10,b = 20
方法一:加減法
a = a + b
b = a - b
a = a - b
或者
a = a - b
b = a + b
a = b -a
…等等

方法二:異或法
a = a ^ b
b = a ^ b
a = a ^ b
異或的性質:
a ^ a = 0
a ^ 0 = a

2 ```c++
#include<iostream>
#include<iomanip>

using namespace std;
int main(){
	enum weekday{ sun,mon,tue,wed,thu,fri,sat };
	const weekday first_day = fri;//don't forget 'const',
	int input_num; 
	weekday output; 

cout<<"Put in an intege between 1 and 31:"<<endl;
cin>>input_num;

//數據有效性檢測
if(input_num < 1 || input_num >31)
{
	cout<<"Date Erro"<<endl;
	return 1;
}
//計算日期對應的星期
output =(weekday) ((input_num + (int)first_day -1) % 7);


//打印日曆頭   
cout<<"colendar	 2006-12"<<endl;
cout<<"---------------------------------------------------"<<endl;
cout<<"Sun  Mon  Tue  Wed  Thu  Fri  Sta"<<endl;
cout<<"---------------------------------------------------"<<endl;

/*if(output == sun) cout<<setw(2)<<input_num;
else if(output == mon) cout<<setw(7)<<input_num;
else if(output == tue) cout<<setw(12)<<input_num;
else if(output == wed) cout<<setw(17)<<input_num;
else if(output == thu) cout<<setw(22)<<input_num;		
else if(output == fri) cout<<setw(27)<<input_num;
else                   cout<<setw(32)<<input_num;*/
switch(output)
case sun:
	cout<<setw(2)<<input_num;
	break;
case mon:
	cout<<setw(7)<<input_num;
	break;
case tue:
	cout<<setw(12)<<input_num;
	break;
case wed:
	cout<<setw(17)<<input_num;
	break;
case thu:
	cout<<setw(22)<<input_num;
	break;
case fri:
	cout<<setw(27)<<input_num;
	break;
case sta:
	cout<<setw(32)<<input_num;
	break;

cout<<endl<<"--------------------------------------------"<<endl;

return 0;

}

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