How to terminate a program with Ctrl-D?

后端 未结 5 1271
太阳男子
太阳男子 2021-01-20 01:06

I am trying to write a simple program that simulates a calculator. I would like the program to exit or turn-off when the Ctrl+D keystroke is made. I se

5条回答
  •  粉色の甜心
    2021-01-20 01:20

    You could make life easier and scan for a keyword like "quit" or "exit". Last time I used a calculator, these words were not on the keypad.

    Many simple calculator programs read the input as text terminated by a newline. They parse the text in memory, then spit out an answer.

    Other more sophisticated calculator programs use windows and are event driven. Some use a button like "=" to signal end of input and start of calculation (doesn't work on RPN calculators).

    These techniques remove the hassle of determining End Of Text input using a control character sequence and are more portable.

提交回复
热议问题