C与指针第四章编程练习

文章展示了C语言中的几种算法实现,包括二分法和牛顿迭代法求平方根,判断质数,以及处理三角形类型和字符串去空白的操作。
摘要由CSDN通过智能技术生成

 

#include <stdio.h>
#include <stdlib.h>

#if 0  //输入一个数,返回它的平方根
//二分法寻找平方根
double GetSquareRoot(double dRaw)
{
    double dLast = 0.0;
    double dMid = 0.0;
    if (dRaw > 1e-8) //判断double大于0
    {
        double dLow = 0.0;
        double dUp = dRaw;
        do
        {
            dMid = (dLow + dUp) / 2;
            double dSquare = dMid * dMid - dRaw;
            if (dSquare > 1e-8)
            {
                dUp = dMid;
            }
            else if (dSquare > -(1e-8) && dSquare < (1e-8))
            {
                break;
            }
            else
            {
                dLow = dMid;
            }
            dLast = dMid;
            
        } while (dUp - dLow > 1e-6);
        
    }
    return dMid;
}
#elif 0
//牛顿迭代法求平方根 比常规二分法效率高(是本次题目的提出的公式的解法)
double GetSquareRoot(double dRaw)
{
    double dRet = 0.0;
    double dLast = 0.0;
    if (dRaw > 1e-8)
    {
        dRet = dRaw;
        do
        {
            dLast = dRet;
            dRet = (dRet + dRaw / dRet) / 2;
        } while (dLast - dRet > 1e-6);
    }
    return dRet;
}
#elif 1
//神迹 比牛顿迭代法效率高(不知道为什么)
float GetSquareRoot(float dRaw)
{
    float dHalf = 0.5f * dRaw;
    int i = *(int*)&dRaw; 
    i = 0x5f375a86 - (i>>1); 
    dRaw = *(float*)&i;
    dRaw = dRaw * (1.5f-dHalf * dRaw * dRaw); 
    dRaw = dRaw * (1.5f-dHalf * dRaw * dRaw); 
    dRaw = dRaw * (1.5f-dHalf * dRaw * dRaw);
    return 1/dRaw;
}
#endif
int main()
{
    double dInput = 0.0;
    printf("Please input a number: ");
    scanf("%lf", &dInput); //dInput是double所以用%lf
    double dOutput = GetSquareRoot(dInput);
    printf("\nThe Square Root is %f\n", dOutput);
    return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#if 1
bool IsPrime(int iInput)
{
    if (iInput < 2)
        return false;
    else if (iInput < 4) //已知1,2,3都是质数
        return true;
    for (int i = 2; i < iInput; ++i)//两个for循环,影响效率,有没有更好的方式
    {
        if (0 == iInput % i)
            return false;
    }
    return true;
}
#elif 0
/*筛选法
采用算法:从2开始,2是质数,2的倍数不是质数,剩下的数里,3是质数,3的倍数不是质数剩下的数里5是质数....
*/
bool IsPrime(int iInput) //未完成
{
    //2是最小的质数
    if (iInput < 2)
        return false;
    else if (iInput < 4)
        return true;
}
#endif
int main()
{
    //方法1
    for (int i = 1; i < 101; ++i)
    {
        if (IsPrime(i))
            printf("prime is %d\n", i);
    }

    return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
//得到三角形的种类:两边之和大于第三边且这两边之差小于第三边
void TriangleType(double dLength1, double dLength2, double dLength3)
{
    //等边
    if (dLength1 == dLength2 && dLength1 == dLength3)
        printf("\nEquilateral triangle");
    else if (dLength1 == dLength2 || dLength1 == dLength3 || dLength2 == dLength3)
        printf("\nIsosceles triangle");
    else
        printf("\nOther triangle");
}


int main()
{
    printf("Please enter the value of the first edge:");
    double dLength1;
    scanf("%lf", &dLength1);

    printf("Please enter the value of the Second edge:");
    double dLength2;
    scanf("%lf", &dLength2);

    printf("Please enter the value of the Third edge:");
    double dLength3;
    scanf("%lf", &dLength3);

    //判断是否是三角形:两边之和大于第三边且这两边之差小于第三边
    if (dLength1 + dLength2 > dLength3 && fabs(dLength1 - dLength2) < dLength3)
        TriangleType(dLength1, dLength2, dLength3);
    else
        printf("\nNot a triangle");
    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>
#if 0 
// 书后面的答案
void copy_n( char dst[], char src[], int n )
{
    int dst_index, src_index;
    src_index = 0;

    for (dst_index = 0; dst_index < n; ++dst_index)
    {
        dst[dst_index] = src[src_index];
        if (src[src_index] != 0)
            ++src_index;
    }
}
#endif
void copy_n( char dst[], char src[], int n )
{
    // 其实入参的n一定表示src的长度,没必要再次判断,否则这个n将没多大意义
    int iLength = sizeof(src)/sizeof(char); //sizeof包括'\0',传参的数组自动退化成字符串所以sizeof应该在外面引用
    n = (iLength < n) ? iLength : n;
    for (int i = 0; i < n; ++i) 
    {
        if (iLength < i)//确保n不会数据越界
            dst[i] = '\0';
        else
        {
            dst[i] = src[i];
        }
    }
}
int main()
{
    char a[10] = "012345678", b[10];
    copy_n(b, a, 5);
    puts(a);
    puts(b);
    return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define LENGTH 128

#if 0 
// 输入一连串的字符串,重复字符串不输出
// 1.读取字符串,判断字符串长度为128(超出的部分截断)
// 2.将字符串同之前输入的比较
// 3.将不同的字符串打印并保存到容器中
struct SMyVector // 建立链表容器,保存不同的字符串
{
    struct SMyVector *pVecNext;
    char chStr[LENGTH];
};
void SaveStr(char *pStrRead, struct SMyVector *pRoot) // 保存不同的字符串
{
    struct SMyVector *pVecRead = pRoot->pVecNext;
    char chStrRead[LENGTH];
    strncpy(chStrRead, pStrRead, LENGTH);

    int iEqual = 1;

    while (pVecRead != NULL) // 比较容器中是否有一个字符串与这个字符串相同
    {
        for (int i = 0; i < LENGTH; ++i)
        {
            if (0 == strcmp(chStrRead, pVecRead->chStr))
            {
                iEqual = 0;
                break;
            }  
        }
        pVecRead = pVecRead->pVecNext;
    }

    if (iEqual > 0) // pVecRead == NULL || 
    {
        struct SMyVector *pVec = (struct SMyVector *)malloc(sizeof(struct SMyVector));
        strcpy(pVec->chStr, chStrRead); // strcpy容易溢出,但是限制了容器长度为128
        pVec->pVecNext = pRoot->pVecNext;
        pRoot->pVecNext = pVec; // 将指针插到开头
    }
}
void PrintStr(struct SMyVector *pRoot) // 输出字符串
{
    printf("-------------------------");
    struct SMyVector *pWrite = pRoot->pVecNext; // 根指针没有数据
    while (pWrite != NULL)
    {
        puts(pWrite->chStr);
        pWrite = pWrite->pVecNext;
    } 
}
#endif
int main()
{
    char chStrRead[LENGTH];
#if 0
    struct SMyVector *pRoot = (struct SMyVector *)malloc(sizeof(struct SMyVector)); // C 语言创建结构体需要带上 struct
    pRoot->pVecNext = NULL; // C 语言指针需要赋初值,不然不一定指向空
  
    while(1)
    {
        printf("intPut:");
        gets(chStrRead); // gets 有可能导致数组溢出
        if (chStrRead[0] == '|')
            break;
        SaveStr(chStrRead, pRoot);
    }
    PrintStr(pRoot);
#endif

#if 1
// 这个代码比较符合题意
    char chStrSave[LENGTH];
    int iContinuousEqual = 0;
    while(1)
    {
        printf("intPut:");
        gets(chStrRead); // gets 有可能导致数组溢出
        if (chStrRead[0] == '|')
            break;
        if (0 == strcmp(chStrRead, chStrSave) && 0 == iContinuousEqual)
        {
            ++iContinuousEqual;
            printf("%s\n", chStrRead);
        }
        else
            iContinuousEqual = 0;
        strcpy(chStrSave, chStrRead);
    }
#endif

    system("pause");
    return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>

int substr( char dst[], char src[], int start, int len )
{
    int iRet = 0;
    if (start < 0 || len <= 0)
        return iRet;
    int i = 0;
    while(src[i] != '\000')
    {
        if (i >= start)
        {
            dst[iRet] = src[i];
            ++iRet;
        }

        if (iRet == len)
            break;
    }
    // if (i <= start)
    return iRet;
}
int main()
{
    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void deblank( char string[] )
{
    int iCount = 0;

    while (string[iCount] != '\000') // 拿到字符串的个数
    {
        ++iCount;
    }

    char *pStr = (char *)malloc(sizeof(char) * iCount);
    strcpy(pStr, string);

    int iStrNo = 0;
    int iBlankCount = 0;
    for (int i = 0; i < iCount; ++i)
    {
        string[i] = '\000';
        if (iBlankCount == 0)
        {
            string[iStrNo] = pStr[i];
            ++iStrNo;
        }
        iBlankCount = iBlankCount + ((pStr[i] == " ") ? 1 : (iBlankCount * (-1)));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值