简体   繁体   English

用C ++创建一个简单的文本编辑器

[英]Creating a simple text editor in C++

I'm in my CS162 class and my assignment for this week is to create a very, very simple text editor which prompts the user to input a paragraph, type # when finished, and then the program will make simple edits such as capitalizing any beginning-of-the-sentence words and changing common errors such as "teh" to "the." 我在CS162课上,本周的工作是创建一个非常非常简单的文本编辑器,该文本编辑器提示用户输入一个段落,完成后键入#,然后程序将进行简单的编辑,例如大写任何开头句子中的单词并将常见错误(例如“ teh”更改为“ the”)。 Now, I always have trouble getting started with these things; 现在,我总是很难开始这些事情。 I know exactly how I'm going to correct the errors (have the program search for misspellings and replace those words with the correct spelling/using .upper to change to upper case), but I can't get started on simply having the user input a paragraph and end it with #. 我确切地知道我将如何纠正错误(程序会搜索拼写错误并用正确的拼写替换这些单词/使用.upper更改为大写),但是我无法简单地开始让用户输入一个段落并以#结尾。 Would I use a loop that allows the user to continue typing until they type #? 我会使用允许用户继续键入直到输入#的循环吗? what would that look like? 那会是什么样? Sorry if this seems excessively basic; 抱歉,这似乎太基本了; I just always have trouble getting started with programs, since I am a very early beginner. 因为我是一个非常初级的初学者,所以我总是很难入门程序。 Thank You. 谢谢。

Use CONIO.H 使用CONIO.H

You can use functions like: 您可以使用以下功能:

getch() - reads a character from the console without buffer or echo getch()-从控制台读取字符,不带缓冲区或回显

kbhit() which determines if a keyboard key was pressed. kbhit()确定是否按下了键盘键。

to get what you're looking for. 得到您想要的东西。

Edit: This is from user Falcom Momot, for Linux systems: 编辑:这是从用户Falcom Momot,对于Linux系统:

#include <unistd.h>
#include <termios.h>
char getch() {
    char buf = 0;
    struct termios old = {0};
    if (tcgetattr(0, &old) < 0)
            perror("tcsetattr()");
    old.c_lflag &= ~ICANON;
    old.c_lflag &= ~ECHO;
    old.c_cc[VMIN] = 1;
    old.c_cc[VTIME] = 0;
    if (tcsetattr(0, TCSANOW, &old) < 0)
            perror("tcsetattr ICANON");
    if (read(0, &buf, 1) < 0)
            perror ("read()");
    old.c_lflag |= ICANON;
    old.c_lflag |= ECHO;
    if (tcsetattr(0, TCSADRAIN, &old) < 0)
            perror ("tcsetattr ~ICANON");
    return (buf);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM