如何实现scanf输入数值后,不换行printf输出?

xraycrl 2011-08-09 03:09:05
比如用scanf输入波长的数值后紧接着用printf输出单位“m”,而不会出现换行后才出现“m”?求大虾们指点,谢谢!
...全文
2296 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yan123wwwy 2011-08-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhao4zhong1 的回复:]
C/C++ code
#include <windows.h>
#include <stdio.h>

void ConPrint(char *CharBuffer, int len);
void ConPrintAt(int x, int y, char *CharBuffer, int len);
void gotoXY(int x, int y);
void ClearCons……
[/Quote]
这位大哥,您这是。。。。肿么了。。。
_了凡_ 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhao4zhong1 的回复:]

C/C++ code
#include <windows.h>
#include <stdio.h>

void ConPrint(char *CharBuffer, int len);
void ConPrintAt(int x, int y, char *CharBuffer, int len);
void gotoXY(int x, int y);
void ClearConsole(v……
[/Quote]
6楼的这个我也搞过,蛮有意思的,可以实现类似TC中conio.h的功能:
http://download.csdn.net/source/2604163
楼主的问题实际上就是“不用回车即可获得输入的内容”,这个要用系统API的,
上面的链接有兴趣的话看看吧,里面有输入立即返回的函数,之后的程序就是逻辑的
问题了。
吾子墨鸿 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 babilife 的回复:]
C/C++ code

char str[1000];
scanf("%s",str);
printf("%d m",atoi(str));
[/Quote]
这个就符合要求了!
xraycrl 2011-08-09
  • 打赏
  • 举报
回复
那就直接用系统api,别用scanf。
另外,你得想下怎么让系统知道你输入完了。scanf是敲下回车。



api是什么?
敲下回车是表示输入结束,并不表示换行啊?
赵4老师 2011-08-09
  • 打赏
  • 举报
回复
#include <windows.h>
#include <stdio.h>

void ConPrint(char *CharBuffer, int len);
void ConPrintAt(int x, int y, char *CharBuffer, int len);
void gotoXY(int x, int y);
void ClearConsole(void);
void ClearConsoleToColors(int ForgC, int BackC);
void SetColorAndBackground(int ForgC, int BackC);
void SetColor(int ForgC);
void HideTheCursor(void);
void ShowTheCursor(void);

int main(int argc, char* argv[])
{
HideTheCursor();
ClearConsoleToColors(15, 1);
ClearConsole();
gotoXY(1, 1);
SetColor(14);
printf("This is a test...\n");
Sleep(500);
ShowTheCursor();
SetColorAndBackground(15, 12);
ConPrint("This is also a test...\n", 23);
SetColorAndBackground(1, 7);
ConPrintAt(22, 15, "This is also a test...\n", 23);
gotoXY(0, 24);
SetColorAndBackground(7, 1);
return 0;
}

//This will clear the console while setting the forground and
//background colors.
void ClearConsoleToColors(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
//not used but we need to capture this since it will be
//written anyway (passing NULL causes an access violation).
DWORD count;

//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
SetConsoleTextAttribute(hStdOut, wColor);
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
}

//This will clear the console.
void ClearConsole()
{
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
// not used but we need to capture this since it will be
// written anyway (passing NULL causes an access violation).
DWORD count;
//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
}

//This will set the position of the cursor
void gotoXY(int x, int y)
{
//Initialize the coordinates
COORD coord = {x, y};
//Set the position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

//This will set the forground color for printing in a console window.
void SetColor(int ForgC)
{
WORD wColor;
//We will need this handle to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;

//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
}

//This will set the forground and background color for printing in a console window.
void SetColorAndBackground(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}

//Direct console output
void ConPrint(char *CharBuffer, int len)
{
DWORD count;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL);
}

//Direct Console output at a particular coordinate.
void ConPrintAt(int x, int y, char *CharBuffer, int len)
{
DWORD count;
COORD coord = {x, y};
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hStdOut, coord);
WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
}

//Hides the console cursor
void HideTheCursor()
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

if(GetConsoleCursorInfo(hStdOut, &cciCursor))
{
cciCursor.bVisible = FALSE;
}
}

//Shows the console cursor
void ShowTheCursor()
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

if(GetConsoleCursorInfo(hStdOut, &cciCursor))
{
cciCursor.bVisible = TRUE;
}
}

大IP 2011-08-09
  • 打赏
  • 举报
回复
你想要的结果是在控制台输入:5m,4m这样的数据,以表示是波长而非简单的数字。要实现这个操作不难,只要注意处理m字符就行了。请看如下代码:

#include <stdio.h>

int main(int argc, char** argv)
{
int t;
int z;
scanf("%d", &t);
getchar();---去除键盘缓冲区内的m
scanf("%d",&z);
getchar();--去除键盘缓冲区内的m
printf("%d\n",z);
printf("%d\n",t);

return 0;
}

在终端输入:

4m
5m

结果为:
4
5
wangw89 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 babilife 的回复:]
C/C++ code

char str[1000];
scanf("%s",str);
printf("%d m",atoi(str));
[/Quote]觉得这个可以吧
至善者善之敌 2011-08-09
  • 打赏
  • 举报
回复

char str[1000];
scanf("%s",str);
printf("%d m",atoi(str));

BABY 2011-08-09
  • 打赏
  • 举报
回复
getchar
luciferisnotsatan 2011-08-09
  • 打赏
  • 举报
回复
那就直接用系统api,别用scanf。
另外,你得想下怎么让系统知道你输入完了。scanf是敲下回车。

70,018

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