注册 登录
编程论坛 C++教室

[求助]考试题目C++

cilubutong 发布于 2007-06-25 10:11, 1575 次点击

C++程序设计

一. 编写程序,打印下面的图形:

*
* *
* *
* *
* *
* *
* *
* *
*



:编写程序,从就键盘输入10个学生的成绩,统计最高分,最低分和平均分.

要求:输入的数据首先存入一个数组中,程序中不得改变数组中这些数据的排列顺序;运行时输入下列数据并记录程序的输出结果:

80 92 78 66 75 96 88 85 99 71

:编写程序,打印M阶魔方阵,M为奇数.

分析题意:魔方阵是这样一个方阵,其行,,对角线元素之和均相等,如图1,是一个3阶的模仿阵:

8 1 6

3 5 7

4 9 2

每行各元素之和为15,没列各元素之和为15,对角线之和也为15.

:下面是定义二次多项式a*x+bx+c所对应的类

#include<iostream.h>

#include<math.h>

Class Quadratic

{

double a,b,c;

public:

Quadratic(){a=b=bc=0;}

Quadratic(double aa, Quadratic bb,double cc);

Quadratic opetator +(Quadratic &x);

Quadratic operator-(Quadratic &x);

double compute(double x);

int Root(double &r)1,double &r2);

void Print();

};

其中加,减操作符重载函数完成*thisx的加或减运算,并讲运算结果返回;Compute函数根据x的值计算二次多项式a*x+bx+c的值并返回;Root函数求出二次 方程a*x+bx+c=0的根,要求当不是二次方程( a=0)时返回-1,当有实根时返回1,并由引用参数r1r2带回这两个实根,当无实根时返回0;Print函数按ax**+bx+c的格式(X2X**2表示)输出多次二项式,并且当bc的值为负时,其前面不能出现加号。试写出这个在类定义中声明的每个成员函数在体外的定义

最好是给我个简单我方法和注释下,我是新手

[此贴子已经被作者于2007-6-25 10:15:29编辑过]

13 回复
#2
yuziguang2007-06-25 10:15

#3
jiushiwo2007-06-25 11:11
#4
游乐园2007-06-25 12:30

1.


#include<iostream>
#include<cmath>
using namespace std;
const int N = 5;
int main()
{
    int i,j;
    for(i=-N; i<=N; i++, cout<<endl)
    for(j=-N; j<=N; j++)
  ( abs(i)+abs(j) == N ) ? cout<<\"*\" : cout<<\" \";
return 0;
}


2.


#include<iostream>
#include<algorithm>
using namespace std;
template<class T>
T averageScore(T a[],int n)
{
T sum =0;
for(int i=0; i<n; i++) sum+=a[i];
return sum/n;
}


const int N = 10;


