C++中的简单逻辑.不允许使用IF语句

mik*_*941 0 c double int if-statement

所以我是编程和学习C/C++的新手.分配是创建一个简单的程序,用于计算项目的给定价格和数量的总和.我难以理解的部分是我们不允许在创建程序时使用"if或switch语句".我们需要询问用户该项目是否应纳税,使用1表示是,0表示否.现在我有程序计算两者并读出税收和非税收.任何帮助将不胜感激,我知道这是业余和最基本的编程.

#include <stdio.h>

#define TAX_RATE 0.065
int main() {

  int item_quantity, taxable;
  float item_price, total_with_tax, total_without_tax, a, b;

  // Read in the price of the item

  printf("What is the price of the item? (Should be less than $100)\n");
  scanf("%f<100", &item_price);

  // Read in the quantity of the item being purchased

  printf("How many of the item are you purchasing? (Should be less than 100) \n");
  scanf("%d<100", &item_quantity);

  // Read in if it is taxable or not

  printf("Is the item a taxed item (1 = yes, 0 = no)?\n");
  scanf("%d", &taxable);

  // Calculate total with tax

  a = item_quantity*item_price*(1 + TAX_RATE);

  // Calculate total without tax

  b = item_quantity*item_price;

  printf("Your total purchase will cost $%.2f\n", a);

  printf("Your total purchase will cost $%.2f\n", b);

  return 0;

}
Run Code Online (Sandbox Code Playgroud)

Jos*_*eld 7

所以taxable0或者1.现在看看这两行之间的相似性:

a = item_quantity*item_price*(1 + TAX_RATE);
b = item_quantity*item_price;
Run Code Online (Sandbox Code Playgroud)

你怎么能让这些单行使用值,taxable以便它执行第一行何时执行的taxable操作1以及第二行何时执行的taxable操作0?哪个位需要改变?你怎么能做到这一点?

既然这是你的任务,我认为这足以说明问题了.


你似乎在挣扎.好的,请考虑以下几行与上面两行完全相同:

a = item_quantity*item_price*(1 + TAX_RATE);
b = item_quantity*item_price*(1);
Run Code Online (Sandbox Code Playgroud)

他们之间有什么区别?有些东西消失了.使用基本数学运算符使一些东西消失的简单方法是什么?

答案与C++的任何特殊内容无关.这只是数学!