그래서 프로그래밍 및 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를 시도 할 수 :
는
수학되고, 0 인 경우에 가산되는 방식으로 방정식에 과세 변수를 추가 //en.wikipedia.org/wiki/Ternary_operation). 이것이 숙제 임으로 저는 구현을 그대로 두겠습니다. – marko