c++

xzhf0816 2008-11-04 08:55:46
#include"string.h"
#include"stdio.h"
#include"time.h"
#include"stdlib.h"
#define MAXSIZE 60
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int Status;
typedef struct std_info
{ int Num;
char Name[8];
float Score;
}ElemType;

typedef struct node
{ElemType data;
struct node *next;
}StackNode;
StackNode *Init_LinkStack(StackNode *top)
{top=NULL;
return top;
}
Status Empty_LinkStack(StackNode *top)
{return (top==NULL);}

StackNode *Push_LinkStack(StackNode *top,ElemType x)
{StackNode *p;
p=(StackNode *)malloc(sizeof(StackNode));
p->data=x;
p->next=top;
top=p;
return top;
}


StackNode *Pop_LinkStack(StackNode *top,ElemType *y)
{StackNode *p;
if (Empty_LinkStack(top))
{printf ("Stack Underflow.");
return OVERFLOW;}
else { *y=top->data;
p=top;
top=top->next;
free(p);
return OK;}
}

StackNode *Stacktop(StackNode *top,ElemType *y)
{if(Empty_LinkStack(top)) return ERROR;
*y=top->data;
printf("The Stack top is\n");
printf("num=%d,Name=%s,score=%f\n",y->data.Num,y->data.Name,y->data.Score);
return top;
}
void Print_LinkStack(StackNode *top)
{StackNode *p;
p=top;
printf("The stack is\n");
while(p!=NULL)
{printf("num=%d,name=%s,score=%f\n",p->data.Num,p->data.Name,p->data.Score);
p=p->next;
}
}
void main()
{int i,j=-1,nn;
float score;
char ss[8];
StackNode *top;
ElemType x,y;
top=Init_LinkStack(top);
while(j)
{printf("please choose operate\n");
printf("1:Push_LinkStack Operate\n");
printf("2:Pop_LinkStack Operate\n");
printf("3:Stacktop Opreate\n");
printf("0:No Opreate\n");
scanf("%d",&j);
switch(j)
{case 0:break;
case 1:top=Push_LinkStack(top,x); break;
case 2:top=Pop_LinkStack(top,&y); break;
case 3:top=Stacktop(top,&y); break;
default:printf("输入错误!,请重新输入!\n");
Print_LinkStack(top);}
}
}
...全文
100 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzhf0816 2008-11-06
  • 打赏
  • 举报
回复
我就是不知道错在那里咯
它反映的就是printf("num=%d,Name=%s,score=%f\n",y->data.Num,y->data.Name,y->data.Score); 这一段出错了
fcwenmingxing001 2008-11-05
  • 打赏
  • 举报
回复
我晕,
我也扔!!!

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

bool note=false;//标识是否是注释
ofstream ofile("out.txt");
char *p;
char Test()
{
if(*p>='a'&&*p<='z'||*p>='A'&&*p<='Z'||*p=='_')
return 'w';//为字母
else if(*p>='0'&&*p<='9')
return 'n';//为数字
else if(*p==' '||*p=='\t')//空格
return ' ';
else if(*p==','||*p==';'||*p=='('||*p==')'||*p=='{'||*p=='}'||*p=='['||*p==']')
return 'b';//界符
else if(*p=='\"')
return '\"';//双引号; 用于检测字符串
else if(*p=='\'')//单引号;用于检测字符
return '\'';
else
return 'y';//运算符;
}
bool Advanced(char *ad)
{
char temp[3];
temp[0]=p[0];
temp[1]=p[1];
temp[2]='\0';
if(strcmp(temp,ad)==0)
{
p++;
p++;//指针下移
return true;
}
else
return false ;
}
void Word()
{
char t[2];
char temp[20],cmp[32][10]={"char","int","float","double","signed","unsigned","short","long",
"struct","union","enum","void","typedef","sizeof","const","volatile",
"static","extern","auto","register","if","else","switch","case","default",
"do","while","for","break","continue","return","goto"};
int i=31;
temp[0]=p[0];
temp[1]=t[1]='\0';
p++;
while(Test()=='w'||Test()=='n')//w表示为字母;
{
t[0]=p[0];
strcat(temp,t);
p++;
}
while(strcmp(temp,cmp[i])!=0&&i>=0)
i--;
if(i<0)
ofile<<"("<<temp<<",2)"<<endl;
else
ofile<<"("<<temp<<",1)"<<endl;
}

void Number()
{
ofile<<"(";
while(Test()=='n'||*p=='.')
{
ofile<<*p++;
}
ofile<<",3)"<<endl;
}

void Bound()
{
ofile<<"("<<*p++<<",5)"<<endl;
}

