我是 Visual C++ 的新手,并编写了以下 C++ 代码。我只是在做一些浮点乘法而已:)。但我有一个问题。
#include "stdafx.h"
#define PI 3.14F
#define totalRound 10.00F
void MultiplyPIArrayStored()
{
printf("\n\nAnalysis\n");
float* defArr = new float[(int)totalRound];
float inc=0.00F;
for(float i=1.00F;i<=totalRound;i++)
{
defArr[(int)i]=i*PI;
//printf("Calculation: #define => %f * %f = %f\n",i,PI,i*PI);
}
float lPI=3.14F;
for(float i=1.00F;i<=totalRound;i++)
{
//printf("Calculation: local variable => %f * %f = %f\n",i,lPI,i*lPI);
printf("#define =>%f; local variable=>%f\n",defArr[(int)i],i*lPI);
if(defArr[(int)i]==i*lPI)
inc++;
}
printf("\nequal rate %f percentage",(inc/totalRound)*100);
printf("\ndifference rate %f percentage",((totalRound-inc)/totalRound)*100);
}
void MultiplyPI()
{
printf("\n\nAnalysis\n\n");
float lPI=3.14F;
float inc=0.00F;
for(float i=1.00F;i<=totalRound;i++)
{
printf("\nCalculation: #define => %f * %f = %f\n",i,PI,i*PI);
printf("Calculation: local variable => %f * %f = %f\n",i,lPI,i*lPI);
printf("#define => %f ; local variable => %f\n",i*PI,i*lPI);
if(i*PI==i*lPI)
inc++;
}
printf("\nEqual rate %f percentage",(inc/totalRound)*100);
printf("\nDifference rate %f percentage",((totalRound-inc)/totalRound)*100);
}
int _tmain(int argc, _TCHAR* argv[])
{
MultiplyPI();
getchar();
MultiplyPIArrayStored();
getchar();
return 0;
}
它给出以下输出。
Analysis
Calculation: #define => 1.000000 * 3.140000 = 3.140000
Calculation: local variable => 1.000000 * 3.140000 = 3.140000
#define => 3.140000 ; local variable => 3.140000
Calculation: #define => 2.000000 * 3.140000 = 6.280000
Calculation: local variable => 2.000000 * 3.140000 = 6.280000
#define => 6.280000 ; local variable => 6.280000
Calculation: #define => 3.000000 * 3.140000 = 9.420000
Calculation: local variable => 3.000000 * 3.140000 = 9.420000
#define => 9.420000 ; local variable => 9.420000
Calculation: #define => 4.000000 * 3.140000 = 12.560000
Calculation: local variable => 4.000000 * 3.140000 = 12.560000
#define => 12.560000 ; local variable => 12.560000
Calculation: #define => 5.000000 * 3.140000 = 15.700001
Calculation: local variable => 5.000000 * 3.140000 = 15.700001
#define => 15.700001 ; local variable => 15.700001
Calculation: #define => 6.000000 * 3.140000 = 18.840001
Calculation: local variable => 6.000000 * 3.140000 = 18.840001
#define => 18.840001 ; local variable => 18.840001
Calculation: #define => 7.000000 * 3.140000 = 21.980001
Calculation: local variable => 7.000000 * 3.140000 = 21.980001
#define => 21.980001 ; local variable => 21.980001
Calculation: #define => 8.000000 * 3.140000 = 25.120001
Calculation: local variable => 8.000000 * 3.140000 = 25.120001
#define => 25.120001 ; local variable => 25.120001
Calculation: #define => 9.000000 * 3.140000 = 28.260001
Calculation: local variable => 9.000000 * 3.140000 = 28.260001
#define => 28.260001 ; local variable => 28.260001
Calculation: #define => 10.000000 * 3.140000 = 31.400001
Calculation: local variable => 10.000000 * 3.140000 = 31.400001
#define => 31.400001 ; local variable => 31.400001
Equal rate 100.000000 percentage
Difference rate 0.000000 percentage
Analysis
#define =>3.140000; local variable=>3.140000
#define =>6.280000; local variable=>6.280000
#define =>9.420000; local variable=>9.420000
#define =>12.560000; local variable=>12.560000
#define =>15.700001; local variable=>15.700001
#define =>18.840000; local variable=>18.840001
#define =>21.980001; local variable=>21.980001
#define =>25.120001; local variable=>25.120001
#define =>28.260000; local variable=>28.260001
#define =>31.400002; local variable=>31.400001
equal rate 40.000000 percentage
difference rate 60.000000 percentage
问题:我在函数' MultiplyPIArrayStored();
'中使用了一个浮点数组。如果您查看输出,则存储在数组中的值会更改(请检查输出的最后一部分。)。数组声明有问题吗?为什么数组值会发生变化?