很简单的几个小问题,望各位大虾帮帮忙。

tianxingjian1273 2002-08-02 01:18:17
你们好,我是一名C++初学者,有几个很小的问题想请教一下。
1.const float pi=3.141592653; 因为float常量只能存储7位有效位精度的实数,所以pi的实际值为3.141593(最后1位4舍5入)。
可是我在VC中输入该语句时,pi值却为:3.14159. 我知道C++默认的流输出数值有效位是6,但float真的是可存储7位有效位啊,想请问一下为什么。
2.getche() 这个在<conio.h>上的函数,我也不知道它是不是一个函数,我总是找不到它是起什么作用的,麻烦帮我解释一下。
3.我看过一本书,里面有这样说法:π是常量,它在C++中有专门的表示,可以利用之,其名称为M_PI,在math.h的头文件中。可是我在VC中用M_PI时VC却无法识别,这是为什么,想请教一下。
4.有一段程序:

#include <iostream.h>

void main()
{
int a;
cout <<"please input a number:\n";
cin >>a;

int c1 = a%3 ==0;
int c2 = a%5 ==0;
int c3 = a%7 ==0;

switch((c1<<2)+(c2<<1)+c3)
{
case 0: cout <<"不能被3,5,7整除.\n"; break;
case 1: cout <<"只能被7整除.\n"; break;
case 2: cout <<"只能被5整除.\n"; break;
case 3: cout <<"可以被5,7整除.\n"; break;
case 4: cout <<"只能被3整除.\n"; break;
case 5: cout <<"可以被3,7整除.\n"; break;
case 6: cout <<"可以被3,5整除.\n"; break;
case 7: cout <<"可以被3,5,7整除.\n";
}
}
其中switch中的表达式我百思不得其解,麻烦解释一下。
THANKS。


...全文
102 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
andy_lau 2002-08-02
  • 打赏
  • 举报
回复
恩,同意Zark(金陵五月) ( )的解释,正确
另外我补充一下:getch()通常用来程序停顿
vc并不是完全符合c++的标准,所以不免有点出入
Zark 2002-08-02
  • 打赏
  • 举报
回复
1.如果你用printf()就会发现float 仍是7位精度.但cout在用<<进行输出改为了6位.
2.getch()是用来从控制台上读一个字符,getche()是用来从控制台上读一个字符,并将其回显到输出控制上.
3. PI在math.h并没有定义.在其他平台上是否用我不清楚.
4. switch的参数中只是使用了两次左移符,i<<2 相当于将i乘上4,i<<1相当于将i乘上2.这样就可以得到0-7的情况,用以枚举被3,5,7整除的情况.

liuns 2002-08-02
  • 打赏
  • 举报
回复
在vc里用标准C++函数是有兼容性问题的,你提到的就是这个属于问题.

_getch, _getche
Get a character from the console without echo (_getch) or with echo (_getche).

int _getch( void );

int _getche( void );

Routine Required Header Compatibility
_getch <conio.h> Win 95, Win NT
_getche <conio.h> Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

Both _getch and _getche return the character read. There is no error return.

Remarks

The _getch function reads a single character from the console without echoing. _getche reads a single character from the console and echoes the character read. Neither function can be used to read CTRL+C. When reading a function key or an arrow key, _getch and _getche must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.

Example

/* GETCH.C: This program reads characters from
* the keyboard until it receives a 'Y' or 'y'.
*/

#include <conio.h>
#include <ctype.h>

void main( void )
{
int ch;

_cputs( "Type 'Y' when finished typing keys: " );
do
{
ch = _getch();
ch = toupper( ch );
} while( ch != 'Y' );

_putch( ch );
_putch( '\r' ); /* Carriage return */
_putch( '\n' ); /* Line feed */
}


Output

Type 'Y' when finished typing keys: Y


ckacka 2002-08-02
  • 打赏
  • 举报
回复
#include <iostream.h>

void main()
{
int a;
cout <<"please input a number:\n";
cin >>a;

int c1 = (a%3 ==0); // 虽然合理利用了算符优先级的定义,
int c2 = (a%5 ==0); // 不过建议在程序中明确指出预算顺序。
int c3 = (a%7 ==0); // 这里隐式地把 bool 类型转化为 int
// 类型,true->1, false->0
// 用来鉴别是否可以整除。

int s1 = (c1<<2); // Shift Operater (移位运算子)
int s2 = (c2<<1); // 基于二进制的,适用于高速运算。
int s3 = c3; // 这里使用的是左移运算子 <<
// 其目的是为了产生在二进制运算中
// 分布在 2 1 0 三个位上的组合
// 于是得到以下几种不同的结果

switch(s1+s2+s3)
{
case 0: cout <<"不能被3,5,7整除.\n"; break;
case 1: cout <<"只能被7整除.\n"; break;
case 2: cout <<"只能被5整除.\n"; break;
case 3: cout <<"可以被5,7整除.\n"; break;
case 4: cout <<"只能被3整除.\n"; break;
case 5: cout <<"可以被3,7整除.\n"; break;
case 6: cout <<"可以被3,5整除.\n"; break;
case 7: cout <<"可以被3,5,7整除.\n";
}
}

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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