皮德常c++全套答案(文檔良心出品)_第1頁(yè)
皮德常c++全套答案(文檔良心出品)_第2頁(yè)
皮德常c++全套答案(文檔良心出品)_第3頁(yè)
皮德常c++全套答案(文檔良心出品)_第4頁(yè)
皮德常c++全套答案(文檔良心出品)_第5頁(yè)
已閱讀5頁(yè),還剩97頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

2

2-5

11

22

#include<iostream>

usingnamespacestd;

voidmain()

{

intdaynum;

floatdaypay,paysum=0;do{

coutvv"(>1

}while(daynumv=1);

)

";cin>>daynum;

for(inti=1;i<=daynum;i++)

{daypay二i/100.0;

coutvv""<<i<<""<<daypay<<"\t";if(i%2==0)

coutvvendl;

paysum+=daypay;//

}

coutvvendl;

coutvv""vvpaysumvv"";//

}

2-7for1/302/29+3/28++30/1

#includeviostream>

usingnamespacestd;

voidmain()

{

inti;

floatsum=0;

for(i=1;iv=30;i++)

sum+=i/float(31-i);

coutvv"sum="vvsum;

2-8

AAAAAAA

AAAAA

AAA

A

AAA

AAAAA

AAAAAAA

#include<iostream>

usingnamespacestd;

voidmain()

{

inti,j,k;

for(i=0;iv=3;i++)

{for(j=0;j<i;j++)

coutvv'';//''

for(k=7-i;k>i;k--)//'A'

cout?endl;

}

for(i=1;i<=3;i++)

{for(j=3;j>i;j--)

coutvv'';//''

for(k=0;k<2*i+1;k++)//

coutvvendl;

}

}

2-9s30

s=2/1+3/2+5/3+

#include<iostream>

usingnamespacestd;

#defineN30

voidmain()

{inti;

floata1,a2,sum=0,temp;〃a1,a2

coutvv'A:

'A'coutvv'A';

for(a1=2,a2=1,sum=0,i=1;i<二N;i++){

sum+=a1/a2;

temp=a1;a仁a1+a2;a2=temp;

}

cout<<"\nsumis:"<<sum;

}

2-10sum二a+aa+…+aa…aan

#include<iostream>

usingnamespacestd;

voidmain()

{

inti,a,n;

longsum,temp;

sum=temp=0;

cout<<"\nEnteraandn:";

cin>>a>>n;

for(i=1;i<二n;i++)

{temp=a+temp*10;

sum+=temp;

}

cout?"theresultis:"<<sum;

}

2-11

#include<iostream>

usingnamespacestd;

voidmain()

{charc;

intlletters=O,uletters=O,spaces=O,digits=O,others=O;

cout<<"Pleaseenterthecharacters:'n";

while((c=cin.get())!='\n')

{if(c>='a'&&c<='z')

lletters++;

elseif(c>='A'&&c<='Z')

uletters++;

elseif(c=='')

spaces++;

elseif(c>='0'&&c<=9)

digits++;

else

others++;

}

cout<<"thenumberoflowercaseletters:"<<lletters<<endl;cout<<"thenumberofuppercaseletters:"<<uletters<<endl;cout<<"thenumberofdigits:"<<digits<<endl;

cout<<"thenumberofspaces:"<<spaces<<endl;cout<<"thenumberofothers:"<<others;

}

3

3-1

#include<iostream>

usingnamespacestd;

longfac(intn)

{

longfac=1;

for(inti=2;i<=n;i++)

fac*=i;

returnfac;

}

longcomb(intn,intk)

{

returnfac(n)/(fac(k)*fac(n-k));

}

voidmain()

{intn,k;

cout<v"Pleaseinputthenandthek:";

cin>>n>>k;

cout?"thecombinationnumberofnandk:"<<comb(n,k);

}

3-2exx10-9

10-9

#include<iostream>

#include<cmath>

usingnamespacestd;

doubleex(doublex,doubleeps)

{

inti=1;

doublee=1,t=1;

while(fabs(t)>eps)

{t=t*x/i;

e=e+t;

i++;

}

return(e);

}

voidmain()

{doublex,eps;

cout<<"Pleaseinoutxandeps:\n";

cin>>x>>eps;cout?"e**x:\n"<<ex(x,eps);

}

3-31000

5

#include<iostream>

#includeviomanip>

#include<cmath>

usingnamespacestd;

voidPrimeNum(intn=1000)

{inti,j,k,flg=1,num=O;for(i=2;i<n;i++)

{

k二sqrt(i);

for(j=2;j<=k;j++)if(i%j==O)

flg=0;

if(flg==1)

{cout<<setw(5)vvi;num++;

if(num%5==0)cout?endl;

}

flg=1;

}

}

voidmain()

{PrimeNum();

}

3-41000

#include<iostream>

#includeviomanip>

usingnamespacestd;

voidprint(intn)//

{inti;

for(i=1;i<=n/2;i++)

if(n%i==0)cout<<setw(5)<<i;cout?endl;

}

voidwanshu(intn)

{inti;

inta,s=0;a=n/2;

for(i=1;i<=a;i++)

if(n%i==0)s+=i;

if(s=二n)

{coutv<n<<"itsfactorsare:";

print(n);

}

}

voidmain()

{inti;

for(i=1;iv=1000;i++)

wanshu(i);

}

3-5

#include<iostream>

usingnamespacestd;

intgcd(intx,inty)

{intr;

r=x%y;

while(r!=0)

{x=y;y=r;r=x%y;}

return(y);

}

//intgcd(inta,intb)

〃{if(a%b==0)

//returnb;

//else

//returngcd(b,a%b);

//}

intgbs(intx,inty)

{intbs,ys;

ys=gcd(x,y);

bs=x*y/ys;

returnbs;

}

voidmain()

{intx,y,g,bs;

cout<<"Entertwonumber:\n”;

cin>>x>>y;

g=gcd(x,y);

bs二gbs(x,y);

cout?"thegcd:"vvgvvendl;cout?"thegbs:"<<bs;

}

3-6

#include<iostream>

usingnamespacestd;

voidrev(intx)/**/

{if(x>0)

{cout<<x%10;

rev(x/10);

}

}

voidmain()

{intx;

cout?"Pleaseinputaninteger:\n";cin>>x;

cout?"reverse:";

if(x<0)

{coutvv"-";

x=-x;

}rev(x);

}

3-8

#include<iostream>

usingnamespacestd;

voidfun(inti)

{

if(i>1)

fun(i/2);

cout<<i%2;

}

voidmain()

{intn;

cout<v"Pleaseinputthedecimalnumber:";cin>>n;

cout<<"thebinarynumber:";fun(n);

}

3-9

3-10minmax

#include<iostream>

usingnamespacestd;

templatevclassT>

Tmin(T&var1,T&va⑵

{if(var1<var2)

return(var1);

else

return(var2);

}

template<classT>

Tmax(T&var1,T&va⑵

{if(var1<var2)

return(var2);

else

return(var1);

}

voidmain(void)

{intuserInt1,userlnt2;

floatuserFloat1,userFloat2;

charuserChar1,userChar2;

cout<<"Pleaseinputtwointegarnumbers:";cin>>userlnt1>>userlnt2;cout<<"theminis:"<<min(userlnt1,userlnt2)?endl;cout<<"themaxis:

"<<max(userlnt1,userlnt2)v<endl;

cout?"Pleaseinputtwofloatnumbers:";

cin>>userFloat1>>userFloat2;

cout<<"theminis:"<<min(userFloat1,userFloat2)v<endl;

cout<<"themaxis:"<<max(userFloat1,userFloat2)<<endl;

cout<<"Pleaseinputtwocharnumbers:";

cin>>userChar1>>userChar2;

cout<<"theminis:"<<min(userChar1,userChar2)?endl;

cout<<"themaxis:"<<max(userChar1,userChar2)<<endl;

}

3-11

#include<iostream>

usingnamespacestd;

templatevclassT>

TAbs(constTvar)

{if(var<0)

return(-var);

else

return(var);

}

voidmain(void)

{//

inttest12,test2=-34;

cout?""<<endl;

cout<<"test"v<test1<v"\ttest1

cout<<"test2="<<test2<<"\ttest2

"<<Abs(test1)vvendl;

"<<Abs(test2)vvendl;//

floattest3=12.67f,test4=-34.43f;

coutvv"

"<<Abs(test3)vvendl;

"<<Abs(test4)vvendl;

}

"<<endl;cout<<"test3="<<test3<<"\ttest3cout<<"test4="<<test4<<"\ttest4

4

4-110

#inelude<iostream>

usingnamespacestd;

#defineN10

voidmain()

{inti,array[N],max,maxid,min,minid;

for(i=0;i<N;i++)

cin>>array[i];

max=min二array[0];

maxid=minid=0;

for(i=1;i<N;i++)

{

if(array[i]>max)

{max=array[i];maxid=i;}if(array[i]<min)

{min=array[i];minid二i;}

}

cout<<"themaxofthearrayis"<<max<<endl;

cout<<"theindexofthemaxis"<<maxid<<endl;

cout<<"theminofthearrayis"vvminvvendl;cout<<"theindexoftheminis"vvminid<<endl;

}

4-2

#include<iostream>usingnamespacestd;

voidWords(charstr[])

{inti;

charch='';//

for(i=0;str[i]!='\0';i++)

{

if(ch==''&&str[i]!='')//

{if(str[i]>='a'&&str[i]<='z')

str[i]-=32;

}

#include<iostream>

ch=str[i];

}

}

voidmain()

{charstr[100];cout?"Pleaseinputastring:";

cin.getline(str,100);

Words(str);

cout?str;

}

4-4

#include<iostream>

usingnamespacestd;

#definearrSize5

intsearchList(intlist[],intnumElems,intvalue)

{for(inti=0;i<numElems;i++)

if(value==list[i])

returni;

return-1;

}

voidmain()

{inttests[arrSize]={5658845,4562555,3852085,4520125,5552012};intresult,x;

coutvv"";

cin>>x;

result=searchList(tests,arrSize,x);

if(result==-1)

cout<<x<<"

else

cout<<x<<"

}4-5

"<<endl;

";

,

#includeviomanip>usingnamespacestd;

#definearrSize5

voidselectionSort(inta[],intn)//

{inti,j,t,minlndex;

for(i=0;i<n-1;i++)

{

minlndex=i;

for(j=i+1;j<n;j++)

if(a[j]<a[minlndex])minlndex=j;

if(minlndex!=i)

{t=a[minlndex];

a[minlndex]=a[i];

a[i]=t;

}

}

}

a

value

intbinarySearch(inta[],intnumElems,intvalue){intlow=0,mid,hight=numElems-1;

while(low<=hight)

{

mid=(low+hight)/2;

if(value==a[mid])//

returnmid;

elseif(value<a[mid])//

hight=mid-1;

else

low=mid+1;

}

return-1;

//

}

voidmain()

{inttests[arrSize]={5658845,4562555,3852085,4520125,5552012};intresult,x;

coutvv"";

cin>>x;

selectionSort(tests,arrSize);

resultbinarySearch(tests,arrSize,x);

if(result==-1)

cout<<x<<"

else

cout<<x<<"

}

4-

"<<endl;

";

6

#include<iostream>

#include<iomanip>

usingnamespacestd;

#defineM3

#defineN4

intq0,q2=0;

intandian(inta[M][N],intp1,intp2)

{inti,j,k,m,f,w,max;

w=0;

for(i=0;i<p1;i++)

{

k=0;

max=a[i][0];

for(j=0;j<p2;j++)

if(a[i][j]>max)

{k=j;

max=a[i][j];}//iki

for(m=0,f=1;m<p1&&f;m++)

if(a[m][k]<max)f=0;//ik

if(f)

{w=1;

}

q1=i;

q2=k;}

}

returnw;

}

voidmain()

{

inta[M][N]={1,2,3,4,4,5,3,6,3,5,6,7};/*{2,4,10,6,3,4,5,8,9,1,2,3};*/intw;w=andian(a,M,N);

if(w)

{cout?""v<a[q1][q2]v<endl;

cout?""vvq1<v"\t"<<q2;

}

elsecout?";

}

4-74X4

#include<iostream>

usingnamespacestd;

intarrSum(intarr[][4])

{

inti,j,sum=0;

for(i=0;i<4;i++)

{

sum+=arr[i][0];

sum+=arr[i][3];

}

for(j=0;j<4;j++)

{

sum+=arr[0][j];

sum+=arr[3][j];

}

sum-二arr[0][0]+arr[3][0]+arr[0][3]+arr[3][3];

returnsum;

}

voidmain()

{

inta[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};coutvv"

arrSum(a);

}

4-8

#include<iostream>

usingnamespacestd;

#defineN4

intsum(inta[][N])

{

inti,s=0;

for(i=0;i<N;i++)

s二s+a[i][i]+a[i][N-i-1];

return(s);

}

voidmain()

{

inti,j,a[N][N];

coutvv"\n";

for(i=0;i<N;i++)

for(j=0;j<N;j++)

{coutvv""vvivv""vvjvv"";

cin>>a[i][j];

}

coutvv""vvsum(a);

}

4-9

4-10

#includeviostream>

#includeviomanip>

usingnamespacestd;

{

intmerge(inta[],intb[],intc[],intm,intn)

{

inti=0,j=0,k=0;

while(i<m&&j<n)if(a[i]<=b[j])c[k++]=a[i++];

elsec[k++]=b[j++];

while(ivm)c[k++]=a[i++];

while(j<n)

c[k++]=b[j++];

returnk;

}

voidmain()

{inta[]={1,4,7,9},b[]={3,5,7,8,12},c[20],i,k;k=merge(a,b,c,4,5);

cout<<"arraya:\n";for(i=0;i<4;i++)cout<<setw(8)<<a[i];

cout<<"\narrayb:\n";for(i=0;i<5;i++)

cout<<setw(8)<<b[i];

cout<<"\nmergedarrayc:\n";for(i=0;i<k;i++)

cout<<setw(8)<<c[i];

}

5

5-14-25-3

#include<iostream>

#includeviomanip>usingnamespacestd;

voidselectionSort(int*a,intn)//

{inti,j,t,minlndex;

for(i=0;i<n-1;i++)

minlndex=i;

{

for(j=i+1;j<n;j++)

if(a[j]<a[minlndex])minlndex=j;

if(minlndex!=i)

{t=a[minlndex];

a[minlndex]=a[i];

a[i]=t;

}

}

}

intmean(int*a,intn)

{

intmean,tmp;

selectionSort(a,n);

tmp=n/2;

if(n%2==0)

mean二a[tmp]+a[tmp-1];

elsemean二a[tmp];

returnmean;

}

voidmain()

{

inta[100],num,i;

cout?"

<100";

cin>>num;

cout?""<<num<<"\n";

for(i=0;i<num;i++)

cin>>a[i];

cout?""<<mean(a,num);

}

5-4

1

2

-1

#include<iostream>

#include<cmath>

#includeviomanip>

#include<ctime>

usingnamespacestd;

constintvalue=10;

intMol(inta[],intvalue);

intmain()

{inta[value];

srand(time(0));

for(inti=0;i<10;i++)

{

a[i]=rand()%10;

coutv<a[i]v<setw(5);

}

cout<<'\n:

intf=Mol(a,value);

if(f==-1)

cout?"

else

cout?"

}

"<<endl;

"vvfvvendl;return0;

intMol(inta[],intn)

{

int*p=a,c=0;

intb[value];

int*q=b;

for(inti=0;i<n;i++)

{

for(intj二i+1,m=0;j<n;j++)

{

if(*(p+i)==*(p+j))

m++;

*(b+i)=m;

}

}

for(intl=O;l<n;l++)

{

if(*(q+l)>*(q+c))

c=l;

}

for(intm=0;m<n;m++)

if(*(q+m)==*(q+c)&&m!二c)

return-1;

return*(p+c);

}

5-5

exchange()

print

exchange

#include<iostream>

#includeviomanip>

usingnamespacestd;

voidexchange(inta[],intvalue);

voidprint(inta[],intvalue);

intmain()

{

intn;

cin>>n;

inta[100];

for(inti=0;i<n;i++)

cin>>a[i];

n

0

print()

12

n

coutvv"

print(a,n);exchange(a,n);coutvv"

print(a,n);

return0;

";

";

}

voidprint(inta[],intvalue)

{

int*p=a,i;

for(i=0;i<value;i++,p++)

cout<<setw(4)vv*p;

cout?endl;

}

voidexchange(inta[],intvalue)

{

inti,m,*p=a,t,*j=a;

for(i=1,m=0;i<value;i++)

(a[m]<a[i])?m=m:m=i;

t=*(p+m);*(p+m)=*p;*p=t;

for(i=0,m=0;i<value;i++)

(a[m]>a[i])?m=m:m=i;

t=*(j+m);*(j+m)=*(j+value-1);*(j+value-1)=t;

}

5-6move()

k123kk>0

k<0

0k<0print()

#include<iostream>

#includeviomanip>

usingnamespacestd;

voidmove(intx[],intvaluentk);

voidprint(intx[],intvalue);

intmain()

{

intx[10],i;

for(i=0;i<10;i++)

cin>>x[i];

cout?"";

print(x,10);

move(x,10,3);

coutvv"

print(x,10);

return0;

}

voidprint(intx[],intvalue)

{

{

for(inti=O;i<value;i++)

coutv<setw(5)v<x[i];

}

cout?endl;

}

5-7

str1st

#include<iostream>

usingnamespacestd;

voidnewstr(charstr1[],charstr2[]);

staticintm=0;

voidmain()

{

"<<endl;

charstr1[1OO];

charstr2[100];

coutvv"

cin.getline(str1,100);

newstr(str1,str2);

coutvv""<<endl;

cout<<str2vvendl;

}

voidnewstr(charstr1[],charstr2[])

{

char*p,*q;

q=str1;

str1

str2.

strl

str2

p=str2;

inti=0;

for(i=0;i<100;i++)

{

if(*(q+i)=='\O')

{

*((p+i/2)+1)='\0:

break;

}

elseif(i%2!=0)

{

*(p+i/2)=*(q+i);

}

}

}

5-8

palin()

palindromia.

MADAM

Madam,l'mAdam

1)

2)

yes

#include<iostream>

usingnamespacestd;

boolpalin(chara[]);

intmain()

{

chara[100];

cout?"

cin>>a;

if(palin(a))

cout<<"yes"<<endl;else

cout<v"no"<<endl;}

palin()

"<<endl;

true,

no.

palin()

false3)

return0;

}

