在用C编写函数时需要帮助 [英] Need help with writing a function in C

查看:65
本文介绍了在用C编写函数时需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我完成我的项目.

我需要订单金额的帮助.我本打算使用if else语句,但是我不确定如何启动它.


项目信息:
编写一个读取文件并创建报告的程序.该报告将包含零件号,

价格,现有数量,再订购点,最低订购量和订购数量.

当手头数量低于重新订购点时,将计算订购数量.

计算方法为再订购点数与最小订购量之和减去现有数量.

在报告末尾提供报告标题,每一列的标题以及报告末尾消息.

打印带有前导零的零件号.

我的代码:

I was wondering if someone can help me with my project.

I need help with the order amount. I was going to use a if else statement, but I''m not sure how to start it.


Project information:
Write a program that reads a file and create a report. The report will contain the part number,

price, quantity on hand, reorder point, minimum order, and order amount.

The order amount is calculated when the quantity on hand falls below reorder point.

It is calculated as the sum of the reorder point and the minimum order less the quantity on hand.

Provide a report heading, captions for each column, and an end of report message at the end of the report.

Print the part number with leading zeros.

My code:

<pre lang="cs">#include "stdafx.h"<br />
#include "stdlib.h"<br />
int main(void)<br />
{<br />
/* num = part no., point= reorder point, order= min order */<br />
FILE* sp1;<br />
int num;<br />
int quantity;<br />
int point;<br />
int order;<br />
float price;<br />
sp1 = fopen("c:\\inventory.txt", "r");<br />
if (!sp1)<br />
{<br />
    printf("Error: Can''t open file\a\n");<br />
    exit (101);<br />
}<br />
<br />
printf("                         Inventory Report          \n");<br />
printf("================================================================\n");<br />
printf("Part No.  Price  Quantity  Reorder Point  Min Order  Order Amount\n\n");<br />
<br />
<br />
 while((fscanf(sp1,"%4d%5f%2d%2d%2d",&num, &price, &quantity, &point, &order)) ==5)<br />
{<br />
    printf("%04d     %2.2f     %2d          %2d           %2d\n",num, price, quantity, point, order);<br />
 }<br />
    printf("=================================================================\n\n\n");<br />
    printf("            End of Report                       \n");<br />
<br />
<br />
<br />
return 0;<br />
}</pre>

推荐答案

在while循环内和printf之前(order_amount应该像其余变量一样在开头定义,但必须在if语句之前重置为零) :
Within your while loop and before printf (order_amount should be defined at the beginning just like rest of variables but must be reset to zero before if statement):
order_amount=0;
if(quantity < order_point)
{
 order_amount = order_point+min_order-quantity;
}






OR

if(quantity < order_point)
 order_amount = order_point+min_order-quantity;
else order_amount = 0;



...还添加了另一种样式...



...added a different style as well...


这篇关于在用C编写函数时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