改寫要求1:分別用指針pa、pb代替數組
改寫要求2:從鍵盤輸入data元素 元素個數任意,輸入0結束
#include <cstdlib> #include <iostream> using namespace std; class DATA { double *pa,*pb; double max,min; double new_max,new_min; int length; public: DATA(double a1[],double x,double y,int len) { int i; length=len; pa=new double[len]; pb=new double[len]; for(i=0;i<length;i++) pa[i]=a1[i]; new_max=x; new_min=y; max=min=pa[0]; for(i=0;i<length;i++) { if(pa[i]>max) max=pa[i]; if(pa[i]<min) min=pa[i]; } } void fun(); void print() { while(*pa!='\0') cout<<*pa++<<'\t'; cout<<endl; cout<<"["<<new_min<<","<<new_max<<"]"<<endl; while(*pb!='\0') cout<<*pb++<<endl; } }; void DATA::fun() { for(int i=0;i<length;i++) pb[i]=(pa[i]-min)*(new_max-new_min)/(max-min)+new_min; } int main(int argc, char *argv[]) { int i,j=0; cout<<"輸入元素個數"<<endl; cin>>i; double *data=new double[i+1]; int x,y; cin>>data[0]; while(data[j]) { j++; cin>>data[j]; } while(*data!='\0') cout<<*data++<<'\t'; cout<<endl; cout<<"輸入x,y"<<endl; cin>>x>>y; DATA test(data,x,y,i); test.fun(); test.print(); system("PAUSE"); return EXIT_SUCCESS; }