boolpalin(chara[])

{

char*p1=a,*p2;

intn,i;

for(i=0,n=0;i<100;i++)

{

if(a[i]>='a'&&a[i]<='Z')

*(p1+n)=a[i];

n++;

}

for(p2=p1+n;p1<p2;)

{

if(*p仁二*p2)

p1++;

p2--;

}

if(p2-p仁=1||p2-p1==0)

returntrue;

else

returnfalse;

}

5-10n123??…n1

m

nmanmcount(),

#include<iostream>

usingnamespacestd;

voidcount(intn,intm,inta[]);

intmain()

{

intn,m;

cout?"

cin>>n;

cout?"

n:"<<endl;

m:"<<endl;

cin>>m;

inta[100];

for(inti=0;i<n;i++)a[i]=i+1;

count(n,m,a);return0;

}

voidcount(intn,intm,inta[]){

inti,k=0,t=0,*p=a;for(i=0;i<n;i++)p[i]=i+1;

while(t<n-1)

{

for(p=a;p<a+n;p++)if(*p!=0){

k++;

if(k==m){

k=0;

cout<<*p<<""

*p=0;

t++;

p=a;

while(*p==0)

p++;

cout<<*p<<endl;

}

3

6-1

1)

2)

10

3)10

4)80

#inelude<iostream>

