Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

AleksandarN1k avatar AleksandarN1k 0 Точки

03. The Pianist Problem

https://pastebin.com/kcXbkJWH

Здревейте! Опитвам се да реша дадената задача от 

01. Programming Fundamentals Final Exam Retake c#,но при изхода не ми изписва последните два примера

Моля за помощ!

 

You are a pianist, and you like to keep a list of your favorite piano pieces. Create a program to help you organize it and add, change, remove pieces from it!

On the first line of the standard input, you will receive an integer n – the number of pieces you will initially have. On the next n lines, the pieces themselves will follow with their composer and key, separated by "|" in the following format: "{piece}|{composer}|{key}".

Then, you will be receiving different commands, each on a new line, separated by "|", until the "Stop" command is given:

  • "Add|{piece}|{composer}|{key}":
    • You need to add the given piece with the information about it to the other pieces and print:

"{piece} by {composer} in {key} added to the collection!"

  • If the piece is already in the collection, print:
  • {piece} is already in the collection!"
  • "Remove|{piece}":
    • If the piece is in the collection, remove it and print:

"Successfully removed {piece}!"

  • Otherwise, print:

"Invalid operation! {piece} does not exist in the collection."

  • "ChangeKey|{piece}|{new key}":
    • If the piece is in the collection, change its key with the given one and print:

"Changed the key of {piece} to {new key}!"

  • Otherwise, print:

"Invalid operation! {piece} does not exist in the collection."

Upon receiving the "Stop" command, you need to print all pieces in your collection in the following format:
"{Piece} -> Composer: {composer}, Key: {key}"

Input/Constraints

  • You will receive a single integer at first – the initial number of pieces in the collection.
  • For each piece, you will receive a single line of text with information about it.
  • Then you will receive multiple commands in the way described above until the command "Stop".

Output

  • All the output messages with the appropriate formats are described in the problem description.

Examples

Input

Output

3

Fur Elise|Beethoven|A Minor

Moonlight Sonata|Beethoven|C# Minor

Clair de Lune|Debussy|C# Minor

Add|Sonata No.2|Chopin|B Minor

Add|Hungarian Rhapsody No.2|Liszt|C# Minor

Add|Fur Elise|Beethoven|C# Minor

Remove|Clair de Lune

ChangeKey|Moonlight Sonata|C# Major

Stop

Sonata No.2 by Chopin in B Minor added to the collection!

Hungarian Rhapsody No.2 by Liszt in C# Minor added to the collection!

Fur Elise is already in the collection!

Successfully removed Clair de Lune!

Changed the key of Moonlight Sonata to C# Major!

Fur Elise -> Composer: Beethoven, Key: A Minor

Moonlight Sonata -> Composer: Beethoven, Key: C# Major

Sonata No.2 -> Composer: Chopin, Key: B Minor

Hungarian Rhapsody No.2 -> Composer: Liszt, Key: C# Minor

Comments

After we receive the initial pieces with their info, we start receiving commands. The first two commands are to add a piece to the collection, and since the pieces are not already added, we manage to add them. The third add command, however, attempts to add a piece, which is already in the collection, so we print a special message and don't add the piece. After that, we receive the remove command, and since the piece is in the collection, we remove it successfully.
Finally, the last command says to change the key of a piece. Since the key is present in the collection, we modify its key.
We receive the Stop command, print the information about the pieces, and the program ends.

Input

Output

4

Eine kleine Nachtmusik|Mozart|G Major

La Campanella|Liszt|G# Minor

The Marriage of Figaro|Mozart|G Major

Hungarian Dance No.5|Brahms|G Minor

Add|Spring|Vivaldi|E Major

Remove|The Marriage of Figaro

Remove|Turkish March

ChangeKey|Spring|C Major

Add|Nocturne|Chopin|C# Minor

Stop

Spring by Vivaldi in E Major added to the collection!

Successfully removed The Marriage of Figaro!

Invalid operation! Turkish March does not exist in the collection.

Changed the key of Spring to C Major!

Nocturne by Chopin in C# Minor added to the collection!

Eine kleine Nachtmusik -> Composer: Mozart, Key: G Major

La Campanella -> Composer: Liszt, Key: G# Minor

Hungarian Dance No.5 -> Composer: Brahms, Key: G Minor

Spring -> Composer: Vivaldi, Key: C Major

Nocturne -> Composer: Chopin, Key: C# Minor

Тагове:
0
C# Fundamentals 27/11/2023 19:43:45
icowwww avatar icowwww 2673 Точки

Здравей,

1. В команда Add не добавяш към речника

2. В команда ChangeKey са объркани условията на проверките. 

https://pastebin.com/cg3jUFC3

В допълнение: Имаш излишни използвания на continue в if-else структура.

Няма смисъл да използваш continue, когато така или иначе няма възможност да се изпълни код преди да се завърти цикъла.

Например имаш цикъл, който съдържа if-else структура, ти ще влезнеш в едно от двете. Няма как да влезнеш в else, ако е изпълнено условието на if-a:

var a = 1; var b = 5;
for (int i = 0; i < 10; i++)
{
    a++;
    if (a == b)
    {
        Console.WriteLine("equal");
        continue;
    }
    else
    {
        Console.WriteLine("not equal");
    }
}

Тук има continue, което така или иначе ще се изпълни. Затова или махаш continue, или махаш else

var a = 1; var b = 5;
for (int i = 0; i < 10; i++)
{
    a++;
    if (a == b)
    {
        Console.WriteLine("equal");
        continue;
    }
    Console.WriteLine("not equal");
}

 

1
AleksandarN1k avatar AleksandarN1k 0 Точки

Здравей, благодаря ти, че помогна. :)

 

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.