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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值