#include<iomanip>

#inelude<cstdlib>

usingnamespacestd;

structstudent

{

intID;

charname[20];

doublescore[3];

};

voidinput(student*,intn);

doubleaverage(student*,intn);

voidprint(student*,intn);

voidsort(student*,intn);

constintstudentNumber=10;

intmain()

{studentstud[10];

input(stud,studentNumber);

print(stud,studentNumber);

//sort(stud,studentNumber);

return0;

}

/*voidinput(student*ps,intn)

{

for(inti=0;i<n;i++)

{

cin>>ps->ID;

cin>>ps->name;

cin>>ps->score[0]>>ps->score[1]>>ps->score[2];ps++;

}

}

:";

:";

voidaverage(student*ps,intn)

{

intaver1=0,aver2=0,aver3=0;

for(inti=0;i<n;i++)

{

aver1+=ps->score[0];

aver2+=ps->score[1];

aver3+=ps->score[2];ps++;

}

averl/二num;

aver2/二num;

aver3/二num;

coutvv"

coutvv"

coutvv"

"vvaverlvvendl;

"v<aver2v<endl;

"v<aver3v<endl;

}voidmax(student*ps,intn)

{

inta=0,b=0,c=0;

for(inti=1;i<n;i++)

{

if((ps+i)->score[0]>(ps+a)->score[0])a=i;

if((ps+i)->score[1]>(ps+b)->score[1])b=i;

if((ps+i)->score[2]>(ps+c)->score[2])c=i;

}

coutvv":";

print((ps+a),1);

coutvv"

print((ps+b),1);

coutvv"

print((ps+c),1);

}

6-4reverse(

voidreverse(NODE*head);

#include<iostream>

#include<iomanip>

#include<cstdlib>

usingnamespacestd;

#defineLENsizeof(NODE)

typedefstructnode

{

intdata;

node*next;

}NODE;

voidreverse(node*head)

{

node*p,*temp1,*temp2;

p=head->next;

temp1=p->next;

temp2=temp1->next;

p->next二NULL;

while(temp2!=NULL)

{

temp1->next=p;

p=temp1;

temp1=temp2;

temp2=temp1->next;

}

temp1->next=p;

head->next=temp1;

}

NODE*initlist(){

NODE*head;

head=(NODE*)malloc(sizeof(NODE));head->next二NULL;

returnhead;

}

NODE*create()

{NODE*p1,*p2,*head;

inta;

p2=head=initlist();

cin>>a;

while(a!=-1)

{

p1=(NODE*)malloc(sizeof(NODE));p1->data=a;

p2->next=p1;

p2=p1;

cin>>a;

p2->next二NULL;return(head);

}

voidprint(NODE*head){

NODE*p;

p=head->next;

if(p!=NULL)

{

while(p!=NULL)

{

cout<<setw(5)vvp->data;p=p->next;

cout<<"\n";

}

}

}

intmain()

{

NODE*head二NULL;

cout<<"\n\tCreatealist:'n";

head二create();

print(head);

reverse(head);

print(head);

return0;

}

6-5

sort()

voidsort(NODE*head);

data

#inelude<iostream>

#include<iomanip>

#inelude<cstdlib>

usingnamespacestd;

#defineLENsizeof(NODE)typedefstructnode{

intdata;node*next;

}NODE;

NODE*initlist()

{NODE*head;

head=(NODE*)malloc(sizeof(NODE));head->next二NULL;returnhead;

}

NODE*create()

NODE*p1,*p2,*head;

inta;

{

cout?"Creatingalist...'n";

p2二head二initlist();

cout?"Pleaseinputanumber(if(-1)stop):";cin>>a;

while(a!=-1)

{

p1=(NODE*)malloc(sizeof(NODE));

p1->data=a;

p2->next=p1;

p2=p1;

cin>>a;

}

p2->next二NULL;

return(head);

}

voidprint(NODE*head)

{NODE*p;

p=head->next;

if(p!=NULL)

{

cout<<"Outputlist:";

while(p!=NULL)

{

cout<<setw(5)vvp->data;p=p->next;

}

cout<<"\n";

}

}

voidsort(NODE*head)

{NODE*p,*n;

inti;

for(n二head->next;n->next!=NULL;n=n->next)for(p=head->next;p->n

ext!=NULL;p=p->next)if((p->data)>(p->next->data)){

i=(p->next->data);

p->next->data=p->data;p->data=i;

}

}intmain()

{

NODE*L仁NULL;

cout<<"\n\tCreatalist:\n";

L1=create();

coutvv"

print(L1);

coutvv"

sort(L1);

print(L1);

return0;

}

";

";

6-6merge()

NODE*merge(NODE*headA,NODE

*headB);

#inelude<iostream>

#includeviomanip>

#include<cstdlib>

usingnamespacestd;

#defineLENsizeof(NODE)typedefstructnode{

intdata;node*next;

}NODE;

NODE*initlist()

{NODE*head;

head=(NODE*)malloc(sizeof(NODE));head->next二NULL;

returnhead;

}

NODE*insert(NODE*head,NODE*s)

{

NODE*p;

p=head;

while(p->next!=NULL&&p->next->data<s->data)p=p->next;s->next=p->next;

p->next=s;returnhead;

}

NODE*create_sort()

{NODE*p,*head=NULL;

inta;

cout<v"Createanincreasinglist...\n";

head=initlist();

cout<<"Pleaseinputanumber(if(-1)exit):";cin>>a;

while(a!=-1)

{

p=(NODE*)malloc(sizeof(NODE));p->data=a;

insert(head,p);

cin>>a;

}

returnhead;

}

voidprint(NODE*head)

{NODE*p;

p=head->next;

if(p!二NULL)

{

cout?"Outputlist:";

while(p!二NULL)

{

cout<<setw(5)vvp->data;p=p->next;

}

cout<<"\n";

}

}

voidfree_list(NODE*head)

{NODE*p;

while(head)

{

p=head;

head二head->next;

free(p);

}

}

NODE*merge(NODE*headA,NODE*headB){

NODE*p仁headB->next,*p2二p1->next;

while(p2!=NULL)

{

insert(headA,p1);

P仁p2;

p2=p1->next;

}

insert(headA,p1);

returnheadA;

}

intmain()

{

NODE*p仁NULL,*p2二NULL,*p二NULL;cout<<"\n\tCreatalist:\n";

p1=create_sort();

print(p1);p2=create_sort();

print(p2);

p=merge(p1,p2);

print(p);

return0;

}

6-7

isSubset(

L1

L2

boolisSubset(NODE

*L1,NODE*L2);

#include<iostream>

#include<iomanip>

#include<cstdlib>

usingnamespacestd;

#defineLENsizeof(NODE)

typedefstructnode

{

intdata;

node*next;

}NODE;

NODE*initlist()

{NODE*head;

head=(NODE*)malloc(sizeof(NODE));

head->next二NULL;

returnhead;

}

NODE*create()

}

NODE*p1,*p2,*head;

inta;

cout?"Creatingalist...'n";p2=head=initlist();

cout?"Pleaseinputanumber(if(-1)stop):";cin>>a;

while(a!=-1)

{

p1=(NODE*)malloc(sizeof(NODE));p1->data=a;

p2->next=p1;

p2=p1;

cin>>a;

}

p2->next二NULL;

return(head);

}

boolsearch(NODE*head,intx)

{

NODE*p;

intn=0;

p=head->next;

while(p!=NULL)

{

if(p->data==x)

{

n+=1;

p=p->next;

}

else

p=p->next;

if(n匸0)

returntrue;

else

returnfalse;

}

boolisSubset(NODE*L1,NODE*L2){

NODE*p;

intn=0;

p=L1->next;

while(p!二NULL)

{

if(search(L2,p->data))p=p->next;

else

{

n+=1;p=p->next;

}

}

if(n==0)

returntrue;

else

returnfalse;

}

intmain()

{

NODE*L1=NULL,*L2=NULL;cout<<"\n\tCreatalist:\n";

L1=create();

L2=create();

if(isSubset(L1,L2))

cout<<"L1isinL2!\n";

else

cout<<"L1isnotinL2!\n";

}

!

…";

return0;

7

7-4

#inelude<iostream>

#inelude<fstream>

#include<iomanip>

#ineludevconio.h>

#inelude<estdlib>usingnamespaeestd;

voidmain(void)

{fstreaminFile;//f

eharstr[80]={'\0'};

coutvv"

ein.getline(FileName,128,'\n');

eharFileName[128]={'\0'};";

inFile.open(FileName,ios::in|ios::noereate);

//

if(!inFile)

{inFile.elose();

coutvv"

exit(0);

"vvendl;geteh();

}

unsignedinti=0;

//

while(!inFile.eof())

{inFile.getline(str,77,'\n');

i++;

eoutvvsetw(2)vvivv":"v<strvvendl;

//24

if(i%24==0)

{eerrvv"

geteh();

}

:";

}

inFile.close();

}

7-5

#inelude<iostream>

#inelude<fstream>

#inelude<cctype>

#include<cstdlib>

#include<cstring>

#includevconio.h>

usingnamespacestd;

intfind(chars[],chart[]);//

voidmain(void)

{fstreaminFile;//

charfindString[50],//

line[1024],

fileName[128];

intcount=O,c;

//openthefiles

coutvv"

cin.getline(fileName,128,'\n');

inFile.open(fileName,ios::in|ios::nocreate);

//

if(!inFile)

{inFile.close();

coutvv"

exit(0);

}

coutvv""vvfileNamevv"http://

while(!inFile.eof())

{inFile.getline(line,100);if((c=find(line,findString))>0)

{coutvvline?endl;

count+=c;

!"vvendl;getch();

:"cin.getline(findString,100);

}

}

inFile.close();

cout<<"\n"<<findString<<"

"<<count?endl;

}

ts-1

intfind(chars[],chart[])

{inti,j,k;

inttimes=0;tsfor(i=0;i<=(int)(strlen(s)-strlen(t));i++)

{for(k=i,j=0;(s[k]==t[j])&&(t[j]!二'\O');k++,j++);

if(t[j]=='\0')

times++;

}

returntimes;

}

7-6

#include<iostream>

#include<fstream>

#include<cctype>

usingnamespacestd;

voidmain()

{

charstr;

intflg=1;

ifstreaminfile("test.txt");

ofstreamoutfile("out.txt");

while(!infile.eof())

{

infile.get(str);

if(infile.fail())

break;

flg++;

if(str=='.')

flg=0;

cout?str;

if(flg==2)

outfile.put(str);

else

outfile.put(tolower(str));

}

infile.close();

outfile.close();

}

7-7

#ineludeviostream>

#ineludevfstream>

#ineludevconio.h>

#includevcstdlib>

usingnamespacestd;

#defineCODING2

voidmain(void)

{fstreaminFile;

fstreamoutFile;

charFileName[128]={'\0'};

";

coutvv"

cin.getline(FileName,128,'\n');

inFile.open(FileName,ios::in|ios::nocreate);

//

if(!inFile)

{inFile.close();

coutvv"

getch();

exit(0);

}

coutvv"";cin.getline(FileName,128,'\n');outFile.open(FileName,ios::out);

//

"<<endl;

!

if(!outFile)

{outFile.close();

coutvv"

exit(O);

}

charch;

inFile.get(ch);

//

while(!inFile.eof())

{ch+二CODING;

"vvendl;getch();

outFile.put(ch);

inFile.get(ch);

}

coutvv"!"<<endl;

inFile.close();

outFile.close();

}

.

7-9

.

.

51

.

.email:

14

age

address

phone:

,

.

.

.

.

1)

2)

3)

4)

5)

#inelude<iostream>

#inelude<fstream>

#inelude<string>

#inelude<estdlib>

51

name21

.

200.

#ineludevconio.h>

usingnamespaeestd;

struetStudInfor

{

eharname[21];

intage;

eharaddress?];

eharphone[14];

eharemail[51];

};

typedefStudInforinfor;

voidadd_the_infor(fstream&);

voidshow_the_infor(fstream&);

voidehange_the_infor(fstream&);

voidseareh_the_infor(fstream&);

voiddelete_the_infor(fstream&);

intmain()

{

intseleetl;

eharseleet2='y';

inforstudent二{"",0,"","",""};

fstreamfile("Student_infor.dat",ios::out|ios::in|ios::binary);

while('Y'==toupper(seleet2))

{

coutvv"\n

<<"

<<"

(1)\n"

(2)\n"

(3)\n"

<<"

<<"

coutvv"

cin>>select1;

cin.ignore();

switch(selectl)

{

case1:

(1-5)\n";

⑷\n"

(5)\n";

add_the_infor(file);

break;

case2:

show_the_infor(file);

break;

case3:

change_the_infor(file);break;

case4:

search_the_infor(file);break;

case5:

delete_the_infor(file);break;

}

coutvv"

}

file.close();

return0;

}

voidadd_the_infor(fstream&file)

{

inforstudent;

coutvv"\n";

coutvv"";

cin.getline(,21);

coutvv"";

cin>>student.age;

cin.ignore();

(Y/N):";cin>>select2;

coutvv"";

cin.getline(student.address,51);

coutvv"";

cin.getline(student.phone,14);

cout<<"email";

cin.getline(student.email,51);

"<<endl;

file.write((char*)&student,sizeof(student));coutvv"

file.flush();

file.clear();

}

voidshow_the_infor(fstream&file)

{

inforstudent;

charch;

coutvv".............

file.seekg(OL,ios::beg);

while(!file.eof())

{

file.read((char*)&student,sizeof(student));if(file.fail())

{coutvv"2error\n";

break;

}

coutvv"

coutvv"

"vvendl;coutvv"

"vvstudent.addressvvendl;coutvv"

"vvstudent.phonevvendl;

cout<v"email"vvstudent.emailvvendl;coutvv"cin.get(ch);

}

coutvv"

file.clear();

}

";

"vvstudent.agevvendl;

";

voidchange_the_infor(fstream&file)

{

stringname;

inforstudent;

boolflg=0;

";

longrecNum=0L;

coutvv"

getline(cin,name);

file.seekg(OL,ios::beg);

file.seekp(OL,ios::beg);

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論