1

我正在尝试在 C++ 中创建一个控制台应用程序,提示用户输入一个浮点数,然后获取该数字并分离出整数部分和小数部分。

示例输出为:-

请输入浮点数:
800.589
整数部分为 800,小数部分为 0.589

我的解决方案如下所示:

#include <iostream>
#include <cmath>
using namespace std;

void spliceAnyNumber (double anyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
fractionPart = fmod(anyNumber,1);
integerPart = anyNumber - fractionPart;
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " << *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}

int main()
{   
cout << "Please enter a floating point number: ";
double anyNumber = 0;
cin >> anyNumber;
cout << endl;    
spliceAnyNumber(anyNumber);
system("Pause");
return 0;   
}

我编写了程序,但我也被要求将指针传递给函数并操作取消引用的值。我试图在下面这样做,但我从编译器那里得到了一堆错误。

#include <iostream>
#include <cmath>
using namespace std;

void spliceAnyNumber (double *pAnyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
&fractionPart = fmod(&anyNumber,1);
&integerPart = &anyNumber - &fractionPart;
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " <<     *pFractionPart << "\n"; *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}

int main()
{   
cout << "Please enter a floating point number: ";
double *pAnyNumber = &anyNumber;
cin >> *pAnyNumber;
cout << endl;    
spliceAnyNumber(*pAnyNumber);
system("Pause");
return 0;   
}

添加指针我哪里出错了?版本 1 有效,但版本 2 无效。

4

4 回答 4

1

我已经在内联注释了这个。

#include <iostream>
#include <cmath>
using namespace std;

void spliceAnyNumber (double *pAnyNumber)
{
  double integerPart = 1;
  double fractionPart = 1;
  double *pIntegerPart = &integerPart;
  double *pFractionPart = &fractionPart;
  &fractionPart = fmod(&anyNumber,1);  // <- you should dereference pAnyNumber instead, and assign to fractionPart (i.e. "fractionPart = fmod(*pAnyNymber, 1);
  &integerPart = &anyNumber - &fractionPart;  // <- similar as above
  cout << "The integer part is " << *pIntegerPart << " and the fraction part is " <<     *pFractionPart << "\n"; *pFractionPart << "\n";
  cout << endl;
  cout << "The address of *pIntegerPart is " << &integerPart << "\n";
  cout << endl;
  cout << "The address of *pFractionPart is " << &fractionPart << "\n";
  cout << endl;
}

int main()
{   
  cout << "Please enter a floating point number: ";
  double *pAnyNumber = &anyNumber;  // <- you haven't declared an 'anyNumber' variable to take the address of
  cin >> *pAnyNumber;
  cout << endl;    
  spliceAnyNumber(*pAnyNumber);
  system("Pause");
  return 0;   
}
于 2013-01-30T21:21:27.007 回答
0

我假设你写的时候anyNumber实际上是指pAnyNumber. 如果你有指针

double* p;

你取消引用*p,不是&p。前者给你一段double时间,后者给你一个double**

于 2013-01-30T21:23:09.083 回答
0

您必须先声明anyNumber,然后才能取消引用它:

double *pAnyNumber = &anyNumber; // references an undeclared variable

只需在传递给函数时获取地址。在此之前,您可以使用普通变量 - 不需要指针:

double anyNumber;
cin >> anyNumber;
cout << endl;
spliceAnyNumber(&anyNumber); 

此外,您在函数中使用了错误的运算符。它应该是这样的:

*pFractionPart = fmod(*pAnyNumber,1);
*pIntegerPart = *pAnyNumber - fractionPart;

另一件事是无效的语法:&variable = ...字面意思是“变量地址 = ”,结果是double**.

所以你必须做的唯一改变是函数参数,并访问它。函数内不需要所有这些指针..

于 2013-01-30T21:19:05.100 回答
0

运算符获取变量的&地址,因此typeof(&anyNumber) == double**. 您需要*运算符。

您应该读double *pAnyNumber作“当我应用*运算符时,我得到一个double”。(你实际上得到了一个左值引用,但这并没有说出来,可能会让你感到困惑......)

你的main功能一团糟;保持与原来相同并更改spliceAnyNumber(pAnyNumber);spliceAnyNumber(&pAnyNumber);.

于 2013-01-30T21:21:04.373 回答