求教程序!!哪位给看看!关于链表与字符串的东东!
程序目的,读入一字符串,型如:1234,1234,0000,3445,0098;的东西(实际上是一长整数,4位数字一个','最后以';'结束)要求每4位数字存入双向循环链表的一个结点,实现长整数加法!
**********
我的问题是怎么实现从键盘读入数据???如果输入错误,就cout<<error,让其重输?
下面是我的一些代码,关于4位判断问题,觉得对,单运行123456,123132;不会error??
**********
程序有些乱,望大仙门帮忙看看,或者教我点这道题的新思路????????谢谢e!!
///计算长整数加法!***********
#include <iostream.h>
typedef struct LNode//定义结构变量
{
char data;
LNode* next;
}* LinkList;
LNode* create() //创建链表函数
{
LNode* ps;
LNode* pend;
LNode* head;
ps=new LNode;
head=NULL;
cin.get(ps->data);
try{
if ((ps->data!=','&&ps->data!=';')&&!(ps->data<='9'&&ps->data>='0')) throw 10;
pend=ps;
while((ps->data)!=';')//开始创建字符链表,';'后结束
{
if(head==NULL)
{
head=ps;
ps->next=NULL;
}
else
{
pend->next=ps;
ps->next=NULL;
}
pend=ps;
ps=new LNode;
cin.get(ps->data);
}//while
delete ps;
if (!head)
{
LNode* he;
he=head;
try{//try2
while (he) {
if(he->data==',')
for(int i=0;i<4;i++)
{
if (he->next->data<'0'||he->next->data>'9') throw 'a';
he=he->next;
}
he=he->next;
}
}//try2
catch (char) {
cout<<"error!"<<endl;
head=NULL;
return head;
}//catch
}//if
}//try
catch (int) {
cout<<"error!"<<endl;
head=NULL;
}
return head;
}
void show (LNode *head)//输出函数
{
cout<<endl;
cout<<"now the items of list are:\n";
if(!head)
cout<<" NULL"<<endl;
while (head)
{
cout<<head->data;
head=head->next;
}
cout<<endl<<endl;
}
void main()//主函数
{
cout<<" create a list:"<<endl;
LNode *la;
la=create();
show(la);
}
问题点数:0、回复次数:6Top
1 楼maxiaoo(小马)回复于 2003-11-01 15:44:58 得分 0
拜托了,谁给看看,好么?????Top
2 楼biduan(笔端)回复于 2003-11-01 15:46:16 得分 0
呵呵,偶调试过了
怎么没有什么错误啊?Top
3 楼plainsong(短歌)()回复于 2003-11-01 15:46:16 得分 0
这样的输入要求最好不要用>>操作符,应该用getline一次读入一行,然后分解开。
使用std::string类型,每次用getline输入一行。
用std::string::find寻找','和';',用std::string的构造函数生成子串。
用atoi和std::string::c_str()把子串转换为整数。Top
4 楼maxiaoo(小马)回复于 2003-11-01 15:54:33 得分 0
对std类不太熟悉,能不能讲的详细点???
最好能写段代码,ok????Top
5 楼buaaaladdin(阿拉丁的灯)回复于 2003-11-01 16:07:26 得分 0
同意 plainsong(短歌) 。
另外std是定义的标准名字空间。你如果用iostream.h版本的头文件,就不必考虑这些了。Top
6 楼maxiaoo(小马)回复于 2003-11-01 16:39:56 得分 0
楼上观点不对,我在#include <iostream.h>后
运行下面两个句子都不对,单msdn上的原例题确是可以的
std::cout << "Hello ";
using namespace std;
哪位高手给解释一下啊 ,为什么msdn上的代码我运行不了??
std到底怎么回事??Top




