【说明】现需要统计某企业员工的月平均工资,即该企业本月发给员工的工资总和除以员工数。假设企业本月发给员工的工资总和为sumSalary,该企业的员工总数为employeeNumber,下面的程序代码计算该企业员工本月的平均工资,其中需要处理employNumber为0的情况。【C++代码】
【问题1】程序运行时,若输入的员工工资总和为6000,员工数为5,则屏幕输出为:请输入当月工资总和与员工数:
【问题2】若程序运行时,第一次输入的员工工资总和为6000,员工数为0,第二次输入的员工工资总和为0,员工数为0,则屏幕输出为:请输入当月工资总和与员工数:
第1题
阅读以下说明和C代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
函数MultibaseOutput(long n, int B)的功能是:将一个无符号十进制整数n转换成B(2≤B≤16)进制数并输出。该函数先将转换过程中得到的各位数字入栈,转换结束后再把B进制数从栈中输出。有关栈操作的诸函数功能见相应函数中的注释。C代码中的符号常量及栈的类型定义如下:
#define MAXSIZE 32
typedef struct {
int *elem; /* 栈的存储区 */
int max; /* 栈的容量,即栈中最多能存放的元素个数 */
int top; /* 栈顶指针 */
}Stack;
[C代码]
int InitStack(Stack *S, int n) /* 创建容量为n的空栈 */
{ S->elem = (int *)malloc(n * sizeof(int));
if(S->elem == NULL) return -1;
S->max = n; (1) = 0 ; return 0;
}
int Push(Stack *S, int item) /* 将整数item压入栈顶 */
{ if(S->top == S->max){ printf("Stack is full!\n"); return -1;}
(2) = item ; return 0;
}
int StackEmpty(Stack S) { return (!S.top) ? 1 : 0; } /* 判断栈是否为空 */
int Pop(Stack *S) /* 栈顶元素出栈 */
{ if(!S->top) { printf("Pop an empty stack!\n"); return -1;}
return (3) ;
}
void MultibaseOutput(long n, int B)
{ int m; Stack S;
if (InitStack(&S, MAXSIZE)) {printf("Failure!\n"); return;}
do {
if (Push(&S, (4) )) {printf("Failure!\n"); return;}
n = (5) ;
}while(n != 0);
while(!StackEmpty(S)) { /* 输出B进制的数 */
m = Pop(&S);
if(m < 10) printf("%d", m); /* 小于10,输出数字 */
else printf("%c", m + 55); /* 大于或等于10,输出相应的字符 */
}
printf("\n");
}
第2题
[函数1.1说明]
函数int factors(int n)的功能是判断整数n(n>=2)是否为完全数。如果n是完全数,则函数返回0,否则返回-1。
所谓“完全数”是指整数n的所有因子(不包括n)之和等于n自身。例如28的因子为1、2、4、7、14,而28=1+2+4+7+14,因此28是“完全数”。
[函数1.1]
int factors(int n)
{
int i,s;
for(i=1,s=0;i<=n/2;i++)
if(n%i==0) (1) ;
if( (2) )return 0;
return -1;
}
[函数1.2说明]
函数int maxint(int a[], int k)的功能是用递归方法求指定数组中前k个元素的最大值,并作为函数值返回。
[函数1.2]
int maxint(int a[],int k)
{
int t;
if( (3) ) return (4) ;
t=maxint(a+1, (5) );
return (a[0]>t)?a[0]:t;
第3题
说明
通常情况下,用户可以对应用系统进行配置,并将配置信息保存在配置文件中。应用系统在启动时首先将配置文件加载到内存中,这些内存配置信息应该有且仅有一份。下面的代码应用了单身模式(Singleton)以保证Configure类只能有一个实例。这样, Configure类的使用者无法定义该类的多个实例,否则会产生编译错误。
C++代码
#include<iostream.h>
class Configure{
(1):
Configure(){} //构造函数
public:
static Configure*Instance();
public:
int GetConf~ureData(){return data;} //获取配置信息
int SetConfigureDate(int m_data)
{data=m_data; return data;} //设置配置信息
private:
static Configure*_instance;
int data; //配置信息
};
(2)=NULL;
Configure*Configure∷Instance(){
if(_instance=NULL){
_instance=(3);
//加载配置文件并设置内存配置信息,此处省略
}
return (4);
}
void main()(
Configure*t=NULL;
t=(5);
int d=t→GetConfigureData();
//获取配置信息后进行其他工作,此处省略
}
第4题
阅读以下说明和 C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
C++标准模板库中提供了 vector 模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空间为 std。vector模板类的部分方法说明如下表所示:
[C++代码]
include <iostream>
include <vector>
using namespace (1) ;
typedef vector< (2) > INTVECTOR;
const int ARRAY_SIZE = 6;
void ShowVector(INTVECTOR &theVector);
int main(){
INTVECTOR theVector;
// 初始化 theVector,将 theVector的元素依次设置为 0 至 5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.push_back( (3) );
ShowVector(theVector); // 依次输出 theVector中的元素
theVector.erase(theVector.begin() + 3);
ShowVector(theVector);
}
void ShowVector(INTVECTOR &theVector) {
if (theVector.empty()) {
cout << "theVector is empty." << endl; return;
}
INTVECTOR::iterator (4) ;
for (theIterator = theVector.begin(); theIterator != theVector.end(); theIterator++){
cout << *theIterator;
if (theIterator != theVector.end()-1) cout << ", ";
}
cout << endl;
}
该程序运行后的输出结果为:
0, 1, 2, 3, 4, 5
(5)
第5题
阅读以下说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
将一正整数序列{K1,K2,…,K9}重新排列成一个新的序列,新序列中,比K1小的数都在K1的前面(左面),比K1大的数都在K1的后面(右面),最后调用writeDat()函数的新序列输出到文件out.dat中。
在程序中已给出了10个序列,每个序列有9个正整数,并存入数组a[10][9]中,分别求出这10个新序列。
例:序列 {6,8,9,1,2,5,4,7,3}
经重排后成为{3,4,5,2,1,6,8,9,7}
【函数】
#include<stdio.h>
#include<conio.h>
void jsValue(int a[10][9])
{int i,j,k,n,temp;
int b[9];
for(i=0;i<10;i++)
{temp=a[i][0];
k=8;n=0;
for(j=8;j=0;j--)
{if(temp<a[i][j]) (1) =a[i][j];
if(temp>a[i][j]) (2) =a[i][j];
if(temp=a[i][j]) (3) =temp;
}
for(j=0;j<9;j++)a[i][j]=b[j];
}
}
void main()
{
int a[10][9]={{6,8,9,1,2,5,4,7,3},{3,5,8,9,1,2,6,4,7},
{8,2,1,9,3,5,4,6,7},{3,5,1,2,9,8,6,7,4},
{4,7,8,9,1,2,5,3,6},{4,7,3,5,1,2,6,8,9},
{9,1,3,5,8,6,2,4,7},{2,6,1,9,8,3,5,7,4},
{5,3,7,9,1,8,2,6,4},{7,1,3,2,5,8,9,4,6}
};
int i,j;
(4) ;
for(i=0;i<10;i++){
for(j=0;j<9;j++){
printf("%d",a[i][j]);
if( (5) )printf(",");
}
printf("\n");
}
getch();
}
第6题
阅读以下说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
该程序的功能是从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中,以行为单位对行中以空格或标点符号为分隔的所有单词进行倒排。最后把已处理的字符串(应不含标点符号)仍按行重新存入字符串数组xx中,最后把结果xx输出到文件OUT6.DAT中。
例如:原文:You He Me
I am a student.
结果:Me He You
student a am I
原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。
【函数】
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
char xx[50][80];
int maxline=0;/*文章的总行数*/
int ReaaDat(void);
void WriteDat(void);
void StrOL(void)
{
char*pl,*p2,t[80];
int i;
for(i=0;i<maxline;i++)
{p1=xx[i];t[0]=0;
while(*p1)p1++;
while(p1>=xx[i])
{while(!isalpha(*p1)&&p1!=xx[i])p1--;
p2=p1;
while( (1) )p1--;
if(p1==xx[i])
if(isalpha(*p1))p1--;
else if(!isalpha(*(p1+1)))break;
p2++;
(2) ;
strcat(t,p1+1);
strcat(t," ");
}
strcpy(xx[i],t);
}
}
void main()
{
if( (3) ){
printf("数据文件in.dat不能打开!\n\007");
return;
}
StrOL();
writeDat();
getch();
}
int ReadDat(void)
{
FILE*fp;
int i=0;
char*p;
if((fp=fopen("e:\\a\\in.dat","r"))==NULL)return 1;
while(fgets(xx[i],80,fp)!=NULL){
p=strchr(xx[i],′\n′);
if(p)*p=0;
i++;
}
maxline= (4)
fclose(fp);
return 0;
}
void WriteDat(void)
{
FILE*fp;
int i;
fp=fopen("e:\\a\\out6.dat","w");
for(i=0;i< (5) ;i++){
printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
第7题
阅读以下说明和 C 代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
在一个简化的绘图程序中,支持的图形种类有点(point)和圆(circle),在设计过程中
用面向对象思想,认为所有的点和圆都是一种图形(shape),并定义了类型 shape_t、poin
和 circle_t 分别表示基本图形、点和圆,并且点和圆具有基本图形的所有特征。
[C 代码]
typedef enum { point,circle } shape_type; /* 程序中的两种图形:点和圆 */
typedef struct { /* 基本的图形类型 */
shape_type type; /* 图形种类标识:点或者圆 */
void (*destroy)(); /* 销毁图形操作的函数指针 */
void (*draw)(); /* 绘制图形操作的函数指针 */
} shape_t;
typedef struct { shape_t common; int x; int y; } point_t; /* 定义点类型, x、 y 为点坐标
void destroyPoint(point_t* this) { free(this); printf("Point destoryed!\n"); } /* 销毁点对象
void drawPoint(point_t* this) { printf("P(%d,%d)", this->x, this->y); } /* 绘制点对象 */
shape_t* createPoint(va_list* ap) { /* 创建点对象,并设置其属性 */
point_t* p_point;
if( (p_point = (point_t*)malloc(sizeof(point_t))) == NULL ) return NULL;
p_point->common.type = point; p_point->common.destroy = destroyPoint;
p_point->common.draw = drawPoint;
p_point->x = va_arg(*ap, int); /* 设置点的横坐标 */
p_point->y = va_arg(*ap, int); /* 设置点的纵坐标 */
return (shape_t*)p_point; /* 返回点对象指针 */
}
typedef struct { /* 定义圆类型 */
shape_t common;
point_t *center; /* 圆心点 */
int radius; /* 圆半径 */
} circle_t;
void destroyCircle(circle_t* this){
free( (1) ); free(this); printf("Circle destoryed!\n");
}
void drawCircle(circle_t* this) {
printf("C(");
(2) .draw( this->center ); /* 绘制圆心 */
printf(",%d)", this->radius);
}
shape_t* createCircle(va_list* ap) { /* 创建一个圆,并设置其属性 */
circle_t* p_circle;
if( (p_circle = (circle_t*)malloc(sizeof(circle_t))) == NULL ) return NULL;
p_circle->common.type = circle; p_circle->common.destroy = destroyCircle;
p_circle->common.draw = drawCircle;
(3) = createPoint(ap); /* 设置圆心 */
p_circle->radius = va_arg(*ap, int); /* 设置圆半径 */
return p_circle;
}
shape_t* createShape(shape_type st, ...) { /* 创建某一种具体的图形 */
va_list ap; /* 可变参数列表 */
shape_t* p_shape = NULL;
(4) (ap, st);
if( st == point ) p_shape = createPoint( &ap); /* 创建点对象 */
if( st == circle ) p_shape = createCircle(&ap); /* 创建圆对象 */
va_end(ap);
return p_shape;
}
int main( ) {
int i; /* 循环控制变量,用于循环计数 */
shape_t* shapes[2]; /* 图形指针数组,存储图形的地址 */
shapes[0] = createShape( point, 2, 3); /* 横坐标为 2,纵坐标为 3 */
shapes[1] = createShape( circle, 20, 40, 10); /* 圆心坐标(20,40),半径为 10 */
for(i=0; i<2; i++) { shapes[i]->draw(shapes[i]); printf("\n"); } /* 绘制数组中图形 */
for( i = 1; i >= 0; i-- ) shapes[i]->destroy(shapes[i]); /* 销毁数组中图形 */
return 0;
}
[运行结果]
P(2,3)
(5)
Circle destoryed
Point destoryed!
第8题
阅读以下说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
函数MultibaseOutput(long n,int B)的功能是:将一个无符号十进制整数n转换成B(2≤B≤16)进制数并输出。该函数先将转换过程中得到的各位数字入栈,转换结束后再把B进制数从栈中输出。有关栈操作的诸函数功能见相应函数中的注释。C代码中的符号常量及栈的类型定义如下:
#define MAXSIZE 32
typedef struct{
int *elem;/*栈的存储区*/
int max; /*栈的容量,即栈中最多能存放的元素个数*/
int top;/*栈顶指针*/
}Stack;
【代码】
int InitStack(Stack *S,int n)/*创建容量为n的空栈*/
{S->elem=(int*)malloc(n *sizeof(int));
if(S->elem==NULL)return-1;
S->max=n; (1) =0;return 0;
}
int Push (Stack *s,int item)/*将整数item压入栈顶*/
{if(S->top==S->max){printf(″Stack is full!\n″);return-1;}
(2) =item;return 0;
}
int StackEmpty(Stack S){return(! S.top)?1∶0;}/*判断栈是否为空*/
int Pop(Stack *S)/*栈顶元素出栈*/
{if(! S->top){printf(″Pop an empty stack!\n″);return -1;}
return (3) ;
}
void MultibaseOutput(long n,int B)
{int m;Stack S;
if(InitStack(&S,MAXSIZE)){printf(″Failure!\n″);return;}
do {
if(Push(&S, (4) )){printf(″Failure!\n″);return;}
n= (5) ;
}while(n !=0);
while(! StackEmpty(S)){/*输出B进制的数*/
m=Pop(& S);
if(m<10)printf(″%d″,m);/*小于10,输出数字*/
else printf(″%c″,m+55);/*大于或等于10,输出相应的字符*/
}
printf(″\n″);
}
第9题
阅读以下预备知识、函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【预备知识】
①对给定的字符集合及相应的权值,采用哈夫曼算法构造最优二叉树,并用结构数组存储最优二叉树。例如,给定字符集合{a,b,c,d}及其权值2、7、4、5,可构造如图3所示的最优二叉树和相应的结构数组Ht(数组元素Ht[0]不用)(见表5)。
图3最优二叉树
表5 结构数组Ht
结构数组Ht的类型定义如下:
define MAXLEAFNUM 20
struct node{
char ch;/*当前结点表示的字符,对于非叶子结点,此域不用*/
int weight;/*当前结点的权值*/
int parent;/*当前结点的父结点的下标,为0时表示无父结点*/
int lchild,rchild;
/*当前结点的左、右孩子结点的下标,为0时表示无对应的孩子结点*/
}Ht[2*MAXLEAFNUM];
②用′0′或′1′标识最优二叉树中分支的规则是:从一个结点进入其左(右)孩子结点,就用′0′(′1′)标识该分支(示例如图3所示)。
③若用上述规则标识最优二叉树的每条分支后,从根结点开始到叶子结点为止,按经过分支的次序,将相应标识依次排列,可得到由′0′、′1′组成的一个序列,称此序列为该叶子结点的前缀编码。例如图3所示的叶子结点a、b、c、d的前缀编码分别是110、0、111、10。
【函数5.1说明】
函数void LeafCode(int root,int n)的功能是:采用非递归方法,遍历最优二叉树的全部叶子结点,为所有的叶子结点构造前缀编码。其中形参root为最优二叉树的根结点下标;形参n为叶子结点个数。
在构造过程中 ,将Ht[p].weight域用作被遍历结点的遍历状态标志。
【函数5.1】
char**Hc;
void LeafCode(int root,int n)
{/*为最优二叉树中的n个叶子结点构造前缀编码,root是树的根结点下标*/
int i,p=root,cdlen=0;char code[20];
Hc=(char**)malloc((n+1)*sizeof(char*));/*申请字符指针数组*/
for(i=1;i<=p;++i)
Ht[i].weight=0;/*遍历最优二叉树时用作被遍历结点的状态标志*/
while(p){/*以非递归方法遍历最优二叉树,求树中每个叶子结点的编码*/
if(Ht[p].weight==0){/*向左*/
Ht[p].weight=1;
if (Ht[p].lchild !=0) { p=Ht[p].lchild; code[cdlen++]=′0′;}
else if (Ht[p].rchild==0) {/*若是叶子结 点,则保存其前缀编码*/
Hc[p]=(char*)malloc((cdlen+1)*sizeof(char));
(1) ;strcpy(He[p],code);
}
}
else if (Ht[p].weight==1){/*向右*/
Ht[p].weight=2;
if(Ht[p].rchild !=0){p=Ht[p].rchild;code[cdlen++]=′1′;}
}
else{/*Ht[p].weight==2,回退*/
Ht[p].weight=0;
p= (2) ; (3) ;/*退回父结点*/
}
}/*while结束*/
}
【函数5.2说明】
函数void Decode(char*buff,int root)的功能是:将前缀编码序列翻译成叶子结点的字符序列并输出。其中形参root为最优二叉树的根结点下标;形参buff指向前缀编码序列。
【函数5.2】
void Decode(char*buff,int root)
{ int pre=root,p;
while(*buff!=′\0′){
p=root;
while(p!=0){/*存在下标为p的结点*/
pre=p;
if( (4) )p=Ht[p].lchild;/*进入左子树*/
else p=Ht[p].rchild;/*进入右子树*/
buff++;/*指向前缀编码序列的下一个字符*/
}
(5) ;
printf(″%c″,Ht[pre].ch);
}
}
第10题
阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
以下程序的功能是:从键盘上输入一个字符串,把该字符串中的小写字母转换为大写字母,输出到文件test.txt中,然后从该文件读出字符串并显示出来。
【程序】
#include<stdio.h>
main()
{FILE*fp;
charstr[100];inti=0;
if((fp=fopen("text.txt" (1) ))==NULL)
{printf("can't open this file.\n");exit(0);}
printf("input astring:\n");gest(str);
while(str[i])
{if(str[i]>=′a′ && str[i]<=′z′)
str[i]= (2) ;
fputc(str[i], (3) );
i++;
}
fclose(fp);
fp=fopen("test.txt", (4) );
fgets(str,100,fp);
printf("%s\n",str);
(5) ;
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!