福师大网络学院C++语言程序设计网络作业.docx

上传人:b****6 文档编号:15895908 上传时间:2023-07-08 格式:DOCX 页数:20 大小:30.85KB
下载 相关 举报
福师大网络学院C++语言程序设计网络作业.docx_第1页
第1页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第2页
第2页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第3页
第3页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第4页
第4页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第5页
第5页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第6页
第6页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第7页
第7页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第8页
第8页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第9页
第9页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第10页
第10页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第11页
第11页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第12页
第12页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第13页
第13页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第14页
第14页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第15页
第15页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第16页
第16页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第17页
第17页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第18页
第18页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第19页
第19页 / 共20页
福师大网络学院C++语言程序设计网络作业.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

福师大网络学院C++语言程序设计网络作业.docx

《福师大网络学院C++语言程序设计网络作业.docx》由会员分享,可在线阅读,更多相关《福师大网络学院C++语言程序设计网络作业.docx(20页珍藏版)》请在冰点文库上搜索。

福师大网络学院C++语言程序设计网络作业.docx

福师大网络学院C++语言程序设计网络作业

C 语言程序设计 作业

编程:

1 编写一个程序求一元二次方程的解。

讨论下述情形:

(1)a=0,不是二次方程。

(2)b^2-4ac=0,有两个相等实根。

(3) b^2-4ac>0,有两个不等实根。

(4) b^2-4ac<0,有两个复根(表示成 x+yi,x-yi)。

2. 编一程序,求出所有各位数字的平方和等于 99 的三位数。

3. 输入一个 2X3 的整数矩阵,输出其中最大值、最小值及其所在的行列下标。

4. 编程:

输入一 3X3 整数矩阵,求其主对角线上元素之和并输出。

用矩阵:

111213

212223

313233验证。

5. 编程序求 y=1+1/1!

-1/2!

+1/3!

-1/4!

+...的值.(精度为 1e-6)。

//1.编写一个程序求一元二次方程的解。

#include

