编程练习4

1. C Primer Plus课本P243第56、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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值