abc123456789

写于2012

 

方法一:

 

 

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    FILE *fp;
    if ((fp=fopen("D:\\C++\\project\\string.in","r"))==NULL)
    {
        printf("cannot open the file.");
    }
    double a1=0,a2=0;
    fscanf(fp,"%lf",&a1);
    fscanf(fp,"%lf",&a2);
    fclose(fp);
    printf("a1:%f.a2:%f\n",a1,a2);
    printf("Result:%.2lf\n",a1+a2);
}



 

 

 

 

 



方法二:

 

 

#include "stdafx.h"
#include <string>
#include <cmath>
#include <iostream>
using namespace std;

double string_process(string &ch)
{
    bool pos_flag=false,dot_flag=false,pow_flag=false;
    double int_part=0,flo_part=0,pow_part=0;
    int j=0;
    int k=1;
    for(;ch[j]!=\'\0\';j++)
    {
        if(j==0)
        {    
        if(ch[0]==\'-\')
        {
            pos_flag=false;
            continue;
        }
        else if (ch[0]==\'+\')
        {
            pos_flag=true;
            continue;
        }
        else
        {
            pos_flag=true;
            int_part=int_part*10+(double)(ch[0]-\'0\');
            continue;
        }
        }//判断第一个字符

        if (dot_flag==false)
        {
            if((ch[j]-\'0\')>=0&&(ch[j]-\'0\')<=9)
            {
                int_part=int_part*10+(int)(ch[j]-\'0\');
                continue;
            }
            else if (ch[j]==\'.\')
            {
                dot_flag=true;
                continue;
            }
            else if(ch[j]==\'e\'||ch[j]==\'E\')
            {
                dot_flag=true;
                pow_flag=true;
                continue;
            }
        }//在小数点以前的部分

        if (dot_flag==true&&pow_flag==false)
        {
            if((ch[j]-\'0\')>=0&&(ch[j]-\'0\')<=9)
            {
            flo_part=(double)(ch[j]-\'0\')/pow(10,k)+flo_part;
            k++;
            continue;
            }
            if (ch[j]==\'e\'||ch[j]==\'E\')
            {
                pow_flag=true;
                continue;
            }
        }//小数点后,指数部分之前

        if (pow_flag==true)
        {
            if ((ch[j]-\'0\')>=0&&(ch[j]-\'0\')<=9)
            {
                pow_part=pow_part*10+(double)(ch[j]-\'0\');
                continue;
            }

        }//指数部分
    }
    if(pos_flag==false)
        return (-1)*(int_part+flo_part)*pow((double)10,pow_part);
    else return (int_part+flo_part)*pow((double)10,pow_part);
}


int _tmain(int argc, _TCHAR* argv[])
{
    string ch1,ch2;
    char c1[100],c2[100];
    FILE *fp;
    if ((fp=fopen("D:\\C++\\project\\string.in","r"))==NULL)
    {
        cout<<"无法打开文件string.in"<<endl;
    }

//    fgets(ch1,255,fp);   error
//    fgets(ch2,255,fp);   error
    fgets(c1,255,fp);
    fgets(c2,255,fp);
    fclose(fp);
    ch1=c1;
    ch2=c2;
    cout<<"预处理字串:\n"<<ch1<<ch2<<endl;
    printf("result:%6.2f\n",string_process(ch1)+string_process(ch2));
//    cout<<string_process(ch2)<<endl;
    return 0;
}

 



分类:

技术点:

相关文章:

  • 2021-10-18
  • 2021-12-01
  • 2021-11-09
  • 2021-10-18
  • 2021-10-18
  • 2021-10-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-17
  • 2021-11-09
  • 2021-08-03
  • 2021-08-08
  • 2021-07-14
  • 2021-05-14
  • 2021-09-10
相关资源
相似解决方案