-2

我目前是 c 的新手,我今天有一个任务要完成。我写了这个作业将近 5 个小时。我一直在谷歌搜索,并阅读stackoverflow。请帮我解决我的代码。我不明白为什么我的“成本”数组没有打印出浮点数。我一直在尝试使用“put()”函数,看看是否可以修复它。它仍然不起作用,因为我认为它只需要字符而不是整数?好吧,我需要帮助。

预期输出是这个链接:https ://imgur.com/N0IZpif

#include <string.h>
#include <stdio.h>
#define cost1 3
#define cost2 2
#define books1 3
#define books2 2


int main()
{
   float cis_total= 0;
   float math_total = 0;
   float total_sales;
   float FIN_AID=.20;
  


   const char* cis_books[books1];
   cis_books[0] = "Programming 1";
   cis_books[1] = "Intro to Networking";
   cis_books[2] = "Javascript";

   float cis_cost[cost1];
   cis_cost[0] = 55.5;
   cis_cost[1] = 35.8;
   cis_cost[2] = 67.5;


   const char* math_books[books2];
   math_books[0] = "Calculus";
   math_books[1] = "Intro to Geometry";
   

   float math_cost[cost2];
   math_cost[0] = 25.5;
   math_cost[1] = 54.5;

   printf("Welcome to the ECPI Bookstore Reservation System!\n");
   printf("\nPlease see your totals below!\n");

   printf("\nCIS Book Totals:\n");
   for(int i=0;i<cost1;i++)
   {      
       if(i == cost1-1)
           puts(cis_books[i]);
           
       else
           puts(cis_books[i]);
   }
   for(int i=0; i <cost1; i++)
   {
       cis_total +=cis_cost[i];

       printf("\nCost of your CIS Books: $%.1f "), cis_cost[i];
   }

   printf("\n\nTotal for your CIS Books is $%.1f \n"), cis_total;


   printf("\nMath Book Totals:\n");
   
   
   for(int i=0; i<cost2; i++)
   {      
       if(i == cost2-1)
           puts(math_books[i]);
       else
           puts(math_books[i]);
   }

   for(int i=0; i<cost2; i++)
   {
       math_total +=math_cost[i];
       printf("\nCost of your Math Books: $%.1f\n"), cis_cost[i];
   }

   printf("\nTotal for your Math Books is $%.1f\n"), math_total;
   
   total_sales = cis_total + math_total;
   printf("Total Sales are $%.1f\n"), total_sales;

   double finance = total_sales * FIN_AID;
   printf("\nThe %.1f percent paid by Financial Aid is $%.1f\n"), FIN_AID*100, finance;

   printf("The total amount you must pay out of pocket is %.1f \n"),total_sales-finance;
    
    puts(cis_books[0]);

   return 0;
}
4

1 回答 1

1

更改了 printf 语句中的拼写错误(将括号放在末尾)

#include <string.h>
#include <stdio.h>
#define cost1 3
#define cost2 2
#define books1 3
#define books2 2


int main()
{
   float cis_total= 0;
   float math_total = 0;
   float total_sales;
   float FIN_AID=.20;
  


   const char* cis_books[books1];
   cis_books[0] = "Programming 1";
   cis_books[1] = "Intro to Networking";
   cis_books[2] = "Javascript";

   float cis_cost[cost1];
   cis_cost[0] = 39.5;
   cis_cost[1] = 50.8;
   cis_cost[2] = 73.5;


   const char* math_books[books2];
   math_books[0] = "Calculus";
   math_books[1] = "Intro to Geometry";
   

   float math_cost[cost2];
   math_cost[0] = 22.6;
   math_cost[1] = 46.2;

   printf("Welcome to the ECPI Bookstore Reservation System!\n");
   printf("\nPlease see your totals below!\n");

   printf("\nCIS Book Totals:\n");
   for(int i=0;i<cost1;i++)
   {      
       if(i == cost1-1)
           puts(cis_books[i]);
           
       else
           puts(cis_books[i]);
   }
   for(int i=0; i <cost1; i++)
   {
       cis_total +=cis_cost[i];

       // added the bracket at the end 
       printf("\nCost of your CIS Books: $%.1f ", cis_cost[i]);
   }

   // added the bracket at the end 
   printf("\n\nTotal for your CIS Books is $%.1f \n", cis_total);


   printf("\nMath Book Totals:\n");
   
   
   for(int i=0; i<cost2; i++)
   {      
       if(i == cost2-1)
           puts(math_books[i]);
       else
           puts(math_books[i]);
   }

   for(int i=0; i<cost2; i++)
   {
       math_total +=math_cost[i];
       // added the bracket at the end 
       printf("\nCost of your Math Books: $%.1f\n", cis_cost[i]);
   }

   // added the bracket at the end 
   printf("\nTotal for your Math Books is $%.1f\n", math_total);
   
   total_sales = cis_total + math_total;
   // added the bracket at the end 
   printf("Total Sales are $%.1f\n", total_sales);

   double finance = total_sales * FIN_AID;

   // added the bracket at the end 
   printf("\nThe %.1f percent paid by Financial Aid is $%.1f\n", FIN_AID*100, finance);

   // added the bracket at the end 
   printf("The total amount you must pay out of pocket is %.1f \n",total_sales-finance);
    
    puts(cis_books[0]);

   return 0;
}

输出:

Welcome to the ECPI Bookstore Reservation System!

Please see your totals below!

CIS Book Totals:
Programming 1
Intro to Networking
Javascript

Cost of your CIS Books: $39.5 
Cost of your CIS Books: $50.8 
Cost of your CIS Books: $73.5 

Total for your CIS Books is $163.8 

Math Book Totals:
Calculus
Intro to Geometry

Cost of your Math Books: $39.5

Cost of your Math Books: $50.8

Total for your Math Books is $68.8
Total Sales are $232.6

The 20.0 percent paid by Financial Aid is $46.5
The total amount you must pay out of pocket is 186.1 
Programming 1
于 2020-07-26T18:01:03.583 回答