void String(char b)
{
Bound();
ofile<<"(";
while(Test()!=b)
ofile<<*p++;
ofile<<",3)"<<endl;
Bound();
}
void Move()
{
while(*p!='\0')
if(!Advanced("*/"))
p++;
else
{
note=false;
return;
}
}
void Operator()
{
if(Advanced("/*"))
{
note=true;// /*注释*/
Move();
}
else
{
switch(*p)
{
case '.':
ofile<<"(.,4)"<<endl;p++;
break;
case '*':
if(Advanced("*="))
ofile<<"(*=,4)"<<endl;
else {ofile<<"(*,4)"<<endl;p++;}
break;
case '-':
if(Advanced("->"))
ofile<<"(->,4)"<<endl;
else if(Advanced("-="))
ofile<<"(-=,4)"<<endl;
else {ofile<<"(-,4)"<<endl;p++;}
break;
case '%':
if(Advanced("%="))
ofile<<"(%=,4)"<<endl;
else {ofile<<"(%,4)"<<endl;p++;}
break;
case '+':
if(Advanced("+="))
ofile<<"(+=,4)"<<endl;
else {ofile<<"(+,4)"<<endl;p++;}
break;
case '<':
if(Advanced("<="))
ofile<<"(<=,4)"<<endl;
else {ofile<<"(<,4)"<<endl;p++;}
break;
case '>':
if(Advanced(">="))
ofile<<"(>=,4)"<<endl;
else {ofile<<"(>,4)"<<endl;p++;}
break;
case '=':
if(Advanced("=="))
ofile<<"(==,4)"<<endl;
else {ofile<<"(=,4)"<<endl;p++;}
break;
case '!':
if(Advanced("!="))
ofile<<"(!=,4)"<<endl;
else {ofile<<"(!,4)"<<endl;p++;}
break;
case '&':
if(Advanced("&&"))
ofile<<"(&&,4)"<<endl;
else {ofile<<"(&,4)"<<endl;p++;}
break;
case '^':
ofile<<"(^,4)"<<endl;p++;
break;
case '|':
if(Advanced("||"))
ofile<<"(||,4)"<<endl;
else {ofile<<"(|,4)"<<endl;p++;}
break;
case '?':
ofile<<"(?,4)"<<endl;p++;
break;
case ':':
ofile<<"(:,4)"<<endl;p++;
break;
case '/':
if(Advanced("/="))
ofile<<"(/=,4)"<<endl;
else {ofile<<"(/,4)"<<endl;p++;}
break;
default:
ofile<<"不可识别的符号:"<<*p++<<endl;
}
}
}
int main()
{
ifstream ifile("a.txt");
char buffer[256];
char re;
p=buffer;
while(!ifile.eof())
{
ifile.getline(p,'255');
while(*p!='\0')
{
re=Test();
if(!note)
{
switch(re)
{
case 'w'://字母
Word();
break;
case 'n'://数字
Number();
break;
case 'b'://界符 其中不包含 双引号 和单引号
Bound();
break;
case '\"':
String('\"');
break;
case '\'':
String('\'');
break;
case'y'://运算符
Operator();//在operator中检测是否是 /* ------*/ 注释
break;
default://空格
p++;
}
}
else
Move();//跳过注释
}
}
return 0;
}
xzhf0816 2008-11-05
  • 打赏
  • 举报
回复
忘记打了
找一下错误
看一下上面有什么错误
herman~~ 2008-11-05
  • 打赏
  • 举报
回复
mark看不懂呀
tdhoter 2008-11-05
  • 打赏
  • 举报
回复
@@@@@!!!
firstdad 2008-11-05
  • 打赏
  • 举报
回复
~~~~~!!@@@@@@@
hznat 2008-11-04
  • 打赏
  • 举报
回复
扔一堆代码出来是做什么的???
jintianfree 2008-11-04
  • 打赏
  • 举报
回复
哪里有问题提出来呀
jintianfree 2008-11-04
  • 打赏
  • 举报
回复
帮你帖一下,这样看起来才舒服



#include"string.h"
#include"stdio.h"
#include"time.h"
#include"stdlib.h"
#define MAXSIZE 60
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int Status;
typedef struct std_info
{ int Num;
char Name[8];
float Score;
}ElemType;

typedef struct node
{ElemType data;
struct node *next;
}StackNode;
StackNode *Init_LinkStack(StackNode *top)
{top=NULL;
return top;
}
Status Empty_LinkStack(StackNode *top)
{return (top==NULL);}

StackNode *Push_LinkStack(StackNode *top,ElemType x)
{StackNode *p;
p=(StackNode *)malloc(sizeof(StackNode));
p->data=x;
p->next=top;
top=p;
return top;
}


StackNode *Pop_LinkStack(StackNode *top,ElemType *y)
{StackNode *p;
if (Empty_LinkStack(top))
{printf ("Stack Underflow.");
return OVERFLOW;}
else { *y=top->data;
p=top;
top=top->next;
free(p);
return OK;}
}

StackNode *Stacktop(StackNode *top,ElemType *y)
{if(Empty_LinkStack(top)) return ERROR;
*y=top->data;
printf("The Stack top is\n");
printf("num=%d,Name=%s,score=%f\n",y->data.Num,y->data.Name,y->data.Score);
return top;
}
void Print_LinkStack(StackNode *top)
{StackNode *p;
p=top;
printf("The stack is\n");
while(p!=NULL)
{printf("num=%d,name=%s,score=%f\n",p->data.Num,p->data.Name,p->data.Score);
p=p->next;
}
}
void main()
{int i,j=-1,nn;
float score;
char ss[8];
StackNode *top;
ElemType x,y;
top=Init_LinkStack(top);
while(j)
{printf("please choose operate\n");
printf("1:Push_LinkStack Operate\n");
printf("2:Pop_LinkStack Operate\n");
printf("3:Stacktop Opreate\n");
printf("0:No Opreate\n");
scanf("%d",&j);
switch(j)
{case 0:break;
case 1:top=Push_LinkStack(top,x); break;
case 2:top=Pop_LinkStack(top,&y); break;
case 3:top=Stacktop(top,&y); break;
default:printf("输入错误!,请重新输入!\n");
Print_LinkStack(top);}
}
}

65,211

社区成员

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

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