问答题

[说明]
①定义私有数据成员code、english分别用于表示考生的编号、英语成绩,它们都是int型的数据。
②完成成员函数void Student::inputinformation()的定义,该函数用于用户输入一个考生对象的信息,输入格式如下:
输入编号:
英语成绩:
计算机成绩:
③利用已实现的类Student的成员函数,完成函数void firstname(Student *A[],int uum)的定义,该函数根据考生信息A[],输出num个考生中总分最高者的编号及其相应的总分,在此不考虑总分相同的情况。
源程序文件test1.cpp清单如下:
#include < iostream. h >
class Student
(1)
int computer;
int total;
public
void getinformation( );
void computesum( );
int getcode( );
int gettotalscore( );
~Student( );
;
void Student: :getinformation( )

(2)
cout <<"英语成绩:";
cin> >english;
cout <<"计算机成绩:";
cin > > computer;
void Student:: computesum ( )
total = english + computer;
cout <<"编号" << code <<"总分:" <<total << endl;
int Student::getcode( )
return code;

int Student:: gettotalscore ( )

return total;

void firstname(Student * A[] ,int num)

(3)
tempsum = ( * A[0] ). gettotalscore( );
for( int i=1; i <num; i++)

if ( ( ( * A[i] ). gettotalscore( ) ) > tempsum)

tempcode = ( * A[i] ). getcode( );
tempsum = ( * A[i] ). gettotalscore( );


cout <<"总分最高者--" << tempcode <<":" << tempsum <<endl;

void main( )

Student * A[3];
int i,n =3
for(i=0;i<n;i ++)
A[i] = new Student;
A[i] -> getinformation( )
for(i=0;i<n;i ++)

A[i] -> computesum( )
firstname ( A,3 );

【参考答案】

itn code;
int english;
(B)cout<<"输入编号:";
cin>......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
[说明] 下列最短路径算法的具体流程如下:首先构造一个只含n个顶点的森林,然后依权值从小到大从连通网中选择不使森林中产生回路的边加入到森林中去,直至该森林变成一棵树为止,这棵树便是连通网的最小生成树。该算法的基本思想是:为使生成树上总的权值之和达到最小,则应使每一条边上的权值尽可能地小,自然应从权值最小的边选起,直至选出 n-1条互不构成回路的权值最小边为止。 [算法] *对图定义一种新的表示方法,以一维数组存放图中所有边,并在构建图的存储结构时将它构造为一个“有序表”。以顺序表MSTree返回生成树上各条边。* typedef struct VertexType vex1;VertexType vex2; VRType weight; EdgeType; typedef ElemType EdgeType; typedef struct 有向网的定义 VertexType vexs [MAX_VERTEX_N U M ]; 顶点信息 EdgeType edge[ MAX_EDGE_NUM]; 边的信息 int vexnum, arcnum; 图中顶点的数目和边的数目 I ELGraph; void MiniSpanTree_Kruskal( ELGraph G,SqList& MSTree) G, edge 中依权值从小到大存放有向网中各边 生成树的边存放在顺序表MSTree中 MFSetF; InitSet( F, G. vexnum ); 将森林F初始化为N棵树的集合 InitList (MSTree, G. vexnum); 初始化生成树为空树 i=0;k=1; while(k< (1) ) e = G. edge[i]; 取第i条权值最小的边 *函数fix_mfset返回边的顶点所在树的树的根代号,如果边的两个顶点所在树的树根相同,则说明它们已落在同一棵树上。 * ri = fix_mfset(F, LocateVex(e. vex1) ); r2= (2) ; 返回两个顶点所在树的树根 if(r1 (3) r2) 选定生成树上第k条边 if(Listlnsert(MSTree,k,e) (4) ; 插入生成树 mix_mfset( E, r1,r2); 将两棵树归并为一棵树 (5) ; 继续考察下一条权值最小边 DestroySet (F);
问答题
[程序5说明] 下列文法可用来描述化学分子式的书写规则(例如,A12(CO3)3”Cu(OH)2): λ→β βλ β→δ δn δ→ξ ξθ (λ) 其中:λ是—个分子式;δ或是一个元素,或是一个带括号的(子)分子式,元素或是一个大写字母(记为ξ),或是一个大写字母和一个小写字母(记为ξθ)β或是一个δ,或是在δ之后接上一个整数n,δn表示β有n个δ的元素或(子)分子式。—个完整的分子式由若干个β组成。 当然一个正确的分子式除符合上述文法规则外,还应满足分子式本身的语义要求。 下面的程序输入分子式,按上述文法分析分子式,并计算出该分子式的分子量。例如:元素H的原子量是1,元素O的原子量是16。输入分子式H2O,程序计算出它的分子量为18 (1×2+16)。程序中各元素的名及它的原子量从文件atom.dat中读入。 [程序5] #include < stdio. h > #include < string. h > #define MAXN 300 #define GMLEN 30 struct elem char name[ ]; * 元素名* double v; *原子量* nTbl [MAXN]; char cmStr [GMLEN], * pos; int c;FILE * fp; double factor( ); double atom( ) * 处理文法符号δ* char w [3];int i; double num; while((c = * pos++) ==’||c ==’ t’); *略过空白字符* if(c == ’ n’) return 0.0; if(c>=’A’ && C <=’Z’) *将元素名存入W * w[i =0]=c;c= * pos ++ if(c >=’a’&& c <=’z’)w[ ++i] =c;else pos--; w[ ++i] =’ 0’, for(i =0;nTbl [i]. v >0.0;i ++) if(strcmp (w,nTbl[i]. name) ==0) return nTbl [i]. v; printf ( n元素表中没有所输入的无素: t%s n’,w); retur n - 1.0; elseif (c = =’(’) if((num= (1) ) <0.0)return -l.0; *包括可能为空的情况* if( * pos ++ ! = ’)’) printf ( 分子式中括号不匹配! n ) ;return - 1.0; return num; printf ( 分子式中存在非法字符: t%c n ,c); return - 1.0; double mAtom( ) * 处理文法符号β* double num ;int n = ]; if((num= (2) ) <0.0)return-l.0; c= *pos++; if(c >=’O’&&c <=’9’) n = 0; while(c > = 0&&c < =’9’) n= (3) ; c= *poss ++; pos --; return num * n; double factor( ) *处理文法符号λ* double num =0.0,d; if(( hum = mAtom ( )) < 0.0) return - 1.0; while( * pos >= ’A’&& * pos <= ’Z’||* pos == ’(’) if((d= (4) ) <0.0)return-1.0; (5) ; return num;void main( ) char fname[ ] = atom. dst ; *元素名及其原子量文件* int i;double num;if((fp=fopon(fname, r )) == NULL) *以读方式打开正文文件* prinff( Can net open%s file. n’ ,fname) ;return *程序非正常结束 * i=0;while(i < MAXN&&fscanf (fp, %s%lf,bTbl[i]. name,&nTbl[i]. v) ==2) i++;fclose(fp) ;nTbl[i]. v =-1.0;while(1) [ *输入分子式和计算分子量循环,直至输入空行结束* printf( n 输入分子式! (空行结束) n ) ;gets(cmStr); pos = cmStr; if(cmStr[0] == ’ 0’) break; if( (num = later( ) ) > 0.0) if( * pos! = ’ 0’)printf( 分子式不完整! n ); else printf( 分子式的分子量为%f n ,num);