#define ENDVALUE_2 10 { int year; float sales = 1000; //鍗曚綅涓囩編鍏? float profit = 100; float decrease_rate = 0.04; printf(" SALES AND PROFIT PROJECION \n"); printf(" ----------------------------- \n"); printf(" \n"); printf(" YEAR EXPECTED SALES PROJECTED PROFIT\n"); printf(" ---- -------------- ----------------\n"); year = 1; while(year <= ENDVALUE_2) { sales = sales - 1000 * (year - 1) * 0.04; profit =profit - 100 * (year - 1) * 0.04; printf("%4d\t%11.2f\t%13.2f\n", year, sales, profit); year = year + 1; } double total_sales; double total_profit; sales = 1000; profit = 100; for(year =1;year <= ENDVALUE_2;year ++) { sales = sales - 1000 * (year - 1) * 0.04; profit =profit - 100 * (year - 1) * 0.04; total_sales += sales; total_profit += profit; } printf("Totals:$%2f $%2f\n",total_sales,total_profit); }
时间: 2024-01-16 14:02:45 浏览: 121
这段代码有一些问题,主要是在定义变量ENDVALUE_2的时候出现了错误。
首先,定义常量应该使用#define关键字,但是在这里却使用了宏定义,导致代码的可读性和可维护性都很差。
其次,在宏定义中没有使用括号将常量和代码块区分开来,这也容易导致一些错误。
最后,变量total_sales和total_profit没有进行初始化,这可能会导致计算结果不准确。
以下是一个修正后的代码,你可以参考一下:
```
#define ENDVALUE_2 10
int main() {
int year;
float sales = 1000; //鍗曚綅涓囩編鍏?
float profit = 100;
float decrease_rate = 0.04;
double total_sales = 0;
double total_profit = 0;
printf(" SALES AND PROFIT PROJECION \n");
printf(" ----------------------------- \n");
printf(" \n");
printf(" YEAR EXPECTED SALES PROJECTED PROFIT\n");
printf(" ---- -------------- ----------------\n");
year = 1;
while(year <= ENDVALUE_2)
{
sales = sales - 1000 * (year - 1) * 0.04;
profit =profit - 100 * (year - 1) * 0.04;
printf("%4d\t%11.2f\t%13.2f\n", year, sales, profit);
year = year + 1;
}
sales = 1000;
profit = 100;
for(year =1;year <= ENDVALUE_2;year ++)
{
sales = sales - 1000 * (year - 1) * 0.04;
profit =profit - 100 * (year - 1) * 0.04;
total_sales += sales;
total_profit += profit;
}
printf("Totals:$%.2f $%.2f\n",total_sales,total_profit);
return 0;
}
```
阅读全文