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

哪位高手能帮小弟我看看这代码错哪了。

2012-04-19 
谁能帮我看看这代码哪里错了。。。#includestdio.h#includestdlib.h /*标准函数库*/#includeconio.h/*

谁能帮我看看这代码哪里错了。。。
#include<stdio.h>
#include<stdlib.h> /*标准函数库*/
#include<conio.h>/*屏幕操作函数*/
#define LEN sizeof(struct txt)

void newtxt();/*新建文件*/
void displaytxt();/*显示文件*/
void modifytxt();/*修改数据*/

void main()
{  
struct txt{
int num;
char word;
struct txt * next;
};
  int c;
printf("*************菜单**************\n");
printf("1.新建文件 \n");
printf("2.显示数据\n");  
printf("3.修改数据\n");  
printf(" 4.退出(任何情况下按Ctrl+C退出程序)\n");
 
do
{  
printf("\n\t\t 输入你的选择(1-4):");
scanf("%d",&c);
}while(c<0||c>4);
switch(c)
{
case 1: system("cls"); newtxt();break;
case 2: system("cls"); displaytxt();break;
case 3: system("cls"); modifytxt();break;
case 4: exit(0);
}
}
void newtxt()/*新建文件*/
{
printf("请输入文件名:");
char name[32];
scanf("%s",name);
system("cls");
printf("请输入数据,输入#号键后按回车结束\n");
FILE *fp;
fp=fopen(name,"w");

char ch;
while((ch=getchar())!='#')
{
fputc(ch,fp);
}
fputc('#',fp);
fclose(fp);

printf("\n\t\t按任意键返回......\n");
getch();
system("cls"); /*清屏*/
return;

void displaytxt()/*显示文件*/
{
printf("请输入文件名:");
char name[32];
scanf("%s",name);
system("cls");
FILE *fp;
fp=fopen(name,"r");

char ch;
while((ch=fgetc(fp))!='#')
{
putchar(ch);
}
fclose(fp);

printf("\n\t\t\t按任意键返回......\n");
getch();
system("cls"); /*清屏*/
return;
}
void modifytxt()

{ struct txt{
int num;
char word;
struct txt * next;
};
  struct txt * del(struct txt *head);
struct txt * insert(struct txt *head);
struct txt * replace(struct txt *head); 
   
printf("请输入文件名:");
char name[32],ch;
scanf("%s",name);
system("cls");
FILE *fp;
fp=fopen(name,"r");
struct txt *head;
struct txt*p1,*p2,*p; 
int n=0; 
head=NULL;
p1=p2=(struct txt *)malloc(LEN);
p1->num=0; 
  while((ch=fgetc(fp))!='#') 
{n++;
p1->num++;
p1->word=ch;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct txt *)malloc(LEN);
}
 
p1->num++;
p1->word='#';
p2=p1;
p2->next=NULL;
fclose(fp);
int j,i;
   



p=head;
for(j=1;j<n;j++)
printf("%c",p->word);
printf("请选择选项:\n");
printf("1.删除;\n");  
printf("2.插入;\n");
printf("3.替换;\n");
printf("4.退出;\n");
scanf("%d",&i);
switch(i)
{
case '1':system("cls");struct txt * del(struct txt *head);n=n-1;break;

case '2':system("cls");struct txt * insert(struct txt *head);n=n+1;break;
 
case '3':system("cls");struct txt * replace(struct txt *head);break; 
 
case '4':system("cls");exit(0);
}
 
printf("请输入文件名:");
scanf("%s",name);
system("cls");
FILE *fp1;
fp1=fopen(name,"w");
p1=head;


  while((ch=p1->word)!='#')
{
fputc(ch,fp);
p1=p1->next;
}
fclose(fp1);
return;
}

struct txt * del(struct txt *head)
{
long del_num;
struct txt * p1,* p2;
printf("请输入要删除第几个节点");
scanf("%d",&del_num);
if(head==NULL) 
{printf("该文件是空文件");return(head);};
  p1=head;
  while(del_num!=(p1->num) && (p1->next)!=NULL)
{
p2=p1;
  p1=p1->num;
}
if(del_num==p1->num)
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("%d点已经删除",del_num);

}
else printf("无法找到该节点");
return(head);
}



