Total value = 1.23 |
Вход и изход
#include <iostream>
using namespace std;
int main()
{
cout << "How
many pennies do you have? ";
int pennies;
cin >> pennies;
cout << "How
many nickels do you have? ";
int nickels;
cin >> nickels;
cout << "How
many dimes do you have? ";
int dimes;
cin >> dimes;
cout << "How
many quarters do you have? ";
int quarters;
cin >> quarters;
double total = pennies
* 0.01 + nickels * 0.05 +
dimes * 0.10 + quarters * 0.25;
/* total value
of the coins */
cout << "Total
value = " << total << "\n";
return 0;
}
How many pennies do you
have? 10
How many nickels do you have? 3 How many dimes do you have? 7 How many quarters do you have? 3 Total value = 1.70 |
10 3 7 3 |
10 3 7
3 |
cout << "How
many nickels do you have? ";
cin >> count;
total = count * 0.05
+ total;
cout << "How
many dimes do you have? ";
cin >> count;
total = count * 0.10
+ total;
cout << "How
many quarters do you have? ";
cin >> count;
total = count * 0.25
+ total;
cout << "Total
value = " << total << "\n";
return 0;
}
* текуща стойност на променлива
* дефиниция на променлива (единствена
!)
-- с инициализация
double total = count * 0.01;
-- без инициализация
int
count;
* оператор за присвояване
total = count * 0.05 + total;
* знакът за равенство в математиката
и в С++
* съкращение за оператора добавяне
на 1 и изваждане на 1
month=month+1
month++
month=month-1
month--
Константи
#include <iostream>
using namespace std;
int main()
{
double bottles;
cout << "How
many bottles do you have? ";
cin >> bottles;
double cans;
cout << "How
many cans do you have? ";
cin >> cans;
double total = bottles * 2 + cans * 0.355;
cout << "The
total volume is " << total << "\n";
return 0;
}
How many bottles do you
have? 5
How many cans do you have? 4 Total volume is 11.42 |
double cans;
cout << "How
many cans do you have? ";
cin >> cans;
const double BOTTLE_VOLUME
= 2.0;
const double CAN_VOLUME
= 0.355;
double total = bottles*BOTTLE_VOLUME
+ cans*CAN_VOLUME;
cout << "The
total volume is " << total << " liter.\n";
return 0;
}
Аритметика
-- събиране | + | a+b |
-- изваждане | - | a-b |
-- умножение | * | a*b |
-- деление | / | a/b |
-- остатък от деление | % | a%b |
операция | резултат | операция | резултат | операция | резултат |
1.25+2.0 | 3.25 | 4.2/2.0 | 2.1 | 5%2 | 1 |
1.25+2 | 3.25 | 5.0/2 | 2.5 | 34%2 | 0 |
1+2 | 3 | 5/2 | 2 | 5.1%2 | грешка |