Практикум по программированию. Основы. Обработка строк. Телеграмма

From AsIsWiki
Jump to: navigation, search

Назад · Задачи


Contents

Java

import java.util.Scanner;

public class Task15 {

    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in, "CP1251");

        System.out.println();
        System.out.println(" Телеграмма ");
        System.out.println("--------------------------------------");

        System.out.print(" Вид (1-простая, 2-срочная): ");
	int vid = Integer.parseInt(in.nextLine());

        System.out.print(" Тип (1-обычная, 2-поздравительная): ");
	int tip = Integer.parseInt(in.nextLine());

        System.out.print(" Текст: ");
	String s = in.nextLine().trim();
        
        // Удаляем лишние пробелы
        while (s.indexOf("  ") > 0) {
            s = s.replace("  ", " ");
        }
        
        int words = 0, pos = 0;
        
        // Подсчитываем слова
        while (pos >= 0) {
            pos = s.indexOf(" ", pos + 1);
            words++;
        }
        
        double price = 1;
        
        if (vid == 1) {
            price = price + words * 0.3;
        } else {
            price = price + words * 0.5;            
        }
        
        if (tip == 2) {
            price = price + 0.5;
        }
        
        System.out.println("--------------------------------------");
        System.out.printf(" Стоимость телеграммы: %.2f\n", price);
    }
}


C++



Pascal




Назад · Задачи

Personal tools
Namespaces

Variants
Actions
Navigation
Tools