当前位置:文档之家› 同济C++实验答案

同济C++实验答案

实验6:

1.编一判断m是否为素数的函数,并在主函数中利用它输出十对最小的孪生素数。所谓孪生素数是指两个相差为2的素数,如3和5,11和13。输出形式如图2.6.1。

函数形式为:

bool isprime(int m);

#include "iostream.h"

int isprime(int m) //判别m是否为质数

{ int i;

for(i=2;m%i!=0;i++);

return (i==m);

}

void main()

{int x,count=0;

x=2;

while(1)

{ if(isprime(x)&&isprime(x+2))

{count++;

cout<<"("<
if(count>=10)break;

}

x++;

}

}



2. 编一函数,功能为构造正整数x的逆序数。再编一主函数,输出10个大于10000的最小的回文数。回文数是指顺读和倒读都相同的数,如5、151、3553等。

函数形式为:int reverse (int x);

#include"iostream.h"

#include"stdlib.h"

#include"time.h"

int f(int a)

{ int b,c=0;

while(a!=0)

{b=a%10;

c=c*10+b;

a/=10;

}

return c;

}

void main()

{ int x,i,k=0,t; bool tag=true;

srand(time(NULL));

for(x=10000;k<10;x++)

{

if(f(x)==x)

{cout<
}

}



3.编一函数,功能为判断一字符串是否为回文,如果是回文则返回1,否则返回0。回文是指顺读和倒读都一样的字符串,如“deed”和“level”是回文。在主函数中对输入的字符串加以调用。

函数形式为:int huiwen(char s[]);

#include

#include

#include

int huiwen(char s[])

{

int i,n=0;

char ch,s1[80];

strcpy(s1,s); //原来的字符串保留在s1中

while(s[n])n++; //求字符串长度

for(i=0;i
{ ch=s[i]; s[i]=s[n-i-1]; s[n-i-1]=ch; }

if(strcmp(s1,s)==0)

return 1;

else

return 0;

}

void main()

{ char s[80]; int i,count=0;

cout<<"输入5个字符串:"<
for(i=0;i<5;i++)

{ gets(s);

if(huiwen(s))count++;

}

cout<<"回文个数:"<
}



4.函数的功能是将学生成绩从高分到低分排序,并统计优秀与不及格的人数。用下面两种方法实现:

(1)函数形式为:int fun(int s[],int n,int *x);

要求优秀人数通过return返回,不及格人数通过指针参数返回结果。

(2)函数形式为:void fun(int s[],int n,int &x,int &y);

要求优秀与不及格的人数通过引用参数返回结果。

分别编二个程序,学生数从键盘输入。

方法一:

#include

#define N 10

int fun(int a[],int n,int *x)

{int i,j,k;

*x=0;

for(i=0;i
{k=i;

for(j=i+1;j
if(a[j]>a[k])

k=j;

if(k!=i)

{int t=a[k]; a[k]=a[i]; a[i]=t;}

}

for(i=

0;i
if(a[i]>=60)

*x=*x+1;

return(n-*x);

}

void main()

{int a[N],i,n,pass,npass;

cin>>n;

for(i=0;i
cin>>a[i];

npass=fun(a,n,&pass);

cout<<"pass="<
cout<<"成绩由高到低依次为:\n";

for(i=0;i
cout<
}



方法2:

#include

#define N 10

void fun(int a[],int n,int &x,int &y)

{int i,j,k;

x=0;

for(i=0;i
{k=i;

for(j=i+1;j
if(a[j]>a[k])

k=j;

if(k!=i)

{int t=a[k]; a[k]=a[i]; a[i]=t;}

}

for(i=0;i
if(a[i]>=60)

x=x+1;

y=n-x;

}

void main()

{int a[N],i,n,pass,npass;

cin>>n;

for(i=0;i
cin>>a[i];

fun(a,n,pass,npass);

cout<<"pass="<
cout<<"成绩由高到低依次为:\n";

for(i=0;i
cout<
}





5. 编一函数,功能为统计字符串中各个字母(不区分大、小写)出现的频率,同时找出频率出现最高的字母及次数。。函数形式为:

void freq(char s[],int p[],char &chmax,int &max)

#include "iostream.h"

#include "stdio.h"

#include "string.h"

void freq(char s[],int p[],char &chmax,int &max)

{ for(int i=0;i<26;i++)

p[i]=0;

strlwr(s);

i=0;

while(s[i]!='\0')

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

p[s[i]-'a']++;

i++;

}

max=p[0]; int k=0;

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

if(p[i]>max)

{ max=p[i];k=i;}

chmax=k+97;

}

void main()

{ int p[26],i,max; char s[80],chmax;

gets(s);

freq(s,p,chmax,max);

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

if(p[i])cout<
cout<
}

6.编写函数max,其功能是将字符串s中最大字符的地址返回,再编写一个主函数,调用该函数,将字符串s中从最大字符开始的子串中的小写字母转换成大写字母,然后输出新字符串s。例如,假设s的内容为“qwertyou”,则从最大字符’y’开始的子串为“you”,处理后的s为“qwertYOU”。

函数形式为:char *max(char s[]);

#include "iostream.h"

#include "string.h"

#include "stdio.h"

char *max(char s[])

{char *p=s;

int i=1,imax=0;

while(s[i]!='\0')

{ if(s[i]>s[imax])imax=i;

i++;

}

while(s[imax]!='\0') //等价于strupr(&s[imax]);

{s[imax]-=32;imax++;}

return p;

}

void main()

{char s[100];

gets(s);

cout<
}

7.编一函数,求级数的部分和,当最后一项的值小于eps时结束。设eps的默认值为10-6 。函数形式为:

double fun(double x, double eps=1e-6);

#include

#include

double fun(double x,double eps=1e-6)

{int n=1;

double t

=1,s=0;

while(fabs(x/t)>1e-6)

{

s=s+x/t;

n=n+2;

t=-t*n*(n-1);

}

return s;

}

void main()

{double x;

cin>>x;

cout<
}

8.编写两个同名的函数,分别求出整型数的两点间距离和浮点型数的两点间距离,调试成功后,再将其改为用函数模板实现。

函数形式为:

double dist(int x1, int y1, int x2, int y2);

double dist(double x1, double y1, double x2, double y2);

#include

#include

double dist(int x1,int y1,int x2,int y2)

{

return(sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));

}

double dist(double x1,double y1,double x2,double y2)

{

return(sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));

}

void main()

{int x1,x2,y1,y2;

double x11,x12,y11,y12;

cin>>x1>>y1>>x2>>y2;

cin>>x11>>y11>>x12>>y12;

cout<<"dist1="<
cout<<"dist2="<
}

用函数模板来实现:

#include

#include

template

double dist(T x1,T y1,T x2,T y2)

{

return(sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));

}

void main()

{int x1,x2,y1,y2;

double x11,x12,y11,y12;

cin>>x1>>y1>>x2>>y2;

cin>>x11>>y11>>x12>>y12;

cout<<"dist1="<
cout<<"dist2="<
}





实验7:

1、

#include "iostream.h"

#define N 5

struct student

{

char *num;

int score;

};

student max(student s[],int n)

{

student t=s[0];

for(int i=1;i
if(s[i].score>t.score)

t=s[i];

return t;

}

void main()

{

student s[N],maxs;

int i;

for(i=0;i
{

s[i].num=new char[10];//假设学号不超过9位

cin>>s[i].num>>s[i].score;

}

maxs=max(s,N);

cout<
}



2、

#include "iostream.h"

#include "string.h"

#define N 5

struct book

{

char name[30];

double price;

};

void sort(book b[],int n)

{

int i,j;

book t;

for(i=0;i
for(j=0;j
if(strcmp(b[j].name,b[j+1].name)>0)

{

t=b[j];

b[j]=b[j+1];

b[j+1]=t;

}

}

void main()

{

book b[N];

int i;

for(i=0;i
cin>>b[i].name>>b[i].price ;

sort(b,N);

for(i=0;i
cout<
}



3、

#include "iostream.h"

void main()

{

struct date

{

int year;

int month;

int

day;

}d;

int dpm[12]={31,28,31,30,31,30,31,31,30,31,30,31},i,s=0;

cin>>d.year>>d.month>>d.day;

if(d.year%400==0||d.year%4==0&&d.year%100!=0)

dpm[1]=29;

for(i=0;i
s=s+dpm[i];

s=s+d.day;

cout<<"s="<
}

4、

#define N 5

#include "iostream.h"

struct node

{

char num[6]; //职工工号

char name[20]; //职工姓名

double wage; //职工工资

node *next;

};

node *deln(node *head,int n)

{

node *p=head,*q;

int i=1;

if(n==1)

head=head->next;

else

{ while(inext!=NULL)

{

q=p;

p=p->next;

i++;

}

q->next =p->next ;

}

delete p;

return head;

}

void print(node *head)

{

node *p=head;

while(p!=NULL)

{

cout<name<<' '<num <<' '<wage <
p=p->next;

}

}

void main()

{

node *head=NULL,*tail=NULL,*newnode;

int i,n;

for(i=0;i
{

newnode=new node;

cin>>newnode->name >>newnode->num>>newnode->wage ;

if(head==NULL)

head=newnode;

else

tail->next=newnode;

tail=newnode;

}

tail->next=NULL;

print(head);

cin>>n; //输入待删除结点的序号

head=deln(head,n);

print(head);

}

5、

#define N 5

#include "iostream.h"

#include "string.h"

struct node

{

char num[6]; //职工工号

char name[20]; //职工姓名

double wage; //职工工资

node *next;

};

void modify(node *head,char num[],double w)

{

node *p=head;

while(p!=NULL&&strcmp(p->num ,num)!=0)

p=p->next ;

if(p==NULL)

cout<
else

p->wage =w;

}

void print(node *head)

{

node *p=head;

while(p!=NULL)

{

cout<name<<' '<num <<' '<wage <
p=p->next;

}

}

void main()

{

node *head=NULL,*tail=NULL,*newnode;

int i;

char num[6];

double w;

for(i=0;i
{

newnode=new node;

cin>>newnode->name >>newnode->num>>newnode->wage ;

if(head==NULL)

head=newnode;

else

tail->next=newnode;

tail=newnode;

}

tail->next=NULL;

print(head);

cin>>num>>w; //输入待修改信息的

工号、新工资

modify(head,num,w);

print(head);

}

6、

#include "iostream.h"

#include "stdio.h"

struct node

{

char ch;

node *next;

};

void main( )

{

struct node *head, *p, *q,*newnode,*tail;

char s[100];

gets(s);

head = new node ; // 在链表中插入伪结点,不关心该结点的数据域的值。

head->next =NULL;

tail = head;

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

{

newnode=new node;

newnode->ch=s[i];

newnode->next =NULL;

p=head->next;

while(p!=NULL&&s[i]>p->ch)

{

q=p;

p=p->next ;

}

if(p==NULL)

{

tail->next =newnode;

tail=newnode;

}

else

{

q->next =newnode;

newnode->next =p;

}

}

p=head->next ;

while(p!=NULL)

{

cout<ch;

p=p->next ;

}

}





实验8:

1、

#include "iostream.h"

#include "stdio.h"

#include "stdlib.h"

void main()

{

FILE *fp;

char s[100];

int i;

fp=fopen("abc.txt","w");

if(fp==NULL)

{

cout<<"can't open abc.txt.\n";

exit(1);

}

gets(s);

i=0;

while(s[i]!='\0')

{

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

s[i]=s[i]-32;

else if(s[i]>='A'&&s[i]<='Z')

s[i]=s[i]+32;

i++;

}

fputs(s,fp);

fclose(fp);

}



2、

#include "iostream.h"

#include "stdio.h"

#include "stdlib.h"

void main()

{

FILE *fp;

char ch;

fp=fopen("abc.txt","r");

if(fp==NULL)

{

cout<<"can't open abc.txt.\n";

exit(1);

}

while(1)

{

ch=fgetc(fp);

if(feof(fp))

break;

cout<
}

}



3、

#include "iostream.h"

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

void main()

{

FILE *fp;

char s[100],t[100];

int i,len;

fp=fopen("abc.txt","w");

if(fp==NULL)

{

cout<<"can't open abc.txt.\n";

exit(1);

}

gets(s);

len=strlen(s);

strcpy(t,s);

for(i=0;i
{

char ch=s[i];

s[i]=s[len-1-i];

s[len-1-i]=ch;

}

fputs(s,fp);

fp

uts(t,fp);

fclose(fp);

}






相关主题
文本预览
相关文档 最新文档