Good morning. The code I have written is made to calculate the amount of change given in a transaction, the change portion only, the paper change is ignored. I would like to do an error check to make sure that the user entered # does not exceed 2 decimal places. This is my code.
早上好。我编写的代码用于计算事务中给出的更改量,仅更改部分,忽略纸张更改。我想做一个错误检查,以确保用户输入的#不超过2位小数。这是我的代码。
#include <stdio.h>
#include <stdlib.h>
void intro();
void instructions();
void getvalues(float *owe, float *paid);
float totalchange(float *owe, float *paid);
void quarters (float *change);
void dimes (float *change);
void nickels (float *change);
void pennies (float *change);
int main()
{
float owe = 0.0, paid = 0.0, change;
int a = 2;
intro();
instructions();
printf("Would you like to continue?\n1: Continue\n0: Exit\n");
scanf("%i", &a);
if (a== 0)
exit(0);
while (a == 1){
getvalues(&owe, &paid);
while (owe > paid)
getvalues(&owe, &paid);
change = totalchange(&owe, &paid);
quarters (&change);
dimes (&change);
nickels (&change);
pennies (&change);
printf("Would you like to make another calculation?\n1: Continue\n0: Exit\n");
scanf("%i", &a);
}
return 0;
}
void intro(){
printf("Program: Homework 1 Part 1 :: Change Calculator\nAuthor: Jason Golightly\nDate:5-13-15\nVersion 1.0\n\n");
}
void instructions(){
printf("This program is designed to calculate the coin\nportion of the change given after a purchase.\n");
printf("When prompted, please enter the purchase amount and the amount paid.\nThe amount paid must exceed the purchase amount.\n");
}
void getvalues(float *owe, float *paid){
printf("Please enter the amounts in a dollars.cents fashion\n\nPurchase amount?\n");
scanf("%f", owe);
printf("\nAmount paid?\n");
scanf("%f", paid);
printf("\n");
if (*owe > *paid)
printf("ERROR. Please enter valid amounts.\n");
if (*owe == *paid)
printf("You have given exact change.\n")
}
float totalchange(float *owe, float *paid){
int a;
a = (*paid - *owe)*100;
a = a % 100;
printf("total change = %i\n",a);
return a;
}
void quarters (float *change){
int q;
q = *change / 25;
printf("Quarters = %i\n", q);
*change = *change - 25*q;
}
void dimes (float *change){
int d;
d = *change / 10;
printf("Dimes = %i\n", d);
*change = *change - 10*d;
}
void nickels (float *change){
int n;
n = *change / 5;
printf("Nickels = %i\n", n);
*change = *change - 5*n;
}
void pennies (float *change){
int p;
p = *change / 1;
printf("Pennies = %i\n\n", p);
*change = *change - 1*p;
}
Also, in case you have not noticed, I am fairly new to programming. If you see anything else I could be doing better, please feel free to point it out.
另外,如果你没有注意到,我对编程很新。如果你看到其他任何我可以做得更好的东西,请随时指出。
Thanks, Jason
1 个解决方案
#1
On simple approach to avoid the in-exact floating point calculation is to read the input as a string, parse it into a int
, and display it as *.##
避免精确浮点计算的简单方法是将输入作为字符串读取,将其解析为int,并将其显示为*。##
i.e.
char number[11];
scanf("%10s", number);
int actual_number = parse(number); // parse the string here.
This actual number is basically dollar*100 + cents - which is an int
. Now perform all the calculations and display like this:
这个实际数字基本上是美元* 100 +美分 - 这是一个int。现在执行所有计算并显示如下:
float f = (float)actual_number/100.f ;
printf("%.2f", f);
In the parse
routine, you only consider the first two digits after encountering a .
在解析例程中,只考虑遇到a后的前两位数。
Here is an example of the parse routine in C.
这是C中的解析例程的示例。
#1
On simple approach to avoid the in-exact floating point calculation is to read the input as a string, parse it into a int
, and display it as *.##
避免精确浮点计算的简单方法是将输入作为字符串读取,将其解析为int,并将其显示为*。##
i.e.
char number[11];
scanf("%10s", number);
int actual_number = parse(number); // parse the string here.
This actual number is basically dollar*100 + cents - which is an int
. Now perform all the calculations and display like this:
这个实际数字基本上是美元* 100 +美分 - 这是一个int。现在执行所有计算并显示如下:
float f = (float)actual_number/100.f ;
printf("%.2f", f);
In the parse
routine, you only consider the first two digits after encountering a .
在解析例程中,只考虑遇到a后的前两位数。
Here is an example of the parse routine in C.
这是C中的解析例程的示例。