所以我對編程和學習C/C++很陌生。這項任務是創建一個簡單的程序,用於計算物品的給定價格和數量的總和。我被難住的部分是我們不允許在創建程序時使用「if或switch語句」。我們需要詢問用戶該項目是否應納稅,使用1作爲是和0作爲否。現在我有計劃來計算兩者並讀出含稅和不含稅。任何幫助將不勝感激,我知道這是業餘和最基本的編程。C++中的簡單邏輯。不允許使用IF語句
#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;
}
您可以嘗試[三元操作符(HTTP: //en.wikipedia.org/wiki/Ternary_operation)。由於這是一項家庭作業,我將使用它完成實施。 – marko