题目内容
请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程prog3,其中声明了ValArray类,该类在内部维护一个动态分配的int型数组v。ValArray类的成员函数cycle用于对数组元素进行向左循环移动。调用一次cycle后,数组的第二个元素至最后一个元素都将向左移动一个位置,而最左端的元素将循环移动到最右端位置上。例如,若ValArray表示的数组为{1,2,3,4,5},则第一次调用cycle后,数组变为{2,3,4,5,1},第二次调用cycle后,数组变为{3,4,5,1,2},依次类推。请编写成员函数cycle。在main函数中给出了一组测试数据,此情况下程序的输出应该是: v={1,2,3,4,5} v={2,3,4,5,1} v={3,4,5,1,2} v={4,5,1,2,3} v={5,1,2,3,4} 要求: 补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 //ValArray.h #include <iostream> using namespace std; class ValArray { int* v; int size; public: ValArray const int* p, int n): size (n) { v = new int[size]; for (int i = 0; i<size; i++) v[i] = p[i]; } ~ValArray() { delete [] v; } void cycle (); void print(ostream& out) const { out << ’{’; for (int i = 0; i <size-1; i++) out << v[i] << ","; out << v[size-1] << ’}’; } }; void writeToFile (const char * ); //main. cpp #include "ValArray. h" void ValArray::cycle () { //将数组v中的size个整数依次移动到它的前一个单元,其中第一个整数移到原来最后元素所在单元。 //******** 333******** //******** 666******** } int main ( ) { const int a[] = {1, 2, 3, 4, 5 }; ValArray v(a, 5); for (int i = 0; i < 5; i++) { cout << "v = "; v.print (cout); cout << endl; v.cycle(); } writeToFile (""); return 0; }
查看答案
搜索结果不匹配?点我反馈
更多问题