C++课程设计题目和代码.docx

上传人:b****3 文档编号:6053992 上传时间:2023-05-09 格式:DOCX 页数:25 大小:30.86KB
下载 相关 举报
C++课程设计题目和代码.docx_第1页
第1页 / 共25页
C++课程设计题目和代码.docx_第2页
第2页 / 共25页
C++课程设计题目和代码.docx_第3页
第3页 / 共25页
C++课程设计题目和代码.docx_第4页
第4页 / 共25页
C++课程设计题目和代码.docx_第5页
第5页 / 共25页
C++课程设计题目和代码.docx_第6页
第6页 / 共25页
C++课程设计题目和代码.docx_第7页
第7页 / 共25页
C++课程设计题目和代码.docx_第8页
第8页 / 共25页
C++课程设计题目和代码.docx_第9页
第9页 / 共25页
C++课程设计题目和代码.docx_第10页
第10页 / 共25页
C++课程设计题目和代码.docx_第11页
第11页 / 共25页
C++课程设计题目和代码.docx_第12页
第12页 / 共25页
C++课程设计题目和代码.docx_第13页
第13页 / 共25页
C++课程设计题目和代码.docx_第14页
第14页 / 共25页
C++课程设计题目和代码.docx_第15页
第15页 / 共25页
C++课程设计题目和代码.docx_第16页
第16页 / 共25页
C++课程设计题目和代码.docx_第17页
第17页 / 共25页
C++课程设计题目和代码.docx_第18页
第18页 / 共25页
C++课程设计题目和代码.docx_第19页
第19页 / 共25页
C++课程设计题目和代码.docx_第20页
第20页 / 共25页
亲,该文档总共25页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C++课程设计题目和代码.docx

《C++课程设计题目和代码.docx》由会员分享,可在线阅读,更多相关《C++课程设计题目和代码.docx(25页珍藏版)》请在冰点文库上搜索。

C++课程设计题目和代码.docx

C++课程设计题目和代码

课程设计报告

一、代码

A1

输出10至99之间每位数的乘积大于每位数的和的数,例如对于数字12,有1*2<1+2,故不输出该数;对于27,有2*7>2+7,故输出该数。

#include"stdafx.h"

#include

usingnamespacestd;

intmain(intargc,char*argv[])

{

inttens;

intunits;

intcount=0;

cout<<"输出10至99之间每位数的乘积大于每位数的和的数,例如对于数字12,有1*2<1+2,故不输出该数;对于27,有2*7>2+7,故输出该数。

\n如下:

\n";

for(inti=10;i<100;i++){

tens=i/10;

units=i%10;

if((tens*units)>(tens+units)){

cout<

count++;

if(count%10==0){

cout<<"\n";

}

}

}

cout<<"\n";

return0;

}

A2

求任意n个数中的最大数和最小数:

先输入一个正整数n(个数),而后再输入任意n个实数,找出这n个数中的最大数及最小数并显示出来。

#include"stdafx.h"

#include"stdio.h"

#include

usingnamespacestd;

voida2(floatarr[],intn){

floatmin=arr[0];

floatmax=arr[0];

for(inti=0;i

if(min>arr[i]){

min=arr[i];

}elseif(max

max=arr[i];

}

}

cout<<"最小的数:

"<

"<

}

intmain(intargc,char*argv[])

{

float*pArray=NULL;

intn,max,min;

cout<<"请输入实数个数n:

";

cin>>n;

pArray=newfloat[n];

for(inti=0;i

cout<<"请输入第"<

";

cin>>pArray[i];

}

a2(pArray,n);

printf("\n");

return0;

}

B3

对两个有序数组进行合并:

设有如下数组A、B,并假设两个数组的元素都已经有序(从大到小降序排列)。

编程序,合并A、B数组形成一个新的数组C,并使C的元素仍有序(从大到小降序排列)。

intA[10]={123,86,80,49,33,15,7,0,-1,-3};