int main()
{


int record[N];
for(int i=0; i<N; ++i) cin>>record[i];
copy(record,record+10,ostream_iterator<int>(cout,\" \"));
cout<<\"\nmax score is: \"<<*max_element(record,record+10);
cout<<\"\nmin score is: \"<<*min_element(record,record+10);
cout<<\"\naverage score is: \"<<averageScore(record,N)<<endl;
return 0;
}

#5
cilubutong2007-06-25 13:05

谢谢了!虽然有点看不懂但我会努力去看的

#6
aipb20072007-06-25 13:29

难得看到游乐园大哥贴代码也!

我也来个:
3,奇数阶魔方

[CODE]#include <iostream>
using namespace std;
const int side_length = 3;

int main(){
int magic_square[side_length][side_length];
//set all the position to 0
for (int i = 0;i < side_length;++i){
for (int j = 0;j < side_length;++j)
magic_square[i][j] = 0;
}
int r = 0,c = side_length / 2;
//set a fixed position to number '1'
magic_square[r][c] = 1;
for (int num = 2;num <= side_length * side_length;++num){
int cpy_r = r,cpy_c = c;
//if index out of the square then comedown(r<0) or come right(c<0)
r - 1 < 0 ? r = side_length - 1 : --r;
c - 1 < 0 ? c = side_length - 1 : --c;
if (magic_square[r][c] != 0){ //the position have been set
cpy_r + 1 > side_length - 1 ? r = 0 : r = cpy_r + 1;
c = cpy_c;
}
//set number num to the right position
magic_square[r][c] = num;
}
//display the magic square
for (i = 0;i < side_length;++i){
for (int j = 0;j < side_length;++j){
if (magic_square[i][j] < 10)
cout << " ";
cout << magic_square[i][j] << " ";
}
cout << endl << endl;
}
return 0;
}[/CODE]

#7
cilubutong2007-06-26 15:38
还有个谁帮我做下啊
#8
zkkpkk2007-06-26 17:24

/*奇数阶幻方*/
#include <stdio.h>
#define MAX 15

int main(void)
{
 int m,mm,i,j,k,ni,nj;
 int magic[MAX][MAX];
 printf(\"Enter the number you wanted\n\");
 scanf(\"%d\",&m);
 for(i = 0;i < m;i ++) //初始化
  for(j = 0;j < m;j ++)
   magic[i][j] = 0;
 if((m > 0)&&(m % 2 != 0))//奇数阶
 {
  mm = m * m;
  i = 0;//第一个值的位置
  j = m / 2;
  for(k = 1;k <= mm;k ++)
  {
   magic[i][j] = k;
   //求右上方方格的坐标
   if(i == 0)//最上一行
    ni = m - 1;//下一个位置在最下一行
   else
    ni = i - 1;
   if(j == m - 1)//最右端
    nj = 0;//下一个位置在最左端
   else
    nj = j + 1;
   //判断右上方方格是否已有数
   if(magic[ni][nj] == 0)//右上方无值
   {
    i = ni;
    j = nj;
   }
   else //右上方方格已填上数
   i ++;
  }
  for(i = 0;i < m;i ++)
  {
   for(j = 0;j < m;j ++)
    printf(\"%4d\",magic[i][j]);
    printf(\"\n\");
  }
 }
else //m<=0或m_是偶数
printf(\"Error in input data.\n\");
return 0;
}
#9
aipb20072007-06-26 17:44
终于可以发贴了。

最后个题目已经有类定义和函数接口了,楼主应该可以自己完成。

[此贴子已经被作者于2007-6-26 17:46:22编辑过]

#10
zkkpkk2007-06-26 17:47
嘿嘿,我看你那么喜欢比卡丘,就贴了个妙蛙花
#11
aipb20072007-06-26 19:48
我说怎么那么眼熟呢!
#12
cilubutong2007-06-26 22:47

不会啊! 想了好久也不会啊!因为中间有东西我没学

#13
hemingsheng2007-06-28 19:00
回复:(cilubutong)[求助]考试题目C++

程序一:
#include <iostream.h>
void main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=1;j<=7-i;j++)
cout<<' ';
for(k=1;k<=2*i-1;k++)
if(k==1||k==2*i-1)
cout<<'*';
else
cout<<' ';
cout<<endl;
}
for(i=1;i<=4;i++)
{
for(j=1;j<=i+2;j++)
cout<<' ';
for(k=1;k<=9-2*i;k++)
if(k==1||k==9-2*i)
cout<<'*';
else
cout<<' ';
cout<<endl;
}
cout<<endl;
}



程序二:
#include <iostream.h>
void main()
{int i,a[10];
float j=0;
int x,y=a[0];
for(i=0;i<10;i++)
{cout<<"请输入成绩: ";
cin>>a[i];
j=j+a[i];
}j=j/10;
cout<<"平均成绩: "<<j<<endl;

cout<<"最高分: ";
for(i=0;i<10;i++)
if(x<a[i]) x=a[i];
cout<<x<<endl;
{cout<<"最低分: ";
for(i=0;i<10;i++)
if(y>a[i]) y=a[i];
cout<<a[i]<<endl;}
for(i=0;i<10;i++)
cout<<"输出成绩: "<<a[i]<<' '<<endl;}

程序三:#include <iostream.h>
const int x=3;
void main()
{
int m[x][x];
for(int i=0;i<x;++i)
{
for(int j=0;j<x;++j)
m[i][j]=0;
}
int r=0,c=x/2;
m[r][c]=1;
for(int num=2;num<=x*x;++num)
{
int cpy_r=r,cpy_c=c;
r-1<0?r=x-1:--r;
c-1<0?c=x-1:--c;
if (m[r][c] != 0)
{
cpy_r+1>x-1?r=0:r=cpy_r+1;
c=cpy_c;
}
m[r][c]=num;
}
for(i=0;i<x;++i)
{
for (int j=0;j<x;++j)
{
if (m[i][j]<10)
cout<<" ";
cout<<m[i][j]<<" ";
}
cout<<endl<<endl;
}
}

程序四:
#include <iostream.h>
#include <math.h>
class Quadratic
{
double a,b,c;
public:
Quadratic(){a=b=c=0;}
Quadratic(double aa,double bb,double cc);
Quadratic operator+(Quadratic &x);
Quadratic operator-(Quadratic &x);
double Compute(double x);
int Root(double &r1,double &r2);
void Print();
};
Quadratic Quadratic::operator+(Quadratic &x)
{
Quadratic m;
m.a=a+x.a;
m.b=b+x.b;
m.c=c+x.c;
return m;
}
Quadratic Quadratic::operator-(Quadratic &x)
{
Quadratic n;
n.a=a-x.a;
n.b=b-x.b;
n.c=c-x.c;
return n;
}
double Quadratic::Compute(double x)
{

return a*x*x+b*x+c;
}
int Quadratic::Root(double &r1,double &r2)
{
if(a==0)
{
cout<<"不是一元二次函数"<<endl; return -1;
}
if(b*b-4*a*c==0)
{
r1=r2=(-b)/2*a; return 1;
}
if(b*b-4*a*c<0)
{
cout<<"无实根"<<endl; return 0;
}
else
{ r1=(-b)+sqrt(b*b-4*a*c)/(2*a);
r2=(-b)-sqrt(b*b+4*a*c)/(2*a);
return 1;
}
}
void Quadratic::Print()
{
if(b<0 && c<0)
{
cout<<a<<"x**2"<<b<<"x"<<c<<endl;
}
if(b<0 && c>0)
{
cout<<a<<"x**2"<<b<<"x"<<"+"<<c<<endl;
}
if(b>0 && c<0)
{
cout<<a<<"x**2"<<"+"<<b<<"x"<<c<<endl;
}

}
Quadratic::Quadratic(double aa,double bb,double cc)
{
a=aa;
b=bb;
c=cc;
}
void main()
{
double a,b,c,x;
double r1,r2;
cout<<"请输入方程三个系数a,b,c的值:"<<endl;
cout<<"a= ";cin>>a;
cout<<"b= ";cin>>b;
cout<<"c= ";cin>>c;
Quadratic p1(a,b,c);
cout<<"该函数是: "<<endl;
p1.Print();
cout<<"该函数为零的两个根是: "<<endl;
p1.Root(r1,r2);
cout<<r1<<" "<<r2<<endl;
cout<<"请输入一个X值: "<<endl;
cin>>x;
cout<<"当输入X的值为: "<<x<<"时该函数的值为: "<<endl;
p1.Compute(x);
cout<<p1.Compute(x)<<endl;
cout<<endl;
}




#14
yuziguang2007-07-04 19:46
回复:(hemingsheng)回复:(cilubutong)[求助]考试...
第四个好象有点小问题啊,那个操作符重载好像没有什么用啊
1