问题描述
- (c语言)利用%ld读取double类型,程序可以运行,但是结果是很大的负数?
-
#include
#define TAX_1 17850
#define TAX_2 23900
#define TAX_3 29750
#define TAX_4 14875
#define TAXRATE_1 0.15
#define TAXRATE_2 0.28
int main(void)
{
double taxsalary,taxpay;
int n;
printf("Please enter your taxsalary(q to quit):n");
while(scanf("%ld",&taxsalary)==1)
{
printf("please enter your kinds of tax(from 1 to 4):n");
printf("1)single tax: $17850 %15 more:%28n");
printf("2)owner tax: $23900 %15 more:%28n");
printf("3)double(together) tax: $29700 %15 more:%28n");
printf("4)double(divorce) tax: $23900 %15 more:%28n");
scanf("%d",&n);
if (n==1)
{
if(taxsalary<=TAX_1)
{
taxpay=taxsalary*TAXRATE_1;
printf("taxpay:%ldn",taxpay);} else { taxpay=TAX_1*TAXRATE_1+(taxsalary-TAX_1)*TAXRATE_2; printf("taxpay:%ldn",taxpay); } } if (n==2) { if (taxsalary<=TAX_2) { taxpay=taxsalary*TAXRATE_1; printf("taxpay:%ldn",taxpay); } else { taxpay=TAX_2*TAXRATE_1+(taxsalary-TAX_2)*TAXRATE_2; printf("taxpay:%ldn",taxpay); } } if (n==3) { if (taxsalary<=TAX_3) { taxpay=taxsalary*TAXRATE_1; printf("taxpay:%ldn",taxpay); } else { taxpay=TAX_3*TAXRATE_1+(taxsalary-TAX_3)*TAXRATE_2; printf("taxpay:%ldn",taxpay); } } if (n==4) { if (taxsalary<=TAX_4) { taxpay=taxsalary*TAXRATE_1; printf("taxpay:%ldn",taxpay); } else { taxpay=TAX_4*TAXRATE_1+(taxsalary-TAX_4)*TAXRATE_2; printf("taxpay:%ldn",taxpay); } } printf("Please enter your taxsalary:(q to quit)n"); } return 0;
}
解决方案
非常感谢,又仔细查了一下。是%lf,记错了,
解决方案二:
%ld, 应该是读取long吧,double不是应该%lf?
时间: 2025-01-21 10:30:19