《【NOIP2000 基础】计算器的改良》 不全对题解

温馨提示,本题难度略大,本人写不出来正确代码,文章代码并不对,只是提供一些思路,希望大家能谅解!

目录

题目描述

输入描述

输出描述

解析

完整代码


描述

NCL是一家专门从事计算器改良与升级的实验室,最近该实验室收到了某公司所委托的一个任务:需要在该公司某型号的计算器上加上解一元一次方程的功能。实验室将这个任务交给了一个刚进入的新手ZL先生。为了很好的完成这个任务,ZL先生首先研究了一些一元一次方程的实例:

4+3x=8
6a-5+1=2-2a
-5+12y=0

编写程序,解输入的一元一次方程, 将解方程的结果(精确至小数点后三位)输出至屏幕。
你可假设对键入的方程的正确性的判断是由另一个程序员在做,或者说可认为键入的一元一次方程均为合法的,且有唯一实数解。
ZL先生被主管告之,在计算器上键入的一个一元一次方程中,只包含整数、小写字母及+、-、=这三个数学符号(当然,符号“─”既可作减号,也可作负号)。方程中并没有括号,也没有除号,方程中的字母表示未知数。

输入描述

一个一元一次方程。

输出描述

解方程的结果(精确至小数点后三位)。

用例输入 1 

6a-5+1=2-2a

用例输出 1 

a=0.750

提示

方程在计算过程中,不会出现超过int范围的整数
【来源】noip2000普及组复赛第1题

来源

noip复赛 字符

 

兄弟们啊,不是你们c++学不会,而是个别老师他教不对啊。

来,就这题你看着懵不懵,懵不懵!

看看老高平时是怎么教我的:

为了完成这个任务,我们需要解析一元一次方程,并求解未知数。以下是解决这个问题的步骤:

  1. 解析方程‌:将输入的字符串方程解析为左右两部分,并提取未知数和常数项。
  2. 整理方程‌:将方程整理为标准形式 ax+b=0。
  3. 求解未知数‌:根据整理后的方程求解未知数,并输出精确到小数点后三位的结果。

我们可以使用简单的字符串处理来实现这个目标。下面是一个可能的C++实现:

 

#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <iomanip>

using namespace std;

// 辅助函数:判断字符是否为运算符
bool isOperator(char c) {
    return c == '+' || c == '-';
}

// 辅助函数:判断字符是否为数字
bool isDigit(char c) {
    return isdigit(c);
}

// 辅助函数:从字符串中提取整数
int extractInt(const string& str, size_t& pos) {
    int num = 0;
    bool negative = false;
    
    // 检查负号
    if (str[pos] == '-') {
        negative = true;
        ++pos;
    }
    
    // 提取数字
    while (pos < str.size() && isDigit(str[pos])) {
        num = num * 10 + (str[pos] - '0');
        ++pos;
    }
    
    return negative ? -num : num;
}

// 辅助函数:提取方程的系数和常数项
void parseEquation(const string& equation, char& variable, double& lhsCoeff, double& lhsConst, double& rhsCoeff, double& rhsConst) {
    size_t pos = 0;
    bool lhs = true;  // 标志当前是在等式的左边还是右边
    
    while (pos < equation.size() && equation[pos] != '=') {
        if (isalpha(equation[pos])) {
            variable = equation[pos];
            // 紧接着的字符应该是运算符或者数字,否则格式不正确(假设输入总是合法的)
            if (pos + 1 < equation.size() && (isDigit(equation[pos + 1]) || isOperator(equation[pos + 1]))) {
                // 如果有运算符,说明有系数,否则系数为1
                if (isOperator(equation[pos + 1]) || pos + 1 == equation.size()) {
                    if (lhs) lhsCoeff = 1;  // 左边系数默认为1
                    else rhsCoeff = 1;      // 右边系数默认为1
                } else {
                    if (lhs) lhsCoeff = extractInt(equation, ++pos);
                    else rhsCoeff = extractInt(equation, ++pos);
                }
            }
        } else if (isDigit(equation[pos]) || isOperator(equation[pos])) {
            // 处理常数项
            int constant = extractInt(equation, pos);
            if (lhs) lhsConst += constant;
            else rhsConst += constant;
        }
        ++pos;
    }
    
    // 转换到等式右边
    if (pos < equation.size() && equation[pos] == '=') {
        lhs = false;
        ++pos;
    }
    
    // 处理等式右边
    while (pos < equation.size()) {
        if (isalpha(equation[pos])) {
            // 右边不应该再出现未知数,假设输入是合法的,所以不需要处理
        } else if (isDigit(equation[pos]) || isOperator(equation[pos])) {
            int constant = extractInt(equation, pos);
            rhsConst += constant;
        }
        ++pos;
    }
}

