fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. float calculateCost(int duration, bool sameOperator) {
  7. const int first30Rate = 3; // 3 den per minute for first 30 minutes
  8. const int after30Rate = 2; // 2 den per minute after 30 minutes
  9. float cost = 0;
  10.  
  11. if (duration <= 30) {
  12. cost = duration * first30Rate;
  13. } else {
  14. cost = (30 * first30Rate) + ((duration - 30) * after30Rate);
  15. }
  16.  
  17. // Apply 30% discount if both numbers are from the same operator
  18. if (sameOperator) {
  19. cost *= 0.7;
  20. }
  21.  
  22. return cost;
  23. }
  24.  
  25. bool sameOperator(const string &num1, const string &num2) {
  26. // Assume numbers are from the same operator if first 3 digits match
  27. return num1.substr(0, 3) == num2.substr(0, 3);
  28. }
  29.  
  30. int main() {
  31. string phoneNumber1, phoneNumber2;
  32. int duration;
  33.  
  34. cout << "Enter the first phone number: ";
  35. cin >> phoneNumber1;
  36. cout << "Enter the second phone number: ";
  37. cin >> phoneNumber2;
  38. cout << "Enter the duration of the call (in minutes): ";
  39. cin >> duration;
  40.  
  41. // Check if numbers are valid (9 digits and start with 0)
  42. if (phoneNumber1.length() != 9 || phoneNumber1[0] != '0' ||
  43. phoneNumber2.length() != 9 || phoneNumber2[0] != '0') {
  44. cout << "Invalid phone number format." << endl;
  45. return 1;
  46. }
  47.  
  48. bool isSameOperator = sameOperator(phoneNumber1, phoneNumber2);
  49. float cost = calculateCost(duration, isSameOperator);
  50.  
  51. cout << "The cost of the call is: " << cost << " den" << endl;
  52.  
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0.01s 5296KB
stdin
075100100 075200200 10
stdout
Enter the first phone number: Enter the second phone number: Enter the duration of the call (in minutes): The cost of the call is: 21 den