c++編程題匯總450份.doc_第1頁
c++編程題匯總450份.doc_第2頁
c++編程題匯總450份.doc_第3頁
c++編程題匯總450份.doc_第4頁
c++編程題匯總450份.doc_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

學(xué)號 姓名 院(部) 專業(yè)考試時間:2008年月 日-密-封-線-c+編程題試卷第三章1.編寫一個求方程ax2 + bx + c = 0的根 的程序,用3個函數(shù)分別求當(dāng)b2-4ac大于零、等于零、和小于零時的方程的根。要求從主函數(shù)輸入a,b,c的值并輸出結(jié)果。 #include #include void equation_1 (int a, int b, int c)double x1, x2, temp;temp = b*b - 4 * a * c;x1 = (-b + sqrt(temp) ) / (2 * a * 1.0);x2 = (-b - sqrt(temp) ) / (2 * a * 1.0);cout兩個不相等的實根 endl;coutx1 = x1, x2 = x2 endl;void equation_2 (int a, int b, int c)double x1, x2, temp;temp = b*b - 4 * a * c;x1 = (-b + sqrt(temp) ) / (2 * a * 1.0);x2 = x1;cout兩個相等的實根 endl;coutx1 = x1, x2 = x2 endl;void equation_3 (int a, int b, int c)double temp, real1, real2, image1, image2;temp = - (b*b - 4 * a * c);real1 = -b / (2 * a *1.0);real2 = real1;image1 = sqrt(temp);image2 = - image1;cout兩個虛根 endl;coutx1 = real1 + image1j endl;coutx2 = real2 + image2j endl;void main()int a, b, c;double temp;cout輸入a,b,c的值abc;cout方程為: ax*x+ bx+ c = 0 0)equation_1 (a, b, c);if(temp = 0)equation_2 (a, b, c);if(temp 0)equation_3 (a, b, c);2.定義函數(shù)up(ch),如字符變量ch是小寫字母就轉(zhuǎn)換成大寫字母并通過up返回,否則字符ch不改變。要求在短小而完全的程序中顯示這個程序是怎樣被調(diào)用的。#include using namespace std;char up (char c)if(c = 97 & c = 122)return (c - 32) ;elsereturn c;void main()int i;char c15 = A,v,e,t,E,T,%,&,4,Y,e,i,9,;for(i = 0 ; i 15 ; i+)cout up(ci), ;cout endl;3.編寫主程序條用帶實數(shù)r和整數(shù)n兩個參數(shù)的函數(shù)并輸出r的n次冪。#include #include double power(double a, int b)int i;double result = 1.0;for(i=0;i b;i+)result = result * a;return result;void main()double r;int n;coutr;coutn;cout r的 n次冪是: power(r,n) endl;4.編寫有字符型參數(shù)C和整形參數(shù)N的函數(shù),讓他們顯示出由字符C組成的三角形。其方式為第1行有1個字符C,第2行有2個字符C ,等等。#include using namespace std;void print_triangle(char c, int n)int i, j;for(i=0; i n; i+)for(j=0; j=i; j+)cout c;cout endl;void main()print_triangle(a,10);5.編寫一個ieqiu字符串長度的函數(shù),strlen(),再用strlen()函數(shù)編寫一個函數(shù)revers(s)的倒序遞歸程序,使字符串s逆序。#include #include using namespace std;int strlen(char *str)int len = 0;while(strlen != 0)len+;return len;void revers(char *b)char c; int j, len; len=strlen(b); j=len/2-1; while(j=0) c=*(b+j); *(b+j)=*(b+len-j-1); *(b+len-j-1)=c; j-; blen=0;void main()char str=1234567890;cout str-的長度: strlen(str) endl;cout str endl;/倒序前revers(str);/cout str endl;/倒序后6.用函數(shù)模板實現(xiàn)3個數(shù)值中按最小值到最大值排序的程序。#include using namespace std;template void sort(T a, T b, T c)T array3,temp;int i,j;array0 = a;array1 = b;array2 = c;for(i=0;i3;i+)for(j=0;jarrayj+1)temp = arrayj;arrayj = arrayj+1;arrayj+1 = temp;cout array0 array1 array2 endl;void main()sort(5,1,9);7.利用函數(shù)模板設(shè)計一個求數(shù)組元素中和的函數(shù),并檢驗之。#include using namespace std;template T sum (T a,int n)int i;T s=0;for(i=0;i n;i+)s = s + ai;return s;void main ()int a5=1,2,3,4,5;int s = sum(a,5);cout s endl;8.重載上題中的函數(shù)模板,使他能夠進行兩個數(shù)組的求和。#include using namespace std;template T sum (T a, int n)int i;T s=0;for(i=0;i n;i+)s = s + ai;return s;template /重載上面的模板T sum (T a, int n, T b, int m)return sum(a,n)+sum(b,m);void main ()int a5=1,2,3,4,5;int b10=1,2,3,4,5,6,7,8,9,10;int s1 = sum(a, 5);int s2 = sum(b, 10);int s3= sum(a, 5, b, 10);cout s1 endl;cout s2 endl;cout s3 endl;第四章1.設(shè)計一個點類Point,再設(shè)計一個矩形類,矩形類使用Point類的兩個坐標(biāo)點作為矩形的對角頂點。并可以輸出4個坐標(biāo)值和面積。使用測試程序驗證程序。#include using namespace std;class Point/點類private:int x, y;/私有成員變量,坐標(biāo)public :Point()/無參數(shù)的構(gòu)造方法,對xy初始化x = 0;y = 0;Point(int a, int b)x = a;y = b;void setXY(int a, int b)x = a;y = b;int getX()/得到x的方法return x;int getY()/得到有的函數(shù)return y;class Rectangle/矩形類private:Point point1, point2, point3, point4;public :Rectangle();/類Point的無參構(gòu)造函數(shù)已經(jīng)對每個對象做初始化啦,這里不用對每個點多初始化了Rectangle(Point one, Point two)point1 = one;point4 = two;init();Rectangle(int x1, int y1, int x2, int y2)point1.setXY(x1, y1);point4.setXY(x2, y2);init();void init()/給另外兩個點做初始化的函數(shù)point2.setXY(point4.getX(), point1.getY() );point3.setXY(point1.getX(), point4.getY() );void printPoint()/打印四個點的函數(shù)coutA:( point1.getX() , point1.getY() ) endl;coutB:( point2.getX() , point2.getY() ) endl;coutC:( point3.getX() , point3.getY() ) endl;coutD:( point4.getX() , point4.getY() ) 0)return area;elsereturn -area;void main()Point p1(-15, 56), p2(89, -10);/定義兩個點Rectangle r1(p1, p2);/用兩個點做參數(shù),聲明一個矩形對象r1Rectangle r2(1, 5, 5, 1);/用兩隊左邊,聲明一個矩形對象r2cout矩形r1的4個定點坐標(biāo): endl;r1.printPoint();cout矩形r1的面積: r1.getArea() endl;coutn矩形r2的4個定點坐標(biāo): endl;r2.printPoint();cout矩形r2的面積: r2.getArea() endl;2.使用內(nèi)聯(lián)函數(shù)設(shè)計一個類,用來表示直角坐標(biāo)系中的任意一條直線并輸出它的屬性。#include #include class Lineprivate:int x1, y1, x2, y2;public :Line();Line(int =0, int =0, int =0, int=0 );void printPoint();double getLength();inline Line:Line(int a, int b, int c, int d)x1 = a;y1 = b;x2 = c;y2 = d;inline void Line:printPoint()coutA: x1 , y1 endl;coutB: x2 , y2 endl;inline double Line:getLength()double length;length = sqrt(x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );return length;void main()Line line(10,80,-10,12);line.printPoint();cout line.getLength() endl;第五章1.聲明復(fù)數(shù)的類,complex,使用友元函數(shù)add實現(xiàn)復(fù)數(shù)加法。#include using namespace std;class Complexprivate:double real, image;public :Complex()Complex(double a,double b)real = a;image = b;void setRI(double a, double b)real = a;image = b;double getReal()return real;double getImage()return image;void print()if(image0)cout復(fù)數(shù): real + image i endl;if(image0)cout復(fù)數(shù): real - image i endl;friend Complex add(Complex ,Complex);/聲明友元函數(shù);Complex add(Complex c1, Complex c2)/定義友元函數(shù)Complex c3;c3.real = c1.real + c2.real;/訪問Complex類中的私有成員c3.image = c1.image + c2.image;return c3;void main()Complex c1(19, 0.864), c2, c3;c2.setRI(90,125.012);c3 = add(c1, c2);cout復(fù)數(shù)一:;c1.print();cout復(fù)數(shù)二:;c2.print();cout相加后:;c3.print();3.編寫一個程序,該程序建立一個動態(tài)數(shù)組,為動態(tài)數(shù)組的元素賦值,顯示動態(tài)數(shù)組的值并刪除動態(tài)數(shù)組。#include using namespace std;void main()int i, n, temp=0;coutn;double *array = new doublen; /用指針,動態(tài)申請數(shù)組大小cout給每個數(shù)組元素賦值: endl;for(i=0; i n; i+)coutarray i temp;*(array+i) = temp;/給數(shù)組元素賦值cout動態(tài)數(shù)組個元素的值如下: endl;for(i=0; i n; i+)coutarray i = arrayi endl;/打印數(shù)組元素delete array;/釋放內(nèi)存4.定義一個Dog類,它用靜態(tài)數(shù)據(jù)成員Dogs記錄Dog的個體數(shù)目,靜態(tài)成員函數(shù)GetDogs用來存取Dogs。設(shè)計并測試這個類。#include using namespace std;class Dogprivate:static int dogs;/靜態(tài)數(shù)據(jù)成員,記錄Dog的個體數(shù)目public :Dog()void setDogs(int a)dogs = a;static int getDogs()return dogs;int Dog : dogs = 25;/初始化靜態(tài)數(shù)據(jù)成員void main()cout未定義Dog類對象之前:x = Dog:getDogs() endl; /x在產(chǎn)生對象之前即存在,輸出25Dog a, b;couta中x: a.getDogs() endl;coutb中x: b.getDogs() endl;a.setDogs(360);cout給對象a中的x設(shè)置值后: endl;couta中x: a.getDogs() endl;coutb中x: b.getDogs() endl;第六章1.設(shè)計一個基類,從基類派生圓柱,設(shè)計成員函數(shù)輸出它們的面積和體積;#include using namespace std;class Basic/基類protected:double r;public :Basic() r = 0; Basic(double a):r(a);class Circular : public Basic/從基類派生圓類protected:double area;public :Circular(double a)r = a;area = area = 3.1415926 * r * r;double getArea()/返回圓面積return area;class Column : public Circular/從圓類派生圓柱類protected:double h;double cubage;public :Column(double a, double b) : Circular(a)h = b;cubage = getArea() * h;double getCubage()/返回圓柱體積函數(shù)return cubage;void main()Circular circular(45);Column column(12, 10);cout圓的面積: circular.getArea() endl;cout圓柱的體積: column.getCubage() endl;3.定義一個線段類作為矩形的基類,基類有起點和終點坐標(biāo),有輸出左邊和長度以及線段和x軸的夾角的成員函數(shù)。矩線段對象的兩個坐標(biāo)作為自己一條邊的位置,它具有另外一條邊,能輸出矩形的4個頂點坐標(biāo)。給出類的定義并用程序驗證它們的功能。#include #include using namespace std;class Point/點類protected:double x, y;public :Point()Point(double a, double b)x = a; y = b;double getX()return x;double getY()return y;class Lineprotected:Point p1, p2;/Point對象做成員double length, angle;public:Line(double a, double b, double c, double d):p1(a, b), p2(c, d)/用兩對坐標(biāo)初始化線段init();Line(Point a, Point b)/用兩個點的對象初始化線段p1 = a; p2 = b;init();void init()/計算線段長度,以及和x軸的夾角的度數(shù)double x1 = p1.getX(), y1 = p1.getY();double x2 = p2.getX(), y2 = p2.getY();length = sqrt(x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);angle = atan( (y2-y1) / (x2-x1) );angle = angle *180/3.141592653;void printXY()cout( p1.getX() , p1.getY() ); ( p2.getX() , p2.getY() ) endl;void printLength()cout線段長度: length endl;void printAngle()cout與x軸的夾角: angle endl;class Rectangle : public Lineprotected:Line *line;public:Rectangle(double a, double b, double c, double d, double e, double f, double g, double h):Line(a,b,c,d)line = new Line(e,f,g,h);Rectangle(Point a, Point b, Point c, Point d) : Line(a, b)/4個點對象,初始化line = new Line(c, d);void printPoint()coutprintXY();void main()Point p1(0, 0), p2(4, 3), p3(12, 89), p4(10, -50);Line l1(0,0,4,3);l1.printXY();l1.printLength();l1.printAngle();Line l2(p1, p2);l2.printXY();l2.printLength();l2.printAngle();Rectangle r1(12,45,89,10,10,23,56,1);r1.printPoint();Rectangle r2(p1, p2, p3, p4);r2.printPoint();4.基類是使用極坐標(biāo)的點類,從它派生一個圓類,圓類用點類的左邊作圓心,圓周通過極坐標(biāo)原點,圓類有輸出圓心直、圓半徑和面積的成員函數(shù)。完成類的設(shè)計并驗證之。#include #include using namespace std;class Point/點類protected:int x, y;public :Point();class Circular : public Point/圓類,繼承點類protected:double r, area;public :Circular(int a, int b)x = a;y = b;r = sqrt( x * x + y * y );area = 3.1415926 * r * r;void printPoint()cout圓形直角坐標(biāo):( x , y ) endl;void printRadius()cout圓的半徑: r endl;void printArea()cout圓的面積: area endl;void main()Circular c(10,25);c.printPoint();c.printRadius();c.printArea();5.設(shè)計一個線段基類,當(dāng)創(chuàng)建五參數(shù)對象時,才要求用戶輸入長度。同樣,其派生的直角三角形類也是在產(chǎn)生對象時要求輸入兩個直角邊的長度。直角三角形在派生矩形類,矩形類的參數(shù)也由鍵盤輸入。設(shè)計這些類并測試他們的功能。#include #include using namespace std;class Line/線段基類protected:double sizeA;public :Line()cout輸入線段的長度:sizeA;Line(double a)sizeA = a;double getLength()return sizeA;class Triangle : public Line/三角形類protected:double sizeB, sizeC;public :Triangle()cout輸入線段長度:sizeB;sizeC = sqrt( sizeB * sizeB + sizeA * sizeA );void printSize()cout直角三角形,三條邊分別為:;coutA: sizeA , b: sizeB , C: sizeC endl;class Rectangle : public Triangle/矩形類protected:double sizeD;public :Rectangle()sizeC = sizeA;sizeD = sizeB;void printSize()cout矩形,四條邊分別為:;coutA: sizeA , b: sizeB , C: sizeC , D: sizeD endl;void main()/*Line *l = new Line();cout線段長度為:getLength() printSize();*/Rectangle *r = new Rectangle();r-printSize();第七章1.使用類模板演示復(fù)制兼容性規(guī)則。#include using namespace std;template class Pointprotected:T x, y;public :Point(T a, T b)x = a;y = b;void show()coutx = x , y = y endl;template class Rectangle : public Pointprivate:T h, w;public :Rectangle(T a, T b, T c, T d) : Point(a, b)h = c;w = d;void show()coutx = x , y = y ; h = h , w = w show();Rectangle * pb = &b;/子類指針pbpb-show();a = b;/派生類對象的屬性值,更新父類對象的屬性值a.show();2.設(shè)計一個點的類模板,分別使用繼承、包含的方法設(shè)計線段類模板,要求演示構(gòu)造函數(shù)和復(fù)制構(gòu)造函數(shù)的設(shè)計方法,并用主程序驗證之。#include using namespace std;template class Pointpublic :T x, y;Point(T a=0, T b=0)x = a;y = b;void show()coutx = x , y = y endl;template class Line_1 : public Point / 繼承Point類模板, 的線段類模板protected:T x1, y1;public :Line_1(T a, T b, T c, T d) : Point(a, b)x1 = c;y1 = d;Line_1(const Line_1 & );/復(fù)制構(gòu)造函數(shù)void show()cout( x , y ); ( x1 , y1 ) endl;template Line_1 : Line_1(const Line_1 & t) : Point(t.x, t.y) x1 = t.x1; y1 = t.y1;template class Line_2 /包含point類模板,的線段類protected:Point p1, p2;public :Line_2(T a, T b, T c, T d)p1.x = a;p1.y = b;p2.x = c;p2.y = d;Line_2(const Line_2 &);/復(fù)制構(gòu)造函數(shù)void show()cout( p1.x , p1.y ); ( p2.x , p2.y ) endl;template Line_2 : Line_2(const Line_2 & t)p1 = t.p1;p2 = t.p2;void main()Line_1 L1(1,2,3,4);coutL1 : ;L1.show();Line_1 L2(L1); /用現(xiàn)有的對象,初始化新對象coutL2 : ;L2.show();Line_2 J1(5,6,7,8);coutJ1 : ;J1.show();Line_2 J2(J1);coutJ2 : ;J2.show();3.已知有一個整型數(shù)組a,其內(nèi)容為1 3 5 7 9 2 4 6 8 10.先對數(shù)組進行升序排列,再使用它產(chǎn)生向量b,然后再在向量的尾部追加11,并按降序排列輸出向量的內(nèi)容和capacity()的內(nèi)容。 #include #include #include using namespace std;void main()int a = 1,3,5,7,9,2,4,6,8,10;sort(a, a+10);/先對數(shù)組進行升序排序copy(a, a+10, ostream_iterator(cout, );cout endl;vector pa(a, a+10); /再聲明向量pa.push_back(11);/向量尾部追加11reverse_copy(pa.begin(), pa.end(), ostream_iterator(cout, );/按降序輸出向量的內(nèi)容coutncapacity : pa.capacity() endl;/輸出capacity()的內(nèi)容第九章1.利用流格式控制,進行成績和名字的輸出,要求名字左對齊,分?jǐn)?shù)右對齊。 #include #include using namespace std;class Studentprivate :string name;float score;public :Student()Student(string n, float s)name = n;score = s;string getName()return name;float getScore()return score;void main()Student s1(liming, 98);Student s2(sdfh, 90);Student s3(vn.fy, 80);Student s4(cnbtrt, 70);Student s5(ryuety, 48);cout.width(15);cout left 姓名 right 分?jǐn)?shù) endl;cout.width(15);cout left s1.getName() right s1.getScore() endl;cout.width(15);cout left s2.getName() right s2.getScore() endl;cout.width(15);cout left s3.getName() right s3.getScore() endl;cout.width(15);cout left s4.getName() right s4.getScore() endl;cout.width(15);cout left s5.getName() right s5.getScore() endl;2.編寫一個產(chǎn)生文本文件的程序。 #include #include using namespace std;void main()char *p = C+程序設(shè)計;ofstream myFile(Worl9_5_2.txt);myFile p;3.編寫一個程序,要求輸入三角形的3條邊,然后判斷是否合理,如果不合理,給出信息并要求重新輸入;如果合理,計算其面積并將結(jié)果存入文件中。 #include #include #include #include #include #include using namespace std;class Triangledouble sizeA, sizeB, sizeC, area;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論