我生成的Complex类(复数类),在接收一个复数(形如 a+bi)的时候,用的是指针接受此字符串,然后经过分析,把字符串中的实部子串,虚部子串取下来,然后用字符串转换函数来把char型变成float类型数据,但是这样非常麻烦!请问大家有什么高招吗?
另外,即便是这样转换,我也遇到了问题,就是使用C中的atof()函数,似乎VC++里边不认识,(头文件已经包含),请问在VC++里边,对于这种char转换成float的,有什么函数可以完成吗?参数以及返回值是什么?
谢谢了!!!
16 个解决方案
#1
#include<math.h>
double atof(const char*)
//VC++ 里是可以用的只不过返回的是double型别的。用强制转换(不推荐是呀强制转换^v^)
double atof(const char*)
//VC++ 里是可以用的只不过返回的是double型别的。用强制转换(不推荐是呀强制转换^v^)
#2
#include<iostream.h>
#include<math.h>
void main()
{
float a;
a=(float)atof("123.456");
cout<<a<<endl;
}
#include<math.h>
void main()
{
float a;
a=(float)atof("123.456");
cout<<a<<endl;
}
#3
楼上的两位仁兄,我用的这个函数,你们看看,我已经包含了这个函数啊!
imaginaryPart = static_cast < float > atof ( imag )
这里边imag是从复数上分离出来的虚部数据,imaginaryPart是类中的虚部数据成员,在类里边的虚部和实部我用的是float型,这里强制转换为float型,但是编译还是出现错误啊!
请问为什么啊?
我的类定义在complex_class.h头文件中,成员函数定义在一个member_define.cpp文件中,main()在main.cpp中
编译之后出现以下错误:
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2146: syntax error : missing '(' before identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2146: syntax error : missing '(' before identifier 'atof'
Error executing cl.exe.
main.exe - 4 error(s), 0 warning(s)
提示的还是atof()函数出现错误,为什么啊~~~~真急死了!!!!!!
imaginaryPart = static_cast < float > atof ( imag )
这里边imag是从复数上分离出来的虚部数据,imaginaryPart是类中的虚部数据成员,在类里边的虚部和实部我用的是float型,这里强制转换为float型,但是编译还是出现错误啊!
请问为什么啊?
我的类定义在complex_class.h头文件中,成员函数定义在一个member_define.cpp文件中,main()在main.cpp中
编译之后出现以下错误:
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2146: syntax error : missing '(' before identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2146: syntax error : missing '(' before identifier 'atof'
Error executing cl.exe.
main.exe - 4 error(s), 0 warning(s)
提示的还是atof()函数出现错误,为什么啊~~~~真急死了!!!!!!
#4
你没有包含以下头文件
#include<math.h>
#include<math.h>
#5
push on
#6
to 化石:
我绝对已经包含了math.h头文件了!
但是现在还出现这个问题,实在太奇怪啦!!!
我绝对已经包含了math.h头文件了!
但是现在还出现这个问题,实在太奇怪啦!!!
#7
up
#8
复数的实部和虚部为什么要用char类型?
#9
楼主可否将代码贴出来看看
不要写太长的代码,你只要试一下atof在你的机子上能不能就可以了。如果不能用将你所测试的代码贴出来。
不要写太长的代码,你只要试一下atof在你的机子上能不能就可以了。如果不能用将你所测试的代码贴出来。
#10
C++中atof()函数是包含在stdlib.h里面的,不是math.h
#11
代码看看
#12
cin能够完美解决你这个问题,假定一次输入一个复数,以回车结束。
cin << float << char << float << char;---12.3 + 34.4 i
就是连着输入也能自动识别,只是要是不按这个格式输入,就死循环了,当然了,不要给自己找麻烦。
cin << float << char << float << char;---12.3 + 34.4 i
就是连着输入也能自动识别,只是要是不按这个格式输入,就死循环了,当然了,不要给自己找麻烦。
#13
to 幻星:
谢谢你的回复,我不明白的是,复数又有数据(实部,虚部),又有运算符号(+/-,i),不用char类型指针接收用什么呢?会不会还有什么更好的办法?我不想用数组
to happycock:
谢谢你的回复,这个cin是怎么用的,这些东西(floa char float),是不是指的变量啊?
谢谢你的回复,我不明白的是,复数又有数据(实部,虚部),又有运算符号(+/-,i),不用char类型指针接收用什么呢?会不会还有什么更好的办法?我不想用数组
to happycock:
谢谢你的回复,这个cin是怎么用的,这些东西(floa char float),是不是指的变量啊?
#14
我还是把代码贴出来吧!麻烦大家给分析分析,谢谢了~~~~~~~~~~~
------------------------
这是类型定义的头文件:complex_class.H
# ifndef COMPLEX_CLASS_H
# define COMPLEX_CLASS_H
class Complex
{
public:
Complex();
void getComplexNumber( char * );
float visitRealPart();
float visitImaginaryPart();
private:
float realPart;
char flag;
float imaginaryPart;
};
# endif
-----------------------------------------------
这是成员函数定义文件:member_define.cpp
# include <iostream.h>
# include <string.h>
# include <iomanip.h>
# include <math.h>
# include "complex_class.h"
Complex::Complex()
{
realPart = 0.0;
imaginaryPart = 0.0;
}
void Complex::getComplexNumber( char *complexC )
{
char *real, *imag, *next, *tmp;
tmp = complexC;
for ( int i = 1; ( ( *tmp ) != '+'); i++, tmp++ );
strncpy ( real, complexC, i );
flag = *tmp;
realPart = static_cast < float > atof ( real );
next = ++tmp;
for ( int j = 1; ( ( *next ) != 'i'); i++, next++ );
strncpy ( imag, next, j );
imaginaryPart = static_cast < float > atof ( imag );
if ( flag == '-' )
imaginaryPart *= ( -1 );
}
float Complex::visitRealPart()
{
return realPart;
}
float Complex::visitImaginaryPart()
{
return imaginaryPart;
}
---------------------------------------------
这是main()函数所在的文件: main.cpp
# include <iostream.h>
# include "member_define.cpp"
# include "complex_class.h"
int main()
{
char method, *complexNum1, *complexNum2;
float real, imag;
Complex c1,c2;
cout << "\n\nPlease in put two complex numbers"
<< " when the computer remind!";
cout << endl << "\nPlease input the first complex number(c1):";
cin >> complexNum1;
c1.getComplexNumber( complexNum1 );
cout << "\nPlease input the operation method ( + or - ):";
cin >> method;
cout << endl << "\nPlease input the second complex number(c2):";
cin >> complexNum2;
c2.getComplexNumber( complexNum1 );
switch ( method )
{
case '+':
{
real = c1.visitRealPart() + c2.visitRealPart();
imag = c1.visitImaginaryPart() + c2.visitImaginaryPart();
break;
}
case '-':
{
real = c1.visitRealPart() - c2.visitRealPart();
imag = c1.visitImaginaryPart() - c2.visitImaginaryPart();
break;
}
default:
{
cout << "\n***********************************************"
<< "\n*Never can reach here, the program is wrong!!!*"
<< "\n***********************************************";
}
}
cout << "\nc1" << method << "c1 = "
<< real << method << imag << "i" << endl;
return 0;
}
------------------------------------------------------------------
代码写的很烂,大家不要笑我,我是初学者... ...
------------------------
这是类型定义的头文件:complex_class.H
# ifndef COMPLEX_CLASS_H
# define COMPLEX_CLASS_H
class Complex
{
public:
Complex();
void getComplexNumber( char * );
float visitRealPart();
float visitImaginaryPart();
private:
float realPart;
char flag;
float imaginaryPart;
};
# endif
-----------------------------------------------
这是成员函数定义文件:member_define.cpp
# include <iostream.h>
# include <string.h>
# include <iomanip.h>
# include <math.h>
# include "complex_class.h"
Complex::Complex()
{
realPart = 0.0;
imaginaryPart = 0.0;
}
void Complex::getComplexNumber( char *complexC )
{
char *real, *imag, *next, *tmp;
tmp = complexC;
for ( int i = 1; ( ( *tmp ) != '+'); i++, tmp++ );
strncpy ( real, complexC, i );
flag = *tmp;
realPart = static_cast < float > atof ( real );
next = ++tmp;
for ( int j = 1; ( ( *next ) != 'i'); i++, next++ );
strncpy ( imag, next, j );
imaginaryPart = static_cast < float > atof ( imag );
if ( flag == '-' )
imaginaryPart *= ( -1 );
}
float Complex::visitRealPart()
{
return realPart;
}
float Complex::visitImaginaryPart()
{
return imaginaryPart;
}
---------------------------------------------
这是main()函数所在的文件: main.cpp
# include <iostream.h>
# include "member_define.cpp"
# include "complex_class.h"
int main()
{
char method, *complexNum1, *complexNum2;
float real, imag;
Complex c1,c2;
cout << "\n\nPlease in put two complex numbers"
<< " when the computer remind!";
cout << endl << "\nPlease input the first complex number(c1):";
cin >> complexNum1;
c1.getComplexNumber( complexNum1 );
cout << "\nPlease input the operation method ( + or - ):";
cin >> method;
cout << endl << "\nPlease input the second complex number(c2):";
cin >> complexNum2;
c2.getComplexNumber( complexNum1 );
switch ( method )
{
case '+':
{
real = c1.visitRealPart() + c2.visitRealPart();
imag = c1.visitImaginaryPart() + c2.visitImaginaryPart();
break;
}
case '-':
{
real = c1.visitRealPart() - c2.visitRealPart();
imag = c1.visitImaginaryPart() - c2.visitImaginaryPart();
break;
}
default:
{
cout << "\n***********************************************"
<< "\n*Never can reach here, the program is wrong!!!*"
<< "\n***********************************************";
}
}
cout << "\nc1" << method << "c1 = "
<< real << method << imag << "i" << endl;
return 0;
}
------------------------------------------------------------------
代码写的很烂,大家不要笑我,我是初学者... ...
#15
这是编译出错信息:
----------------------------------------------------------
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2146: syntax error : missing '(' before identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2146: syntax error : missing '(' before identifier 'atof'
Error executing cl.exe.
main.exe - 4 error(s), 0 warning(s)
----------------------------------------------------------
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2146: syntax error : missing '(' before identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2146: syntax error : missing '(' before identifier 'atof'
Error executing cl.exe.
main.exe - 4 error(s), 0 warning(s)
#16
是的,变量类型,换成对应的变量名就可以了。
cin是从标准设备(键盘)读取,自动类型匹配,你要是读入一个数字,结果却敲了一个字符,它会不认;等输入完事,会在输入流中自动截取。
就像《C++编程思想》说的,你想要的数据就被魔术般的创造出来了。
我是在输入表达式的时候发现的,你可以看看这篇,看看我是怎么输入一个表达式的
http://www.csdn.net/develop/read_article.asp?id=19372
cin是从标准设备(键盘)读取,自动类型匹配,你要是读入一个数字,结果却敲了一个字符,它会不认;等输入完事,会在输入流中自动截取。
就像《C++编程思想》说的,你想要的数据就被魔术般的创造出来了。
我是在输入表达式的时候发现的,你可以看看这篇,看看我是怎么输入一个表达式的
http://www.csdn.net/develop/read_article.asp?id=19372
#1
#include<math.h>
double atof(const char*)
//VC++ 里是可以用的只不过返回的是double型别的。用强制转换(不推荐是呀强制转换^v^)
double atof(const char*)
//VC++ 里是可以用的只不过返回的是double型别的。用强制转换(不推荐是呀强制转换^v^)
#2
#include<iostream.h>
#include<math.h>
void main()
{
float a;
a=(float)atof("123.456");
cout<<a<<endl;
}
#include<math.h>
void main()
{
float a;
a=(float)atof("123.456");
cout<<a<<endl;
}
#3
楼上的两位仁兄,我用的这个函数,你们看看,我已经包含了这个函数啊!
imaginaryPart = static_cast < float > atof ( imag )
这里边imag是从复数上分离出来的虚部数据,imaginaryPart是类中的虚部数据成员,在类里边的虚部和实部我用的是float型,这里强制转换为float型,但是编译还是出现错误啊!
请问为什么啊?
我的类定义在complex_class.h头文件中,成员函数定义在一个member_define.cpp文件中,main()在main.cpp中
编译之后出现以下错误:
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2146: syntax error : missing '(' before identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2146: syntax error : missing '(' before identifier 'atof'
Error executing cl.exe.
main.exe - 4 error(s), 0 warning(s)
提示的还是atof()函数出现错误,为什么啊~~~~真急死了!!!!!!
imaginaryPart = static_cast < float > atof ( imag )
这里边imag是从复数上分离出来的虚部数据,imaginaryPart是类中的虚部数据成员,在类里边的虚部和实部我用的是float型,这里强制转换为float型,但是编译还是出现错误啊!
请问为什么啊?
我的类定义在complex_class.h头文件中,成员函数定义在一个member_define.cpp文件中,main()在main.cpp中
编译之后出现以下错误:
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2146: syntax error : missing '(' before identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2146: syntax error : missing '(' before identifier 'atof'
Error executing cl.exe.
main.exe - 4 error(s), 0 warning(s)
提示的还是atof()函数出现错误,为什么啊~~~~真急死了!!!!!!
#4
你没有包含以下头文件
#include<math.h>
#include<math.h>
#5
push on
#6
to 化石:
我绝对已经包含了math.h头文件了!
但是现在还出现这个问题,实在太奇怪啦!!!
我绝对已经包含了math.h头文件了!
但是现在还出现这个问题,实在太奇怪啦!!!
#7
up
#8
复数的实部和虚部为什么要用char类型?
#9
楼主可否将代码贴出来看看
不要写太长的代码,你只要试一下atof在你的机子上能不能就可以了。如果不能用将你所测试的代码贴出来。
不要写太长的代码,你只要试一下atof在你的机子上能不能就可以了。如果不能用将你所测试的代码贴出来。
#10
C++中atof()函数是包含在stdlib.h里面的,不是math.h
#11
代码看看
#12
cin能够完美解决你这个问题,假定一次输入一个复数,以回车结束。
cin << float << char << float << char;---12.3 + 34.4 i
就是连着输入也能自动识别,只是要是不按这个格式输入,就死循环了,当然了,不要给自己找麻烦。
cin << float << char << float << char;---12.3 + 34.4 i
就是连着输入也能自动识别,只是要是不按这个格式输入,就死循环了,当然了,不要给自己找麻烦。
#13
to 幻星:
谢谢你的回复,我不明白的是,复数又有数据(实部,虚部),又有运算符号(+/-,i),不用char类型指针接收用什么呢?会不会还有什么更好的办法?我不想用数组
to happycock:
谢谢你的回复,这个cin是怎么用的,这些东西(floa char float),是不是指的变量啊?
谢谢你的回复,我不明白的是,复数又有数据(实部,虚部),又有运算符号(+/-,i),不用char类型指针接收用什么呢?会不会还有什么更好的办法?我不想用数组
to happycock:
谢谢你的回复,这个cin是怎么用的,这些东西(floa char float),是不是指的变量啊?
#14
我还是把代码贴出来吧!麻烦大家给分析分析,谢谢了~~~~~~~~~~~
------------------------
这是类型定义的头文件:complex_class.H
# ifndef COMPLEX_CLASS_H
# define COMPLEX_CLASS_H
class Complex
{
public:
Complex();
void getComplexNumber( char * );
float visitRealPart();
float visitImaginaryPart();
private:
float realPart;
char flag;
float imaginaryPart;
};
# endif
-----------------------------------------------
这是成员函数定义文件:member_define.cpp
# include <iostream.h>
# include <string.h>
# include <iomanip.h>
# include <math.h>
# include "complex_class.h"
Complex::Complex()
{
realPart = 0.0;
imaginaryPart = 0.0;
}
void Complex::getComplexNumber( char *complexC )
{
char *real, *imag, *next, *tmp;
tmp = complexC;
for ( int i = 1; ( ( *tmp ) != '+'); i++, tmp++ );
strncpy ( real, complexC, i );
flag = *tmp;
realPart = static_cast < float > atof ( real );
next = ++tmp;
for ( int j = 1; ( ( *next ) != 'i'); i++, next++ );
strncpy ( imag, next, j );
imaginaryPart = static_cast < float > atof ( imag );
if ( flag == '-' )
imaginaryPart *= ( -1 );
}
float Complex::visitRealPart()
{
return realPart;
}
float Complex::visitImaginaryPart()
{
return imaginaryPart;
}
---------------------------------------------
这是main()函数所在的文件: main.cpp
# include <iostream.h>
# include "member_define.cpp"
# include "complex_class.h"
int main()
{
char method, *complexNum1, *complexNum2;
float real, imag;
Complex c1,c2;
cout << "\n\nPlease in put two complex numbers"
<< " when the computer remind!";
cout << endl << "\nPlease input the first complex number(c1):";
cin >> complexNum1;
c1.getComplexNumber( complexNum1 );
cout << "\nPlease input the operation method ( + or - ):";
cin >> method;
cout << endl << "\nPlease input the second complex number(c2):";
cin >> complexNum2;
c2.getComplexNumber( complexNum1 );
switch ( method )
{
case '+':
{
real = c1.visitRealPart() + c2.visitRealPart();
imag = c1.visitImaginaryPart() + c2.visitImaginaryPart();
break;
}
case '-':
{
real = c1.visitRealPart() - c2.visitRealPart();
imag = c1.visitImaginaryPart() - c2.visitImaginaryPart();
break;
}
default:
{
cout << "\n***********************************************"
<< "\n*Never can reach here, the program is wrong!!!*"
<< "\n***********************************************";
}
}
cout << "\nc1" << method << "c1 = "
<< real << method << imag << "i" << endl;
return 0;
}
------------------------------------------------------------------
代码写的很烂,大家不要笑我,我是初学者... ...
------------------------
这是类型定义的头文件:complex_class.H
# ifndef COMPLEX_CLASS_H
# define COMPLEX_CLASS_H
class Complex
{
public:
Complex();
void getComplexNumber( char * );
float visitRealPart();
float visitImaginaryPart();
private:
float realPart;
char flag;
float imaginaryPart;
};
# endif
-----------------------------------------------
这是成员函数定义文件:member_define.cpp
# include <iostream.h>
# include <string.h>
# include <iomanip.h>
# include <math.h>
# include "complex_class.h"
Complex::Complex()
{
realPart = 0.0;
imaginaryPart = 0.0;
}
void Complex::getComplexNumber( char *complexC )
{
char *real, *imag, *next, *tmp;
tmp = complexC;
for ( int i = 1; ( ( *tmp ) != '+'); i++, tmp++ );
strncpy ( real, complexC, i );
flag = *tmp;
realPart = static_cast < float > atof ( real );
next = ++tmp;
for ( int j = 1; ( ( *next ) != 'i'); i++, next++ );
strncpy ( imag, next, j );
imaginaryPart = static_cast < float > atof ( imag );
if ( flag == '-' )
imaginaryPart *= ( -1 );
}
float Complex::visitRealPart()
{
return realPart;
}
float Complex::visitImaginaryPart()
{
return imaginaryPart;
}
---------------------------------------------
这是main()函数所在的文件: main.cpp
# include <iostream.h>
# include "member_define.cpp"
# include "complex_class.h"
int main()
{
char method, *complexNum1, *complexNum2;
float real, imag;
Complex c1,c2;
cout << "\n\nPlease in put two complex numbers"
<< " when the computer remind!";
cout << endl << "\nPlease input the first complex number(c1):";
cin >> complexNum1;
c1.getComplexNumber( complexNum1 );
cout << "\nPlease input the operation method ( + or - ):";
cin >> method;
cout << endl << "\nPlease input the second complex number(c2):";
cin >> complexNum2;
c2.getComplexNumber( complexNum1 );
switch ( method )
{
case '+':
{
real = c1.visitRealPart() + c2.visitRealPart();
imag = c1.visitImaginaryPart() + c2.visitImaginaryPart();
break;
}
case '-':
{
real = c1.visitRealPart() - c2.visitRealPart();
imag = c1.visitImaginaryPart() - c2.visitImaginaryPart();
break;
}
default:
{
cout << "\n***********************************************"
<< "\n*Never can reach here, the program is wrong!!!*"
<< "\n***********************************************";
}
}
cout << "\nc1" << method << "c1 = "
<< real << method << imag << "i" << endl;
return 0;
}
------------------------------------------------------------------
代码写的很烂,大家不要笑我,我是初学者... ...
#15
这是编译出错信息:
----------------------------------------------------------
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2146: syntax error : missing '(' before identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2146: syntax error : missing '(' before identifier 'atof'
Error executing cl.exe.
main.exe - 4 error(s), 0 warning(s)
----------------------------------------------------------
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(20) : error C2146: syntax error : missing '(' before identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2061: syntax error : identifier 'atof'
f:\microsoft visual studio\myprograms\complex\member_define.cpp(24) : error C2146: syntax error : missing '(' before identifier 'atof'
Error executing cl.exe.
main.exe - 4 error(s), 0 warning(s)
#16
是的,变量类型,换成对应的变量名就可以了。
cin是从标准设备(键盘)读取,自动类型匹配,你要是读入一个数字,结果却敲了一个字符,它会不认;等输入完事,会在输入流中自动截取。
就像《C++编程思想》说的,你想要的数据就被魔术般的创造出来了。
我是在输入表达式的时候发现的,你可以看看这篇,看看我是怎么输入一个表达式的
http://www.csdn.net/develop/read_article.asp?id=19372
cin是从标准设备(键盘)读取,自动类型匹配,你要是读入一个数字,结果却敲了一个字符,它会不认;等输入完事,会在输入流中自动截取。
就像《C++编程思想》说的,你想要的数据就被魔术般的创造出来了。
我是在输入表达式的时候发现的,你可以看看这篇,看看我是怎么输入一个表达式的
http://www.csdn.net/develop/read_article.asp?id=19372