intB[10]={100,64,51,50,27,19,15,12,5,2};

#include"stdafx.h"

#include"stdio.h"

#include

usingnamespacestd;

voidb3(intarrA[],intlengthA,intarrB[],intlengthB,intarrC[]){

intlengthC=lengthA+lengthB;

intcountA=0;

intcountB=0;

intcountC=0;

while(true){

if(arrA[countA]>arrB[countB]){

arrC[countC]=arrA[countA];

countC++;

countA++;

}else{

arrC[countC]=arrB[countB];

countC++;

countB++;

}

while(countA>=lengthA&&countB

arrC[countC++]=arrB[countB++];

}

while(countA=lengthB){

arrC[countC++]=arrA[countA++];

}

if(countA>=lengthA&&countB>=lengthB){

break;

}

}

cout<<"\n数组A:

\n";

for(intj1=0;j1

cout<

}

cout<<"\n数组B:

\n";

for(intj2=0;j2

cout<

}

cout<<"\n组合的新数组C:

\n";

for(intj3=0;j3

cout<

}

}

intmain(intargc,char*argv[])

{

cout<<"\n对两个有序数组进行合并:

设有如下数组A、B,并假设两个数组的元素都已经有序(从大到小降序排列)。

编程序,合并A、B数组形成一个新的数组C,并使C的元素仍有序(从大到小降序排列)。

\n";

intarrA[10]={123,86,80,49,33,15,7,0,-1,-3};

intarrB[10]={100,64,51,50,27,19,15,12,5,2};

intarrC[20]={0};

b3(arrA,10,arrB,10,arrC);

printf("\n");

return0;

}

B4

有一个分数序列:

1/2,1/3,1/4,1/5,1/6,1/7,……,编写函数求序列前n项之和,要求在主程序中提示用户输入整数n,并判断所输入数是否合法(大于1为合法),如果合法则调用求和函数并输出结果。

#include"stdafx.h"

#include

usingnamespacestd;

voidb4(intn){

floatnum=0;

for(inti=2;i

num=num+1.0/i;

}

cout<<"\n该序列的前"<

"<

}

intmain(intargc,char*argv[])

{

intn=0;

cout<<"\n有一个分数序列:

1/2,1/3,1/4,1/5,1/6,1/7,……,编写函数求序列前n项之和,要求在主程序中提示用户输入整数n,并判断所输入数是否合法(大于1为合法),如果合法则调用求和函数并输出结果。

\n";

cout<<"\n请输入正整数n:

";

cin>>n;

while(n<=1){

cout<<"输入的n不合法,请重新输入n:

";

cin>>n;

}

b4(n);

return0;

}

B5

计算两个日期之间的间隔天数:

从键盘输入两个日期(如以year1,month1,day1以及year2,month2,day2的方式来输入它们),而后计算出这两个日期的间隔天数并在屏幕上显示出结果。

要求编制具有如下原型的函数difs2Date:

longGetDayDifference(inty1,intm1,intd1,inty2,intm2,intd2);并在主函数中调用向屏幕上输出计算结果。

#include"stdafx.h"

#include

#include

usingnamespacestd;

/*比较两个数的大小若num1>num2则返回ture,否则返回false*/

boolcompare(intnum1,intnum2){

if(num1-num2>0){

returntrue;

}else{

returnfalse;

}

}

/*判断是否是闰年是则返回true,否则返回false*/

boolisLeap(intyear){

boolresult=false;

if((year%4==0&&year%100!

=0)||(year%400==0)){

result=true;

}

returnresult;

}

/*得到某月的天数,此处不对二月做判断处理,故需另行处理*/

intgetMonthDays(intmonth){

intdays=30;/*默认返回小月的天数,即30天*/

intbigMonth[]={1,3,5,7,8,10,12};/*大月即天数为31天的月份*/

for(inti=0;i<7;i++){

if(month==bigMonth[i]){

days=31;

}

}

returndays;

}

