我无法弄清楚如何运行/在哪里放置不同的方程式。作业低于代码。
#include <stdio.h>
#define OJ 10
//main function
int main () {
int amount_taken, weight, times_taken, cost_per_oz, num_times;
float cost_of_container, leftovers, total_cost;
printf("What is the weight (in oz.) of the original container of OJ?\n");
scanf("%d", &weight);
printf("What is the cost of the original container of OJ in dollars?\n");
scanf("%f", &cost_of_container);
printf("How many times did your roommate take your juice?\n");
scanf("%d", ×_taken);
for(num_times = 0; num_times < times_taken; num_times++) {
. while ( total_cost <= OJ) {
printf("How much juice did your roommate take this time (in oz.)?\n");
scanf("%d", &amount_taken);
total_cost = cost_per_oz * amount_taken;
cost_per_oz = cost_of_container / weight;
if ( total_cost >= OJ) {
printf("Your roommate owes you $10.00.\n");
total_cost = total_cost - 10;
}//if
printf("How much juice did your roommate take this time (in oz.)?\n");
scanf("%d", &amount_taken);
}// while
} //for
leftovers = total_cost - 10;
printf("Your roommate owes you $%.2f.\n", leftovers);
return 0;
}
你和你的室友经过大量的橙汁。你注意到你的室友比你更多地经历橙汁。当它们用完时,它们会占用你的一些。
你每次带上一些橙汁时都会给你的室友充电。但是从他们那里一次收集50或75美分感觉有点傻。你已经拿出了一个精彩的计划!他们每次吃橙汁时不会给它们充电,而是在它们花了10美元的果汁之后从它们那里收钱。
程序设置:
编写一个程序来模仿这个过程。要求用户输入您购买的果汁容器的大小(以盎司为单位),以及这些容器的价格(以美元计)。然后提示用户输入室友取汁的次数。最后,阅读室友每次花费的金额。每当果汁的总价值等于或超过10美元时,打印出“你的室友欠你10美元。”输入所有数字后,如果室友欠任何钱,打印出欠款值。
示例运行
What is the weight (in oz.) of the original container of OJ?
64
What is the cost of the original container of OJ in dollars?
3.79
How many times did your roommate take your juice?
10
How much juice did your roommate take this time (in oz.)?
30
How much juice did your roommate take this time (in oz.)?
34
How much juice did your roommate take this time (in oz.)?
24
How much juice did your roommate take this time (in oz.)?
40
How much juice did your roommate take this time (in oz.)?
64
Your roommate owes you $10.00.
How much juice did your roommate take this time (in oz.)?
64
How much juice did your roommate take this time (in oz.)?
64
How much juice did your roommate take this time (in oz.)?
18
Your roommate owes you $10.00.
How much juice did your roommate take this time (in oz.)?
20
How much juice did your roommate take this time (in oz.)?
20
Your roommate owes you $2.38.
答案 0 :(得分:1)
请尝试此代码可以帮助您。变化不是很大,现在似乎打印正确的输出。
#include <stdio.h>
#include <time.h>
#include <math.h>
#define OJ 10
int main() {
float amount_taken, weight, cost_per_oz;
int times_taken, num_times;
float cost_of_container, total_cost = 0;
printf("What is the weight (in oz.) of the original container of OJ?\n");
scanf("%f", &weight);
printf("What is the cost of the original container of OJ in dollars?\n");
scanf("%f", &cost_of_container);
cost_per_oz = cost_of_container/weight;
printf("How many times did your roommate take your juice?\n");
scanf("%d", ×_taken);
for (num_times = 0; num_times < times_taken; num_times++) {
printf("How much juice did your roommate take this time (in oz.)?\n");
scanf("%f", &amount_taken);
total_cost += cost_per_oz * amount_taken;
if (total_cost >= OJ) {
printf("Your roommate owes you $10.00.\n");
total_cost = total_cost - 10;
}
}
printf("Your roommate owes you $%.2f.\n", total_cost);
return 0;
}
测试
What is the weight (in oz.) of the original container of OJ?
10
What is the cost of the original container of OJ in dollars?
10
How many times did your roommate take your juice?
3
How much juice did your roommate take this time (in oz.)?
9
How much juice did your roommate take this time (in oz.)?
2
Your roommate owes you $10.00.
How much juice did your roommate take this time (in oz.)?
2
Your roommate owes you $3.00.