C语言新人问一个有关问题【自己花了点时间 没能成功解决】
C语言新人问一个问题【自己花了点时间 没能成功解决】
首先我想写一个TEST的小程序。
我是个新手, 刚刚学到指针。
我遇到的问题是这样的

我自己试着解决了一下 我在网上找到了 把光标移动到指定位置上的代码 自己测试了下

需要用到 X Y 坐标。 我用strlen 计算出来 比如第一个题的长度 然后X坐标就是 这个长度减1 光标就跳过了“)”,然后到了括号内 可是 Y坐标我没法确定 也就是行数。 我试着输入了一下 发现输入的东西会把
”)“ 给抵消了 就像开了insert模式似的
然后我就放弃了这种方法。
关于控制输入的问题 我个人选择的是 用if 语句 只接受A B C D 感觉好麻烦
这是问题三: 我在网上看到的控制输入的语句 是这样的:while (scanf ("%d", &a)== 1)
我个人的理解是 这样的:判断输入是否为1,为1则进入循环 但是我感觉理解错了
因为在primer plus 上看到不少这样控制输入的语句
都是 while (scanf ("%d", &a)== 1) 感觉不可能是 判断是不是1 这对输入的控制也太狭窄了
还有不懂的是 scanf 这个函数怎么还能 == 一个值 很奇怪
下面是我的问题 : 希望各位大牛回答小生问题是 附带上问题号 方便你我 感谢!
问题1: 我贴上那个光标控制代码的原理 【简单说下吧,我刚学到指针理解能力有限】
问题2:我想要实现的功能该怎么处理【我想要实现的功能,在第一张图里】
问题3:关于while (scanf ("%d", &a)== 1)【上面红色字体的标注一下部分, 是我对问题的详细描述】
------解决思路----------------------
问题3:scanf是有返回值的,它返回读取了几个变量。
比如这里的scanf ("%d", &a)
如果输入一个整数,那么返回值就是1,表示成功读取到1个整数。
如果输入一个字母,scanf读不到整数,返回0
------解决思路----------------------
用\b控制控制光标往移就行了
------解决思路----------------------
仅供参考:
首先我想写一个TEST的小程序。
我是个新手, 刚刚学到指针。
我遇到的问题是这样的
我自己试着解决了一下 我在网上找到了 把光标移动到指定位置上的代码 自己测试了下
需要用到 X Y 坐标。 我用strlen 计算出来 比如第一个题的长度 然后X坐标就是 这个长度减1 光标就跳过了“)”,然后到了括号内 可是 Y坐标我没法确定 也就是行数。 我试着输入了一下 发现输入的东西会把
”)“ 给抵消了 就像开了insert模式似的
然后我就放弃了这种方法。
关于控制输入的问题 我个人选择的是 用if 语句 只接受A B C D 感觉好麻烦
这是问题三: 我在网上看到的控制输入的语句 是这样的:while (scanf ("%d", &a)== 1)
我个人的理解是 这样的:判断输入是否为1,为1则进入循环 但是我感觉理解错了
因为在primer plus 上看到不少这样控制输入的语句
都是 while (scanf ("%d", &a)== 1) 感觉不可能是 判断是不是1 这对输入的控制也太狭窄了
还有不懂的是 scanf 这个函数怎么还能 == 一个值 很奇怪
下面是我的问题 : 希望各位大牛回答小生问题是 附带上问题号 方便你我 感谢!
问题1: 我贴上那个光标控制代码的原理 【简单说下吧,我刚学到指针理解能力有限】
问题2:我想要实现的功能该怎么处理【我想要实现的功能,在第一张图里】
问题3:关于while (scanf ("%d", &a)== 1)【上面红色字体的标注一下部分, 是我对问题的详细描述】
------解决思路----------------------
问题3:scanf是有返回值的,它返回读取了几个变量。
比如这里的scanf ("%d", &a)
如果输入一个整数,那么返回值就是1,表示成功读取到1个整数。
如果输入一个字母,scanf读不到整数,返回0
------解决思路----------------------
用\b控制控制光标往移就行了
#include <stdio.h>
int main(void)
{
char choice;
printf("1. 第一题的答案是( ) A.1 B.2 C.3 D.4\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
choice = getchar();
printf("Your choice is %c.\n", choice);
getchar();
return 0;
}
------解决思路----------------------
仅供参考:
#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(5000);
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;
SetConsoleCursorInfo(hStdOut, &cciCursor);
}
}
//Shows the console cursor
void ShowTheCursor()
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if(GetConsoleCursorInfo(hStdOut, &cciCursor))
{
cciCursor.bVisible = TRUE;
SetConsoleCursorInfo(hStdOut, &cciCursor);
}
}