程序設計基礎實驗報告_第1頁
程序設計基礎實驗報告_第2頁
程序設計基礎實驗報告_第3頁
程序設計基礎實驗報告_第4頁
程序設計基礎實驗報告_第5頁
已閱讀5頁,還剩53頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

程序設計基礎程序設計基礎試驗一(3)<2>讀入三個整數(shù)a、b、c,交換它們中數(shù),使a存放b值,b存放c值,c存放a值。#include<iostream.h>voidmain(){ inta,b,c,temp; cout<<"inputa,b,c"; cin>>a>>b>>c; temp=a; a=b; b=c; c=temp; cout<<a<<""<<b<<""<<c;}(3)<3>對任意輸入四位整數(shù),分別求出其各位數(shù)字,并按從后到前次序依次輸出。比如,輸入為1234時,輸出結果為4,3,2,1。#include<iostream.h>voidmain(){ inta,b,c,d,n; cout<<"inputnumbern"; cin>>n; if(n>999&&n<10000){ a=n/1000; b=(n%1000)/100; c=(n%100)/10; d=n%10; cout<<d<<c<<b<<a<<endl; }elsecout<<"error";}試驗二(2)<1>求解下面函數(shù)值。ex+yx<0,y<0z=ln(x+y)1≤x+y〈10log10|x+y|+1其它情況#include<iostream.h>#include<math.h>voidmain(){ floatx,y,z; cout<<"input2number"; cin>>x>>y; if(x<0&&y<0){ z=exp(x+y); } if(x+y>=1&&x+y<10){ z=log(x+y); }elsez=log10(fabs(x+y)+1); cout<<z<<endl;}<2>編程求解以下各計算式:1)S==1+2+3+…+100#include<iostream.h>voidmain(){ inti,n,s; s=0; for(i=1;i<=100;i++){ s=s+i; } cout<<s<<endl;}2)S==1?。玻。?!#include<iostream.h>voidmain(){ inti,t,s; s=0; t=1; for(i=1;i<=7;i++){ t=t*i; s=s+t; } cout<<s<<endl;}Y=X-+-+…+(-1)n+1+…值,精準到10-6。#include<iostream>#include<cmath>usingnamespacestd;floatf(intx,intn){ ints=1; for(inti=1;i<=n;i++)s*=i; returnpow(x,n)/s;}intmain(){ floatx,y=0; cout<<"enterx"<<endl; cin>>x; for(inti=1;i+=2;){ y+=pow(-1,i+1)*f(x,i); if(f(x,i)<1e-6)break; } cout<<y<<endl; return0;}<3>打印下面圖形。1131135311357531135797531…13……21………31#include<iostream.h>#include<math.h>#include<iomanip.h>intmain(){ for(inti=1;i<=11;i++){ for(intj=1;j<=11-i;j++)cout<<""; for(j=1;j<=2*i-1;j++)cout<<setw(3)<<2*i-1-abs(i-j)*2; cout<<endl; } return0;}<4>編程產生出1到10以內全部數(shù)對<i,j>并輸出,其中i>j。#include<iostream.h>voidmain(){ inti,j; for(i=1;i<=10;i++){ for(j=1;j<i;j++) cout<<"<"<<i<<","<<j<<">"; cout<<endl; }}<5>編程求出10000以內全部符合以下條件數(shù):其高位數(shù)字小于低位數(shù)字。如12,238,3578等。但21,548不符合條件。#include<iostream.h>voidmain(){ inti;for(i=10;i<100;i++){ if(i/10<i%10)cout<<i<<""; } for(i=100;i<1000;i++){ if(i/100<(i%100)/10&&(i%100)/10<i%10)cout<<i<<""; } for(i=1000;i<10000;i++){ if(i/1000<(i%1000)/100&&(i%1000)/100<(i%100)/10&&(i%100)/10<i%10)cout<<i<<""; }}試驗三<1>編程產生以下數(shù)組,并輸出。2)(13610152128364555)#include<iostream.h>voidmain(){ inti,a[10]; for(i=0;i<10;i++){ a[i]=(i+1)*(i+2)/2; } for(i=0;i<10;i++) cout<<a[i]<<"";}⑵二維數(shù)組4)ABCDEFBCEHLQCDFIMRDEGJNSEFHKOT#include<iostream.h>voidmain(){ inti,j; chara[5][6]; for(i=0,j=0;j<6;j++) a[i][j]='A'+j; for(i=1;i<5;i++) for(j=0;j<6;j++){ a[i][j]='A'+i+j*(j+1)/2; } for(i=0;i<5;i++){ for(j=0;j<6;j++) cout<<a[i][j]<<""; cout<<endl; }}<4>隨機輸入一組數(shù)組元素值,利用題<2>使一個數(shù)組有序。然后隨機輸入一個數(shù),用折半查找法在數(shù)組中查找,如在數(shù)組中,則輸出元素在數(shù)組中位置;如不在,則輸出提醒#include<iostream>usingnamespacestd;constintn=10;intmain(){ inti,k,temp,a[n]; for(k=0;k<n;k++) cin>>a[k]; for(k=0;k<10;k++){ for(i=0;i<10;i++){ if(a[i]>a[i+1]){ temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } } for(i=0;i<10;i++) cout<<a[i]<<""; cout<<endl; intmid; intlow=1,high=n; intnum;cin>>num;while(low<=high){ mid=(low+high)/2; if(num==a[mid]) break; elseif(num<a[mid]) high=mid-1; elselow=mid+1; }if(num==a[mid]) cout<<"所查找數(shù)在第"<<mid+1<<"個位置"<<endl;elsecout<<"不存在這個數(shù)"<<endl;return0;}<2>判斷一個二維數(shù)組是否有“鞍點”,即該位置上元素在該行上最大,在該列上最小。如有,輸出其行列號和值;若無,給出提醒。(數(shù)組元素值用scanf輸入)#include<iostream.h>#include<stdio.h>constintrow=3;constintcol=3;voidmain(){ inti=0,j=0,d; intb,c,count=0; inta[row][col]; cout<<"輸入數(shù)組"<<endl; for(i=0;i<row;i++) for(j=0;j<col;j++)scanf("%d",&a[i][j]); for(i=0;i<row;i++){ b=i;c=j; for(j=0;j<col;j++){ if(a[b][c]<=a[i][j]){ b=i; c=j; } } for(d=0;d<row;d++){ if(a[d][c]<a[b][c])break; } if(d==row){ cout<<"("<<b<<","<<c<<")"<<a[b][c]<<endl; count++; } } if(count==0)cout<<"沒有鞍點"<<endl; }試驗四(2)編寫程序實現(xiàn)以下問題求解。<1>求方程ax2+bx+c=0根,用三個函數(shù)分別求b2-4ac大于0、等于0和小于0時根,并輸出結果。從主函數(shù)輸入a、b、c值。#include<iostream.h>#include<math.h>voidf1(floatx,floaty,floatz){ floatx1,x2; x1=((-y)+sqrt(y*y-4*x*z))/(2*x); x2=((-y)-sqrt(y*y-4*x*z))/(2*x); cout<<"x1="<<x1<<endl; cout<<"x2="<<x2<<endl;}voidf2(floatx,floaty){ floatx1,x2; x1=x2=(-y)/(2*x); cout<<"x1=x2="<<x1<<endl;}voidf3(float){ cout<<"無實數(shù)根"<<endl;} voidmain(){ floata,b,c; cin>>a>>b>>c; if(b*b>4*a*c)f1(a,b,c); if(b*b==4*a*c)f2(a,b); if(b*b<4*a*c)f3(a);}<3>編寫一個將十進制整數(shù)轉換為十六進制字符串函數(shù)。#include<iostream>usingnamespacestd;voidmain(){ charc[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; charr[100],i=0; intn; cout<<"輸入一個十進制數(shù)"<<endl; cin>>n; while(n>0){ r[i]=n%16; i++; n/=16; }cout<<"轉化成16進制后"<<endl;for(intj=i-1;j>=0;j--)cout<<c[r[j]];cout<<endl;}試驗五輸入三個整數(shù),按由小到大次序輸出,然后將程序改為:輸入三個字符串,按由小到大次序輸出。#include<iostream.h>voidmain(){ inti,j,temp,a[3]; for(i=0;i<3;i++) cin>>a[i]; for(i=0;i<3;i++) for(j=0;j<3;j++){ if(a[j]>a[j+1]){ temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } for(i=0;i<3;i++) cout<<a[i]<<"";}將一個3*3矩陣轉置,用一個函數(shù)實現(xiàn)。在主函數(shù)中用scanf輸入以下矩陣元素:{2,4,6,8,10,12,14,16,18}。將數(shù)組名作為函數(shù)參數(shù)。函數(shù)調用后在主函數(shù)中輸出已轉置矩陣。#include<iostream>usingnamespacestd;voids(inta[]){ printf("轉置后\n"); printf("%d%d%d\n%d%d%d\n%d%d%d\n",a[0],a[3],a[6],a[1],a[4],a[7],a[2],a[5],a[8]);}intmain(){ inta[9]; printf("輸入9個整數(shù)\n"); scanf("%d%d%d%d%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8]); printf("轉置前\n");printf("%d%d%d\n%d%d%d\n%d%d%d\n",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]); s(a); return0;}自己寫一個strcmp函數(shù),實現(xiàn)兩個字符串比較。兩個字符串s1,s2由main函數(shù)輸入,strcmp函數(shù)返回值也在main函數(shù)中輸出。#include<iostream>usingnamespacestd;intstrcmp(chara[],charb[]){ inti,j; for(i=0;(a[i]!='\0')||(b[i]!='\0');i++) { if(a[i]>b[i]){j=1;break;} elseif(a[i]=b[i])j=0; else{j=-1;break;} } returnj;}intmain(){ inti,j; chara[20],b[20]; cout<<"inputtowstrings"<<endl; cin>>a>>b; cout<<strcmp(a,b)<<endl; return0;}動態(tài)鏈表每個結點包含一個整數(shù)和一個后繼指針。分別編寫過程完成以下操作:(1)從鍵盤輸入數(shù)據(jù)建立鏈表,并按數(shù)據(jù)輸入次序建立鏈表。(2)依次打印其中各結點數(shù)據(jù)。#include<stdlib.h>#include<stdio.h>structlist{intvalue;structlist*next;};voidmain(){intn,i=0,m;structlist*p,*q,*head,*tmp;//第一行輸入數(shù)據(jù)個數(shù)n;printf("請輸入鏈表長度:");scanf("%d",&n);if(n<1)return;//第二行依次輸入n個整數(shù);printf("請輸入節(jié)點值:");p=(structlist*)malloc(sizeof(structlist));head=p;q=p;q->next=NULL;scanf("%d",&p->value);for(i=1;i<n;i++){p=(structlist*)malloc(sizeof(structlist));scanf("%d",&p->value);q->next=p;q=p;q->next=NULL;}//第三行輸入欲刪除數(shù)據(jù)m;printf("請輸入要刪除數(shù)值:");scanf("%d",&m);//第一行輸出原始單鏈表長度;q=head;i=0;while(q->next){i++;q=q->next;}printf("原鏈表長度為:%d\n",i+1);//第二行依次輸出原始單鏈表數(shù)據(jù);printf("鏈表數(shù)據(jù)為:\n");q=head;while(q->next){printf("%d",q->value);q=q->next;}printf("%d\n",q->value);//刪除p=head;while(head->value==m&&head->next!=NULL){head=head->next;free(p);p=head;}if(head->value==m&&head->next==NULL){printf("刪除后鏈表長度為0\n");printf("刪除后鏈表為空\n");return;}p=q=tmp=head;while(q->next){q=q->next;while(q->value==m&&q->next!=NULL){tmp=q;q=q->next;free(tmp);}if(q->next==NULL&&q->value==m){p->next=NULL;}else{p->next=q;}p=q;}//第三行輸出完成刪除后單鏈表長度;q=head;i=0;while(q->next){i++;q=q->next;}printf("刪除后鏈表長度為%d\n",i+1);//第四行依次輸出完成刪除后單鏈表數(shù)據(jù);printf("刪除后鏈表為:\n");q=head;while(q->next){printf("%d",q->value);q=q->next;}printf("%d\n",q->value);}試驗六<1>在某系成績登記冊中,每個班最多有40個學生,每份成績表中成績信息包含:學號(9位字符),姓名(8位字符),成績(百分制),備注(20位字符)。設計程序以處理一個班級成績信息,包含輸入、輸出、查詢(給定分數(shù)以上或以下學生信息)、按分數(shù)排序等。#include<iostream.h>structstudent{ intnum; charname[8]; intgread; charbeizhu[20];}student[40];voidmain(){ inti,j,k,temp; for(i=0;i<40;i++){ cout<<"輸入學號"; cin>>student[i].num; cout<<"輸入姓名"; cin>>student[i].name; cout<<"輸入成績"; cin>>student[i].gread; cout<<"輸入備注"; cin>>student[i].beizhu; } for(i=0;i<40;i++) cout<<student[i].num<<""<<student[i].name<<""<<student[i].gread<<""<<student[i].beizhu<<endl; cout<<"輸入分數(shù)線"; cin>>j; for(i=0;i<40;i++){ if(student[i].gread>j) cout<<student[i].num<<""<<student[i].name<<""<<student[i].gread<<""<<student[i].beizhu<<endl; } for(i=0;i<40;i++){ for(k=0;k<40;k++){ if(student[k].gread>=student[k+1].gread){ temp=student[k].gread;student[k].gread=student[k+1].gread;student[k+1].gread=temp; } } } for(i=0;i<40;i++) cout<<student[i].num<<""<<student[i].name<<""<<student[i].gread<<""<<student[i].beizhu<<endl;}試驗七1.下面程序定義了一個以hours,minutes和seconds作為數(shù)據(jù)組員Time類。設計了組員函數(shù)將兩個Time對象相加(即時間相加),并進行對應檢驗,查看增加分鐘數(shù)及秒數(shù)是否大于59。假如秒數(shù)大于59,則分鐘數(shù)向前遞增1。類似地,假如分鐘數(shù)大于59,則小時數(shù)向前增1。#include<iostream.h>classTime{private: inthours,minutes,seconds;public: voidget_time() { cin>>hours>>minutes>>seconds; } voiddisplay_time() { cout<<hours<<':'<<minutes<<':'<<seconds<<endl; } voidadd_time(Time&t1,Time&t2) { hours=t1.hours+t2.hours; minutes=t1.minutes+t2.minutes; seconds=t1.seconds+t2.seconds; if(seconds>=60) { seconds-=60; minutes++; } if(minutes>=60) { minutes-=60; hours++; } if(hours>=24) { hours-=24; } }};voidmain(){ Timeone,two,three; cout<<"\nEnterthefirsttime(hoursminutesseconds):"; one.get_time(); cout<<"\nEnterthesecondtime(hoursminutesseconds):"; two.get_time(); three.add_time(one,two); cout<<"theresultis:"<<endl; three.display_time();}2.定義一個圓類(Circle),屬性為半徑(radius)、圓周長和面積,操作為輸入半徑并計算周長、面積,輸出半徑、周長和面積。要求定義結構函數(shù)(以半徑為參數(shù),缺省值為0,周長和面積在結構函數(shù)中生成)和拷貝結構函數(shù)。#include<iostream>usingnamespacestd;#definepi3.1415926classcircle{ doubler,l,s;public: circle(constcircle&a); circle(){ r=0; cout<<"inputr"<<endl; cin>>r; cout<<"r="<<r<<endl; cout<<"l="<<2*pi*r<<endl; cout<<"s="<<pi*r*r<<endl; }};circle::circle(constcircle&a){ r=a.r;}intmain(){ circlea; circleb(a); return0;}試驗八定義一個類CDate用于統(tǒng)計日期(包含年、月、日),其中提供組員函數(shù)從標準輸入讀入日期詳細值、將類對象日期屬性輸出到標準輸出以及當前對象表示日期為星期幾等函數(shù)。時間輸入、輸出方式應該盡可能與人們熟悉方式相近。#include<iostream.h>classdata{private: intyear; intmonth; intday;public: voidget(inta,intb,intc) { year=a;month=b;day=c; } voidprint(); voidpxq();};voiddata::print(){ cout<<"這一天是:"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;}voiddata::pxq(){ inty,c,m,d,a,b; a=year/1000; b=(year-a*1000)/100; c=a*10+b; y=year-a*1000-b*100; d=day; if(month==1)m=13; elseif(month==2)m=14; elsem=month; intchoice=y+y/4+c/4-2*c+(26*m+26)/10+d-1; choice=choice%7; switch(choice) { case0: cout<<"這一天是星期日"<<endl; break; case1: cout<<"這一天是星期一"<<endl; break; case2: cout<<"這一天是星期二"<<endl; break; case3: cout<<"這一天是星期三"<<endl; break; case4: cout<<"這一天是星期四"<<endl; break; case5: cout<<"這一天是星期五"<<endl; break; case6: cout<<"這一天是星期六"<<endl; }}intmain(){ datad1; inta,b,c; cout<<"輸入年,月,日:"; cin>>a>>b>>c; d1.get(a,b,c); d1.print(); d1.pxq(); return0;}首先,定義一個學生類CStudent,數(shù)據(jù)組員是:學生姓名和學號,組員函數(shù)是輸出學生姓名和學號。然后,定義一個學生類派生類,描述學生成績CScore,添加數(shù)據(jù)組員數(shù)學、語文、化學,添加獲取數(shù)學、語文和化學成績組員函數(shù)。最終,主函數(shù)中,創(chuàng)建2個CScore類對象,輸出總分最大同學姓名、學號和總成績。#include<iostream>#include<string>usingnamespacestd;classcstudent{private: intnum; stringname;public: voiddisplay_1(){ cout<<"students'nameis"<<""<<name<<","<<"num="<<num<<endl; } voidget_value_2(){cout<<"inputnum,name"<<endl;cin>>num>>name;}};classcscore:publiccstudent{ doublemath,chinese,chem;public: doublea; voidget_value_1(){cout<<"inputmath,chinese,chem"<<endl;cin>>math>>chinese>>chem;} doublesum_score(){a=math+chinese+chem;return(a);}};intmain(){cscorea1,a2; a1.get_value_1();a1.get_value_2(); a2.get_value_1();a2.get_value_2(); if(a1.sum_score()>a2.sum_score()){a1.display_1();cout<<"sum_score="<<a1.a;} else{a2.display_1();cout<<"sum_score="<<a2.a;} return0;}小型企業(yè)人員信息管理某小型企業(yè),主要有四類人員:經(jīng)理、技術人員、銷售經(jīng)理和推銷員。要求存放這些人員姓名、編號、級別、當月薪水,計算月薪總額并顯示全部信息。人員編號基數(shù)為1000,每輸入一個人員信息,編號次序加1。程序要對全部些人員有提升級別功效。為簡單起見,全部些人員初始級別均為1級,然后進行升級,經(jīng)理升為4級,技術人員和銷售經(jīng)理升為3級,推銷員仍為1級。月薪計算方法是:經(jīng)理拿固定月薪8000元;技術人員按每小時100元領取月薪;推銷員月薪按該推銷員當月銷售額4%分成;銷售經(jīng)理既拿固定月薪也領取銷售分成,固定月薪為5000元,銷售分成為所管轄部門當月銷售總額5‰。依照上述需求,設計一個基類employee,然后派生出technician(技術人員)類、manager(經(jīng)理)類和salesman(推銷員)類。因為銷售經(jīng)理(salesmanager)既是經(jīng)理又是銷售人員,兼具兩類人員特點,所以同時繼承manager和salesman兩個類。在基類中,除了定義結構函數(shù)和析構函數(shù)以外,還應統(tǒng)一定義對各類人員信息都應有操作,這么能夠規(guī)范各派生類基本行為。不過各類人員月薪計算方法不一樣,不能在基類employee中統(tǒng)一定義計算方法。各類人員信息顯示內容也不一樣,一樣不能在基類中統(tǒng)一定義顯示方法。所以,在employee類中用純虛函數(shù)方式定義了計算月薪函數(shù)pay()和顯示信息函數(shù)displayStatus(),然后在派生類中再依照各自同名函數(shù)實現(xiàn)詳細功效。因為salesmanager兩個基類又有公共基類employee,為防止二義性,這里將employee類設計為虛基類。#include"iostream.h"#include"string.h"classemployee{protected: charname[20]; intgrade; floatpay;public: intNO; employee(){} employee(charNAME[],intno) { strcpy(name,NAME); NO=no; grade=1; } voiddisplay() { cout<<"姓名為"<<name<<endl<<"編號為"<<NO<<endl<<"級別為"<<grade<<endl; }};classtechnician:publicemployee{private: floattime;public: technician(){} technician(charNAME[],intno,floatTIME):employee(NAME,no) { time=TIME; pay=time*100; grade=3; } voidgetT(){cout<<"月薪為"<<pay<<endl;}};classmanager:virtualpublicemployee{public: manager(){} manager(charNAME[],intno):employee(NAME,no) { pay=8000; grade=4; } voidgetM(){cout<<"月薪為"<<pay<<endl;}};classsalesman:virtualpublicemployee{public: floatsale; salesman(){} salesman(charNAME[],intno,floatSALE=0):employee(NAME,no) { sale=SALE; pay=sale*5/100; } voidgetS(){cout<<"月薪為"<<pay<<endl

溫馨提示

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

評論

0/150

提交評論