1. C Primer Plus课本P243第5、6、7、8题
第五题:
<span style="font-size:14px;">#include<iostream>
#include<stdio.h>
using namespace std;
void large_of (double &a, double &b);
int main()
{
double a,b;
cout<<"please input two numbers, this program will change the small one into the large one."<<endl;
cin>>a>>b;
cout<<"the input number are: a= "<<a<<", b= "<<b<<endl;
large_of(a,b);
return 0;
}
void large_of (double &a, double &b)
{
if(a<b) a=b;
else b=a;
cout<<"after this change,the two number are: a= "<<a<<", b= "<<b<<endl;
}</span>
第六题:
<span style="font-size:14px;">#include<iostream>
#include<stdio.h>
using namespace std;
void position (char &ch);
int main()
{
char ch;
cout<<"please input a char, this program will tell the position the char is in the alphabet."<<endl;
cin>>ch;
cout<<"the input char is: "<<ch<<endl;
position(ch);
return 0;
}
void position (char &ch)
{
int pos=1;
if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&ch>='A'))
{
ch=tolower(ch);
pos+=(ch-97);
}
cout<<"the position of the char in the alphabet is :"<<pos<<endl;
}</span>
第七题:
<span style="font-size:14px;">#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
double powerfun(double a,int b);
int main()
{
double a,power;
int b;
cout<<"please input a number and the integer power."<<endl;
while(scanf("%lf %d",&a,&b)==2)
{
power=powerfun(a,b);
cout<<"power of the number is :"<<power<<endl;
}
return 0;
}
double powerfun(double a, int b) /* function definition */
{
double pow = 1;
int i;
if (b == 0)
{
printf("Zero power of any number is always:\n");
pow = 1.0;
}
else if (a == 0)
{
printf("the power of zero is always:\n");
pow = 0.0;
}
else if (b > 0)
for(i = 1; i <= b; i++)
pow *= a;
else
pow = 1.0 / powerfun(a, - b);
return pow;
}</span>
2.使用选择法、冒泡法对10个数进行排序,并输出排序前后的数列。
<span style="font-size:14px;">#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
//用冒泡法对10个数进行排序
int main()
{
int i,j,a[10];
for(i=0; i<10; i++)
scanf("%d",&a[i]);
for(i=0; i<10-1; i++)
{
for(j=i+1; j<10; j++)
{
if(a[j]>a[i])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0; i<10; i++)
printf(" %d",a[i]);
}
//用选择排序对10个数进行排序
#include<stdio.h>
#define N 10
int main()
{
int i,j,max,t,a[N];
for(i=0; i<N; i++)
scanf("%d",&a[i]);
for(i=0; i<N; i++)
{
max=i;
for(j=i+1; j<N; j++)
if(a[j]>a[max])
max=j;
if(max!=i)
{
t=a[i];
a[i]=a[max];
a[max]=t;
}
}
for(i=0; i<10; i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
</span>
3.定义一个函数,用于判断三角形的三条边能否构成三角形,如果能,则判断是普通三角形,等腰三角形,还是等边三角形。
<span style="font-size:14px;">#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
void funtion(int a, int b, int c);
int main()
{
double a,b,c;
printf("please input 3 numbers,this program will tell neither it can form which kind of triangle.\n");
scanf("%lf %lf %lf",&a,&b,&c);
funtion(a,b,c);
return 0;
}
void funtion(int a, int b, int c)
{
if(((a+b)>c)&&((a+c)>b)&&((b+c)>a))
{
if((a==b)&&(a==c)) printf("this is a equilateral triangle\n");
else if(((a==b)&&(a!=c))||((a==c)&&(a!=b))||((b==c)&&(a!=b))) printf("this is a isosceles triangle \n");
else printf("this is a ordinary triangle \n");
}
}
</span>
4.定义一个函数,用于将一个字符串反序存放。例如,原串为ABCDEF,则处理后的字符串为:FEDCBA。
<span style="font-size:14px;">#include<iostream>
#include<stdio.h>
#include <string.h>
using namespace std;
void antitonefun(char *a);
int main()
{
char a[100];
printf("please input a string,this program will output the antitone of it.\n");
scanf("%s",a);
antitonefun(a);
printf("%s\n",a);
return 0;
}
void antitonefun(char *a)
{
int k=strlen(a);
int temp;
for(int j=0;j<k/2;j++)
{
temp=a[j];
a[j]=a[k-1-j];
a[k-1-j]=temp;
}
}
</span>
5.利用递归函数,对一个给定的整数,输出其阶乘。
<span style="font-size:14px;">#include<iostream>
#include<stdio.h>
#include <string.h>
using namespace std;
int stratumfun(int n);
int main()
{
int n,N;
printf("please input a string, this program will output its stratum.\n");
scanf("%d",&n);
N=stratumfun(n);
printf("the stratum of n is: %d \n", N);
return 0;
}
int stratumfun(int n)
{
if(n>1) return n*stratumfun(n-1);
else return n;
}
</span>