在 C++ 程序中只使用 const 常量而不使用宏常量,即 const 常量完 全取代宏常量。
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 int extract_int() 6 { 7 char ch; 8 int n=0; 9 while(ch=cin.get()) 10 if (ch>='0' && ch<='9') 11 { 12 cin.putback(ch); 13 cin>>n; 14 break; 15 } 16 return n; 17 } 18 //main()函数 19 20 int main(int argc, char** argv) { 21 //提取字符串中的数字 22 int a=extract_int(); 23 int b=extract_int(); 24 int c=extract_int(); 25 26 //显示结果 27 cout<<a<<"+"<<b<<"="<<c<<endl; 28 29 return 0; 30 }