CC++相关练习

mac2024-12-01  23

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)