| 发表于:2007-01-07 00:29:421楼 得分:0 |
此问题以解决 代码如下: 此程序是 c++ primer plus 是第七节7练习题6(p221) // ch7_6.cpp : defines the entry point for the console application. // #include "stdafx.h " #include <iostream> using namespace std; const int le=5; int fill_array(double ar[],int be); void show_array(double ar[],int le); void reverse_array(double ar[],int le); int main() { double ar[le]; int count=fill_array(ar,le); cout < <endl; show_array(ar,count); cout < <endl; reverse_array(ar,count); cout < <endl; show_array(ar,count); cout < <endl; reverse_array(ar,(count-1)); cout < <endl; show_array(ar,count); cin.get(); cin.get(); return 0; } int fill_array(double ar[],int le) { int count=0; cout < < "enter double number: \n "; int i=0; while(cin> > ar[i]) { i++; count++; if(i> =le) break; } cin.clear(); while(cin.get()!= '\n ') continue; return count; } void show_array(double ar[],int le) { for(int i=0;i <le;i++) cout < <ar[i] < < " "; } void reverse_array(double ar[],int le) { double *mi=new double[le]; if(le==0) return; if(le%2==0) { for(int a=le/2-1,b=le/2; b <le ;b++,a--) { mi[b]=ar[b]; ar[b]=ar[a]; ar[a]=mi[b]; } } if(le%2==1) { for(int a=le/2,b=le/2+1; b <le ; b++,a--) { mi[b]=ar[b]; ar[b]=ar[a]; ar[a]=mi[b]; } } } | | |
|