收藏 分销(赏)

西北工业大学c--实验课选考题.doc

上传人:仙人****88 文档编号:8517470 上传时间:2025-02-16 格式:DOC 页数:5 大小:41.04KB 下载积分:10 金币
下载 相关 举报
西北工业大学c--实验课选考题.doc_第1页
第1页 / 共5页
西北工业大学c--实验课选考题.doc_第2页
第2页 / 共5页


点击查看更多>>
资源描述
西北工业大学C++实验课选考习题 例9.1有两个长方体,其长,宽,高分别为1,2,3和10,20,30。分别求它们的体积。设计一个类表示长方体,在类中用带参数的构造函数。 #include<iostream> using namespace std; class Cuboid{ public: Cuboid(int l,int h,int d); int volumn(){return length*height*depth;}; private: int length,height,depth; }; Cuboid::Cuboid(int l,int h,int d) { length=1,height=h,depth=d; cout<<"Cuboid:"<<"L="<<l<<",H="<<h<<",D="<<d<<endl; } int main() { Cuboid a(1,2,3); cout<<"volumn"<<a.volumn()<<endl; Cuboid b(10,20,30); cout<<"volumn="<<b.volumn()<<endl; return 0; } 例9.2 平面上有两个点,其x,y坐标分别为0,0和1,2。编成显示坐标值。设计一个类表示平面上的点,三和积两个带参数的和不带参数版本的构造函数。 #include<iostream> using namespace std; class Point{ public: Point(){x=y=0;} Point(int a,int b):x(a),y(b){} void display(){cout<<"x="<<x<<",y="<<y<<endl;} private: int x,y; }; int main() { Point m; m.display(); Point n(1,2); n.display(); return 0; } 例9.3用带默认参数的构造函数改进例9.2。 #include<iostream> using namespace std; class Point{ public: Point(int a=0,int b=0):x(a),y(b){} void display(){cout<<"x="<<x<<",y="<<y<<endl;} private: int x,y; }; int main() {Point k,m(1),n(1,2); k.display();m.display();n.display(); return 0; } 例9.4设计一个类表示字符串,长度可以动态变化。 #include<iostream> using namespace std; class CString{ public: CString(const char*str); ~CString(); void show(){cout<<p<<endl;} private: char*p; }; CString::CString(const char*str) { p=new char [strlen(str)+1]; strcpy(p,str); cout<<"构造:"<<str<<endl; } CString::~CString() { cout<<"析构:"<<p<<endl; delete[]p;; }int main() { CString s1("C++"),s2="JavaScript"; s1.show();s2.show(); return 0; } 例14.1将源文件每行文本添加一个行号输入到目的文件中。 #include<fstream> #include<iomanip> using namespace std; int main() { char s1[500]; int cnt=0; ifstream inf("a.cpp"); if(!inf.fail()){ ofstream outf("b.cpp"); while(!inf.eof()){ inf.getline(s1,sizeof(s1)-1); outf<<setfill('0')<<setw(4)<<++cnt<<" "<<s1<<endl; } outf.close(); inf.close(); } return 0; } 例14.2复制文件到目的文件,支持命令行文件名输入。 #include<iostream> #include<fstream> using namespace std; int main(int argc,char *argv[]) { char src[260],dest[260],buff[16384]; if(argc<2)cin>>src; else strcpy(src,argv[1]); if(argc<3)cin>>dest; else strcpy(dest,argv[2]); ifstream inf(src,ios_base::out|ios_base::binary); if(!inf.fail()){ ofstream outf(dest,ios_base::out|ios_base::binary); while(!inf.eof()){ inf.read(buff,inf.gcount()); outf.write(buff,inf.gcount()); } outf.close(); inf.close(); } return 0; } 例14.3已知文件book,dat中有100个书籍销售记录,每个销售记录由代码(char c[5]),书名单价和数量4个部分组成。文件每哈国包含代码书名单价数量数据,用Tab间隔,格式如下; 1001 软件世界 5 100 1002 计算机工程 6 120 ……. 读取这100个销售记录,将每个销售记录的内存数据写入out.dat文件;然后将out.dat的第一个记录(0位头文件)覆盖到最后的记录中。 #include<fstream> using namespace std; struct BOOK{ char c[5]; char n[11]; int p; int q; }; int main() { BOOK a; ifstream inf("book.dat"); ios_base::openmode m=ios_base::in|ios_base::out; fstream iof("out.dat",m|ios_base::trunc|ios_base::binary); if(inf.fail()||iof.fail())return-1; while(!inf.eof()){ inf>>a.c>>a.n>>a.p>>a.q; iof.write((char*)&a,sizeof(BOOK)); } inf.close(); iof.seekg(1*sizeof(BOOK),ios_base::beg); iof.read((char*)&a,sizeof(BOOK)); iof.seekg(1*sizeof(BOOK),ios_base::end); iof.write((char*)&a,sizeof(BOOK)); iof.close();return 0; } 例14.4输入加减运算的表达式(如1000+200-30+4+0.4),计算其值。 #include<iostream> #include<sstream> using namespace std; int main() { string s1,s2; ostringstream oss(ostringstream::out); istringstream iss(istringstream::in); char c1='+',c2; double val,sum=0.0; cin>>s2; iss.str(s2); while(c1!=' '){ iss>>val>>c2; if(c1=='+')sum=sum+val; else if(c1=='-')sum=sum-val; c1=c2,c2=' '; } oss<<sum;s1=oss.str(); cout<<s1<<endl; return 0; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服