没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Polynomial.cpp
#include "polynomial.h"
vector <Polynomial> poly_list;
bool isDouble(string str)
{
size_t len = str.size();
int i = 0;
//遍历字符串,如果有不是数字或者'.'号的字符就退出循环,因为字符串最后一个字符肯
定不是,所以不必
//担心越界
if (str == "0") {
return true;
}
if (str[i] == '-') {
i++;
}
while (std::isdigit(str[i]) || str[i] == '.')
{
//如果'.'号在首尾,直接返回 false
if (str[i] == '.' && (i == 0 || i == len - 1)) {
return false;
}
//如果首位为 0,但第二位不是'.'号,也直接返回 false,当然,可以允许数据前置 0
的写法,不过解析起来就比较麻烦
if (str[i] == '0' && i == 0) {
if (str[i + 1] != '.') {
return false;
}
}
++i;//蒋索引+1
}
//如果退出循环之后 i 索引指向的字符不是字符串结尾符,返回 false
if (i != len) {
return false;
}
return true;
}
bool serch_id(string poly_id) {
int list_size = poly_list.size();
for (auto i = poly_list.begin(); i != poly_list.end(); i++) {
//便利多项式列表判断 id 是否已经存在
if (i->getid() == poly_id) {
return 1;
}
}
return 0;
}
bool varlify_poly_string(string poly_str) {
vector <single_Poly> jduge; //用于暂时储存多项式的每一项
的参数 来判断是否有输入相同项的问题
int len = poly_str.size();
if (poly_str.find("=") == string::npos) { //如果没有找到等于
号,直接退出
return 0;
}
else {
string id = poly_str.substr(0, poly_str.find("=")); //如果名字
已经存在在多项式列表中,则要求重新输
if (id.empty()) {
return 0;
} else if (serch_id(id)) {
cout << "the name has existed" << endl;
return 0;
}
for (int i = poly_str.find("=") + 1; i < len; i++) {
if (poly_str[i] == '(') { //找到第一个左括
号和第一个右括号
int point1 = i;
int point2 = poly_str.find(')', i);
i = point2;
if (point2 == string::npos) { //如果没有找到
右括号的话,就退出
return 0;
}
else {
string single_poly;
single_poly = poly_str.substr(point1 + 1, point2 -
point1 - 1); //把括号里的,字符串提出来
int point3 = single_poly.find(',');
//找到逗号 把逗号左边的 和逗号右边的 字符串隔离出来 隔离出来的两个字符串则是数字
if (point3 == string::npos) {
return 0;
}
string xishu = single_poly.substr(0, point3);
string cifang = single_poly.substr(point3 + 1,
single_poly.size() - point3 - 1);
if (xishu.empty() || cifang.empty()) {
return 0;
}
if (!isDouble(xishu) || !isDouble(cifang)) {
//判断这两个字符串到底是不是数字
return 0;
}
else {
jduge.push_back({ stod(xishu), stod(cifang) });
//把多项式系数存入 jduge
}
}
}
else {
return 0;
}
}
sort(jduge.begin(), jduge.end(), compare);
//首先对多项式进行降幂排序
int len = jduge.size();
for (int x = 0; x < len - 1; x++) {
//判断相邻两项能力是否相同,如果相同的话 就说输入错误
if (jduge[x].power == jduge[x + 1].power) {
return 0;
}
}
return 1;
}
}
bool varlify_poly_string_no_id(string poly_str) {
int len = poly_str.size();
vector <single_Poly> jduge; //使用 jduge 用于暂时储存多项式的每
一项的参数 来判断是否有输入相同项的问题
for (int i = 0; i < len; i++) {
if (poly_str[i] == '(') { //找到第一个左括号和第一个右括号
int point1 = i;
int point2 = poly_str.find(')',i);
i = point2;
if (point2 == string::npos) { //如果没有找到右括号的话,就退
出
return 0;
}
else {
string single_poly;
single_poly = poly_str.substr(point1 + 1, point2 -
point1 - 1); //把括号里的,字符串提出来
int point3 = single_poly.find(',');
//找到逗号 把逗号左边的 和逗号右边的 字符串隔离出来 隔离出来的两个字符串则是数字
if (point3 == string::npos) {
return 0;
}
string xishu = single_poly.substr(0, point3);
string cifang = single_poly.substr(point3 + 1,
single_poly.size() - point3 - 1);
if (xishu.empty() || cifang.empty()) {
return 0;
}
if (!isDouble(xishu) || !isDouble(cifang)) {
//判断这两个字符串到底是不是数字
return 0;
}
else {
jduge.push_back({ stod(xishu), stod(cifang) }); //
把多项式系数存入 jduge
}
}
}
else {
return 0;
}
}
sort(jduge.begin(), jduge.end(), compare);
//首先对多项式进行降幂排序
int lentmp = jduge.size();
for (int x = 0; x < lentmp - 1; x++) {
if (jduge[x].power == jduge[x + 1].power) {
//判断相邻两项能力是否相同,如果相同的话 就说输入错误
return 0;
}
}
return 1;
}
void input_poly() {
string poly_str;
int flag = 0;
ifstream readIn;
readIn.open("Polynomial.txt"); //read every line from
"Polynomial.txt" until the end of file and add poly into polynomialList
for (int i = 0; ; i++) {
if (!getline(readIn, poly_str)) {
break;
}
string id = poly_str.substr(0, poly_str.find("="));
if (!serch_id(id)) {
Polynomial tmp(poly_str);
poly_list.push_back(tmp);
}
}
ofstream PolynomialRecord;
PolynomialRecord.open("Polynomial.txt", ofstream::app);
cout << "input polynomial you want to operate and input '0' to
finish input" << endl; //提供提示信息
cout << "the correct form is p=(1,2)(1,1) to indicate p = x^2+x"
<< "\n"
<< "and no space in anywhere unless you want waste your time"
<< endl;
while (true) { // to input polynomial
cin >> poly_str;
if (varlify_poly_string(poly_str)) {
//判断是不是带名字的多项式输入 是否合法
Polynomial tmp(poly_str);
poly_list.push_back(tmp); //如
果输入合法把输入的多项式放入 poly_list
PolynomialRecord << poly_str << endl; // add
poly into polynomialList
}
else if (poly_str == "0") { //如果输入为零就退出
break;
}
else {
cout << "please input correct form!such as 'p=(1,2)(1,1)'
to indicate p = x^2+x" << endl; //如果输入错误就继续输入 直到输入成
功,或者退出
cout << "input polynomial you want to operate and input '0'
to finish input" << endl;
}
}
readIn.close();
PolynomialRecord.close();
show_poly_list();
}
void add_operation() {
string poly1_str, poly2_str, jduge;
cout << "please input the first polynomial such as (1,2)(2,3) or
the name of polynomial which you had input and input 'e' to back to
menu" << endl;
cin >> poly1_str;
if (poly1_str == "e") { //判断是否为,退出操作 后面的 e 都代
表退出此模块,回到主页面 ,所以不予复述
return;
}
while (!varlify_poly_string_no_id(poly1_str) && !
serch_id(poly1_str)) { //判断判断输入的多项式是否合法,是否已经存在?
cout << "please input right polynomial such as (1,2)(2,3) or
the name of polynomial which you had input and input 'e' to back to
menu" << endl;
cin >> poly1_str;
if (poly1_str == "e") {
return;
}
}
cout << "please input the second polynomial such as (1,2)(2,3) or
the name of polynomial which you had input and input 'e' to back to
menu" << endl;
cin >> poly2_str;
if (poly2_str == "e") {
return;
}
剩余22页未读,继续阅读
资源评论
- xiaojie4492017-06-10非常好的东西,很高的学习价值
B站:阿里武
- 粉丝: 6539
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功