如何用stack类 模板 获取文件内容

naixian1983 2010-10-05 12:02:41
如何stack类 模板 template <class DataType> 要求自定义push()并获取以下文件数值


a.txt

9,50,16,934,100,101
8,45,34,450,99,34
3,44,21,349,854,222

stack.h





template <class DataType>
class stack
{
public:
stack(int size)
{
m_size = size;
m_current = -1 ; // initialize stack
m_data = new DataType[size] ;
}


int push(const DataType &data)
{
if (m_current<6)

{
m_current++;
m_data[m_current]=data;

return 1;

}

else
{
return 0;
}

void print()
{

cout<<"print data"<<endl;

for (int i=0;i<=m_current;i++)

{
cout<<m_data[i]<<";";

}
cout<<endl;
}

private:
int m_size ; // number of elements on Stack.
int m_current ;
DataType* m_data ;

}


main.cpp



void breakValue(char line[])
{
char *p;


p=strtok(line,",");
if (p)
{


cout<<p<<";"<<endl;
}
cout<<"AFTER break "<<endl;

while(p)
{
p=strtok(NULL,",");
if(p)
{
num=atof(p);
cout<<num<<"; ";
intVec.push(num);
}
}
}

void openfile()
{
int i=0;


inFile.open(m_str1.c_str());
if (!inFile)
{
cout << "Unable to open file";
}

if (getline(inFile,m_string))

{

while(inFile.getline(word,256))
{
cout<<"before break "<<word<<endl<<endl;
breakValue(word);


}


}

}




如何能获取每一行的第三个数值16,43 和21,并相加求值?

...全文
85 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lly212 2010-10-05
  • 打赏
  • 举报
回复
说简单确实简单 你数数pop就行了

没有通用性

遇到这种建议你用stl的map 键--值对应
qq120848369 2010-10-05
  • 打赏
  • 举报
回复
咱不知道你在干吗,描述清楚点.
tangxianghenggood 2010-10-05
  • 打赏
  • 举报
回复
文件搞定

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