我有没有更好的方式来编写这个程序?

时间:2014-02-21 14:04:18

标签: c++ performance variables if-statement codeblocks

#include <iostream>

using namespace std;

int main()

{

   int a;
   int b;
   int c;
   int d;
   int e;
   int f;

   int aa = 0;
   int bb = 0;
   int cc = 0;
   int dd = 0;
   int ee = 0;
   int ff = 0;

   const string odd = "ODD";
   const string even = "EVEN";

   cout << "enter 6 numbers " << endl;
   cin >> a;
   cin >> b;
   cin >> c;
   cin >> d;
   cin >> e;
   cin >> f;

   aa = a % 2;
   bb = b % 2;
   cc = c % 2;
   dd = d % 2;
   ee = e % 2;
   ff = f % 2;

   if(aa == 0){
           cout << even << endl;
   }else{
       cout << odd << endl;
   } 

   if(bb == 0){
           cout << even << endl;
   }else{
       cout << odd << endl;
   }

   if(cc == 0){
           cout << even << endl;
   }else{
       cout << odd << endl;
   }

   if(dd == 0){
           cout << even << endl;
   }else{
       cout << odd << endl;
   }

   if(ee == 0){
           cout << even << endl;
   }else{
       cout << odd << endl;
   }

   if(ff == 0){
           cout << even << endl;
   }else{
       cout << odd << endl;
   }
   return 0;
   }

例如,有没有办法让它做同样的事情,但代码更少,我应该包括什么?

是否有一种比编写6 if / else语句更简单的方法 - 有没有办法在一个语句或循环中完成所有6个?

我怎样才能提高效率?

4 个答案:

答案 0 :(得分:1)

写下功能

void outputEvenness(int n)
{
    static const string odd = "ODD";
    static const string even = "EVEN";
    if(n % 2){
        cout << odd<< endl;
    } else {
       cout << even << endl;
    }
}

然后使用outputEvenness(a); outputEvenness(b);等来调用它。

答案 1 :(得分:1)

首先,如果使用class std :: string。

,则应该包含标头<string>

当将这些字符串用作字符串文字时,也没有意义。而不是不同的变量,最好只定义一个数组。辅助变量也是不必要的。

如果假设您可能不使用数组,那么我会按以下方式编写程序

#include <iostream>
#include <initializer_list>

int main()
{
   const size_t N = 6;

   int a = 0;
   int b = 0;
   int c = 0;
   int d = 0;
   int e = 0;
   int f = 0;

   const char *odd  = "ODD";
   const char *even = "EVEN";

   std::cout << "enter " << N << " numbers: ";
   std::cin >> a >> b >> c >> d >> e >> f;

   for ( int x : { a, b, c, d, e, f } )
   {
      if ( x % 2 == 0 )
      {
         std::cout << x << " is " << even << std::endl;
      }
      else
      {
         std::cout << x << " is " << odd << std::endl;
      }
   } 

   return 0;
}

如果允许使用数组,则程序可能看起来像

#include <iostream>

int main()
{
   const size_t N = 6;
   int a[N] = {};

   const char *odd  = "ODD";
   const char *even = "EVEN";

   std::cout << "enter " << N << " numbers: ";
   for ( int &x : a ) std::cin >> x;

   for ( int x : a )
   {
      if ( x % 2 == 0 )
      {
         std::cout << x << " is " << even << std::endl;
      }
      else
      {
         std::cout << x << " is " << odd << std::endl;
      }
   } 

   return 0;
}

对于这个简单的程序,没有必要定义一个单独的函数来检查一个数字是偶数还是奇数,因为这个程序就是这样一个函数。:)

答案 2 :(得分:0)

使用数组和循环:

int a[6];  // Array of 6 ints
cout << "enter 6 numbers" << endl;
// Input the 6 numbers
for (int i = 0; i < 6; i++)
{
     cin >> a[i];
}
// Output the results
for (int i = 0; i < 6; i++)
{
     cout << a[i] << " is " << (a[i] & 1 ? "ODD" : "EVEN") << endl;
}

答案 3 :(得分:0)

int value = 0;
string response = "";
cout << "enter 6 numbers " << endl;
for(int i=0; i<6; i++)
{
    cin >> value;
    value % 2 == 0 ? response+="even\n" : response+="odd\n";
}
cout << response;