首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

fstream文件读取有关问题

2012-03-13 
fstream文件读取问题大家好,在一个文件里有2个一元多项式,用号分开,例如2x^2-12x-1-x+2我想用fstream里

fstream文件读取问题
大家好,

在一个文件里有2个一元多项式,   用;号分开,   例如   2x^2-12x-1;-x+2;

我想用fstream里的.peek()跟> > 来读出每个一元多项式的系数跟指数,   我想弄出下边的结果:

一元多项式   1:   2x^2-12x-1
第1项系数:2
第1项指数:2
第2项系数:-12
第2项指数:1
第3项系数:-1
第3项指数:0

一元多项式   2:   2x^2-12x-1
第1项系数:-1
第1项指数:1
第2项系数:2
第2项指数:0

我写了这些,   有高手能帮帮我吗?
ifstream   file;
file.open( "test.txt ");

int   num;
char   skip;

while(!file.eof())
{
if(file.peek()   ==   'x '   ||   file.peek()   ==   '^ ')
file   > >   skip;
else
{
file   > >   num;
cout   < <   num   < <   "\n ";
}
}

file.close();

最难的是判断如果x前边没有系数,   那么,   x的系数就是1或者-1,   还有如果x后边没有^那么他的指数就是1,   如果没有x,   那么指数就是0

希望高手能帮帮忙,   谢谢

[解决办法]
#include <sstream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
ifstream ifile( "test.txt ");
string line, str;
string::size_type index, start;
vector <int> coefficient, exponent;

getline(ifile, line);
while(!ifile.fail())
{
cout < < "读取自文件内容: " < <line < <endl;
start = 0;
index = line.find( "; ");
while(index != string::npos)
{
str = line.substr(start, index-start); //分解一元多项式
cout < <endl < < "一元多项式: " < <str < <endl;

coefficient.clear();
exponent.clear();
string::size_type i, s=0;
string element;
i=str.find_first_of( "+- ", 1);
while(s != string::npos)
{
element=str.substr(s, i-s); //分解 一元多项式 的各个项
string::size_type xpos=element.find( "x "); //寻找 x
if(xpos == string::npos) //没有 x 的项, 指数为 0, 该项为系数
{
istringstream tmp(element);
int num;
tmp> > num;
coefficient.push_back(num);
exponent.push_back(0);
}
else
{
if(xpos = 0) //x 在第一个字符,则系数为1,指数再分解
{
coefficient.push_back(1);
if(element.length()> 2 && element[xpos+1]== '^ ')
{
istringstream tmp(element.substr(xpos+2));
int num;
tmp> > num;
exponent.push_back(num);
}
else exponent.push_back(1);
}

else if(xpos=1 && element[0]== '- ')//x 在第二个字符,第一个字符为-
{
coefficient.push_back(-1);
if(element.length()> 3 && element[xpos+1]== '^ ')
{
istringstream tmp(element.substr(xpos+2));


int num;
tmp> > num;
exponent.push_back(num);
}
else exponent.push_back(1);
}
else //其他情况
{
if(element.length()> xpos+2)
{
istringstream tmp(element);
int num1, num2;
char c;
tmp> > num1> > c> > c> > num2;
coefficient.push_back(num1);
exponent.push_back(num2);
}
else
{
istringstream tmp(element);
int num;
tmp> > num;
coefficient.push_back(num);
exponent.push_back(1);
}
}
}

s = i;
i = str.find_first_of( "+- ", s+1);
}
//输出结果
int count=0;
for(count=0; count <coefficient.size(); count++)
cout < < "第 " < <count+1 < < "项的系数为: " < <coefficient[count] < < ",指数为: " < <exponent[count] < <endl;
cout < <endl < <endl;

start = index+1;
index = line.find( "; ", start);
}

getline(ifile, line);
}
system( "PAUSE ");
return 0;
}


test.txt 文件内容:
2x^2-12x-1;-x+2;
[解决办法]
修改了一下jixingzhong的答案,有些东西,比如说存在两行表达式中间一个空行的问题,这些按照楼主的要求实际上是不必要加上去的,jixingzhong的答案主要是漏了一个处理最后一个分号后内容的步骤
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>

using namespace std;

int main()
{
ifstream fin( "test.txt ");
vector <int> coefficient, exponent;

if(!fin)
{
cerr < < "Unable to open test.txt " < <endl;
return -1;
}

int start = 0;
int length;
int index;
string str;
string line;
getline(fin, line);
while(!fin.fail() )
{
if(line
start = 0;

length = line.length();
if(length == start) //防止两行表达式中间出现一个空行
{
getline(fin, line);
continue;
}
index = line.find( "; ");
//一行表达式中没有;
if(index == string::npos)
{
index = length;
}
bool first = false; //处理一行表达式中最后一个分号以后的内容
while(index != string::npos || first)
{
if(first == true)
{
index = length;
}
coefficient.clear();
exponent.clear();
str = line.substr(start, index - start);
//str就是一个式子
string::size_type i, s = 0;
string element;
i = str.find_first_of( "+- ", 1);
while(s != string::npos)
{
element = str.substr(s, i - s);
string::size_type xpos = element.find( "x ");
if(xpos == string::npos) //没有x
{
//指数为0,该项为系数
istringstream tmp(element);
int num;


tmp> > num; //将string类型的element转换成num
coefficient.push_back(num);
exponent.push_back(0);
}
else
{
if(xpos == 0) //x在第一个字符,则系数为1,
{
coefficient.push_back(1);
if(element.length() > 2 && element[xpos+1] == '^ ')
{
istringstream tmp(element.substr(xpos + 2));
int num;
tmp > > num;
exponent.push_back(num);
}
else
{
//指数为1
exponent.push_back(1);
}
}
else if(xpos == 1 && element[0] == '- ') //x在第二个字符,第一个字符为-
{
//系数为-1
coefficient.push_back(-1);
if(element.length() > 3 && element[xpos + 1] == '^ ')
{
istringstream tmp(element.substr(xpos + 2));
int num;
tmp > > num;

exponent.push_back(num);
}
else
{
exponent.push_back(1);
}
}
else
{
if(element.length() > xpos + 2)
{
istringstream tmp(element);
int num1, num2;
char c;
tmp> > num1> > c> > c> > num2;
coefficient.push_back(num1);
exponent.push_back(num2);
}
else
{
istringstream tmp(element);
int num;
tmp> > num;
coefficient.push_back(num);
exponent.push_back(1);
}
}
}
s = i;
i = str.find_first_of( "+- ", s + 1);
}
//输出结果
int count = 0;
for(count = 0; count < coefficient.size(); ++count)
cout < < "第 " < <count+1 < < "项的系数为: " < <coefficient[count] < < ",指数为: " < <
exponent[count] < <endl;
cout < <endl;

if(first == true)
break;
if(index == length)
break;
start = index + 1;
index = line.find( "; ", start);
if(index == string::npos)
{
if(first == false)
first = true;

}
}
getline(fin, line);
}

return 0;
}

热点排行