题解 | #二次方程计算器#

二次方程计算器

https://www.nowcoder.com/practice/071f1acaada4477f94193f8c0b9054f4

//将所有变量和常量都移到等式左边,得到aX^2+bX+c=0,在用公式做
#include "stdio.h"
#include "math.h"
#include "string"
#include "iostream"
#include "algorithm"
using namespace std;
string str;

int calculate(int pos){//计算x^2,x前的数字(num1,num2)的位数
    int count = 0;
    for (int i = pos-1; i >= 0; --i) {
        if (str[i] >= '0' && str[i] <= '9'){
            ++count;
            continue;
        } else
            break;
    }
    ++count;
    return count;
}

int calculate2(int pos){//计算普通数字num3的位数
    int count = 0;
    for (int i = pos; str[i] >= '0' && str[i] <= '9'; ++i) {
        ++count;
    }
    return count;
}

int main(){
    ;int sum1=0,sum2=0,sum3=0;//sum1为x^2系数,sum2为x系数,sum3为常数(都移到方程左侧)
    while (getline(cin,str)){
        /*
         * 对str初始化,对x,x^2变成1x,1x^2,开头的再变成+/-1x,+/-x^2,相当于规格化,便于后续处理
         */
        for (int i = 0; i < str.size(); ++i) {//加1
            if (str[i] == 'x'){
                if (str[i-1] == '+' || str[i-1] == '-' || str[i-1] == '=' || i==0){
                    str.insert(i,"1");
                    continue;
                }
            }
            if (str[i] > '0' && str[i] <= '9'){
                if (str[i-1] == '=' || i == 0){
                    str.insert(i,"+");
                    continue;
                }
            }
        }
        if (str[1] == 'x')//加"+"
            str.insert(0,"+");
        int pos_equal = str.find("=");
        if (str[pos_equal+2] == 'x')
            str.insert(pos_equal+1,"+");
        while (str.find("x^2")!=string::npos){//算出sum1 ->a
            int pos = str.find("x^2");
            int count = calculate(pos);
            if (pos < pos_equal){
                sum1 += stoi(str.substr(pos-count,count));
                str.erase(pos-count,count+3);
            } else{
                sum1 -= stoi(str.substr(pos-count,count));
                str.erase(pos-count,count+3);
            }
            pos_equal = str.find("=");
        }
        while (str.find("x") != string::npos){//算sum2 ->b
            int pos = str.find("x");
            int count = calculate(pos);
            if (pos < pos_equal){
                sum2 += stoi(str.substr(pos-count,count));
                str.erase(pos-count,count+1);
            } else{
                sum2 -= stoi(str.substr(pos-count,count));
                str.erase(pos-count,count+1);
            }
            pos_equal = str.find("=");
        }
        for (int i = 0; i < str.size(); ++i) {//算num3
            if (str[i] > '0' && str[i] <= '9'){
                int count = calculate2(i);
                if (i < pos_equal){
                    sum3 += stoi(str.substr(i-1,count+1));
                    str.erase(i-1,count+1);
                } else{
                    sum3 -= stoi(str.substr(i-1,count+1));
                    str.erase(i-1,count+1);
                }
                pos_equal = str.find("=");
            }
        }
        double temp = sum2*sum2*1.0-4*sum1*sum3;
        if (temp<0)
            printf("No Solution\n");
        else{
            double x = (-sum2 - sqrt(temp))/(2*sum1);
            double y = (-sum2 + sqrt(temp))/(2*sum1);
            if (x > y)
                swap(x,y);
            printf("%.2llf %.2llf\n",x,y);
        }
    }
}

全部评论

相关推荐

10-15 03:05
门头沟学院 Java
CADILLAC_:凯文:我的邮箱是死了吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务