//伙计我的代码有问题,并试图解决这个问题而撕裂我的头发。问题是我正在尝试验证我的代码,因此它不会计算负数。如果任何人可以帮助顺利完成这个程序,我会非常感激。请。
//税务计算器计划的目标是使用C编程计算每个Kudler Fine Food商店(DelMar,Encinitas和La Jolla)的销售税 //标准输入/输出处理
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
//Starting point
int main ()
{
//Variables defined
//st = sales tax, tax rated = tr, pa = total purchase amount, sa = purchase amoount
float fstDelmar;
float ftrDelmar;
float fpaDelmar;
float fstEncinitas;
float ftrEncinitas;
float fpaEncinitas;
float fstLajolla;
float ftrLajolla;
float fpaLajolla;
float fsaPurchaseAmount;
int fStoreselect;
//Variable initializations for tax rates and purchase amount
ftrDelmar = .0725;
ftrEncinitas = .075;
ftrLajolla = .0775;
fsaPurchaseAmount = 0;
//Header & Introduction
printf("\n***********************************************************************");
printf("\n* Kudler Fine Foods *");
printf("\n* Sales Tax Calculator *");
printf("\n* Version 3.0 *");
printf("\n***********************************************************************\n");
//Ask user to select store.
printf ("\n STORE LOCATION \n");
printf ("\n 1) Del Mar \n");
printf ("\n 2) Encinitas \n");
printf ("\n 3) La Jolla \n");
printf ("\n Please select the store location (1-3): \n");
scanf ("%d", &fStoreselect);
//Validate store selection.
while (fStoreselect < 1 || fStoreselect > 3) {
fflush (fStoreselect);
printf ("INVALID SELECTION! Please select a valid location (1-3): \n");
scanf ("%d", &fStoreselect);
}
//Ask user to enter in total purchase amount.
printf ("\n What was your purchase amount? ");
scanf("$%f", &fsaPurchaseAmount); //user enters variable amount
//Validation to ensure that user's enter in purchase amounts using correct format.
while (fsaPurchaseAmount <= 0.0)
{
fflush(fsaPurchaseAmount);
printf("\n INVALID SELECTION! Please enter a valid purchase amount greater than zero.\n");
printf("\n The purchase amount is: $ ");
scanf("%f", &fsaPurchaseAmount);
}
//Calculation of sales tax in dollars for each of the store locations
fstDelmar = fsaPurchaseAmount * ftrDelmar;
fstEncinitas = fsaPurchaseAmount * ftrEncinitas;
fstLajolla = fsaPurchaseAmount * ftrLajolla;
//Calculation of total sales amount for each of the locations
fpaDelmar = fsaPurchaseAmount + fstDelmar;
fpaEncinitas = fsaPurchaseAmount + fstEncinitas;
fpaLajolla = fsaPurchaseAmount + fstLajolla;
//Displaying sales amount for purchase for each of the different locations
switch (fStoreselect) {
case 1:
//for Delmar location
printf("\n Purchase Amount Sales Tax Total Sales Amount ");
printf("\n _______________ _________ _________________ ");
printf("\n $%.2f $%.2f $%.2f",fsaPurchaseAmount, fstDelmar, fpaDelmar);
break;
case 2:
//for Encinitas location
printf("\n Purchase Amount Sales Tax Total Sales Amount ");
printf("\n _______________ _________ _________________ ");
printf("\n $%.2f $%.2f $%.2f",fsaPurchaseAmount, fstEncinitas, fpaEncinitas);
break;
case 3:
//for La Jolla location
printf("\n Purchase Amount Sales Tax Total Sales Amount ");
printf("\n _______________ _________ _________________ ");
printf("\n $%.2f $%.2f $%.2f",fsaPurchaseAmount, fstLajolla, fpaLajolla);
break;
}
printf("\n Hit the ENTER key to exit program\n");
//Pause the screen and wait for user to hit the ENTER key
getchar();
//EOF
}
答案 0 :(得分:2)
这一行有错误:
scanf(“ $ %f”,&amp; fsaPurchaseAmount); //用户输入可变金额
它应该是:scanf(“%f”,&amp; fsaPurchaseAmount); //用户输入可变金额
答案 1 :(得分:1)
以下是对所呈现代码的一些观察:
不要使用浮点钱。它只会导致悲伤。理想情况下,请咨询会计专业人士。与此同时,以美分为单位进行算术运算,并以美元为单位进行比例显示。
销售税等计算可能可以通过浮点乘法正确完成,但请确保遵守公认的回合整数美分的做法。再次,向会计专业人士寻求建议。
fflush()
没有按照您的想法行事。对于为写入而打开的文件描述符(例如stdout
),它保证该描述符上的所有输出都已完成。在打印提示后使用,在调用类似scanf()
之类的之前使用来读取输入。例如:printf("What is your favorite color? "); fflush(stdout); fgets(color, sizeof(color), stdin);
始终检查scanf()
的返回值。它返回成功的转换次数。如果它与格式说明符的数量不匹配,那么只有那些成功的值才会将值写入命名变量。
警惕除scanf()
格式字符串中出现的空格以外的文字字符。这些必须与输入文本完全匹配,否则转换将失败。因此像"$%f"
这样的格式只与包含文字美元符号的输入相匹配。这可能不是您的用户期望的。格式"%f"
对用户来说更容易。如果您想允许可选的美元符号,则scanf()
可能不是最佳选择。