int main() {
    string equation;
    getline(cin, equation);
    
    char variable;
    double lhsCoeff = 0, lhsConst = 0;
    double rhsCoeff = 0, rhsConst = 0;
    
    parseEquation(equation, variable, lhsCoeff, lhsConst, rhsCoeff, rhsConst);
    
    // ax + b = cx + d  ->  (a-c)x = d-b
    double totalCoeff = lhsCoeff - rhsCoeff;
    double totalConst = rhsConst - lhsConst;
    
    // x = (d-b) / (a-c)
    double solution = totalConst / totalCoeff;
    cout << fixed << setprecision(3) << variable << "=" << solution << endl;
    
    return 0;
}

在这段代码中,isOperator 和 isDigit 是辅助函数,用于判断字符是否为运算符或数字。extractInt 函数用于从字符串中提取整数,考虑到了负号的情况。parseEquation 函数负责解析方程,将方程的系数和常数项提取出来。最后,在 main 函数中,我们读取输入的方程,调用 parseEquation 函数进行解析,然后计算并输出结果。

下面是去掉注释后的代码:

#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <iomanip>

using namespace std;

bool isOperator(char c) {
    return c == '+' || c == '-';
}

bool isDigit(char c) {
    return isdigit(c);
}

int extractInt(const string& str, size_t& pos) {
    int num = 0;
    bool negative = false;
    
    if (str[pos] == '-') {
        negative = true;
        ++pos;
    }
    
    while (pos < str.size() && isDigit(str[pos])) {
        num = num * 10 + (str[pos] - '0');
        ++pos;
    }
    
    return negative ? -num : num;
}

void parseEquation(const string& equation, char& variable, double& lhsCoeff, double& lhsConst, double& rhsCoeff, double& rhsConst) {
    size_t pos = 0;
    bool lhs = true;
    
    while (pos < equation.size() && equation[pos] != '=') {
        if (isalpha(equation[pos])) {
            variable = equation[pos];
            if (pos + 1 < equation.size() && (isDigit(equation[pos + 1]) || isOperator(equation[pos + 1]))) {
                if (isOperator(equation[pos + 1]) || pos + 1 == equation.size()) {
                    if (lhs) lhsCoeff = 1;
                    else rhsCoeff = 1;
                } else {
                    if (lhs) lhsCoeff = extractInt(equation, ++pos);
                    else rhsCoeff = extractInt(equation, ++pos);
                }
            }
        } else if (isDigit(equation[pos]) || isOperator(equation[pos])) {
            int constant = extractInt(equation, pos);
            if (lhs) lhsConst += constant;
            else rhsConst += constant;
        }
        ++pos;
    }
    
    if (pos < equation.size() && equation[pos] == '=') {
        lhs = false;
        ++pos;
    }
    
    while (pos < equation.size()) {
        if (isDigit(equation[pos]) || isOperator(equation[pos])) {
            int constant = extractInt(equation, pos);
            rhsConst += constant;
        }
        ++pos;
    }
}

int main() {
    string equation;
    getline(cin, equation);
    
    char variable;
    double lhsCoeff = 0, lhsConst = 0;
    double rhsCoeff = 0, rhsConst = 0;
    
    parseEquation(equation, variable, lhsCoeff, lhsConst, rhsCoeff, rhsConst);
    
    double totalCoeff = lhsCoeff - rhsCoeff;
    double totalConst = rhsConst - lhsConst;
    
    double solution = totalConst / totalCoeff;
    
    cout << fixed << setprecision(3) << variable << "=" << solution << endl;
    
    return 0;
}

结束了!

对了,忘说了一句话:

要想c++成绩好,就来jiabei小课堂

还有,点我主页,看我简介,别给那三个人点赞就完了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值