zoukankan      html  css  js  c++  java
  • 2012年中科大复试上机第一题

    写于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
    ",a1,a2);
        printf("Result:%.2lf
    ",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]!='';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<<"预处理字串:
    "<<ch1<<ch2<<endl;
        printf("result:%6.2f
    ",string_process(ch1)+string_process(ch2));
    //    cout<<string_process(ch2)<<endl;
        return 0;
    }


  • 相关阅读:
    AdminLTE组件之表格DataTable
    爬虫:通过滑动或者点触验证码的方法及实现(点触+滑动)
    爬虫:滑动验证解决方法及python实现
    django文件上传地址以及media的设置
    基于cropper和sweetalert的简单图片/头像裁剪上传
    学写网站(二)前端配置之glup
    轩辕剑陆和外传平台版设置功能
    植物大战僵尸
    仙剑类更新
    VSCode注册关联自定义类型文件
  • 原文地址:https://www.cnblogs.com/abc123456789/p/3433461.html
Copyright © 2011-2022 走看看