|
发表于 2013-5-24 13:35:08
|
显示全部楼层
abs52121 发表于 2013-5-23 23:08
应该是不输入,参数数量的方法,C语言学习者,若低级错误请原谅 - #include <iostream>
- using namespace std;
- void calc(double a);
- void calc(double x,double y);
- void calc(double i,double j,double k);
- int main()
- {
- int i = 0;
- double a[3],temp;
- cout << "请输入数字(输完按Enter后可按Ctrl + Z结束输入):" << endl;
- while( scanf("%lf",&temp) != EOF )
- {
- a[i] = temp;
- i++; // i是计算输入数字的多少
- if( i >= 3 )
- break; // 当输入数字大于3时,舍弃多于数字直接执行下面语句
- }
- switch( i )
- {
- case 1: calc( a[0] );break;
- case 2: calc( a[0],a[1] ); break;
- case 3: calc( a[0],a[1], a[2] );break;
- default:
- break;
- }
-
- return 0;
- }
- void calc(double a)
- {
- double aOut;
- aOut = a*a;
- cout<<a<<"的平方="<<aOut<<endl;
- }
- void calc(double x,double y)
- {
- double abOut;
- abOut = x*y;
- cout<<x<<"和"<<y<<"的积="<<abOut<<endl;
- }
- void calc(double i,double j,double k)
- {
- double abcOut;
- abcOut = i+j+k;
- cout<<i<<"和"<<j<<"及"<<k<<"的和="<<abcOut<<endl;
- }
复制代码 |
|