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

POJ1028总是RE,向大家请问一上

2012-08-15 
POJ1028总是RE,向大家请教一下我的代码如下:[codeC/C++][/code]/*1028 Web Navigation*/#includestdio.h

POJ1028总是RE,向大家请教一下
我的代码如下:
[code=C/C++][/code]
/*1028 Web Navigation*/
#include<stdio.h>
#include<string.h>
void getcommand(char [][200]);/*函数用于获得命令*/ 
void run(char [][200]);/*执行的主要函数*/ 
char command[200][200];
int main(void)
{
  getcommand(command);
  run(command);

void getcommand(char command[][200])
{
  int c,i,j;
  for(i = 0;;i++){
  for(j = 0;(c = getchar()) != 0 &&c != '\n';j++)
  command[i][j] = c;
  if(c == '\n'){
  command[i][j] = c;
  j++;
  }
  command[i][j] = '\0';  
  if(strcmp(command[i],"QUIT\n") == 0)
  return;
  } 
}
char *back[200],*forward[200];
char *current = "http://www.acm.org/\n";
int bpp = 0,fpp = 0;
void backpush(void);
void forpush(void);
void forpop(void);
void backpop(void); 
int isforempty(void);
int isbackempty(void);
void run(char command[][200])
{
  int i;
  for(i = 0;i < 200;i++){
  if(strcmp(command[i],"QUIT\n") == 0)
  break;
  else if(strcmp(command[i],"FORWARD\n") == 0){
  if(isforempty())
  printf("Ignored\n");
  else{
  backpush();
  forpop();
  printf("%s",current);
  }
  }else if(strcmp(command[i],"BACK\n") == 0){
  if(isbackempty())
  printf("Ignored\n");
  else{
  forpush();
  backpop();
  printf("%s",current);
  }
  }else if(command[i][0] == 'V'){
  backpush();
  current = &command[i][6];
  fpp = 0;
  printf("%s",current);
  }
  }  
}
void backpush(void)
{
  back[bpp++] = current;
}
void forpush(void)
{
  forward[fpp++] = current;
}
void backpop(void)
{
  current = back[--bpp];
}
void forpop(void)
{
  current = forward[--fpp];
}
int isforempty(void)
{
  return (fpp == 0);  
}
int isbackempty(void)
{
  return (bpp == 0);  
}


[解决办法]

C/C++ code
#include <stdio.h>#include <string.h>#define STACK_SIZE 100typedef struct {    char pages[100][71];    char (*top)[71];} Stack;/* Push element to the stack: */void Push(Stack* stack, const char* element){    strcpy(*stack->top++, element);}/* Pop the element from the stack: */char* Pop(Stack* stack){    return stack->top == stack->pages ? 0 : *--stack->top;}int main(void){    /* Initialize the current page at the first page: */    char currentPage[71] = "http://www.acm.org/";    char page[71];    char* popPage;    Stack backward, forward;    char command[8];    /* Initialize stacks: */    backward.top = backward.pages;    forward.top = forward.pages;    /* Read commands and URLs*/    for (scanf("%s", command);         strcmp(command, "QUIT");         scanf("%s", command))    {        if (!strcmp(command, "VISIT")) {            scanf("%s", page);            Push(&backward, currentPage);            puts(strcpy(currentPage, page));            forward.top = forward.pages;        } else if (!strcmp(command, "BACK")) {            if (popPage = Pop(&backward)) {                Push(&forward, currentPage);                puts(strcpy(currentPage, popPage));            } else                puts("Ignored");        } else { /* FORWARD: */            if (popPage = Pop(&forward)) {                Push(&backward, currentPage);                puts(strcpy(currentPage, popPage));            } else                puts("Ignored");        }    }    return 0;} 

热点排行