编写一个程序,输入a、b、c三个值,输出其中最大值。
解:
mian()
{int a,b,c,max;
printf(“请输入三个数a,b,c:\n”);
scanf(“%d,%d,%d”,&a,&b,&c);
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
printf(“最大数为:“%d”,max);
}
大学学习资料免费下载网 有
在 其他公共课程 版块
标题:
C语言程序设计第三版谭浩强课后习题答案完整版txt
谭浩强C语言程序设计习题参考解答
C程序设计题解与上机指导 答案
还有很多相关资料:
谭浩强编《C语言程序设计》视频教程(曾怡讲解) 在线 下载
谭浩强编《C语言程序设计》(电子书+纸质书)
下载不用积分
我可以帮助你,你先设置我最佳答案后,我百度Hii教你。
#include<stdioh>
int main()
{int a,b,c,t;
scanf("%d,%d,%d",&a,&b,&c);
if(a>b)
{t=a;
a=b;
b=c;
}
if(a>c)
{t=a;
a=c;
c=t;
}
if(b>c)
{t=b;
b=c;
c=t;
}
printf("%5d,%5d,%5d\n",a,b,c);
return 0;
}
实验6:
1.编一判断m是否为素数的函数,并在主函数中利用它输出十对最小的孪生素数。所谓孪生素数是指两个相差为2的素数,如3和5,11和13。输出形式如图261。
函数形式为:
bool isprime(int m);
#include "iostreamh"
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<<"("<<x<<","<<x+2<<")"<<endl;
if(count>=10)break;
}
x++;
}
}
2 编一函数,功能为构造正整数x的逆序数。再编一主函数,输出10个大于10000的最小的回文数。回文数是指顺读和倒读都相同的数,如5、151、3553等。
函数形式为:int reverse (int x);
#include"iostreamh"
#include"stdlibh"
#include"timeh"
int f(int a)
{ int b,c=0;
while(a!=0)
{b=a%10;
c=c10+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<<x<<endl;k++;if(k==10)break;}
}
}
3.编一函数,功能为判断一字符串是否为回文,如果是回文则返回1,否则返回0。回文是指顺读和倒读都一样的字符串,如“deed”和“level”是回文。在主函数中对输入的字符串加以调用。
函数形式为:int huiwen(char s[]);
#include <iostreamh>
#include <stringh>
#include <stdioh>
int huiwen(char s[])
{
int i,n=0;
char ch,s1[80];
strcpy(s1,s); //原来的字符串保留在s1中
while(s[n])n++; //求字符串长度
for(i=0;i<n/2;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个字符串:"<<endl;
for(i=0;i<5;i++)
{ gets(s);
if(huiwen(s))count++;
}
cout<<"回文个数:"<<count<<endl;
}
4.函数的功能是将学生成绩从高分到低分排序,并统计优秀与不及格的人数。用下面两种方法实现:
(1)函数形式为:int fun(int s[],int n,int x);
要求优秀人数通过return返回,不及格人数通过指针参数返回结果。
(2)函数形式为:void fun(int s[],int n,int &x,int &y);
要求优秀与不及格的人数通过引用参数返回结果。
分别编二个程序,学生数从键盘输入。
方法一:
#include <iostreamh>
#define N 10
int fun(int a[],int n,int x)
{int i,j,k;
x=0;
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;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<n;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<n;i++)
cin>>a[i];
npass=fun(a,n,&pass);
cout<<"pass="<<pass<<",npass="<<npass<<endl;
cout<<"成绩由高到低依次为:\n";
for(i=0;i<n;i++)
cout<<a[i]<<endl;
}
方法2:
#include <iostreamh>
#define N 10
void fun(int a[],int n,int &x,int &y)
{int i,j,k;
x=0;
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;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<n;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<n;i++)
cin>>a[i];
fun(a,n,pass,npass);
cout<<"pass="<<pass<<",npass="<<npass<<endl;
cout<<"成绩由高到低依次为:\n";
for(i=0;i<n;i++)
cout<<a[i]<<endl;
}
5 编一函数,功能为统计字符串中各个字母(不区分大、小写)出现的频率,同时找出频率出现最高的字母及次数。。函数形式为:
void freq(char s[],int p[],char &chmax,int &max)
#include "iostreamh"
#include "stdioh"
#include "stringh"
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<<char(i+97) <<"----"<<p[i]<<endl;
cout<<chmax<<"----"<<max<<endl;
}
6.编写函数max,其功能是将字符串s中最大字符的地址返回,再编写一个主函数,调用该函数,将字符串s中从最大字符开始的子串中的小写字母转换成大写字母,然后输出新字符串s。例如,假设s的内容为“qwertyou”,则从最大字符’y’开始的子串为“you”,处理后的s为“qwertYOU”。
函数形式为:char max(char s[]);
#include "iostreamh"
#include "stringh"
#include "stdioh"
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<<max(s)<<endl;
}
7.编一函数,求级数的部分和,当最后一项的值小于eps时结束。设eps的默认值为10-6 。函数形式为:
double fun(double x, double eps=1e-6);
#include <iostreamh>
#include <mathh>
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=-tn(n-1);
}
return s;
}
void main()
{double x;
cin>>x;
cout<<fun(x)<<endl;
}
8.编写两个同名的函数,分别求出整型数的两点间距离和浮点型数的两点间距离,调试成功后,再将其改为用函数模板实现。
函数形式为:
double dist(int x1, int y1, int x2, int y2);
double dist(double x1, double y1, double x2, double y2);
#include <iostreamh>
#include <mathh>
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="<<dist(x1,y1,x2,y2)<<endl;
cout<<"dist2="<<dist(x11,y11,x12,y12)<<endl;
}
用函数模板来实现:
#include <iostreamh>
#include <mathh>
template <class T>
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="<<dist(x1,y1,x2,y2)<<endl;
cout<<"dist2="<<dist(x11,y11,x12,y12)<<endl;
}
实验7:
1、
#include "iostreamh"
#define N 5
struct student
{
char num;
int score;
};
student max(student s[],int n)
{
student t=s[0];
for(int i=1;i<n;i++)
if(s[i]score>tscore)
t=s[i];
return t;
}
void main()
{
student s[N],maxs;
int i;
for(i=0;i<N;i++)
{
s[i]num=new char[10];//假设学号不超过9位
cin>>s[i]num>>s[i]score;
}
maxs=max(s,N);
cout<<maxsnum<<' '<<maxsscore<<endl;
}
2、
#include "iostreamh"
#include "stringh"
#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<n-1;i++)
for(j=0;j<n-1-i;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<N;i++)
cin>>b[i]name>>b[i]price ;
sort(b,N);
for(i=0;i<N;i++)
cout<<b[i]name<<' '<<b[i]price <<endl;
}
3、
#include "iostreamh"
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>>dyear>>dmonth>>dday;
if(dyear%400==0||dyear%4==0&&dyear%100!=0)
dpm[1]=29;
for(i=0;i<dmonth-1;i++)
s=s+dpm[i];
s=s+dday;
cout<<"s="<<s<<endl;
}
4、
#define N 5
#include "iostreamh"
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(i<n&&p->next!=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<<p->name<<' '<<p->num <<' '<<p->wage <<endl;
p=p->next;
}
}
void main()
{
node head=NULL,tail=NULL,newnode;
int i,n;
for(i=0;i<N;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 "iostreamh"
#include "stringh"
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<<num<<"not exist\n";
else
p->wage =w;
}
void print(node head)
{
node p=head;
while(p!=NULL)
{
cout<<p->name<<' '<<p->num <<' '<<p->wage <<endl;
p=p->next;
}
}
void main()
{
node head=NULL,tail=NULL,newnode;
int i;
char num[6];
double w;
for(i=0;i<N;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 "iostreamh"
#include "stdioh"
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<<p->ch;
p=p->next ;
}
}
实验8:
1、
#include "iostreamh"
#include "stdioh"
#include "stdlibh"
void main()
{
FILE fp;
char s[100];
int i;
fp=fopen("abctxt","w");
if(fp==NULL)
{
cout<<"can't open abctxt\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 "iostreamh"
#include "stdioh"
#include "stdlibh"
void main()
{
FILE fp;
char ch;
fp=fopen("abctxt","r");
if(fp==NULL)
{
cout<<"can't open abctxt\n";
exit(1);
}
while(1)
{
ch=fgetc(fp);
if(feof(fp))
break;
cout<<ch<<"("<<(int)ch<<")";
}
}
3、
#include "iostreamh"
#include "stdioh"
#include "stdlibh"
#include "stringh"
void main()
{
FILE fp;
char s[100],t[100];
int i,len;
fp=fopen("abctxt","w");
if(fp==NULL)
{
cout<<"can't open abctxt\n";
exit(1);
}
gets(s);
len=strlen(s);
strcpy(t,s);
for(i=0;i<len/2;i++)
{
char ch=s[i];
s[i]=s[len-1-i];
s[len-1-i]=ch;
}
fputs(s,fp);
fputs(t,fp);
fclose(fp);
}
#include "stdioh"
main(void)
{
printf(" >
以上就是关于谭浩强C语言程序设计教程(第三版)》的课后习题答案全部的内容,包括:谭浩强C语言程序设计教程(第三版)》的课后习题答案、c语言程序设计课后习题答案(高等教育出版社何钦铭 颜晖 主编)、c语言程序设计实训教程龚义建版答案编写一程序,实现输入3个整数并按从小到大的顺序输出等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)