int main(){

//假设这个方程是 aX^2 + bX + c = 0 的标准形式

float a,b,c;

scanf("%f",&a);

scanf("%f",&b);

scanf("%f",&c);

if(a==0){//a 为零 ,非 2 次方程 不计算

printf("这不是一个 2 次方程");

return 0;

}else if((b*b-4*a*c) == 0) { // 相等的两实根

printf("这个方程有两个相等的根,是:

" +(-1)*b/(2*a) );

}else if((b*b-4*a*c) > 0){ //不相等的两实根

printf("这个方程有两个不相等的实根,\n 分别是:

" +

(Math.sqrt(b*b-4*a*c)- b)/(2*a) + "\n 与" + ((-1)*Math.sqrt(b*b-4*a*c)-

b)/(2*a));

}else if((b*b-4*a*c) < 0){ //不相等的复根

printf("这个方程有两个不相等的复根,\n 分别是:

" + (-1)*b/(2*a)

+""+(Math.sqrt(b*b-4*a*c)/(2*a))+"i" + "\n 与" + b/(2*a)

+""+(Math.sqrt(b*b-4*a*c)/(2*a))+"i";

}

return 0;

}

//--------------------------------------------------

//2.寻找所有各位数字的平方和等于 99 的三位数

#include

int main(){

int hundreds = 0;

int tens = 0;

int singles = 0;

for(int i=100;i<1000;i++){

hundreds = i/100; //取得百位数

tens = (i%100)/10; //取得十位数

singles = i%10;//取得个位数

if((hundreds*hundreds + tens*tens + singles*singles) ==

99){

cout<

}

}

return 0;

}

//--------------------------------------------------

//3.输入一个 2X3 的整数矩阵,输出其中最大值、最小值及其所在的行列下标。

#include

int main(){

int a[2][3] = {5,1,3,4,6,2}; //初始化矩阵

int max = a[0][0]; //初始化最大值

int maxX = 0; //初始化最大值的横坐标

int maxY = 0; //初始化最大值的纵坐标

int min = a[0][0]; //初始化最小值

int minX = 0; //初始化最小值横坐标

int minY = 0; //初始化最小值纵坐标

for(int i=0 ; i<2 ; i++){

for(int j=0; j<3 ; j++){

if(a[i][j] > max){

max = a[i][j];

maxX = i;

maxY = j;

}

else if(a[i][j] < min){

min = a[i][j];

minX = i;

minY = j;

}

}

}

cout<<"最大的数是"+max+",它的行列下标是:

 第"+maxX+"行,第"+maxY+"

列。

\n";

cout<<"最小的数是"+mix+",它的行列下标是:

 第"+mixX+"行,第"+mixY+"

列。

\n";

}

//--------------------------------------------------

//4.输入一 3X3 整数矩阵,求其主对角线上元素之和并输出。

#include

int main() {

int a[3][3],i,j,msum = 0,ssum = 0;

for(i = 0; i < 3; ++i) {

for(j = 0; j < 3; ++j) {

printf("a[%d][%d] = ",i,j);

scanf("%d",&a[i][j]);

}

}

printf("矩阵元素表:

\n");

for(i = 0; i < 3; ++i) {

for(j = 0; j < 3; ++j) {

printf("%4d",a[i][j]);

}

printf("\n");

}

for(i = 0; i < 3; ++i) {

msum += a[i][i];

ssum += a[i][2 - i];

}

printf("主对角线的和是:

%d\n 斜对角线的和是:

%d\n\n",msum,ssum);

return 0;

}

//--------------------------------------------------

//5、编程序求 y=1+1/1!

-1/2!

+1/3!

-1/4!

+...的值.(精度为 1e-6)。

#include

int main()

{

int i,j,m,n,n;

for(i = 0; i< j; i++)

{

if == 0

y = 1;

else

if(i % 2 == 1

{

for(n = 1; n <= i; n++)

{

m = m + n * i;;

}

y = y + m;

}

else

{

for(n = 1; n <= i; n++)

{

m= m+ n * i;

}

y =-y - m;

})

return 0; 0; 0;

}

}

C++语言程序设计第二次作业

填充下面的划线部分,使其完成所要求的功能。

答案卷只要写题号与填充的答

案,不要题目:

如:

1. xxxxxxx2.xxxxxxx…。

将答案卷直接粘贴到作业栏。

1.计算下列分段函数,X 由键盘输入。

0 (x<=-10.0)

y= x (-10.0

2x-3(x>10.0)

#include"stdio.h"

main()

{

float x,y;

if(x<=-10.0)y=0;

else if (1x<=10.0) y=x;

else y=2*x-3;

printf(“%f\n”,x);

}

2. invert()函数的功能是将一个字符串 str 的内容倒序存放;

例如:

 字符串 str 原内容为:

abcde,函数调用后变为:

edcba。

#include

#include

void invert (char str[ ])

{int i, j, k;

j=_____2 strlen(str)_____;

for (i=0; i

{ k=str[i];

str[i]=str[j];

str[j]=__3_k_____;

}

}

void main()

{ char test[]="abcde";

invert (test);

cout<

}

3. .下列程序打印出 1000 以内的所有“完全数”。

“完全数”是指一个正整数,

其所有

小于该数的因子之和等于该数本身。

例如:

6=1+2+3,又如:

28=1+2+4+7+14。

 

#include “iostream.h”

#include”iomanip.h”

void main()

{int i, j,s;

for (j=2; j<=1000; j++)

{ s=0;

for (i=1; i

if (___4_s%i == 0___)s+=i;

if (___5s == j____) cout<

}}

4. 以下程序显示如下所示的矩阵,矩阵中每个元素形成的规律是:

右上三角阵

(含

对角线)元素值为 1,其它元素值为:

行下标—列下标+1。

11111

21111

32111

43211

54321

#include”iostream.h”

#include”iomanip.h”

main()

{int i,j, a[5][5];

for( i=0; i<=4; i++ )

for(j=0; j<5; j++ )

if(___6_i<=j___)a[i][j] = 1;

else __7 a[i][j]___=i-j+l;

for(i=0; i<5; i++)

{ for(j=0; j<5; j++) cout<

cout<< "\n";

}

}

5. 以下 scat 函数将字符串 str2 连接到字符串 strl 之后。

运行时,

若输入:

Li Ming,回车

Good morning!

回车

则输出:

Li Ming,Good morning!

请填空完成程序。

#include "iostream.h"

#include”stdio.h”

scat( char *strl, char *str2)

{ while(*strl !

=___8_’,’____)strl++;

while( *strl++ =___9_str2_____);/*将 str2 连接到 strl 的后而*/

}

main()

{char a[500],b[300];

gets( a );//从键盘输入一字符串放入 a,

gets( b );//字符串中可包含空格

scat( a, b );

cout<

}

6. 以下程序是用来输出如下图形:

#

*#*

#*#*#

*#*#*#*

#*#*#*#*#

#include"iostream.h"

main()

{

int i, j;

for( i=1;i<=5;i++ )

{

for( j=1;j<=5-i; j++ )cout<< " ";

for( j=1;j<=2*i-1;j++ )

if (__10 j%2 == 0____ ) cout<< "*" ;

else cout<< "#" ;

cout<< "\n";

}

}

7. 以下程序是用来输入 5 个整数,并存放在数组中,找出最大数与最小数所在

的下标位置,并把二者对调,然后输出调整后的 5 个数。

#include"iostream.h"

main()

{

inta[5], t, i, maxi, mini;

for( i=0;i<5;i++)

cin>> a[i];

mini=maxi=___11 0____;

for( i=1;i<5;i++)

{

if (___12_a[mini] > a[i] ___)mini=i;

if( a[i]>a[maxi] ) ___13_ maxi = i___;

}

cout<< "最小数的位置是:

"<< mini<<”\n”;

cout<< "最大数的位置是:

"<< maxi<<”\n”;

t=a[maxi];

____14__a[maxi] = a[mini]____;

a[mini]=t;

cout<< "调整后数的数为:

";

for( i=0;i<5;i++ )

cout<

cout<<"\n";

}

C++语言程序设计第三次作业

编写程序:

1. 定义一个 Point 类来处理三维点 points(x,y,z).该类有一默认的

constructor,一 copy constructor, 一 negate()成员函数将 point 的 x,y

和 z 值各乘-1, 一 norm()成员函数返回该点到原点(0,0,0)的距离,一

个 print()成员函数显示 x,y,和 z 的值。

#include "iostream"

using namespace std;

class Point

{

private:

double x,y,z;

public:

Point();

Point(double x0, double y0,double z0);

void negate();

void norm();

void print();

};

Point:

:

Point()

{

x = 0;

y = 0;

z = 0;

}

Point:

:

Point(double x0,double y0,double z0)

{

x = x0;

y = y0;

z = z0;

}

void Point:

:

negate()

{

x = -1*x;

y = -1*y;

z = -1*z;

}

void Point:

:

norm()

{

x = 0;

y = 0;

z = 0;

}

void Point:

:

print()

{

cout<<"("<

}

void main()

{

Point point;

point.print();

Point point1(1,2,-3);

point1.negate();

point1.print();

point1.norm();

point1.print();

}

2.定义一个 Person 类,它的每个对象表示一个人。

数据成员必须包含姓名、出

生年份、死亡年份,一个默认的构造函数,一析构函数,读取数据的成员函数,

一个 print()成员函数显示所有数据。

#include "iostream"

using namespace std;

class Person

{

private:

char* name;

char* birth;

char* death;

public:

Person();

~Person();

void readIn();

void print();

};

Person:

:

Person()

{

name = "空";

birth = "空";

death = "空";

}

Person:

:

~Person()

{

cout<<"类被析构"<

}

void Person:

:

readIn()

{

cout<<"输入姓名:

";

gets(name);

cout<<"输入出生年份";

gets(birth);

cout<<"输入死亡年份";

gets(death);

}

void Person:

:

print()

{

cout<<"姓名:

"<

"<

"<

}

void main()

{

Person person;

person.print();

person.readIn();

person.print();

}

3。

定义一个 Shape 基类,由它派生出 Rectanglr 和 Circle 类,二者都有

GetArea( )函数计算对象的面积。

使用 Rectangle 类创建一个派生类 Square。

#include "iostream"

using namespace std;

class Shape

{

public:

double getArea();

};

 

double Person:

:

Shape()

{

cout<<"面积:

";

}

class Rectanglr:

public Shape

{

private:

double a,b;

public:

Rectanglr();

double getArea();

};

Rectanglr:

:

Rectanglr()

{

a = 2;

b = 3;

}

double Rectanglr:

:

getArea()

{

retrun a*b;

}

class Circle:

public Shape

{

private:

double r;

public:

Circle();

double getArea();

};

Circle:

:

Circle()

{

r = 3;

}

double Circle:

:

getArea()

{

return 3.14*r*r;

}

 

void main()

{

Shape *s = new Rectanglr();

s->getArea();

}

4.定义一个 Shape 抽象类,由它派生出 Rectanglr 和 Circle 类,二者都有

GetArea( )函数计算对象的面积,GetPerim( ) 函数计算对象的周长。

#include "iostream"

using namespace std;

class Shape

{

public:

virtual double getArea();

virtual double GetPerim();

};

class Rectanglr:

public Shape

{

private:

double a,b;

public:

Rectanglr();

double getArea();

double GetPerim();

};

Rectanglr:

:

Rectanglr()

{

a = 2;

b = 3;

}

double Rectanglr:

:

getArea()

{

retrun a*b;

}

double Rectanglr:

:

GetPerim()

{

return 2*(a+b);

}

class Circle:

public Shape

{

private:

double r;

public:

Circle();

double getArea();

double GetPerim();

};

Circle:

:

Circle()

{

r = 3;

}

double Circle:

:

getArea()

{

return 3.14*r*r;

}

double Circle:

:

GetPerim()

{

return 2*3.14*r;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2