栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

实数加法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实数加法

// 错误写法
void zero( char str[] )
{
    memset( str,0,sizeof(str) );
}

'数组传参之后就变成指针了 sizeof不能得到数组的长度 只能得到指针的长度'

//  实数加法
#include
using namespace std;

const int MAXN=222;
char str1[MAXN],str2[MAXN];
char A[MAXN],a[MAXN],B[MAXN],b[MAXN],ANS[MAXN],ans[MAXN];   // char
int Alen,alen,Blen,blen,ANSlen,anslen;


// 整数部分有最高位进位问题 ( 小数部分进位至个位 )
void solve()
{
    int i,pos;

    pos=0; for( i=0;str1[i]!='.';i++ ) A[pos++]=str1[i];     Alen=pos;

    pos=0; for( i++;str1[i];i++ )      a[pos++]=str1[i]-'0'; alen=pos;

    pos=0; for( i=0;str2[i]!='.';i++ ) B[pos++]=str2[i];     Blen=pos;

    pos=0; for( i++;str2[i];i++ )      b[pos++]=str2[i]-'0'; blen=pos;

    ANSlen=Alen>Blen?Alen:Blen;
    anslen=alen>blen?alen:blen;
}

void my_strrev( char str[] )
{
    char *p1=str;
    char *p2=str+strlen(str)-1;
    char temp;

    while( p1=0;i-- )          // do not only anslen, pay attention to -1
    {
        ans[i]=( a[i]+b[i]+pro )%10;
        pro=( a[i]+b[i]+pro )/10;
        if( flag )                      // reverse position 
        {
            if( ans[i]==0 ) anslen--;   // == 0
            else flag=0;
        }
    }

    my_strrev(A); convert(A);
    my_strrev(B); convert(B);

    for( i=0;i<=ANSlen;i++ )
    {
        ANS[i]=( A[i]+B[i]+pro )%10;
        pro=( A[i]+B[i]+pro )/10;
    }

    for( i=ANSlen; ANS[i]==0 && i>=1;i-- );
    for( ;i>=0;i-- ) printf("%d",ANS[i]);
    printf(".");

    for( i=0;i

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/713778.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号