2016-02-27 27 views
1

所以我无法弄清楚为什么发生这种情况。该程序要求用户针对输入整数,取决于它是否分别是偶数或奇数任加倍数字或三倍的数字。问题是,对于只有偶数整数循环,它为每个循环迭代减去第一位数字1。奇数整数的循环与偶数整数相同,只是数字增加了三倍,而不是加倍,并且工作得很好。有任何想法吗?计划仅偶数值输出减去1

例:

输入:12
预期输出:1122
实际输出:1121

输入:13
预期输出:111333
实际输出:111333

//This program asks the user to enter an integer and depending on whether the integer is even or odd, doubles or triples 
//each digit in the integer respectively. It then reads out the result and asks the user if they would like to enter 
//another integer and run the program again. 
int main() 
{ 
    string restart; 
    int integer, remainder; 

    while (restart != "n" && restart !="N") 
    { 
     cout << "Enter an integer: "; 
     cin >> integer; 
     cout << endl; 

     //Creates variable "temp" and "mycount" and divides "integer" by 10 until remainder is 0 and counts number of 
     //steps to do this to count the number of significant digits of the integer 
     int temp = integer, mycount = 0; 
     while (temp != 0) 
     { 
      temp = temp/10; 
      mycount++; 
     } 

     //Computes if the integer is even or odd by determining the remainder: 0 remainder = even, Non-0 remainder = odd 
     //Creates two integer variables "exponent" and "sum" and sets them to 0 
     //Assigns variable "temp" to the integer value 
     remainder = integer % 2; 
     int exponent = 0; 
     int sum = 0; 
     temp = integer; 

     //If integer is even, doubles each significant digit 
     if (remainder == 0) 
     { 
      //Begins for loop which runs for the number of times equal to the number of significant digits stored in "mycount" 
      for (int i = mycount; i > 0; i--) 
      { 
       //Stores current significant digit in "digit" 
       //Removes current significant digit by dividing by 10 
       //Multiplies current significant digit by 11 and stores it in "timesTwo" 
       //Multiplies current significant digit by 10^exponent then adds it to other modified digits 
       //Adds 2 to current exponent to increase digit multiplier by 100 
       int digit = temp % 10; 
       temp = temp/10; 
       int timesTwo = digit * 11; 
       sum = (timesTwo * pow(10, exponent)) + sum; 
       exponent = exponent + 2; 
       cout << sum << endl; 
       cout << endl; 
      } 

      cout << "Number is even, doubling each digit in the integer ..." << endl; 
      cout << sum << endl; 
     } 

     //If integer is odd this runs the same as the above function except it triples the digit and adds 3 to the multiplier 
     else 
     { 
      for (int i = mycount; i > 0; i--) 
      { 
       int digit = temp % 10; 
       temp = temp/10; 
       int timesThree = digit * 111; 
       sum = (timesThree * pow(10, exponent)) + sum; 
       exponent = exponent + 3; 
       cout << sum << endl; 
      } 
      cout << "Number is odd, tripling each digit in the integer ..." << endl; 
      cout << "Result: " << sum << endl; 
     } 

     cout << "Would you like to enter another integer? (y/n): "; 
     cin >> restart; 
    } 

    return 0; 
} 
+3

你有没有通过代码加强与调试器,为了亲眼看看数字是如何移动的?如果不是,为什么不呢?它在我将这个例子修改为[mcve]之后按预期工作。 –

+0

我没有看到你提到的错误发生。我建议你不要为'sum'使用'int'。使用'ulong'或仅用于显示目的,每次将值附加到一个字符串并显示它的反向。 – Erobrere

+0

http://stackoverflow.com/questions/7937286/return-value-of-pow-gets-rounded-down-if-assigned-to-an-integer – 2016-02-27 22:32:42

回答

1

为了调试这个,我会在第一个循环中打印所有输入,而不仅仅是第e输出。唯一可疑的事情,我可以看到的是,pow()返回一个浮点数,这可能会在转换回一个整数得到四舍五入。

为什么不避免pow()和隐式转换干脆使用你乘了每轮不是整数的因素?

sum = (timesTwo * factor) + sum; 
factor += 100; 

顺便说一句:这不是真的有必要算数字,并有不同的循环,对于两种情况 - 可以简化程序的核心,像

bool even = (integer & 1) == 0; 
int digitFactor = even ? 11 : 111; 
int stepFactor = even ? 100 : 1000; 
int result = 0; 
while(integer != 0) { 
    int digit = integer % 10; 
    result += digit * digitFactor; 
    integer /= 10; 
    digitFactor *= stepFactor; 
} 
+0

所以我还是很新的编码,不明白是怎么回事,在您的示例digitFactor和stepFactor线。你能解释一下怎么回事,以及它们如何与上面的布尔语句相关联? – pja0807

+0

digitFactor是currnet数字需要得到与得到“复制”或“三倍”,并到正确的地方相乘的因素。 stepFactor是digitFactor在每个步骤需要乘以将下一个数字“移位”到正确位置(它将digitFactor从11移动到1100,然后移动到110000等)的因子。在while循环中打印所有值可能会更清楚地详细说明发生了什么。 –