struct txt * insert(struct txt *head)
{ long insert_num;
printf("请输入要插入到节点之前还有要插入的值");
scanf("%d",&insert_num);
scanf("%d,%c",&stu->num,&stu->word);
struct txt *p0,*p1,*p2;
  p1=head;
  p0=stu;
if(head==NULL)
{head=p0;p0->next=NULL}
else 
{
while((insert_num>p->num)&&(p1->!=NULL))
{
p2=p1;
  p1=p1->next;
}
if(insert_num<=p1->num)
{
if(head==p1) head=p0;
  else 
  p2->next=p0; 
p0->next=p1;
}
else
{
p1->next=p0;
  p0->next=NULL;
}
}

return(head);
}
struct txt * replace(struct txt *head)
  {long replace_num;
printf("请输入要替换的节点");
scanf("%d",replace_num);
scanf("%d,%c",&stu->num,&stu->word);
struct txt *p0,*p1,*p2;
p1=head;
p0=stu;
if(head==NULL)
{head=p0;p0->next=NULL}
else 
{
while((replace_num>p->num)&&(p1->!=NULL))
{
p2=p1;
  p1=p1->next;

if(replace_num==p1->num)
  p1=stu;
  else
  printf("无法找到该节点");
  }
return(head);
}







[解决办法]
错误有点多,先把struct txt的定义放在main外面再调把。你定义在main里面是局部变量。
外面看不到的。另外楼主还是多熟悉一下语法
[解决办法]
帮顶,太长了,我急性子,
[解决办法]
你这错误太多了 不懂你的逻辑 懒得看啊

C/C++ code
#include<stdio.h>#include<stdlib.h> /*标准函数库*/#include<conio.h>/*屏幕操作函数*/#define LEN sizeof(struct txt)void newtxt();/*新建文件*/void displaytxt();/*显示文件*/void modifytxt();/*修改数据*/ struct txt{    int num;    char word;    struct txt * next;};void main(){    int c;    printf("*************菜单**************\n");    printf("1.新建文件 \n");    printf("2.显示数据\n");       printf("3.修改数据\n");       printf(" 4.退出(任何情况下按Ctrl+C退出程序)\n");    do    {           printf("\n\t\t 输入你的选择(1-4):");        scanf("%d",&c);    }while(c<0||c>4);    switch(c)    {    case 1: system("cls"); newtxt();break;    case 2: system("cls"); displaytxt();break;    case 3: system("cls"); modifytxt();break;    case 4: exit(0);    }} void newtxt()/*新建文件*/{    printf("请输入文件名:");    char name[32];    scanf("%s",name);    fflush(stdin);    system("cls");    printf("请输入数据,输入#号键后按回车结束\n");    FILE *fp;    fp=fopen(name,"w");    char ch;    while((ch=getchar())!='#')    {        fputc(ch,fp);    }    fputc('#',fp);    fclose(fp);    printf("\n\t\t按任意键返回......\n");    getch();    fflush(stdin);    system("cls"); /*清屏*/    return;}   void displaytxt()/*显示文件*/{    printf("请输入文件名:");    char name[32];    scanf("%s",name);    fflush(stdin);    system("cls");     FILE *fp;    fp=fopen(name,"r");    char ch;    while((ch=fgetc(fp))!='#')    {        putchar(ch);     }    fclose(fp);    printf("\n\t\t\t按任意键返回......\n");    getch();    system("cls"); /*清屏*/    fflush(stdin);    return;}void modifytxt(){     struct txt * del(struct txt *head);    struct txt * insert(struct txt *head);    struct txt * replace(struct txt *head);       printf("请输入文件名:");    char name[32],ch;    scanf("%s",name);    fflush(stdin);    system("cls");     FILE *fp;    fp=fopen(name,"r");    struct txt *head;    struct txt *p1,*p2,*p;      int n=0;      head=NULL;    p1=p2=(struct txt *)malloc(LEN);    p1->num=0;      while((ch=fgetc(fp))!='#')      {        n++;        p1->num++;        p1->word=ch;        if(n==1)            head=p1;        else            p2->next=p1;        p2=p1;        p1=(struct txt *)malloc(LEN);    }     p1->num++;    p1->word='#';    p2=p1;    p2->next=NULL;    fclose(fp);    int j,i;     p=head;    for(j=1;j<n;j++)        printf("%c",p->word);    printf("请选择选项:\n");    printf("1.删除;\n");       printf("2.插入;\n");     printf("3.替换;\n");     printf("4.退出;\n");     scanf("%d",&i);    switch(i)    {    case '1':system("cls");struct txt * del(struct txt *head);n=n-1;break;    case '2':system("cls");struct txt * insert(struct txt *head);n=n+1;break;    case '3':system("cls");struct txt * replace(struct txt *head);break;       case '4':system("cls");exit(0);    }    printf("请输入文件名:");    scanf("%s",name);    system("cls");     FILE *fp1;    fp1=fopen(name,"w");    p1=head;    while((ch=p1->word)!='#')    {        fputc(ch,fp);        p1=p1->next;    }    fclose(fp1);    return;}struct txt * del(struct txt *head) {    long del_num;    struct txt * p1,* p2;    printf("请输入要删除第几个节点");    scanf("%d",&del_num);    if(head==NULL)      {        printf("该文件是空文件");        return(head);    }    p1=head;     while(del_num!=(p1->num) && (p1->next)!=NULL)    {        p2=p1;        p1=p1->next;     }     if(del_num==p1->num)    {        if(p1==head)             head=p1->next;        else            p2->next=p1->next;        printf("%d点已经删除",del_num);    }    else         printf("无法找到该节点");        return(head);}struct txt * insert(struct txt *head){     long insert_num;    struct txt *stu = (struct txt *) malloc( sizeof(struct txt));    printf("请输入要插入到节点之前还有要插入的值");    scanf("%d",&insert_num);    scanf("%d,%c",&stu->num,&stu->word);    struct txt *p0,*p1,*p2;    p1=head;    p0=stu;     if(head==NULL)    {        head=p0;        p0->next=NULL;    }    else      {        while((insert_num > p1->num)&&(p1->next!=NULL))        {            p2=p1;            p1=p1->next;         }         if(insert_num<=p1->num)        {            if(head==p1)                head=p0;            else                  p2->next=p0;               p0->next=p1;        }         else        {            p1->next=p0;            p0->next=NULL;        }     }     return(head);}struct txt * replace(struct txt *head) {    long replace_num;    struct txt *stu = (struct txt *) malloc( sizeof(struct txt));    printf("请输入要替换的节点");    scanf("%d",&replace_num);    scanf("%d,%c",&stu->num,&stu->word);    struct txt *p0,*p1,*p2;    p1=head;    p0=stu;     if(head==NULL)    {        head=p0;        p0->next=NULL;    }    else      {        while((replace_num>p1->num)&&(p1->next!=NULL))        {            p2=p1;            p1=p1->next;         }           if(replace_num==p1->num)            p1=stu;        else            printf("无法找到该节点");    }     return(head);} //没有编译错误至于逻辑有没有错就不知道了 自己检查吧 


[解决办法]
自己调试一下,迟早你要学会的
[解决办法]
对头,只有经过了几次痛苦的调试修改的过程,你就会进步的:)
[解决办法]

C/C++ code
do{   printf("\n\t\t 输入你的选择(1-4):");scanf("%d",&c);}while(c<0||c>4);switch(c){case 1: system("cls"); newtxt();break;case 2: system("cls"); displaytxt();break;case 3: system("cls"); modifytxt();break;case 4: exit(0);}
[解决办法]
程序太长了,错误提示呢?可以看提示改的,学会调试吧

热点排行