问答题使用VC6打开
下的源程序文件modi3.cpp。
学校教务处要为任课老师开发一个学生管理系统,需求如下:
(1)学生的信息是要受到严密保护和限制的,包括姓名、五个英文字符的学号,以及精确到小数点后一位的一科成绩,只有任课老师可以修改,如果学生升学了,比如由本科升到研究生,原来的信息还有效,而只需添加研究生的必要信息,不过现在不必马上实现,但应当有所考虑。
(2)学生的所有信息可以查看,但也只有学生自己才有这个权利。
(3)学生的信息可以从文件中读取来构造信息库,需要把信息输出到屏幕以便核实,但这需要任课老师来完成。
(4)老师能提供所教学生的完整信息列表,同时可按成绩高低排序。
(5)为了便于学校评定奖学金,需要单独查找成绩最高的同学(一般不采用把成绩单全部排列后来选取,可能这比较耗费时间,虽然要求不太合理,但要按用户的需求操作,同时需要指出的是,如果成绩最高者有数名,需要一一列出)。
(6)由于学校的机器比较老,内存严重不足,每个老师带的学生数不一样,但也不会超过50人。
以上功能的程序框架已经形成,考生需要按照需求来逐个实现。
(1)请在注释//********1********之后添加适当的语句,以便实现功能需求(1)。
(2)请在注释//********2********处添加适当的语句,成绩单排序功能。
(3)请在注释//********3********处实现查找成绩最高学生名单。
无需修改main()主函数,当得到下面的结果,该系统可能被采用。
输入的学生信息:
输入的学生信息:
姓名
周华
李强
刘星
贺兰
周红
赵邦
高俊
学号
0001
0002
0003
0004
0005
0006
0007
成绩
78.5
98
89
78
76.5
85
98
排序后的学牛信息:
姓名
李强
高俊
刘星
赵邦
周华
贺兰
周红
学号
0002
0007
0003
0006
0001
0004
0005
成绩
98
98
89
85
78.5
78
76.5
查询成绩最高的学生信息:
高俊 0007 98
李强 0002 98
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include<fstream.h>
#include<assert.h>
#include <string.h>
#define NMAX 50
class Teacher;
class Student
{
public:
Student(char *name="未知",char *number="00000",double mark=0.0)
{
int len=strlen(name);
m_name=new char[len+1];
strcpy(m_name,name);
strcpy(m_number,number);
m_mark=mark;
}
~Student()
{
delete []m_name;
}
void DisplayMsg()
{
cout
m name
"/t"
m_number
"/t"
m_mark
endl;
}
protected:
char *m_name;
char m_number[5];
double m_mark;
//********1********
};
class Teacher
{
public:
Teacher(){};
~Teacher(){
for(int i=0;i<m_count; i++)
{
delete m_pStu[i];
m_pStu[i]=NULL;
}
};
int BestScore(Student *pS[]);
void InputScore();
void Display();
void SortScore();
protected:
Student *m_pStu[NMAX];
double m_average;
int m_count;
};
void Teacher::InputScore()
{
ifstream in("data.txt");
assert (in);
char name[20];
char number[5];
double mark=0;
m_count=0;
cout
"姓名/t"
"学号/t"
"成绩/t"
endl;
for(int i=0; !in.eof(); i++) {
in
>
>
name;
in
>
>
number;
in
>
>
mark;
m_count++;
m_pStu [i]=new Student (name, number,mark);
cout
name
" / t"
number
" / t"
mark
endl;
}
}
void Teacher::SortScore()
double sum=0;
for (int i=0; i<m_count; i++)
{
int index=i;
for (int j=i+1; j<m_count; j++)
{
if (m_pStu [j]->m_mark> m_pStu[index] ->m_mark)
index=j;
}
//********2********
if()
{
Student *ps=m_pStu[i];
m_pStu[i]=m_pStu[index];
m_pStu[index]=ps;
}
}
}
void Teacher::Display()
{
cout
"姓名/t"
"学号/t"
"成绩 /t"
endl;
for(int j=0; j<m_count; j++)
{
m_pStu [j]->DisplayMsg ();
}
}
int Teacher::BestScore (Student *pS [])
{
int index=0;
int count=1;
//********3********
for(int j=1; j<m_count; j++)
{
if(m_pStu[j]->m_mark> m_pStu[index]->m_mark)
{
count=1;
index=j;
pS[count++]=m_pStu
[index];
}
else if(m_pStu[j]->m_mark ==m_pStu[index]->m_mark)
{
index=j;
pS[count++]=m_pStu[index];
}
}
return count;
}
void main ()
{
Teacher Teacher;
Student *pStudent[NMAX];
cout
"输入的学生信息: "
endl;
Teacher.InputScore();
Teacher.SortScore();
cout
endl
"排序后的学生信息: "
endl;
Teacher.Display();
cout
endl
"查询成绩最高的学生信息: "
endl;
int n=Teacher.BestScore (pStudent);
while(n--)
{
pStudent[n]->
DisplayMsg();
}
}
问答题请使用[答题]菜单命令或直接用VC6打开考生文件夹下的工程proj3,其中声明的IntSet是一个用于表示正整数集合的类。IntSet的成员函数IsSubSet的功能是判断集合B是否是集合A的子集。在IsSubSet中可以使用成员函数IsMembeOf睐判断一个正整数是否在此集合中,请编写成员函数IsSubSet。在main函数中给出了一组测试数据,此时程序的输出应该为:
集合A:1 2 3 5 8 10
集合B:2 8
集合B是集合A的子集
注意:只能在函数IsSubSet的“//********333********”和“//********666********”之间填入若干语句,不要改动程序中的其他内容。
程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//Intset.h
#include <iostream>
using namespace std;
const int Max=100;
class IntSet
{
public:
IntSet () //构造一个空集合
{
end=-1;
}
IntSet (int a[], int size) //构造一个包含数组a中size个元素的集合
{
if (size>=Max)
end=Max-1;
else
end = size-1;
for(int i=0; i<=end; i++)
element[i]=a[i];
}
bool IsMemberOf (int a) //判断a是否为集合的元素
{
for(int i=0; i<=end; i++)
if (element[i] == a)
return true;
return false;
}
int GetEnd() { return end; }
int getElement (int i) { return element[i]; } //返回下标i处的元素
bool IsSubSet (IntSet //判断集合set是否为当前集合的子集
void Print () //输出集合中的所有元素
{
for(int i=0; i<=end; i++)
if((i+1)% 20==0)
cout << element[i] << endl;
else
cout <<element[i] <<'';
cout << endl;
}
private:
int element[Max];
int end;
};
void writeToFile (const char * );
//main.cpp
#include "IntSet. h"
bool IntSet::IsSubSet(IntSet
int b[] ={2, 8};
IntSet set1 (a, 6), set2 (b, 2);
cout << "集合A:";
set1.Print ();
cout << "集合B:";
set2.Print ();
if (set1.IsSubSet (set2))
cout <<"集合B是集合A的子集" << endl;
else
cout <<"集合B不是集合A的子集" << endl;
writeToFile ("");
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数convert(char*des,char*str,char c,char*str2)的功能是:如果str中包含字符“!”,则替换成'a';如果str中包含字符“&”,则替换成'b';如果str中包含字符“*”,则替换成str2。并用函数返回目标转换后的指针。注意:只能补充函数convert(char*des*char*str,char*str2).#include<iostream.h>#include<cstring>#defineMAXLEN1024void convert(char*des,char*Str,char*str2){}void main(){char dest[MAXLEN];char* str="!&cefghi*!&";char*str2="jklm";convert(dest,str,str2);cout<<dest<<endl;return;}
问答题用VC6打开考生文件夹下的源程序文件modi3.cpp,这个程序完成输出到屏幕一些特定的信息,但工程有错误或者不完整,请完成以下功能:(1)初始化m Num2的值为j,请在注释∥*******1*******后添加适当的语句。(2)补充全局函数fun()使之能够调用类TestClass的成员变量,请在注释//*******2********后添加适当的语句。(3)初始化静态变量, 请在注释//*******3*******后添加适当的语句。(4)修改注释//*******4*******后的语句。使得程序输出以下内容: m—Num1=1 m—Num2=2 m—Num3=1 m—Num1=4 m—Num2=2 m—Num3=7注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。#include<iostream.h>Class TeStClass{public:∥*******1*******TestClass(int i,int j) { m Numl=i; m Num3+=i; } void Print() { cout<<“m Num1=”<<m Numl<<endl; cout<<“m Num2=”<<m Num2<<endl; cout<<“m Num3=”<<m Num3<<endl; } void Add(int i) { m Num3+=i; }private: int m Numl; const int m Num2; Static int m Num3; //*******2******* }; //*******3******* Void fun() { TeStClass Num(1,2); Num.m Numl=4; ∥*******4******* Num.Add(); Num.Print(); } void main() { TeStClass Num(1,2); Num.Print(); fun(); return;}
问答题综合应用题
使用VC6打开考生文件夹下的工程test8_3,此工程包含一个源程序文件test8_3.cpp,该文件设计了用于输出乘法九九表的类。请认真阅读已有的代码,按要求完成下列操作,将程序补充完整。
(1)定义类Table的私有数据成员x和y,分别用于表示九九表中的两个乘数(x*y),它们都是int型的数据。请在注释"//**1**"之后添加适当的语句;
(2)完成类Table的成员函数print()的定义,该函数以"x*y=z"的格式打印出九九表中的一个乘法算式,请使用格式化输出函数printf实现,在注释"//**2**"之后添加适当的语句;
(3)完成类Table9的成员函数print()的定义,该函数调用基类Table的print()函数,将九九表输出到屏幕,请在注释"//**3**"之后添加适当的语句;
(4)补充主函数,在屏幕上输出九九表,以便测试所定义的类的正确性。请在注释"//**4**"之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件test8_3.cpp清单如下:
#include
#include
class Table
{
//**1**
int z;
public:
void print(int x,int y,int z);
};
void Table::print (int x,int y,int z)
{
//**2**
}
class Table9:public Table
{
public:
void print();
};
void Table9::print()
{
//**3**
int x,y,z;
for (i=1;i<10;i++)
{
for(j=1;j
问答题使用VC6打开下的源程序文件modi1.cpp,但该程序运行有问题,请改正main函数中的错误,使程序的输出结果是:112233445566778899注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。#include<iostream.h>#include<iomanip.h>voidmain(){inta[3][3]={{1,2,3},{4,5,6},{7,8,9}};int*p,i;//********error********p=a;for(i=0;i<9;i++){coutsetw(2)*(p+i);//********error********coutsetw(2)*(a+i);if(i%3==2)coutendl;}}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。此程序的运行结果为:In CDerive's display().b=1In CDerive2's display().b=2其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。(1)定义函数display()为无值型纯虚函数。请在注释//********1********之后添加适当的语句。(2)建立类CDerive的构造函数,请在注释//********2********之后添加适当的语句。(3)完成类CDerive2成员函数diaplay0的定义。请在注释//********3********之后添加适当的语句。(4)定义类DeriveI的对象指针d1,类CDerive2的对象指针d2。其初始化值分别为1和2。请在注释//********4********之后添加适当的语句。注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<ioStream>using namespace std;class CBase{public:CBase(int i){b=i;}//********1********protected:int b;};class CDerive:public CBase{public://********2********void display(){cout<<"In CDerive'sdisplay()."<<"b="<<b<<endl;}};class CDerive2:public CBase{public:CDerive2(inti):CBase(i){)//********3********};void func(CBase*obj){obj->display();}void main(){//********4********func(d1);func(d2);}
问答题使用VC6打开考生文件夹下的源程序文件modi2.epp。阅读下列函数说明和代码。函数show0的功能是将1、2、3、4四个数字,组成互不相同且无重复数字的四位数,并将这些数输出到屏幕,输出的内容如下:
1234 1243 1324 1342 1423 1432 2134 2143
2314 2341 2413 2431 3124 3142 3214 3241 3412 3421
4123 4132 4213 4231 4312 4321
将函数show()补充完整。
注意:请勿改动主函数。
#include
void show()
{
}
int main()
{
show();
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序,使输入某年某月某日,可判断这一天是这一年的第几天。
程序分析:以3月5日为例,应该先把前两个月的天数加起来,然后再加上5天即本年的第几天(特殊情况:闰年输入月份大于3时需考虑多加一天)。
注意:只能补充函数func(int year,int month.int day),请勿改动其他部分的内容。
#include
int func(int year,int month,int
day)
{
}
void main()
{
cout<
问答题请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程包含一个源程序文件proj3.epp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象myA~ay中,然后对整数序列按非递减排序,最后由函数writeToFile选择序列中的部分数据输出到文件out.dat中。文件in.dat中的整数个数不大于300个。要求:补充编制的内容写在“//**********333**********”与“//**********666**********”两行之间。实现对整数序列按非递减排序,并将排序结果在屏幕上输出。不得修改程序的其他部分。注意:程序最后已将结果输出到文件out.dat中。输出函数writeToFile已经给出并且调用。//proj 3.cpp#include<iostream>#include<fstream>#include<cstring>using namespace std;class intArray{private: int*array;//整数序列首地址 int length;//序列中的整数个数public: //构造函数,从文件中读取数据用于初始化新对象。参数是文件名 intArray(char*filename); void sort();//对整数序列按非递减排序 一intArray(); void writeToFi le(char*fi lename); ); intArray::intArray(char*filename) { ifstream myFile(filename); int len=300; array=new int[1en]; length=0; while(myFile>>array[1ength++]); length一一; myFile.close(); } void intArray::sort(){ //**********333********** //**********666********** } intArray::intArray() { delete[]array; } void intArray::writeToFile(char。 filename) { int step=0; ofstream outFile(filename); for(int i=0;i<length;i=i+step){outFile<<array[i]<<endl;step++;}outFile.close();}void main(){ intArray myArray(”in.dat”); myArray.sort(); myArray.writeToFile(”out.dat”);}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序,使函数fun()实现以下功能:找出一个整数,它加上100后是一个完全平方数,再加上268又是一个完全平方数,请问该数是多少?程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后开方,如果开方后的结果满足条件,即是结果。#include<iostream.h>#include<cmath>void fun(){}int main(){fun();return0;}
问答题综合应用题
使用VC6打开考生文件夹下的工程kt9_3,此工程包含一个源程序文件kt9_3.cpp,其中定义了Circle类与Money类,Circle类可对半径为r的圆进行周长与面积的计算,而Money类用于计算一圆形游泳池的造价。游泳池四周有原形过道,过道外围上栅栏,过道宽度为3米,根据键入的游泳池半径,每米栅栏价格及每平方米过道价格,即可计算出游泳池的造价。请按要求完成下列操作,将程序补充完整。
(1)定义符号常量PI(值为3.14159f)与WIDTH(值为3.00f),分别用于表示圆周率与过道的固定宽度。请在注释“//**1**”之后添加适当的语句。
(2)定义Circle类默认构造函数,把私有成员radius初始化为参数r的值。请在注释“//**2**”之后添加适当的语句;
(3)完成Money类默认构造函数的定义,把私有成员FencePrice(每米栅栏的价格)、ConcretePrice(每平方米过道的价格)初始化为参数f,c的值。请在注释“//**3**”之后添加适当的语句。
(4)完成Money类成员函数floatMoney::TotalMoney(floatfencelen,floatconarea)的定义,根据参数fencelen(栅栏的长度)和conarea(过道的面积),返回栅栏与过道的总造价。请在注释“//**4**”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件kt9_3.cpp清单如下:
#include
//**1**
classCircle
{ private:
floatradius;
public:
//**2** floatCircumference(){return2*PI*radius;}
floatArea(){returnPI*radius*radius;} };
classMoney
{ private:
floatFencePrice;
floatConcretePrice;
public:
Money(floatf,floatc);
floatTotalMoney(floatfencelen,floatconarea); };
Money::Money(floatf,floatc)
{ //**3** }
floatMoney::TotalMoney(floatfencelen,floatconarea)
{ //**4** }
voidmain()
{ floatradius,fence,concrete;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout>radius;
cout>fence;
cout>concrete;
CirclePool(radius);
CirclePoolRim(radius+WIDTH);
Moneymon(fence,concrete);
floattotalmoney=mon.TotalMoney(PoolRim.Circumference(),(PoolRim.Area()-Pool.Area()));
cout<<"ThetotalmoneyisRMB"<
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程中声明的Array是一个表示数组的类。一个Array对象可以包含多个整型元素。Array的成员说明如下:
成员函数add用于向数组的末尾添加一个元素;
成员函数get用于获取数组中指定位置的元素;
数据成员a表示实际用于存储数据的整型数组;
数据成员size表示数组的容量,数组中的元素个数最多不能超过size;
数据成员num表示当前数组中的元素个数。
SortedArray是Array的派生类,表示有序数组。SortedArray重新定义了Arrav中的add函数,以确保有序数组中的元素始终按照升序排列。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
10,9,8,7,6,5,4,3,2,1,
1,2,3,4,5,6,7,8,9,10,
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream>
using namespace std;
class Array{
public:
Array(unsigned int s)
{
size=s;
num=0;
a=new int[s];
}
virtual Array () {delete[] a;
virtual void add(int e)
{
if(num<size){
//**********found**********
______
num++;
}
}
int get (unsigned int i) const
{
if (i<size)
return a[i];
return 0;
}
protected:
int*a;
unsigned int size,num;
};
class SortedArray : public Array {
public:
SortedArray (unsigned int s)
______{ }
virtual void add (int e)
{
if (num>=size)
return;
int i=0,j;
while (i<num) {
for (j=num;j>i;j--) {
//**********found**********
______;
break;
}
i++;
}
if (i==num)
}
};
void fun (Array
for (i=10; i>=1; i--) {
a.add (i);
}
for(1=0;i<10;i++) {
cout<<a.get(i)<<",";
}
cout<<endl;
}
int main ()
{
Array a (10);
fun (a);
SortedArray sa (10);
fun (sa);
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)完成类TestClass1的成员函数seta定义,定义seta对象x为int类型,请在注释//********1********后添加适当的语句。 (2)完成类TestClass1(共有)和类TestClass2(私有)派生类TestClass3的定义,请在注释//********2********后添加适当的语句。 (3)定义类TestClass3中的私有成员C为int,请在注释//********3********添加适当的语句。 (4)完成setc中对基类的变量a的赋值,请在注释//********4********后添加适当的语句。 注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream.h>Class TestClass1{ int a;public: //********1******** { return a=x: } Void showa() { cout<<a<<endl: )};Class TestClass2{ int b;public: Void Setb(int x) { b=x; } Void showb() { cout<<b<<endl; }};//********2********{private: //********3********public: void setc(int x,int y,int z) { C=Z; //********4******** setb(y); } Void Showc() { Cout<<c<<endl; } }; Void main() { TestClass3 c; c.Seta(5); c.Showa(); c.Setc(5,7,4); c.Showc();}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使该程序的输出结果为: 20,15 15,20 注意:错误的语句在//********error********的下面,修改该语句即可。#inClude<iostream.h>//********error********void Exchangel(int m,int n){ int t=m; m=n; n=t;}//********error********void Exchange2(int m,int n){ int t=。m; *m =*n; *n =t;}Void main(){ int b=2 0; int a=15: Exchangel(a,b); cout<<a<<','<<b<<endl; //********error******** Exchange2(a,b); cout<<a<<','<<b<<endl;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹pmjl下的工程projl,此工程中含有一个源程序文件pmjl.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: Constructor called. The vahe is 10 Copy constructor called. The value is 10 Destructor called. Destructor called. 注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。//projl.cpp#include<iostream>using namespace std;class MyClass{public://ERROR**********found********** MyClass(int i) {value=i;cout<<”Constructor called.”<<endl;)//EFROR**********found********** MyClass(const MyClass P) { value=P.value; cout <<”Copy constructorcalled.”<<endl; } void Print() {cout<<”The value is” <<value<<endl;)//ERROR**********found********** void—MyClass《) {cout<<”Destructor called.” <<endl;}private: int value;};int main(){ MyClass objl; objl.Print(); MyClass obj2(objl); obj2.Print(); return 0;}
问答题请使用VC6或使用[答题]菜单打开
proj1下的工程proj1,该工程含有一个源程序文件prw1. cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
The value is 10
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1. cpp
#include <iostream>
using namespace std;
class MyClass {
int value;
public:
//ERROR ********found********
void MyClass(int val): value(val) {}
int GetValue()const {return value;}
void SetValue(int val);
};
//ERROR ********found********
inline void SetValue(int val) {value=val;}
int main()
{
MyClass obj(0);
obj. SetValue(10);
//ERROR ********found********下列语句功能是输出obj的成员value的值
cout<<"The value is"<<obj. value<<endl;
return 0;
}
问答题综合应用题
使用VC6打开考生文件夹下的工程kt11_3。此工程包含一个kt11_3.cpp,其中定义了类queue,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。
(1)完成类queue的无参数的构造函数的定义,要求把数据成员bl和el都初始化为0,同时输出queueinitialized。请在注释“//**1**”之后添加适当的语句。
(2)完成类queue的成员函数qput(intj)的定义,它的功能是把新的元素加入队列,过程是先依据bl的值判断数组是否已经满了,如果是就输出queueisfull,否则bl自加一,并且把参数j的值存入bl指向的数组元素中,请在注释“//**2**”之后添加适当的语句。
(3)完成类queue的成员函数qget()的定义,它的功能是把队列开头的元素提取出队列,并返回该值,过程是先比较el和bl的值判断队列是否已空,如果是就输出queueisempty,
否则el自加一,并且把el指向的数组元素返回,请在注释“//**3**”之后添加适当的语句。
程序输出结果如下:
queueinitialized
queueinitialized
3311
4422
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件kt11_3.cpp清单如下:
#include
classqueue
{ intq[100];
intbl,el;
public:
queue();
voidqput(intj);
intqget(); };
queue::queue()
{ //**1** }
voidqueue::qput(intj)
{ //**2**
{ cout<<"queueisfull\n";
return; }
bl++;
q[bl]=j; }
intqueue::qget()
{ //**3**
{ cout<<"queueisempty\n";
return0; }
el++;
returnq[el]; }
voidmain()
{
queueaa,bb;
aa.qput(11);
bb.qput(22); aa.qput(33);
bb.qput(44);
cout<
问答题请编写函数fun(),其功能是计算并输出当x<0.97时下列多项式的值,直到|Sn-Sn-1|<0.000001为止。 Sn=1+0.5x+0.5(0.5-1)x2/2!+0.5(0.5-1)(0.5-2)x3/3!+…0.5(0.5-1)(0.5-2)…(0.5-n+1)xn/n! 例如:主函数从键盘给x输入0.21后,则输出为s=1.100000。 注意:部分源程序以存在文件test38_2.cpp中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。 文件test38_2.cpp的内容如下: #include<stdio.h> #include<iostream.h> #include<math.h> double fun(double x) void main( ) double x, s; cout<<"Input x:"<<endl; cin>>x; s=fun(x); cout<<"s="<<s<<endl;
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中定义了MyString类,一个用于表示字符串的类。成员函数reverse的功能是将字符串进行“反转”。例如,将字符串ABCDEF“反转”后,得到字符串FEDCBA;将字符串ABCDEFG“反转”后,得到字符串GFEDCBA。请编写成员函数reverse。在main函数中给出了一组测试数据,此时程序运行中应显示:
读取输入文件...
---反转前---
STR1=ABCDEF
STR2=ABCDEFG
---反转后---
STR1=FEDCBA
STR2=GFEDCBA
要求:
补充编制的内容写在“//********333********”与“//*********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中,输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
//mgsering.h
#include<iostream>
#include<cstring>
using namespace std;
class MyString{
public:
MyStrincj(const char*s)
{
str=new char[strlen(s)+1];
strcpy(str, s);
}
~MyString(){delete[]str; }
void reverse();
friend ostream
Eeturn os;
}
private;
char*str;
};
void writeToFile(char*, constMyString
//main.cpp
#include"mystring.h"
#include<fstream>
void MyString::reverse()
{
//******333******
//*******666******
}
int main()
{
char inname[128], pathname[80];
strcpy(pathname, "");
sprintf(inname, "in.dat", pathname);
cout<<"读取输入文件.../n/n";
ifstream infile(inname);
if(infile.fail()){
cerr<<"打开输入文件失败!";
exit(1);
}
char buf[4096];
infile.getline(buf, 4096);
MyString strl("ABCDEF"), str2("ABCDEFG"), str3(buf);
cout<<"---反转前---/n";
cout<<"STR1="<<str1<<<endl;
cout<<"STR2="<<str2<<endl<<endl;
str1.reverse();
str2.reverse();
str3.Eeverse();
cout<<"---反转后---/n";
cout<<"STR1="<<str1<<endl;
cout<<"STR2="<<str2<<endl<<endl;
writeToFile(pathname, str3);
return 0;
}