题目1103:二次方程计算器

java实现:

import java.util.Scanner;
import java.io.IOException;
import java.io.FileReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Formatter;

class Main
{
	
	public static final boolean DEBUG = false;
	
	public static void main(String[] args) throws IOException 
	{
		Scanner cin;
		Pattern p = Pattern.compile("(\\+|-|=)");
		String s;
		double a, b, c;
		boolean bEqual, bMinus;
		int loc1;
		
		if (DEBUG) {
			cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
		} else {
			cin = new Scanner(System.in); 
		}
		
		while (cin.hasNext()) {
			s = cin.next();
			a = b = c = 0;
			bEqual = bMinus = false;
			loc1 = 0;
			String sub;
			
			Matcher m = p.matcher(s);
			
			while (m.find()) {
				sub = s.substring(loc1, m.start());
				
				if (sub.indexOf('x') == -1) {
					if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
						if (sub.compareTo("") != 0) c += Integer.parseInt(sub);
					} else {
						if (sub.compareTo("") != 0)c -= Integer.parseInt(sub);
					}
				} else if (sub.indexOf('^') == -1) {
					if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
						if (sub.length() == 1) b += 1;
						else b += Integer.parseInt(sub.substring(0, sub.length() - 1));
					} else {
						if (sub.length() == 1) b -= 1;
						else b -= Integer.parseInt(sub.substring(0, sub.length() - 1));
					}
				} else {
					if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
						if (sub.length() == 3) a += 1;
						else a += Integer.parseInt(sub.substring(0, sub.length() - 3));
					} else {
						if (sub.length() == 3) a -= 1;
						else a -= Integer.parseInt(sub.substring(0, sub.length() - 3));
					}
				}
				
				if (m.group().compareTo("=") == 0) bEqual = true;
				else if (m.group().compareTo("-") == 0) bMinus = true;
				else bMinus = false;
				
				loc1 = m.end();
			}
			
			sub = s.substring(loc1, s.length());
			if (sub.indexOf('x') == -1) {
				if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
					if (sub.compareTo("") != 0) c += Integer.parseInt(sub);
				} else {
					if (sub.compareTo("") != 0) c -= Integer.parseInt(sub);
				}
			} else if (sub.indexOf('^') == -1) {
				if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
					if (sub.length() == 1) b += 1;
					else b += Integer.parseInt(sub.substring(0, sub.length() - 1));
				} else {
					if (sub.length() == 1) b -= 1;
					else b -= Integer.parseInt(sub.substring(0, sub.length() - 1));
				}
			} else {
				if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
					if (sub.length() == 3) a += 1;
					else a += Integer.parseInt(sub.substring(0, sub.length() - 3));
				} else {
					if (sub.length() == 3) a -= 1;
					else a -= Integer.parseInt(sub.substring(0, sub.length() - 3));
				}
			}
			
			if (b * b - 4 * a * c < 0) {
				System.out.println("No Solution");
			} else {
				double x1 = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
				double x2 = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
				
				if (x1 > x2) {
					double tmp = x1; 
					x1 = x2;
					x2 = tmp;
				}
				Formatter fmt= new Formatter();
				fmt.format("%.2f %.2f", x1, x2);
				System.out.println(fmt);
			}
		}
	}
}

C++实现:

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>

using namespace std;

int main()
{
    string s;
    string sop = "+-=";
    size_t loc1, loc2;
    float a, b, c;
    bool bEqual, bMinus;

#ifndef ONLINE_JUDGE
    ifstream cin("d:\\OJ\\uva_in.txt");
#endif // ONLINE_JUDGE

    while (cin >> s) {
        loc1 = -1;
        loc2 = 0;
        bEqual = false;
        bMinus = false;
        a = b = c = 0;

        while (loc2 < s.length()) {
            loc2 = s.find_first_of(sop, loc1 + 1);
            if (loc2 == string::npos) {
                loc2 = s.length();
            }

            string sub = s.substr(loc1 + 1, loc2 - loc1 - 1);
            //cout << "sub:" << sub << endl;

            if (sub.find('x') == string::npos) {
                if ((!bEqual && !bMinus) || (bEqual && bMinus))
                    c += atoi(sub.c_str());
                else
                    c -= atoi(sub.c_str());
            } else if (sub.find('^') == string::npos) {
                if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
                    if (sub.length() == 1) {
                        b += 1;
                    } else {
                        b += atoi(sub.c_str());
                    }
                } else {
                    if (sub.length() == 1) {
                        b -= 1;
                    } else {
                        b -= atoi(sub.c_str());
                    }
                }
            } else {
                if ((!bEqual && !bMinus) || (bEqual && bMinus)) {
                    if (sub.length() == 3) {
                        a += 1;
                    } else {
                        a += atoi(sub.c_str());
                    }
                } else {
                    if (sub.length() == 3) {
                        a -= 1;
                    } else {
                        a -= atoi(sub.c_str());
                    }
                }
            }

            loc1 = loc2;
            if (loc2 < s.length() && s.at(loc2) == '-') bMinus = true;
            else if (loc2 < s.length() && s.at(loc2) == '=') bEqual = true;
            else bMinus = false;

            //cout << "a:" << a << " b:" << b << " c:" << c << endl;
        }

        if (b * b - 4 * a * c < 0) {
            cout << "No Solution" << endl;
        } else {
            float result1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
            float result2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
            if (result1 > result2) swap(result1, result2);

            cout.precision(2);
            cout.setf(ios::fixed);
            cout << result1 << " " << result2 << endl;
        }
    }
    return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.m或d论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值