我有一个计算器应用程序,它要求用户输入一个算术表达式,例如 10 + 15
,后面跟一个 =
符号,然后按 Enter。程序应该计算表达式并打印结果。
但是,答案位于新行,即使我希望它位于同一行,例如
10 + 15 = 25
我也尝试使用ungetch
,但没有成功。
那么如何删除换行符以获得上面的结果呢?
最佳答案
如果您的终端支持它们,您可以使用终端转义码来移动光标。
#include <stdio.h>
int main ( ) {
char input[200] = "";
int ch = 0;
int count = 0;
printf ( "\033[2J");//clear screen and move cursor to upper left corner
printf ( "\033[8;H");//move cursor to line 8
printf("Enter equation\n");//\n advances to line 9
while ( ( ch = getchar ( )) != '\n' && ch != EOF) {
input[count] = ch;//store input for processing
count++;//count characters
}
printf ( "\033[9;%dH", count + 1);//move cursor to row 9 col count + 1
printf ( "answer here\n");
return 0;
}
关于c - 删除换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36290603/