• 题目1103:二次方程计算器(字符串操作以及基础数学知识)


    题目链接:http://ac.jobdu.com/problem.php?pid=1103

    详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

    参考代码:

    //
    //  1103 二次方程计算器.cpp
    //  Jobdu
    //
    //  Created by PengFei_Zheng on 07/05/2017.
    //  Copyright © 2017 PengFei_Zheng. All rights reserved.
    //
     
    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <cstring>
    #include <cmath>
    #include <climits>
    #include <stack>
    #define MAX_SIZE 101
    //#define debug
     
    using namespace std;
     
    int leftA, rightA;
    int leftB, rightB;
    int leftC, rightC;
     
    char equation[1001];
     
    void init(){
        leftA = rightA = 0;
        leftB = rightB = 0;
        leftC = rightC = 0;
    }
     
    void cal(string str, int &a, int &b, int &c){
        int len = (int)str.size();
        int i = 0;
        for( ; i < len ; i++){
            if(str[i]=='+') continue;
            else if(str[i]=='-'){
            }
            else if(str[i]=='x'){// 系数为1
                if(i+1 < len && str[i+1]=='^'){
                    if(i-1>=0 && str[i-1]=='-'){
                        a-=1;
                    }
                    else{
                        a+=1;
                    }
                    i+=3;
                }
                else{
                    if(i-1>=0 && str[i-1]=='-'){
                        b-=1;
                    }
                    else{
                        b+=1;
                    }
                    i+=1;
                }
            }
            else{
                int tmp = 0;
                int j = i;
                while(isdigit(str[i])){
                    tmp = tmp*10 + str[i]-'0';
                    i++;
                }
                if(j-1>=0 && str[j-1] == '-'){
                    tmp = 0 - tmp;
                }
                if(i < len && str[i]=='x'){
                    if(i+1<len && str[i+1]=='^'){
                        a+=tmp;
                        i+=3;
                    }
                    else{
                        b+=tmp;
                        i+=1;
                    }
                }
                else{
                    c+=tmp;
                }
            }
        }
    }
    int main(){
    #ifdef debug
        freopen("/Users/pengfei_zheng/Desktop/input.txt", "r", stdin);
    #endif
        while(scanf("%s",equation)!=EOF){
            string s(equation);
            int len = (int)s.size();
            int idx = 0;
            for(int i = 0 ; i < len ; i++){
                if(s[i]=='='){
                    idx = i;
                    break;
                }
            }
            init();
            string strLeft = s.substr(0,idx);
            string strRight = s.substr(idx+1);
            cal(strLeft,leftA,leftB,leftC);
            cal(strRight,rightA,rightB,rightC);
            int a = leftA - rightA;
            int b = leftB - rightB;
            int c = leftC - rightC;
            int tmp = b*b - 4*a*c;
            if(tmp<0){
                printf("No Solution
    ");
            }
            else{
                double x1,x2;
                x1 = (double)(0-b+sqrt(tmp))/(2*a);
                x2 = (double)(0-b-sqrt(tmp))/(2*a);
                if(x1<x2){
                    printf("%.2lf %.2lf
    ",x1,x2);
                }
                else {
                    printf("%.2lf %.2lf
    ",x2,x1);
                }
            }
        }
        return 0;
    }
     
    /**************************************************************
        Problem: 1103
        User: zpfbuaa
        Language: C++
        Result: Pending
    ****************************************************************/
  • 相关阅读:
    【持续更新】Java知识点整理-util
    【持续更新】Java知识点整理-基础
    【持续更新】Java知识点整理-JVM
    notepad++中写markdown
    VirtualBox中Alpine Linux + Docker安装记录
    Alpine Linux配置网络
    从编码的历史了解编码
    关于摄影器材的一些知识点
    服务器(Linux)上运行python总结
    命令行运行Python脚本时传入参数的三种方式
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/6822359.html
Copyright © 2020-2023  润新知