




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
...wd......wd......wd...2-45編寫一個程序,輸入三角形的三條邊的邊長,求三角形的面積。2-45#include<iostream.h>#include<math.h>intmain(){ inta,b,c; ints,S; cin>>a>>b>>c; s=(a+b+c)/2; S=powl(s*(s-a)*(s-b)*(s-c),0.5); if(S==0) cout<<"此三邊不能構成三角形!"<<endl; else { cout<<"面積:"<<endl; cout<<S<<endl; } return0;}2-46從鍵盤輸入一個大寫字母,然后改用小寫字母在屏幕輸出。2-46#include<iostream.h>intmain(){ charch; inta; cout<<"輸入一個大寫字母:"<<endl; cin>>ch; if(ch>=65&&ch<=91) { ch=ch+32;// a=(int)ch; cout<<ch<<endl; } elsecout<<"該字母不是字母或不是字母大寫!"<<endl;return0;}2-47用戶輸入兩個整數,編程輸出稍大于第一個整數而又是第2個整數的倍數的數。計算公式是:valuel+value2-value1%value2.2-47#include<iostream.h>intmain(){ intvalue1,value2; intresult; cin>>value1>>value2; result=value1+value2-value1%value2; cout<<"該整數為:"<<result<<endl; return0;}2-48華氏溫度轉換為攝氏溫度的公式是:C=〔F-32〕*5/9.。編寫一個程序,輸入一個華氏溫度,程序輸出相應的攝氏溫度。請將32和5/9用const型變量表示。2-48#include<iostream.h>intmain(){ constfloati=32.0; constfloatj=5.0/9.0; floatdegFahr; floatdegCel; cin>>degFahr;// for(degFahr=0;degFahr<=300;degFahr+=10)// { degCel=j*(degFahr-i); cout<<"華氏溫度:"<<degFahr<<endl; cout<<"攝氏溫度:"<<degCel<<endl;// } return0;}2-49從鍵盤輸入20個整數,檢查100是否存在于這些整數中,假設是的話,求出他是第幾個被輸入的。2-49#include<iostream.h>intmain(){ intArray[20]; inti,count=0,c=0; for(i=0;i<20;i++) cin>>Array[i]; for(i=0;i<20;i++) { if(Array[i]==100&&count==0) { cout<<"100存在該數組中!"<<endl; count++; } if(Array[i]!=100) { c++; if(c==19) cout<<"100不存在該數組中!"<<endl; } } for(i=0;i<20;i++) { if(Array[i]==100) cout<<"它是第"<<i+1<<"個被輸入的!"<<endl; } return0;}2-50從鍵盤輸入一個NXN的整型數組,并將每一行的最大值顯示輸出。2-50#include<iostream.h>intmain(){ intArray[100][100]={0}; intn,temp; cin>>n; for(inti=0;i<n;i++) { for(intj=0;j<n;j++) cin>>Array[i][j]; for(intx=0;x<n;x++) for(inty=x+1;y<n;y++) if(Array[i][x]<Array[i][y]) { temp=Array[i][x]; Array[i][x]=Array[i][y]; Array[i][y]=temp; } } cout<<"每一行的最大值:"<<endl; for(i=0;i<n;i++) cout<<Array[i][0]<<""; cout<<endl;}2-51輸入三個整數,采用指針方法將三個數按從大到小的順序輸出。2-51#include<iostream.h>#include<math.h>intmain(){ intArray[3]; int*pA=Array; int*a=Array; inti; inttemp; for(i=0;i<3;i++) cin>>Array[i]; for(i=0;i<3;i++) { a++; if(*pA<*a) { temp=*pA; *pA=*a; *a=temp; a++;pA++; } a=&Array[0]; } for(i=0;i<3;i++) cout<<Array[i]<<""; return0;}2-52采用指針方法將一個數組中的所有元素顛倒順序,結果仍然存放在原來的數組中,要求使用最少的輔助存儲單元2-52#include<iostream.h>intmain(){ intn; cout<<"輸入數組的長度(小于100):"<<endl; cin>>n; cout<<"輸入"<<n<<"個數:"<<endl; intArray[100]; int*pA=Array; inti; for(i=0;i<n;i++) { cin>>Array[i];pA++; } for(i=0;i<n;i++) {pA--; cout<<*pA<<""; }return0;}2-53輸入兩個字符串,如果兩個字符串的字符和長度都一樣〔認為它們相等〕,在屏幕上輸出“Equal〞,否則在屏幕上輸出“Unequal〞。要求使用字符指針。2-53#include<iostream.h>intmain(){ char*pstr="Enterastring:"; charstr1[100],str2[100]; cout<<pstr<<endl; cin.get(str1,100); cin.get(); cin.get(str2,100); intlen1=0,len2=0; while(str1[len1]!='\0') len1++; while(str2[len2]!='\0') len2++; if(len1!=len2) cout<<"Unequal"<<endl; elseif(len1==len2) { inti=0; for(i=0;i<len1;i++) if((str1[i]==str2[i])&&i<len1) continue; if(i=len1-1) cout<<"Equal"<<endl; elsecout<<"Unequal"<<endl; } return0;}2-54編程將一個整數轉換成對應的數字串,例如將值1234轉換為數字串“1234〞。2-54#include<iostream.h>intmain(){ intnumber; intArray[100]; inti,j; cin>>number; for(i=0;number!=0;i++) { Array[i]=number%10; number=number/10; } j=i; for(i=j;i>=0;i--) cout<<Array[i-1]<<""; cout<<endl; return0;}2-55編程求兩個復數的和。2-55#include<iostream.h>intmain(){ floata[2],b[2],c[2]; inti; for(i=0;i<2;i++) cin>>a[i]; for(i=0;i<2;i++) cin>>b[i]; for(i=0;i<2;i++) c[i]=a[i]+b[i]; cout<<c[0]<<""<<c[1]<<endl; return0;}2-56使用構造變量表示每個學生的信息:姓名、學號和三門課的成績。從鍵盤輸入10個學生的數據,然后輸出每個學生的姓名和三門課的平均成績。2-56#include<stdio.h>structstudent { charnum[10]; charname[20]; floatgrade[3]; floataverage; };intmain(){ studentstu[10]; inti,j; floatsum=0; for(i=0;i<=9;i++) { printf("Enternum:\n"); scanf("%s",&stu[i].num); printf("Entername:\n"); scanf("%s",&stu[i].name); printf("Enterthreegrades:\n"); for(j=0;j<3;j++) { scanf("%f",&stu[i].grade[j]); sum+=stu[i].grade[j]; } stu[i].average=sum/3; printf("%f%s\n",stu[i].average,stu[i].name); } return0;}2-57用構造數組建設并初始化一個工資表,然后輸入一個人的姓名,查詢其工資情況,并在屏幕上輸出。2-572-58用枚舉值MON、TUE、WED、THU、FRI、SAT和SUN表示一個星期中的7天。從鍵盤輸入一個0~6之間的整數,根據輸入的整數輸出對應的英文縮寫。2-582-59編寫一個求解一元二次方程的根的程序,方程的系數由用戶輸入。2-59#include<iostream.h>#include<math.h>intmain(){ inta,b,c; intR; floatp; floatx1,x2; cin>>a>>b>>c; R=b*b-4*a*c; if(R>=0) { p=sqrt(R); x1=(-b+p)/(2*a); x2=(-b-p)/(2*a); if(x1==x2) cout<<"僅且只有一個根"<<x1<<endl; else cout<<"有兩個不同的根"<<x1<<x2<<endl; } elseif(R<0)cout<<"無根"<<endl;return0;}2-60從鍵盤輸入一個字符,判斷輸入的字符是m、a、n或其他字符。如果是m則輸出“Goodmorning!〞;如果是a則輸出“Goodafternoon!〞;如果是n則輸出“Goodnight!〞;如果是其他字符則輸出“Ican‘tundersrand!〞。2-602-61編程實現兩個整數的加、減、乘、除四則運算,運算式形如“32+120〞。2-612-62編寫一個程序,利用swith語句將百分制的學生成績轉換為優、良、中、及格和不及格5分制成績。2-622-63從鍵盤輸入一個字符,判斷輸入的字符是數字、空格還是其他字符,并給出相應的提示信息。2-63#include<iostream.h>intmain(){ charch; cout<<"請輸入一個字符:"; cin.get(ch); if(ch=='') cout<<"這是一個空格!"<<endl; elseif((ch>='0')&&(ch<='9')) cout<<"這是一個數字!"<<endl; else cout<<"這是一個其他字符!"<<endl; return0;}2-64從鍵盤輸入一個字符序列,編程統計其中的數字個數和英文字母個數。輸入的字符序列以“#〞作為完畢符。2-64#include<iostream.h>intmain(){ charsymbol[100]; inti,end,j=0,k=0; cin>>symbol; for(i=0;i<100;i++) if(symbol[i]=='#') { end=i; break; } for(i=0;i<end;i++) { if((symbol[i]>='0')&&(symbol[i]<='9')) j++; elsek++; } cout<<"digit:"<<j<<endl; cout<<"letter:"<<k<<endl; return0;}2-65輸入一個由假設干單詞組成的文本串,每個單詞之間用一些空格分隔,統計此文本串單詞的個數。2-65#include<iostream.h>intmain(){ charstr[1000]; intcount=1; cin.get(str,1000);// while(!str[0])// { for(inti=0;str[i]!=0;i++) if(str[i]==''&&str[i+1]!='') count++;// } cout<<"thecountis:"<<count<<endl; return0;}2-67編程求π值,使用如下公式:π/4=1-1/3+1/5-1/7+…,直到最后一項的絕對值小于10-6為止。2-67〔1〕#include<iostream.h>#include<math.h>intmain(){ inti,n=1,j=1; doubles=0; for(i=0;(2*i+1)<pow(10,6);i++) { /*cout<<i<<endl; cout<<"******************"; cout<<2*i+1<<endl; cout<<"********************************"; cout<<pow((2*i+1),-1)<<endl; cout<<"******************";*/ s+=pow((2*i+1),-1)*n; //j=2*i+1; n=n*(-1); } cout<<i<<endl; cout<<4*s<<endl; return0;}2-67(2)#include<iostream.h>#include<math.h>intmain(){floati=0;doubles=0;for(i=0;2*i+1<pow(10,6);i++){s+=pow(-1,i)*(1/(2*i+1));}cout<<s*4<<endl;return0;}2-68把100~150之間不能被3整除的數輸出,要求一行輸出10個數。2-682-69編程輸出一個九九乘法表。2-69#include<iostream.h>intmain(){ chartable[9][9]; inti,j,X,Y; for(i=0;i<9;i++) { for(j=0;j<9;j++) { X=i+1; Y=j+1; if(X>=Y) cout<<X*Y<<''; } cout<<endl; } return0;}2-70編程計算整型數各位數字之和,例如數2007各位數字之和為2+0+0+7=9.2-70#include<iostream.h>intmain(){ intnum; intsum=0,i; cin>>num; for(i=0;num!=0;i++) { sum+=num%10; num=num/10; } cout<<"sum="<<sum<<endl; return0;}2-71輸入n個整數,利用冒泡排序法將它們從小到大排列,并在屏幕上輸出。2-71#include<iostream.h>intmain(){ intn; inti,j,temp; intnum[100]; cin>>n; for(i=0;i<n;i++) cin>>num[i]; for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(num[i]<num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } for(i=0;i<n;i++) cout<<num[i]<<''; cout<<endl; return0;}2-72編程求出從鍵盤輸入的10個數之和,遇到負數時終止輸入求和。2-72#include<iostream.h>intmain(){ intArray[10]; intsum=0; for(inti=0;i<10;i++) { cin>>Array[i]; if(Array[i]>0) sum+=Array[i]; else break; } cout<<sum<<endl; return0;}2-73編程求出從鍵盤輸入的10個數中所有正數之和,負數不參加求和。2-73#include<iostream.h>intmain(){ intnum[10]; inti,sum=0; for(i=0;i<=9;i++) { cin>>num[i]; if(num[i]>0) sum+=num[i]; } cout<<"sumis"<<sum<<endl;return0;}2-74設計函數prime(),它只帶一個整型參數,當這個參數的值是素數時,該函數返回非0,否則返回0.利用這個函數編寫一個程序來驗證哥德巴赫猜想:任何一個大于2的偶數都可以表示成兩個素數之和。2-74#include<iostream.h>intprime(intx){ inti; for(i=2;i<x/2;i++) if(x%i==0) return0; returnx;} intmain(){ inta,b,c,d; intj=2,k; cin>>a; if(a>2&&a%2==0) { for(j=2;j<a;j++) { b=prime(j); if(b==j) c=a-b; d=prime(c); if(d==c) cout<<"該定理成立!"<<endl; } } elsecout<<"此數不是大于2的偶數!"<<endl;return0;}2-75編制如下函數原型的函數:intindex(constchar*str,charc),這個函數返回字符串str中第一次出現字符c的位置。2-75#include<iostream.h>intindex(constchar*str,charc){ intcount=1; intCcount=0; for(inti=0;str[i]!='\0';i++) { if(str[i]==c&&Ccount==0) { Ccount++; break; } count++; } returncount;} intmain(){ charstring[100]; charC; intV=0; intresult; cin>>string; cin>>C; while(string[V]!='\0') V++; result=index(string,C); if((V+1)==result) cout<<"此字符不在字符串中!"<<endl; else cout<<result<<endl; return0;}2-76首先編寫以下函數聲明的函數:voidswap(float*px,float*py),該函數用于交換兩個實型變量的值,然后編寫一個主函數驗證函數swap()的功能。2-76#include<iostream.h>voidswap(float*px,float*py){ floattemp; temp=*px; *px=*py; *py=temp;}intmain(){ floatX,Y; cin>>X>>Y; swap(&X,&Y); cout<<"使用swap函數的值"; cout<<X<<''<<Y<<endl;/* floattemp; { temp=X; X=Y; Y=temp; }*/ cout<<"驗證值:"<<X<<''<<Y<<endl;return0;}2-77定義將一個字符串反轉的函數,例如將字符串“abcd〞反轉為“dcba〞。2-77#include<iostream.h>#include<string.h>intArray(char*s){ intj,n; charstring[100]; strcpy(string,s); for(j=0;j<100;j++) if(string[j]=='\0') n=j; returnn;}intmain(){ charstr[100]; inti,z; cin>>str; z=Array(str);// cout<<z; for(i=z;i>=0;i--) cout<<str[i]; cout<<endl;return0;}2-78首先編寫一個冒泡排序函數,然后在主函數中調用排序函數對10個整數從小到達進展排序。提示:采用數組名作為函數參數。2-782-79將習題2-76中的swap()函數改為內聯函數,并實現一樣的程序功能。2-79#include<iostream.h>inlinevoidswap(float*px,float*py){ floattemp; temp=*px; *px=*py; *py=temp;}intmain(){ floatX,Y; cin>>X>>Y; swap(&X,&Y); cout<<"使用swap函數的值"; cout<<X<<''<<Y<<endl;/* floattemp; { temp=X; X=Y; Y=temp; }*/ cout<<"驗證值:"<<X<<''<<Y<<endl;return0;}2-80編寫一個函數maxmin(),該函數有兩個實型參數,執行函數后,第一個參數為兩個參數中值較大者,第二個參數為較小者。要求使用引用作為函數參數,并編寫主函數驗證函數功能。2-80#include<iostream.h>voidmaxmin(int&A,int&B){ inttemp; if(A<B) { temp=A; A=B; B=temp; }}intmain(){ intX,Y; cin>>X>>Y; maxmin(X,Y); cout<<X<<''<<Y<<endl;return0;}2-81編寫一個函數swapstruct(),實現交換兩個構造變量的功能。編寫主函數驗證函數swapstruct()的功能,要求使用引用傳遞參數。2-81#include<iostream.h>#include<string.h>structstudent{ charname[20]; intscore;};voidswapstruct(student&s1,student&s2){ inttemp; chars[20]; strcpy(s,); strcpy(,); strcpy(,s); temp=s1.score; s1.score=s2.score; s2.score=temp;}intmain(){ studentstu1={"zhangsan",90}; studentstu2={"lisi",85}; cout<<"stu1:"<<<<''<<stu1.score<<endl; cout<<"stu2:"<<<<''<<stu2.score<<endl; swapstruct(stu1,stu2); cout<<"stu1:"<<<<''<<stu1.score<<endl; cout<<"stu2:"<<<<''<<stu2.score<<endl;return0;}2-82編寫一個程序,在主函數main()的外部和內局部別聲明兩個同名的整型變量并賦值,然后在主函數main()中分別訪問兩個變量。2-82#include<iostream.h>inti=3;intmain(){ cout<<i<<endl; inti=5; cout<<i<<endl; return0;}2-83一個程序由兩個C++源文件組成,在一個源文件中定義主函數main()并聲明一個外部整型變量n,在另一個源文件中定義一個不帶參數的函數factorial(void),該函數用于計算變量n的階乘。編程在主函數main()中輸入一個整數并求它的階乘。2-83#include<iostream.h>intfactorial(intx){ inty,pro=1; for(y=x;y>0;y--) pro*=y; return(pro);}voidmain(){ intn,R; cout<<"Thenumberis:"; cin>>n; R=factorial(n); cout<<"Theproductis:"<<R<<endl;}2-84采用外部函數的方式使用習題2-75中的函數index(),即在一個源文件中定義該函數,然后在另一個源文件中調用該函數。2-842-85編寫一段程序,利用new運算動態分配float型、long型和char型三個內存單元,將它們的首地址分別賦給指針pf、pl和pc。給這些存儲但愿賦值,并在屏幕上顯示它們的值。最后利用delete運算釋放所有動態分配的內存單元。2-85#include<iostream.h>intmain(){ float*pf=newfloat; *pf=3.14; long*pl=newlong; *pl=2135567889; char*pc=newchar; *pc='A'; cout<<*pf<<endl; cout<<*pl<<endl; cout<<*pc<<endl; deletepf; deletepl; deletepc; return0;} 2-86編寫一個程序,用new運算為一個整型數組動態分配內存空間,對其進展賦值,并在屏幕上輸出。2-86#include<iostream.h>intmain(){ intsize; intnum; cin>>size; int*f=newint[size]; for(inti=0;i<size;i++) { cin>>num; f[i]=num; } for(i=0;i<size;i++) cout<<f[i]<<''; delete[]f;return0;}2-87采用動態內存分配方法設計一個學生成績處理程序,要求輸入任何數量學生的學號、姓名和四門課的成績,并按平均成績的上下輸出每個學生的上下輸出每個學生的姓名和成績。2-87#include<iostream.h>#include<string.h>structstudent{ intID; charname[20]; intgrade[4]; floataverage;};intmain(){ intnum; inti,j,k,temp; floatsum=0; chars[20]; cin>>num; student*stu=newstudent[num]; for(i=0;i<num;i++) { cout<<"輸入第"<<i+1<<"人的信息"<<endl; cout<<"ID:"; cin>>stu[i].ID; cout<<"name:"; cin>>stu[i].name; cout<<"grade:"; for(j=0;j<4;j++) { cin>>stu[i].grade[j]; sum+=stu[i].grade[j]; } stu[i].average=sum/4; sum=0; cout<<"average:"<<stu[i].average<<endl; } for(i=0;i<num;i++)//排序 for(j=i+1;j<num;j++) if(stu[i].average<stu[j].average) { { strcpy(s,stu[i].name); strcpy(stu[i].name,stu[j].name); strcpy(stu[j].name,s); } for(k=0;k<4;k++) { temp=stu[i].grade[k]; stu[i].grade[k]=stu[j].grade[k]; stu[j].grade[k]=temp; } } cout<<"成績排名:"<<endl; cout<<"name"<<""<<"grade"<<endl; for(i=0;i<num;i++) { cout<<stu[i].name<<""; for(j=0;j<4;j++) cout<<stu[i].grade[j]<<''; cout<<endl; } return0;}2-88輸入一行字符,建設一個鏈表,鏈表的每一個結點含有一個輸入的字符,通過訪問鏈表中的每個結點計算鏈表中結點的總個數。2-882-89使用帶參數的宏定義計算兩個實數之和,并編寫主函數驗證宏的功能。2-89#include<iostream.h>#definesum(a,b)(a+b)intmain(){ intx,y; cin>>x>>y; cout<<"主函數計算結果:"<<x+y<<endl; cout<<"宏定義函數結果:"<<sum(x,y)<<endl; return0;}2-90定義一個帶參數的宏,求出三個數中最大的一個數,并進展驗證。2-90#include<iostream.h>#defineMAX(a,b)((a)>(b)?(a):(b))voidmain(){ intx,y,z; cin>>x>>y>>z; cout<<"宏定義參數結果"; cout<<MAX(MAX(x,y),z)<<endl; if(x>y) { if(x>z) cout<<"驗證結果為"<<x<<endl; elsecout<<"驗證結果為"<<z<<endl; } else { if(y>z) cout<<"驗證結果為"<<y<<endl; elsecout<<"驗證結果為"<<z<<endl; }}2-91輸入一個字符串,根據需要設置條件編譯,使之能將輸入的字符串以大寫字母的形式或小寫字母的形式輸出。2-91#include<iostream.h>intmain(){ charstr[100]; charX; inti; cin>>str; cout<<"是否輸出大寫形式(YorN)"<<endl; cin>>X; if(X=='Y') for(i=0;str[i]!='\0';i++) { str[i]=str[i]-32; cout<<str[i]; } elseif(X=='N') for(i=0;str[i]!='\0';i++) cout<<str[i]; cout<<endl; return0;}2-92修改習題2-61中的求和程序,在程序中定義一個調試宏,利用條件編譯指令編譯不同的代碼段,使得在調試程序時能夠輸出一些調試信息。2-922-93假設有三個文件:test13.h、test2.h和test.cpp,在test1.h中定義了一個宏PI,test2.h文件包含了test1.h文件,而test.cpp文件又包含了test1.h文件和test2.h文件。請問編譯時會出現什么錯誤假設何解決提示:宏的重復定義。2-933-44一個名為CPerson的類有如下屬性:姓名、身份證號、性別和年齡,請用C++語言定義這個類,并為上述屬性定義相應的方法。#include<iostream.h>#include<string.h>classCPerson{private:charName[10];charID[20];charSex[4];intAge;public:CPerson(char*na,char*id,char*se,intag);voidShow();};CPerson::CPerson(char*na,char*id,char*se,intag){strcpy(Name,na);strcpy(ID,id);strcpy(Sex,se);Age=ag;}voidCPerson::Show(){cout<<"姓名:"<<Name<<endl;cout<<"身份證:"<<ID<<endl;cout<<"性別:"<<Sex<<endl;cout<<"年齡:"<<Age<<endl;}voidmain(){CPersonperson("王三",,"男",21);person.Show();}3-45設計一個日期類Date,該類用于表示日期值〔年、月、日〕。要求除了能夠通過相應得成員函數設置和獲取日期外,還能夠實現將日期加一天的操作。#include<iostream.h>classdate{intyear;intmonth;intday;boolflag;public:date(){year=0;month=0;day=0;}date(intyr,intmo,intda);voidsetdate();intgetyear();intgetmonth();intgetday();voidaddday();voidshow();};date::date(intyr,intmo,intda){flag=false;if(mo>=1&&mo<=12&&da>=1&&da<=31){year=yr;month=mo;day=da;}else{flag=true;}}voiddate::setdate(){cout<<"請輸入年分"<<endl;cin>>year;cout<<"請輸入月份(1-12)"<<endl;cin>>month;while(month<1||month>12){cout<<"輸入有誤,請重新輸入月份(1-12)"<<endl;cin>>month;}cout<<"請輸入日期(1-31)"<<endl;cin>>day;while(day<1||day>31){cout<<"輸入有誤,請重新輸入日期(1-31)"<<endl;cin>>day;}flag=false;}voiddate::show(){if(!flag)cout<<year<<'/'<<month<<'/'<<day<<endl;elsecout<<"日期設置有誤,不能輸出"<<endl;}intdate::getyear(){returnyear;}intdate::getmonth(){returnmonth;}intdate::getday(){returnday;}voiddate::addday(){day++;if(month==2)//判斷是否是二月{boolleapyear;leapyear=((year%4==0&&year%100!=0)||(year%400==0));//定義閏年if(leapyear){if(day>29)//假設是閏年的二月當Day大于29時,Day=1,Mon增加一個月{day=1;month++;}}else{if(day>28)//假設不是閏年的二月當Day大于28時,Day=1,Mon增加一個月{day=1;month++;}}}elseif(month==1||month==3||month==5||month==7||month==8||month==10||month==12){if(day>31)//假設不是二月月大時,Day=1,Mon增加一個月{day=1;month++;}}else{if(day>30)//假設不是二月月小時,Day=1,Mon增加一個月{day=1;month++;}}if(month>12)//假設月大于12則Mon=1,Year增加一年{month=1;year++;}}voidmain(){dated1(1999,5,30);d1.show();d1.setdate();d1.show();cout<<"日期增加一天后為:";d1.addday();d1.show();}3-46#include<iostream>usingnamespacestd;classCRectangle{private:doubleX;doubleY; doublelength; doublewidth;public:CRectangle(doubles,doublee,doublel,doublew) { X=s; Y=e; length=l; width=w; } ~CRectangle(){} voidMove(double,double); voidSize(double,double); voidWhere(); voidArea();};voidCRectangle::Move(doublex,doubley){ cout<<"矩形按向量("<<x<<","<<y<<")移動"<<endl; cout<<"現在矩形左上角所在的位置:"<<endl; cout<<"("<<X+x<<","<<Y+y<<")"<<endl;}voidCRectangle::Size(doublecl,doublecw){ cout<<"要更改的長和寬:"<<cl<<cw; length=cl; width=cw; cout<<length*width<<endl;}voidCRectangle::Where(){ cout<<"現在矩形左上角所在的位置:"<<endl; cout<<"("<<X<<","<<Y<<")"<<endl;}voidCRectangle::Area(){doublearea;area=length*width;cout<<area;}intmain(){ CRectanglecr(2,3,5,4); cr.Where(); cr.Area(); cr.Move(1,2); cr.Size(2,3); return0;}3-47#include<iostream>usingnamespacestd;classCRectangle{private:doubleX;doubleY; doublelength; doublewidth;public:CRectangle(doubles,doublee,doublel,doublew) { X=s; Y=e; length=l; width=w; } ~CRectangle(){} voidMove(double,double); voidSize(double,double); voidWhere(); voidArea();};voidCRectangle::Move(doublex,doubley){ cout<<"矩形按向量("<<x<<","<<y<<")移動"<<endl; cout<<"現在矩形左上角所在的位置:"<<endl; cout<<"("<<X+x<<","<<Y+y<<")"<<endl;}voidCRectangle::Size(doublecl,doublecw){ cout<<"要更改的長和寬:"<<cl<<cw; length=cl; width=cw; cout<<length*width<<endl;}voidCRectangle::Where(){ cout<<"現在矩形左上角所在的位置:"<<endl; cout<<"("<<X<<","<<Y<<")"<<endl;}voidCRectangle::Area(){doublearea;area=length*width;cout<<area;}intmain(){ CRectanglecr(2,3,5,4); cr.Where(); cr.Area(); cr.Move(1,2); cr.Size(2,3); return0;}3-48#include<iostream.h>#include<string.h>classBank{private: charnumber[20]; intmoney;public: voidsetnumber(char*num,intmon) {strcpy(number,num); money=mon; } voidchanger(intmon) { money+=mon; } voidshow() { cout<<"當前的總人民幣數為:"<<money<<endl; }};voidmain(){ charnum[20]; intmoney; intaddmoney; cout<<"輸入用戶名和存入的人民幣:"<<endl;cin>>num>>money; Bankbank; bank.setnumber(num,money); bank.show(); cout<<"輸入轉賬的人民幣:"<<endl; cin>>addmoney;bank.changer(addmoney); bank.show();}3-49#include<iostream>usingnamespacestd;classProduct{private: charname[20]; doubleprice; intm_count;public: Product(){} ~Product(){} voidsetproduct(); voidsellproduct(); voidshow(); };voidProduct::setproduct(){ intn; cout<<"輸入生產產品數量:"<<endl; cin>>n; m_count=n;}voidProduct::sellproduct(){ intn; cout<<"輸入銷售產品數量:"<<endl; cin>>n; m_count-=n;}voidProduct::show(){ cout<<"輸出剩余產品數量:"<<endl; cout<<m_count<<endl;}intmain(){ Productp; p.setproduct(); p.sellproduct(); p.show(); return0;}3-50建設一個名為Student的類,該類有以下幾個私有成員變量:學生姓名、學號、性別和年齡。還有以下兩個成員函數:一個用于初始化學生姓名、學號、性別和年齡的構造函數,一個用于輸出學生信息的函數。編寫一個主函數,聲明一個學生對象,然后調用成員函數在屏幕上輸出學生信息。#include<iostream.h>#include<string.h>classStudent{public:Student(char*name,char*num,char*sex,intage);~Student();voidshow();private:char*Name;char*Num;char*Sex;intAge;};Student::Student(char*name,char*num,char*sex,intage){Name=newchar[strlen(name)+1];//注意字符串的賦值strcpy(Name,name);Num=newchar[strlen(num)+1];strcpy(Num,num);Sex=newchar[strlen(sex)+1];strcpy(Sex,sex);Age=age;}Student::~Student(){deleteName;deleteNum;deleteSex;}voidStudent::show(){cout<<"姓名:"<<Name<<endl;cout<<"學號:"<<Num<<endl;cout<<"性別:"<<Sex<<endl;cout<<"年齡:"<<Age<<endl;}voidmain(){Studentstudent("張三","0401011201","男",18);student.show();}3-51設計一個CPetrol類,包含以下幾個私有成員:90號93號98號汽油加油量和單價,當天的總收入。類還包含以下幾個成員函數:設置有關數據成員的構造函數,輸入加油量并計算總收入的成員函數。利用類編寫主函數:假設加油站某天909398號汽油單價分別為3.964.054.38計算并輸出加油站一天的收入。#include<iostream>usingnamespacestd;classCPetrol{public:CPetrol();voidsetamount();doubletotal;private:doubleam_90;doubleam_93;doubleam_98;doubleprice_90;doubleprice_93;doubleprice_98;};CPetrol::CPetrol(){price_90=3.96;price_93=4.05;price_98=4.38;}voidCPetrol::setamount(){cout<<"inputthreeamounts!"<<endl;cin>>am_90>>am_93>>am_98;total=am_90*price_90+am_93+price_93+am_98+price_98;}voidmain(){CPetrolc;c.setamount();cout<<"Thetotalis"<<c.total<<endl;}3-52修改習題3-50中的類Student,添加一個靜態成員變量,用于表示已創立對象的數量;添加兩個靜態成員函數,一個用于輸出已創立對象的數量,一個用于輸出一個學生的姓名和學號。#include<iostream.h>#include<string.h>classStudent{public:Student(char*name,char*num,char*sex,intage);~Student();staticvoidshow(Student&a);staticvoidshowstudentnum();private:char*Name;char*Num;char*Sex;intAge;staticintstudentnum;};intStudent::studentnum=0;Student::Student(char*name,char*num,char*sex,intage){studentnum++;Name=newchar[strlen(name)+1];strcpy(Name,name);Num=newchar[strlen(num)+1];strcpy(Num,num);Sex=newchar[strlen(sex)+1];strcpy(Sex,sex);Age=age;}Student::~Student(){deleteName;deleteSex;}voidStudent::showstudentnum(){cout<<"學生的數量是:"<<studentnum<<endl;}voidStudent::show(Student&a){cout<<"姓名:"<<a.Name<<endl;cout<<"學號:"<<a.Num<<endl;cout<<"性別:"<<a.Sex<<endl;cout<<"年齡:"<<a.Age<<endl;}voidmain(){Studentstudent1("張三","0401011201","男",18);Student::show(student1);//注意調用方式,靜態成員可以通過類名調用Student::showstudentnum();//注意調用方式Studentstudent2("李四","0401011202","男",18);Student::show(student2);Student::showstudentnum();}3-53編寫程序用靜態成員的方法實現對班費的管理,要求定義一個類Student,除了聲明一個存放班費的靜態成員,還要分別定義一個上交班費的成員函數Contribute〔〕,花費班費的成員函數Spend〔〕和顯示班費的靜態成員函數Display〔〕//student.cpp#include<iostream>usingnamespacestd;classStudent{private:staticdoublefee;//fee--班費,靜態成員數據public:Student(){}//默認構造函數,析構函數~Student(){}voidContribute(doublen);//n--上繳的班費數額voidSpend(doublen);//n--花費班費數量staticvoidDisplay();//靜態成員函數};doubleStudent::fee=0;//類聲明外面對靜態數據成員初始化//類方法voidStudent::Contribute(doublen){fee=fee+n;}voidStudent::Spend(doublen){if(fee<n)cout<<"班費不夠,請求失敗!\n";elsefee=fee-n;}voidStudent::Display(){cout<<"現有班費:"<<fee<<endl;}intmain(){Studentstu;stu.Display();stu.Contribute(103.4);//交人民幣stu.Display();stu.Spend(42.3);//花人民幣stu.Display();return0;}放在了一個文件里了,上面是類聲明,下面是測試小程序,運行過了,沒問題3-54定義一個類A,該類除了有兩個數據成員x,y外,還有一個對象備份函數copy。copy函數的功能說明如下:對于類A的對象a1和a2,函數調用a1.copy〔a2〕表示將對象a2賦值給對象a1.〔提示利用this指針防止一個對象對自己賦值〕#include<iostream>usingnamespacestd;classTest{private:char*a,*b;public:Test(){a=newchar[100];b=newchar[100];}~Test(){delete[]a;delete[]b;}Test?(Test&B){if(this==&B)return*this;intlen=strlen(B.a);delete[]a;a=newchar[len+1];strcpy(a,B.a);len=strlen(B.b);delete[]b;b=newchar[len+1];strcpy(b,B.b);return*this;}voidmytest(char*str1,char*str2){strcpy(a,str1);strcpy(b,str2);}voidmyprint(){cout<<a<<""<<b<<endl;}}A,B;intmain(){B.mytest("thisis","B");B.myprint();A.copy(B);A.myprint();return0;}3-55將習題3-45中類Date的“日期加一天〞成員函數改為友員函數。#include<iostream.h>classdate{intyear;intmonth;intday;boolflag;public:date(){year=0;month=0;day=0;}date(intyr,intmo,intda);voidsetdate();intgetyear();intgetmonth();intgetday();friendvoidaddday(date&d);voidshow();};date::date(intyr,intmo,intda){flag=false;if(mo>=1&&mo<=12&&da>=1&&da<=31){year=yr;month=mo;day=da;}else{flag=true;}}voiddate::setdate(){cout<<"請輸入年分"<<endl;cin>>year;cout<<"請輸入月份(1-12)"<<endl;cin>>month;while(month<1||month>12){cout<<"輸入有誤,請重新輸入月份(1-12)"<<endl;cin>>month;}cout<<"請輸入日期(1-31)"<<endl;cin>>day;while(day<1||day>31){cout<<"輸入有誤,請重新輸入日期(1-31)"<<endl;cin>>day;}flag=false;}voiddate::show(){if(!flag)cout<<year<<'/'<<month<<'/'<<day<<endl;elsecout<<"日期設置有誤,不能輸出"<<endl;}intdate::getyear(){returnyear;}intdate::getmonth(){returnmonth;}intdate::getday(){returnday;}voidaddday(date&d){d.day++;if(d.month==2)//判斷是否是二月{boolleapyear;leapyear=((d.year%4==0&&d.year%100!=0)||(d.year%400==0));//定義閏年if(leapyear){if(d.day>29)//假設是閏年的二月當Day大于29時,Day=1,Mon增加一個月{d.day=1;d.month++;}}else{if(d.day>28)//假設不是閏年的二月當Day大于28時,Day=1,Mon增加一個月{d.day=1;d.month++;}}}elseif(d.month==1||d.month==3||d.month==5||d.month==7||d.month==8||d.month==10||d.month==12){if(d.day>31)//假設不是二月月大時,Day=1,Mon增加一個月{d.day=1;d.month++;}}else{if(d.day>30)//假設不是二月月小時,Day=1,Mon增加一個月{d.day=1;d.month++;}}if(d.month>12)//假設月大于12則Mon=1,Year增加一年{d.month=1;d.year++;}}voidmain(){dated1(1999,5,30);d1.show();d1.setdate();d1.show();cout<<"日期增加一天后為:";addday(d1);d1.show();}3-56將習題3-50中類Student的學生信息輸出函數改為友員函數。#include<iostream.h>#include<string.h>classStudent{public:Student(char*name,char*num,char*sex,intage);~Student();friendvoidshow(Student&);private:char*Name;char*Num;char*Sex;intAge;};Student::Student(char*name,char*num,char*sex,intage){Name=newchar[strlen(name)+1];strcpy(Name,name);Num=newchar[strlen(num)+1];strcpy(Num,num);Sex=newchar[strlen(sex)+1];strcpy(Sex,sex);Age=age;//注意字符串的賦值}Student::~Student(){deleteName;deleteSex;}voidshow(Student&stu){cout<<"姓名:"<<stu.Name<<endl;cout<<"學號:"<<stu.Num<<endl;cout<<"性別:"<<stu.Sex<<endl;cout<<"年齡:"<<stu.Age<<endl;}voidmain(){Studentstudent("張三","0401011201","男",18);show(student);}3-57設計一個直線類Line〔設直線方程為ax+bx+c=0〕,其中,包含三個數據成員a、b、c,一個顯示數據成員的disp成員函數和一個求兩直線交點的友元函數setpoint,并在main()中給出對象或數據進展測試。請填空完成程序并上機運行驗證。#include<iostream.h>classLine{friendvoidsetpoint(Line&A,Line&B);private: doublea; doubleb; doublec;public: Line(doublea1,doubleb1,doublec1) { a=a1; b=b1; c=c1; } ~Line(){};};voidsetpoint(Line&A,Line&B){ doublex; if(A.a/B.a!=A.b/B.b) { x=-100; while(1) { if(((-A.c-A.a*x)/A.b-(-B.c-B.a*x)/B.b)<
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 瓣膜置換的圍術期護理
- 人教A版 (2019)選擇性必修 第一冊3.2 雙曲線獲獎教案
- 2024中鋁共享服務(天津)有限公司校園招聘1人筆試參考題庫附帶答案詳解
- 鳳仙花的一生(教學設計)-2024-2025學年科學三年級下冊人教鄂教版
- 人教部編版一年級下冊20 咕咚第2課時教學設計
- 人教版(2024)八年級上冊(2024)第4節 眼睛和眼鏡教案
- 2024中建一局二級公司總工程師公開競聘1人筆試參考題庫附帶答案詳解
- 釘釘使用詳盡培訓
- 2024中國郵政福建建省分公司校園招聘預筆試參考題庫附帶答案詳解
- 人美版三年級下冊第3課 豎彎鉤教案及反思
- 中國高職院校畢業生薪酬報告(2024年度)
- 2025-2030中國團餐行業市場發展現狀分析及發展前景與投資機會研究報告
- 山東省濟南西城實驗中學2024-2025學年高一下學期4月月考地理試題(原卷版+解析版)
- IT系統架構規劃與設計手冊
- 口腔門診6S管理
- 沉浸式體驗活動設計合同
- 檔案檔案管理基礎知識試題及答案
- 2025-2030中國金紅石發展現狀及未來趨勢研究報告
- 2025-2030中國慢性腰痛治療行業市場現狀供需分析及投資評估規劃分析研究報告
- 演出經紀人與文化經濟試題
- pcb抄板合同范例
評論
0/150
提交評論