longb5(inty1,intm1,intd1,inty2,intm2,intd2){

longintervalDay=0;

boolisBigMonth=false;//判断m1是否为大的月,默认不是

intstartYear=y1;

intendYear=y2;

intstartMonth=m1;

intendMonth=m2;

intstartDay=d1;

intendDay=d2;

intyearD=0;

intmonthD=0;

intdayD=0;

intmonthDS=0;

intmonthDE=0;

/*年月都相同*/

if(y1==y2&&m1==m2){

if(abs(d1-d2)==0){

intervalDay=0;

}else{

intervalDay=abs(d1-d2)-1;

}

}elseif(y1==y2){

/*年相同月不同日随意*/

if(compare(m1,m2)){

isBigMonth=true;

}

dayD=d2-d1;

if(isBigMonth){

startMonth=m2;

endMonth=m1;

dayD=d1-d2;

}

for(inti=startMonth;i

if(i==2){

if(isLeap(y1)){

intervalDay=intervalDay+29;

}else{

intervalDay=intervalDay+28;

}

}else{

intervalDay=intervalDay+getMonthDays(i);

}

}

intervalDay=intervalDay+dayD-1;

}else{

/*年不同月日随意*/

if(compare(y1,y2)){

startYear=y2;

endYear=y1;

startMonth=m2;

endMonth=m1;

startDay=d2;

endDay=d1;

}

for(inti=startYear+1;i

if(isLeap(i)){

yearD=yearD+366;

}else{

yearD=yearD+365;

}

}

for(inti1=startMonth;i1<12;i1++){//求开始年的开始月到开始年年末的天数

if(i1==2){

if(isLeap(startYear)){

monthDS=monthDS+29;

}else{

monthDS=monthDS+28;

}

}

monthDS=monthDS+getMonthDays(i);

}

dayD=31-startDay;

monthDS=monthDS+dayD;

for(inti2=1;i2

if(i2==2){

if(isLeap(startYear)){

monthDE=monthDE+29;

}else{

monthDE=monthDE+28;

}

}

monthDE=monthDE+getMonthDays(i);

}

dayD=endDay-1;

monthDE=monthDE+dayD;

monthD=monthDE+monthDS;

intervalDay=abs(yearD+monthD);

}

returnintervalDay;

}

intmain(intargc,char*argv[])

{

cout<<"\n计算两个日期之间的间隔天数:

从键盘输入两个日期(如以year1,month1,day1以及year2,month2,day2的方式来输入它们),而后计算出这两个日期的间隔天数并在屏幕上显示出结果。

要求编制具有如下原型的函数difs2Date:

longGetDayDifference(inty1,intm1,intd1,inty2,intm2,intd2);并在主函数中调用向屏幕上输出计算结果。

"<

cout<<"\n默认日期1:

2012年3月1日"<

cout<<"默认日期2:

2013年2月1日"<

cout<<"日期1和日期2相隔"<

inty1,m1,d1,y2,m2,d2;

cout<<"\n请输入第一个日期的年:

";

cin>>y1;

cout<<"请输入第一个日期的月:

";

cin>>m1;

cout<<"请输入第一个日期的日:

";

cin>>d1;

cout<<"请输入第二个日期的年:

";

cin>>y2;

cout<<"请输入第二个日期的月:

";

cin>>m2;

cout<<"请输入第儿个日期的日:

";

cin>>d2;

cout<<"自定义日期1:

"<

cout<<"自定义日期2:

"<

cout<<"日期1和日期2相隔"<

return0;

}

C7

声明并定义一个日期类CDate,其中数据成员m_iYear,m_iMonth,m_iDay,分别表示年、月、日,成员函数SetDate()用来设置年、月、日,成员函数IsLeapYear()用来判断当前的年份是否为闰年,构造函数带有默认形参值,可接收外部参数对m_iYear,m_iMonth,m_iDay进行初始化,另要求编写测试程序,定义一个CDate类对象,将其日期设置为2005年1月1日,调用成员函数IsLeapYear()判断该年份是否为闰年,并输出判断结果.

