首页 > 解决方案 > 如何在我的代码中增加 2.5% 的价格?

问题描述

我有一个关于我正在做的任务的问题。我正在编写一个应该从 books.txt 文件中读取的程序,显示图书信息。要求用户输入价格增加的百分比,更改所有书籍的价格。将更新的图书信息写回同一文件。再次从文件中读取,显示更新的图书信息。

我已经完成了代码的第一部分,但我不知道如何计算价格上涨的百分比。代码没有错误,但问题是程序无法识别代码的第二部分,即要求用户输入价格上涨百分比的部分。当我编译并运行代码时,它只返回第一部分。

这是 books.txt 文件:

ID: 321
Author: Harry Potter J. K. Rowling
Price: 20.00
 Year: 1997

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <windows.h>
#include <math.h>

int main(){
   FILE *file = fopen("books.txt", "r");
   if(file == NULL){
       puts("I'm sorry, but this file does not exist, and cannot be read.");
       exit(1);
   }

   struct NameofBook{
   int id;
   char author[30];
   int year;
   double price;
};
struct NameofBook nb1 = {135, "Twilight Stephenie Myer", 2005, 10.00};
struct NameofBook nb2, nb3, nb4;
strcpy(nb2.author, "Diary of a Wimpy Kid Jeff Kinney");
nb2.id = 246;
nb2.price = 15.00;
nb2.year = 2013;

nb3.id = 432;
strcpy(nb3.author, "The Hunger Games Suzanne Collins");
nb3.price = 25.00;
nb3.year = 2010;


nb4.id = 321;
strcpy(nb4.author, "Harry Potter J.K. Rowling");
nb4.price = 20.00;
nb4.year = 1997;


printf("\nThe 1st book is: \n Id: %d\n Author: %s\n Price: %f\n Year: %d\n \n", nb1.id, nb1.author, nb1.price, nb1.year);
printf("\nThe 2nd book is: \n Id: %d\n Author: %s\n Price: %f\n Year: %d\n \n", nb2.id, nb2.author, nb2.price, nb2.year);
printf("\nThe 3rd book is: \n Id: %d\n Author: %s\n Price: %f\n Year: %d\n \n", nb3.id, nb3.author, nb3.price, nb3.year);
printf("\nThe 4th book is: \n Id: %d\n Author: %s\n Price: %f\n Year: %d\n \n", nb4.id, nb4.author, nb4.price, nb4.year);
fscanf(file,"\n%d\n, %s\n %f\n, %d\n \n" ,nb1.id, nb1.author, nb1.price, nb1.year);
fscanf(file, "\n%d\n, %s\n %f\n, %d\n \n" ,nb2.id, nb2.author, nb2.price, nb2.year);
fscanf(file, "\n%d\n, %s\n, %f\n, %d\n \n" ,nb3.id, nb3.author, nb3.price, nb3.year);
fscanf(file, "\n%d\n, %s\n, %f\n, %d\n \n" ,nb4.id,nb4.author, nb4.price, nb4.year);

printf("Please enter the percent of price increase: \n");
float percentofpriceincrease = 0.025;
struct NameofBook nb1pricenew = {135, "Twilight, Stephanie Myer", 2005, 10.00* percentofpriceincrease};
struct NameofBook nb2pricenew, nb3pricenew, nb4pricenew;
strcpy(nb2pricenew.author, "Diary of a Wimpy Kid Jeff Kinney");
nb2pricenew.id = 246;
nb2pricenew.price = 15.00 * percentofpriceincrease;
nb2pricenew.year = 2013;
nb3pricenew.id = 432;
strcpy(nb3pricenew.author, "The Hunger Games Suzanne Collins");
nb3pricenew.price = 25.00 * percentofpriceincrease;
nb3pricenew.year = 2010;
nb4pricenew.id = 321;
strcpy(nb4pricenew.author, "Harry Potter J.K. Rowling");
nb4pricenew.price = 20.00* percentofpriceincrease;
nb4pricenew.year = 1997;
printf("The price is changed to: %f\n", percentofpriceincrease);
printf("\nThe 1st book's new price is: \n Id: %d\n Author: %s\n Price: %f\n Year: %d\n \n", nb1pricenew.id, nb1pricenew.author, nb1pricenew.price, nb1pricenew.year);
printf("\nThe 2nd book's new price is: \n Id: %d\n Author: %s\n Price: %f\n Year: %d\n \n", nb2pricenew.id, nb2pricenew.author, nb2pricenew.price, nb2pricenew.year);
printf("\nThe 3rd book is: \n Id: %d\n Author: %s\n Price: %f\n Year: %d\n \n", nb3.id, nb3.author, nb3.price, nb3.year);
printf("\nThe 4th book is: \n Id: %d\n Author: %s\n Price: %f\n Year: %d\n \n", nb4.id, nb4.author, nb4.price, nb4.year);
fscanf(file, "nb1.price * percentofpriceincrease", nb1pricenew.price);
fscanf(file, "nb2.price * percentofpriceincrease", nb2pricenew.price);
fscanf(file, "nb3.price * percentofpriceincrease", nb3pricenew.price);
fscanf(file, "nb4.price * percentofpriceincrease", nb4pricenew.price);

更新:这里的一些乐于助人的人要求我包括编译器警告。问题是根本没有编译器警告。当我运行代码时,只显示与打印所有书籍相关的代码的第一部分,但我编写的与价格上涨百分比有关的部分根本没有显示,好像我没有不写任何东西。

标签: c

解决方案


将价格提高 2.5% 乘以 1.025


推荐阅读