# [工具程式]C++語言...20點...急需

. 請設計一程式，判斷所輸入的數字是奇數(odd)或偶數(even)。

(1)Input number: 2 (2)Input number:1

even odd

(請考慮除數不得為0，若使用者輸入除數為0，必須顯示不能運算。)

(1)Input a,b: 6,2 (2)Input a,b:6,0

a+b=8 a+b=6

a-b=4 a-b=6

a*b=12 a*b=0

a/b=3 a/b= can’t operate

a%b=0 a%b= can’t operate

### 1 個解答

• 1 0 年前
最佳解答

//Power by Microsoft Visual Studio 2005//可以使用 Dev-C++ 編譯此程式#include<iostream>#include<cstdlib>#include<iomanip>using namespace std;int main(int argc,char **argv){ //=====START=====// bool odd(int NUM); void operate(int COUNTER,double A,double B); int number,i; double a,b; cout<<"1)Input number: "; cin>>number; cout<<(odd(number)?"Odd":"Even")<<endl; cout<<"2)"<<endl<<"Input a: "; cin>>a; cout<<"Input b: "; cin>>b; for(i=0;i<5;i++){  operate(i,a,b); } //=====END=====// system("PAUSE"); return EXIT_SUCCESS;}bool odd(int NUM){ return (NUM&1)!=0;}void operate(int COUNTER,double A,double B){ switch(COUNTER){  case 0:   cout<<"a+b= "<<A+B<<endl;   break;  case 1:   cout<<"a-b= "<<A-B<<endl;   break;  case 2:   cout<<"a*b= "<<A*B<<endl;   break;  case 3:   cout<<"a/b= ";   if(B!=0){    cout<<setprecision(3)<<A/B<<endl;   }else{    cout<<"can't operate"<<endl;   }   break;  case 4:   cout<<"a%b= ";   if(B!=0){    cout<<static_cast<int>(A)%static_cast<int>(B)     <<endl;   }else{    cout<<"can't operate"<<endl;   }   break; }}