说明:

闰年的年份可以被4整除而不能被100整除,或者能被400整除.

#include"stdafx.h"

#include

usingnamespacestd;

classCDate{

private:

intm_iYear,m_iMonth,m_iDay;

public:

CDate(inty=2005,intm=1,intd=1);

voidSetDate(inty,intm,intd);

boolIsLeapYear();

~CDate(){};

};

voidCDate:

:

SetDate(inty,intm,intd){

m_iYear=y;

m_iMonth=m;

m_iDay=d;

cout<<"\n这是自定义日期:

"<

}

CDate:

:

CDate(inty,intm,intd){

m_iYear=y;

m_iMonth=m;

m_iDay=d;

cout<<"\n这是默认日期:

"<

}

boolCDate:

:

IsLeapYear(){

if((m_iYear%4==0&&m_iYear%100!

=0)||m_iYear%400==0){

cout<

returntrue;

}

cout<

returnfalse;

}

intmain(intargc,char*argv[])

{

cout<<"\n声明并定义一个日期类CDate,其中数据成员m_iYear,m_iMonth,m_iDay,分别表示年、月、日,成员函数SetDate()用来设置年、月、日,成员函数IsLeapYear()用来判断当前的年份是否为闰年,构造函数带有默认形参值,可接收外部参数对m_iYear,m_iMonth,m_iDay进行初始化,另要求编写测试程序,定义一个CDate类对象,将其日期设置为2005年1月1日,调用成员函数IsLeapYear()判断该年份是否为闰年,并输出判断结果.\n";

CDatedate;

date.IsLeapYear();

date.SetDate(2012,11,26);

date.IsLeapYear();

return0;

}

C8

编写一个程序计算两个给定长方形的面积,其中在设计类成员函数GetTotalArea()(用于计算两个长方形的总面积)时使用对象作为参数。

#include"stdafx.h"

#include

usingnamespacestd;

classRectangle{

private:

floatlength,width;

public:

floatgetArea();

voidsetSize(floatlen,floatwid);

};

floatRectangle:

:

getArea(){

returnlength*width;

}

voidRectangle:

:

setSize(floatlen,floatwid){

length=len;

width=wid;

}

floatgetTotalArea(Rectanglerec1,Rectanglerec2){

floatrec1Area=rec1.getArea();

floatrec2Area=rec2.getArea();

return(rec1Area+rec2Area);

}

intmain(intargc,char*argv[])

{

cout<<"\n编写一个程序计算两个给定长方形的面积,其中在设计类成员函数GetTotalArea()(用于计算两个长方形的总面积)时使用对象作为参数。

\n";

Rectanglerec1,rec2;

rec1.setSize(1.5,1);

rec2.setSize(1.5,2);

floattotalArea=getTotalArea(rec1,rec2);

cout<<"\n这两个长方形的面积和为:

"<

return0;

}

C9

设计一个时间类Time,包括3个数据成员,时(h)、分(m)、秒(s),另外包括存取各数据成员和设置时间的成员函数,按上、下午各12小时或按24小时输出时间的成员函数,以及默认构造函数,默认时间值为0时0分0秒。

#include"stdafx.h"

#include"stdio.h"

#include

usingnamespacestd;

classTime{

private:

inthour,minutes,seconds;

public:

Time(){

hour=0;

minutes=0;

seconds=0;

}

voidsetTime(inthours,intminutes,intseconds);

voidshowTime();

voidshowTimePMOrAM();

};

voidTime:

:

setTime(intnewH,intnewM,intnewS){

hour=newH;

minutes=newM;

seconds=newS;

}

voidTime:

:

showTime(){

cout<<"24小时制:

"<

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

当前位置:首页 > 医药卫生 > 药学

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

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