VC++作业.docx
- 文档编号:28323459
- 上传时间:2023-07-10
- 格式:DOCX
- 页数:114
- 大小:7.74MB
VC++作业.docx
《VC++作业.docx》由会员分享,可在线阅读,更多相关《VC++作业.docx(114页珍藏版)》请在冰豆网上搜索。
VC++作业
_____________大学
课程实践报告
设计题目:
程序设计(VC++)实践
设计时间:
至
学院(系):
专业班级:
学生姓名:
学号:
指导老师:
20年月
基础题:
设计:
通过PP构造函数进行赋,通过fun()函数求多项式的值,通过process()函数完成求前n项的和,通过show()函数输出值。
类的层次图:
classPP
{
intn;doublex;double*p;
public:
PP(intnum,doublex1){}//构造函数
~PP(){}//析构函数
doublefun(intn1,doublex){}//求多项式的和
voidprocess(){}//求前n项和
voidshow(){}//输出结果
};
fun()函数定义如下:
doublefun(intn1,doublex)
{
if(n1==0)return1;
elseif(n1==1)returnx;
return((2*n1-1)*x*fun(n1-1,x)-(n1-1)*fun(n1-2,x))/n1;
}
源程序:
#include
classPP
{
intn;
doublex;
double*p;
public:
PP(intnum,doublex1)
{
n=num;
x=x1;
p=newdouble[n+1];
}
~PP()
{
if(p)delete[]p;
}
doublefun(intn1,doublex)
{
if(n1==0)return1;
elseif(n1==1)returnx;
return((2*n1-1)*x*fun(n1-1,x)-(n1-1)*fun(n1-2,x))/n1;
}
voidprocess()
{
inti;
for(i=0;i<=n;i++)
p[i]=fun(i,x);
}
voidshow()
{
inti;
for(i=1;i<=n+1;i++)
{
cout<
if(i%4==0)cout<<'\n';
}
}
};
voidmain()
{
intnum;
doublex1;
cout<<"num=";
cin>>num;
cout<<"x1=";
cin>>x1;
PPitems(num,x1);
items.fun(num,x1);
items.process();
items.show();
}
小结:
学会运用递归函数。
设计:
通过SP()函数进行赋值;通过power()函数求m的n次方;通过fun()函数进行累加;通过show()函数输出结果。
类的层次图:
classSP
{
intn,k;
public:
SP(intn1,intk1){}//构造函数
intpower(intm,intn){}//求m的n次方
intfun(){}//累加
voidshow(){}//输出结果
};
power()函数定义如下:
intpower(intm,intn)
{
intp=1,i;
for(i=1;i<=n;i++)
p=p*m;
returnp;
}
fun()函数定义如下:
intfun()
{
ints=0,i;
for(i=0;i<=n;i++)
s=s+power(i,k);
returns;
}
源程序:
#include
classSP
{
intn,k;
public:
SP(intn1,intk1)
{
n=n1;
k=k1;
}
intpower(intm,intn)
{
intp=1,i;
for(i=1;i<=n;i++)
p=p*m;
returnp;
}
intfun()
{
ints=0,i;
for(i=0;i<=n;i++)
s=s+power(i,k);
returns;
}
voidshow()
{
cout<<"当n="< } }; voidmain() { intn1,k1; cout<<"n1="; cin>>n1; cout<<"k1="; cin>>k1; SPs(n1,k1); s.power(n1,k1); s.fun(); s.show(); } 小结: 学会调用函数求和。 设计: 通过MOVE()函数进行赋值;通过average()函数求出平均值并重新排序;通过print()函数输出结果。 类的层次图: classMOVE { floatarray[20];intn; public: MOVE(floatb[],intm){}//构造函数 voidaverage(){}//求平均数并排序 voidprint(){}//输出结果 }; average()函数定义如下: voidaverage() { floataver,s=0,c[20];inti; for(i=0;i s=s+array[i]; aver=s/n;cout<<"平均值为: "< intk=0,l=n-1; for(i=0;i { if(array[i]<=aver) { c[k]=array[i]; k++; } else { c[l]=array[i]; l--; } } for(i=0;i array[i]=c[i]; } 源程序: #include classMOVE { floatarray[20]; intn; public: MOVE(floatb[],intm) { inti; for(i=0;i array[i]=b[i]; n=m;} voidaverage() { floataver,s=0,c[20];inti; for(i=0;i s=s+array[i]; aver=s/n;cout<<"平均值为: "< intk=0,l=n-1; for(i=0;i { if(array[i]<=aver) { c[k]=array[i]; k++; } else { c[l]=array[i]; l--; } } for(i=0;i array[i]=c[i]; } voidprint() { for(inti=0;i cout< } }; voidmain() { floata[]={1.3,6.2,3,9.1,4.8,7.4,5.6,9.2,2.3}; MOVEa1(a,9); a1.average(); a1.print(); } 小结: 学会比较大小。 设计: 通过MOVE()函数进行赋值;通过exchange()函数将最大元素和最小元素进行位置调换;通过print()函数输出结果。 类的层次图: classMOVE { int*array;intn; public: MOVE(intb[],intm){}//构造函数 voidexchange(){}//换位置 voidprint(){}//输出结果 ~MOVE(){}//析构函数 }; exchange()函数定义如下: voidexchange() { ints=0,max,min,x,y; floataver; max=array[0]; min=array[0]; for(inti=0;i { s=s+array[i]; if(array[i]>max){max=array[i];x=i;} if(array[i] } aver=s/n; array[x]=min; array[y]=max; cout<<"平均值为: "< } 源程序: #include classMOVE { int*array; intn; public: MOVE(intb[],intm) { n=m; array=newint[n]; for(inti=0;i array[i]=b[i]; } voidexchange() { ints=0,max,min,x,y; floataver; max=array[0]; min=array[0]; for(inti=0;i { s=s+array[i]; if(array[i]>max){max=array[i];x=i;} if(array[i] } aver=s/n; array[x]=min; array[y]=max; cout<<"平均值为: "< } voidprint() { for(inti=0;i cout< } ~MOVE() { if(array)delete[]array; } }; voidmain() { inta[]={21,65,45,87,12,84,44,97,32,55}; MOVEa1(a,10); a1.exchange(); a1.print(); } 小结: 学会元素互换和数组指针的运用。 设计: 通过Palindrome()函数进行赋值;通过huiwen()函数判断是否为回文数;通过show()函数输出结果。 类的层次图: classPalindrome { intn;inty; public: Palindrome(intx){}//构造函数 voidhuiwen(){}//判断回文数 voidshow(){}//输出结果 }; huiwen()函数定义如下: voidhuiwen() { int*p_10,*p_2,m,y_10=1,y_2=1,len_10=0,len_2=0,i; for(m=n;m;len_10++) m/=10; p_10=newint[len_10]; m=n; for(i=0;i { p_10[i]=m%10; m=m/10; } for(i=0;i { if(p_10[i]! =p_10[len_10-i]) y_10=0; } for(m=n;m;len_2++) m/=2; m=n; p_2=newint[len_2]; for(i=0;i { p_2[i]=m%2; m/=2; } for(i=0;i { if(p_2[i]! =p_2[len_2-i]) y_2=0; } if(y_10! =0&&y_2! =0) y=1; } 源程序: #include #include classPalindrome { intn; inty; public: Palindrome(intx) { n=x; y=0; } voidhuiwen() { int*p_10,*p_2,m,y_10=1,y_2=1,len_10=0,len_2=0,i; for(m=n;m;len_10++) m/=10; p_10=newint[len_10]; m=n; for(i=0;i { p_10[i]=m%10; m=m/10; } for(i=0;i { if(p_10[i]! =p_10[len_10-i]) y_10=0; } for(m=n;m;len_2++) m/=2; m=n; p_2=newint[len_2]; for(i=0;i { p_2[i]=m%2; m/=2; } for(i=0;i { if(p_2[i]! =p_2[len_2-i]) y_2=0; } if(y_10! =0&&y_2! =0) y=1; } voidshow() { if(y=0) cout< else cout< } }; voidmain() { inta; cout<<"请输入数字: "< cin>>a; Palindromep(a); p.huiwen(); p.show(); } 小结: 学会十进制和二进制的转换。 设计: 通过String()函数进行初始化;通过huiwen()函数判断字符串是否为回文数;通过show()函数输出结果。 类的层次图: classString { char*str;inty; public: String(char*s){}//构造函数 voidhuiwen(){}//判断回文数 voidshow(){}//输出结果 }; huiwen()函数定义如下: voidhuiwen() { char*t,*p; t=str; p=str; y=1; while(*t)t++; t--; for(;p if(*p! =*t)y=0; if(y) cout<<"该字符串为回文字符串."< else cout<<"该字符串不是回文字符串."< } 源程序: #include #include classString { char*str; inty; public: String(char*s) { str=newchar[strlen(s)+1]; strcpy(str,s); y=0; } voidhuiwen() { char*t,*p; t=str; p=str; y=1; while(*t)t++; t--; for(;p if(*p! =*t)y=0; if(y) cout<<"该字符串为回文字符串."< else cout<<"该字符串不是回文字符串."< } voidshow() { cout< } }; voidmain() { chars[]="ababcedbaba"; Stringtest(s); test.huiwen(); test.show(); } 小结: 学会字符指针的运用。 设计: 通过PHALANX()函数进行数据的初始化;通过process()函数产生n行方阵;通过print()函数输出结果。 类的层次图: classPHALANX { int(*p)[20];intstartnum;intn; public: PHALANX(ints,intm){}//构造函数 voidprocess(){}//产生方阵 voidprint(){}//输出结果 ~PHALANX(){}//析构函数 }; process()函数定义如下: voidprocess() { for(inti=0;i<=n-1;i++) for(intj=i;j { for(intk=0;k<=j;k++) p[k][j]=startnum++; for(intl=j-1;l>=0;l--) p[j][l]=startnum++; } } 源程序: #include classPHALANX { int(*p)[20]; intstartnum; intn; public: PHALANX(ints,intm) { p=newint[20][20]; startnum=s; n=m; } voidprocess() { for(inti=0;i<=n-1;i++) for(intj=i;j { for(intk=0;k<=j;k++) p[k][j]=startnum++; for(intl=j-1;l>=0;l--) p[j][l]=startnum++; } } voidprint() { for(inti=0;i { for(intj=0;j cout< cout< } } ~PHALANX() { if(p)delete[]p; } }; voidmain() { ints,n; cout<<"startnum="< cin>>s; cout<<"n="< cin>>n; PHALANXtest(s,n); test.process(); test.print(); } 小结: 学会指针数组的运用。 设计: 通过CString()函数进行赋值;通过Replace()函数完成字符的替换;通过Show函数输出结果。 类的层次图: classCString { char*str;char*str1;char*str2;intflag; public: CString(char*s,chars1[],chars2[]){}//构造函数 voidReplace(){}//字符替换 voidShow(){}//输出结果 ~CString(){}//析构函数 }; Replace()函数定义如下: voidReplace() { char*p1,*p2,*p3,*p4; p1=str; p2=str1; p4=str2; inta=strlen(str); intb=strlen(str1); for(inti=0;i { if(*p1==*p2) { p3=p1; for(intj=0;j { if(*p1==*p2) { p1++; p2++; } if(*p1! =*p2) break; } } elsep1++; if(*p2==0) { flag=1; for(intj=0;j { *p3=*p4; p3++; p4++; } p4=str2; p2=str1; } } 源程序: #include #include classCString { char*str; char*str1; char*str2; intflag; public: CString(char*s,chars1[],chars2[]) { str=newchar[strlen(s)+1]; str1=newchar[strlen(s1)+1]; str2=newchar[strlen(s2)+1]; strcpy(str,s); strcpy(str1,s1); strcpy(str2,s2); flag=0; } voidReplace() { char*p1,*p2,*p3,*p4; p1=str; p2=str1; p4=str2; inta=strlen(str); intb=strlen(str1); for(inti=0;i { if(*p1==*p2) { p3=p1; for(intj=0;j { if(*p1==*p2) { p1++; p2++; } if(*p1! =*p2)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VC 作业