我是C的新手,正在尝试创建一个程序来查找银行帐户的复利。
我使用三个带指针的函数,一个用于获取数据,一个用于计算,另一个用于将数据打印到表中。我假设我对指针的混淆导致计算值在表中打印不正确。
目前我没有收到任何错误,但有关我的指针“默认为int”的一些警告。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Function declarations
void GETDATA(double* StartAmnt, float* IntrRate, int* NumYears, int* StartYear);
void Mathemagic (double StartAmnt, float IntrRate, int NumYears, int StartYear,
float* IntrEarned, float* PercentGained,long double* FutureValue,
int* FutureYear);
void PRINTTABLE (double StartAmnt, float IntrRate, int NumYears, int StartYear,
float IntrEarned, float PercentGained,long double FutureValue,
int FutureYear);
int main(void)
{
//Local Declarations
double StartAmnt;
float IntrRate;
float IntrEarned;
float PercentGained;
long double FutureValue;
int NumYears;
int StartYear;
int FutureYear;
//Statements
GETDATA (&StartAmnt, &IntrRate, &StartYear, &NumYears);
Mathemagic (StartAmnt, IntrRate, StartYear, NumYears, &IntrEarned,
&PercentGained, &FutureValue, &FutureYear);
PRINTTABLE (StartAmnt, IntrRate, NumYears, StartYear, IntrEarned,
PercentGained, FutureValue, FutureYear);
return 0;
}//main
void GETDATA(double* StartAmnt, float* IntrRate, int* NumYears, int* StartYear)
{
//Statements
printf("COP 2220-51014 Project 2: Michael Walt\n\n");
printf("Enter a Starting amount (dollars and cents): ");
scanf("%lf", StartAmnt);
printf("Enter an Interest rate (ex. 2.5 for 2.5%): ");
scanf("%f", IntrRate);
printf("Enter the Number of years (integer number): ");
scanf("%d", NumYears);
printf("Enter the Starting year (four digits): ");
scanf("%d", StartYear);
return;
}//GETDATA
void Mathemagic (double StartAmnt, float IntrRate, int NumYears, int StartYear,
float* IntrEarned, float* PercentGained,long double* FutureValue,
int* FutureYear)
{
//Statements
*FutureValue = StartAmnt*pow((1+(IntrRate/100)),NumYears);
*PercentGained =((*FutureValue - StartAmnt)/StartAmnt)*100;
*IntrEarned = (*FutureValue-StartAmnt);
*FutureYear = (StartYear+NumYears);
return;
}//Mathemagic
PRINTTABLE(StartAmnt, IntrRate, NumYears, StartYear, IntrEarned,
PercentGained, FutureValue, FutureYear)
{
printf("\n+-----------------------------+--------------+\n");
printf("| Description | Input Data |\n");
printf("|-----------------------------+--------------|\n");
printf("| Starting amount | $ %.2f |\n", StartAmnt);
printf("| Interest rate | %f%% |\n", IntrRate);
printf("| Number of Years | %d |\n", NumYears);
printf("| Starting year | %d |\n", StartYear);
printf("+-----------------------------+--------------+\n");
printf("| Future value | Results |\n");
printf("|-----------------------------+--------------|\n");
printf("| In %d the balance will be | $ %f |\n", FutureYear, FutureValue);
printf("| Interest earned | $ %f |\n", IntrEarned);
printf("| Total percent gained | %f%% |\n", PercentGained);
printf("+-----------------------------+--------------+\n");
return;
}
答案 0 :(得分:1)
您提供的参数顺序不正确,这是您的结果未正确计算的原因。您也没有在PRINTTABLE的函数定义中指定参数类型。它的工作现在正确。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Function declarations
void GETDATA(double* StartAmnt, float* IntrRate, int* NumYears, int* StartYear);
void Mathemagic (double StartAmnt, float IntrRate, int NumYears, int StartYear,
float* IntrEarned, float* PercentGained,long double* FutureValue,
int* FutureYear);
void PRINTTABLE (double StartAmnt, float IntrRate, int NumYears, int StartYear,
float IntrEarned, float PercentGained,long double FutureValue,
int FutureYear);
int main(void)
{
//Local Declarations
double StartAmnt;
float IntrRate;
float IntrEarned;
float PercentGained;
long double FutureValue;
int NumYears;
int StartYear;
int FutureYear;
//Statements
GETDATA (&StartAmnt, &IntrRate, &NumYears, &StartYear);
Mathemagic (StartAmnt, IntrRate, NumYears, StartYear, &IntrEarned,
&PercentGained, &FutureValue, &FutureYear);
PRINTTABLE (StartAmnt, IntrRate, NumYears, StartYear, IntrEarned,
PercentGained, FutureValue, FutureYear);
return 0;
}//main
void GETDATA(double* StartAmnt, float* IntrRate, int* NumYears, int* StartYear)
{
//Statements
printf("COP 2220-51014 Project 2: Michael Walt\n\n");
printf("Enter a Starting amount (dollars and cents): ");
scanf_s("%lf", StartAmnt);
printf("Enter an Interest rate (ex. 2.5 for 2.5%): ");
scanf_s("%f", IntrRate);
printf("Enter the Number of years (integer number): ");
scanf_s("%d", NumYears);
printf("Enter the Starting year (four digits): ");
scanf_s("%d", StartYear);
return;
}//GETDATA
void Mathemagic (double StartAmnt, float IntrRate, int NumYears, int StartYear,
float* IntrEarned, float* PercentGained,long double* FutureValue,
int* FutureYear)
{
//Statements
*FutureValue = StartAmnt*pow((1+(IntrRate/100)),NumYears);
*PercentGained =((*FutureValue - StartAmnt)/StartAmnt)*100;
*IntrEarned = (*FutureValue-StartAmnt);
*FutureYear = (StartYear+NumYears);
return;
}//Mathemagic
void PRINTTABLE(double StartAmnt, float IntrRate, int NumYears, int StartYear,
float IntrEarned, float PercentGained,long double FutureValue,
int FutureYear)
{
printf("\n+-----------------------------+--------------+\n");
printf("| Description | Input Data |\n");
printf("|-----------------------------+--------------|\n");
printf("| Starting amount | $ %.2f |\n", StartAmnt);
printf("| Interest rate | %f%% |\n", IntrRate);
printf("| Number of Years | %d |\n", NumYears);
printf("| Starting year | %d |\n", StartYear);
printf("+-----------------------------+--------------+\n");
printf("| Future value | Results |\n");
printf("|-----------------------------+--------------|\n");
printf("| In %d the balance will be | $ %f |\n", FutureYear, FutureValue);
printf("| Interest earned | $ %f |\n", IntrEarned);
printf("| Total percent gained | %f%% |\n", PercentGained);
printf("+-----------------------------+--------------+\n");
return;
}
答案 1 :(得分:1)
主要问题是您的PRINTTABLE函数定义错误:
PRINTTABLE(StartAmnt, IntrRate, NumYears, StartYear, IntrEarned,
PercentGained, FutureValue, FutureYear)
虽然它已被声明为
void PRINTTABLE (double StartAmnt, float IntrRate, int NumYears, int StartYear,
float IntrEarned, float PercentGained,long double FutureValue,
int FutureYear);
前
如果启用所有警告,您将看到此
*.c:76:1: warning: return type defaults to 'int' [-Wreturn-type]
PRINTTABLE(StartAmnt, IntrRate, NumYears, StartYear, IntrEarned,
^
*.c:76:1: warning: conflicting types for 'PRINTTABLE' [enabled by default]
*.c:12:6: note: previous declaration of 'PRINTTABLE' was here
void PRINTTABLE (double StartAmnt, float IntrRate, int NumYears, int StartYear,
^
因为您对PRINTTABLE有不同的声明和定义,并且编译器正在使用您的代码中出现的最后一个。在C99之前,没有声明类型,所有参数都默认为int类型,返回值也暗示为int,从而使冲突类型警告在上面。
因此,它会在错误的转换和格式中引起许多其他警告,如
*.c:82:5: warning: format '%f' expects argument of type 'double', but argument 2 has type 'int' [-Wformat=]
printf("| Starting amount | $ %.2f |\n", StartAmnt);
那是因为没有类型,StartAmnt
将是int并且你将它打印为double
另一个问题是第49行,你错过了百分号
printf("Enter an Interest rate (ex. 2.5 for 2.5%): ");
↑
您也使用了错误的格式。 FutureValue
类型为long double,但您要按%f
打印它。它应该是%Lf
。
printf("| In %d the balance will be | $ %f |\n", FutureYear, FutureValue);
无论如何,如果您正在使用MSVC,那么double
和long double
是相同的,因为很久以前它已经放弃了对80位扩展精度类型的支持。我也不知道你为什么要使用混合浮点数,双倍和长双倍,它只是花费你时间来转换类型。在大多数应用中,长双倍是不必要的,因为它需要更多的空间,并且由于更高的精度和旧x87的使用而更慢。通常,double是推荐的类型。浮子最常用于存储,因为精度非常有限。
不要将ALL CAPS用于那样的函数或变量