简体   繁体   English

这是在 C 中执行此操作的正确方法吗?

[英]Is this a correct way of doing this in C?

Should I change something in written code?我应该更改书面代码吗? Compiler says that everything is right — no errors or warnings.编译器说一切正常——没有错误或警告。

  • You are building a new home and you have calculated exactly how much cement you need for the foundation.你正在建造一个新家,你已经准确地计算出地基需要多少水泥。
  • Ideally you'd like to purchase this exact amount of cement, but the store only sells cement in 120-pound bags.理想情况下,您希望购买这一确切数量的水泥,但商店只出售 120 磅袋装的水泥。
  • Each of these bags costs 45 dollars.这些袋子每只售价 45 美元。

Please write a C program that calculates the cost of the cement you will have to purchase to build your foundation.请编写一个 C 程序来计算建造地基所需购买的水泥成本。

  • Your program should first read a decimal number representing the amount of cement needed (in pounds) for the foundations of your new home.你的程序应该首先读取一个十进制数,代表你的新家地基所需的水泥量(以磅为单位)。
  • Your program should then display the total cost of the cement bags you have to purchase to have enough cement to build your foundation.然后您的程序应该显示您必须购买的水泥袋的总成本,以便有足够的水泥来建造您的地基。
  • To make your program simpler, you are guaranteed that the amount of cement needed will NEVER be a multiple of 120.为了使您的程序更简单,您可以保证所需的水泥量永远不会是 120 的倍数。

My code so far:到目前为止我的代码:

#include <stdio.h>
#include <math.h>
int main(void) {
    int price=45, totalPrice, OneBag=120;
    float needed;
    do
    {
        printf("Enter the amount of cement you need for your foundation that is not dividable by 120: ");
        scanf("%f", &needed);
    } while (fmodf(needed, OneBag)==0);
    
    totalPrice = ((int)needed/OneBag+1)*(price);
    printf("Total cost of cement you will need for your foundation is %d", totalPrice);
    

    return 0;
}

To be honest I don't see any real mistakes in your code, but in my opinion there is room for improvement:老实说,我在您的代码中看不到任何真正的错误,但我认为还有改进的余地:

Price per bag and size of a bag, are both constants, which you can actually make clear in your code, this is more readable and it allows the compiler to optimize your code better.每袋的价格和袋子的大小都是常量,您实际上可以在代码中明确说明,这更具可读性,并且允许编译器更好地优化您的代码。

You also don't actually have to check if the input is a multiple of 120, because it is a given that it is not.您实际上也不必检查输入是否是 120 的倍数,因为它不是。

There is also something called the ceil function (or ceilf if working with floats), which actually takes any number and increases upward to the nearest integer.还有一个叫做ceil function (或ceilf如果使用浮点数)的东西,它实际上取任意数字并向上增加到最近的 integer。 Which is pretty useful for this assignment.这对这项任务非常有用。

One last thing just because the compiler says it's all right, doesn't mean it atcually is.最后一件事仅仅因为编译器说没关系,并不意味着它实际上是。

code:代码:

#include <stdio.h>
#include <math.h>

const float price_per_bag = 45;
const float bag_size = 120;

int main(void) {
    float needed;
    printf("Enter the amount of cement needed:\n");
    scanf("%f", &needed);

    float price = ceilf(needed / bag_size) * price_per_bag;
    printf("total price: %i\n", (int)price);
}
#include <stdio.h>

int main(void){
      
   float totalC;
   int count=0, totalPrice, i ;
      
   scanf("%f", &totalC);

   for (i=0; i<totalC; i+=120)
        {
           count = count+1;   
         }
     

         totalPrice = count *45;
         printf("%d", totalPrice);
         return 0;

      }
#include<stdio.h>

int main(void){
   double reqCement;
   int bags;
   int amount;
   printf("Type amount of cement:");
   scanf("%lf",&reqCement);
   bags = (reqCement/120)+1;
   amount = bags*45;
   printf("Amount = %d", amount);
   return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM