| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1153 人关注过本帖
标题:这样调用函数正确么?
只看楼主 加入收藏
maiiho
Rank: 1
等 级:新手上路
帖 子:94
专家分:7
注 册:2008-7-17
结帖率:50%
收藏
 问题点数:0 回复次数:16 
这样调用函数正确么?
红色部分.
cx()                               /*查询函数*/
{
    struct xx XX;
    FILE *fp;
    int flag;
    int total;
    char ch[20];
    int b;
    int a;

    if((fp=fopen("xinxi.txt","rb"))==NULL)
      {
         printf("错误!不能打开记录文件(xinxi.txt)!");
         getch();
         main();         }

      rewind(fp);
      system("cls");
      printf("( 1.按姓名查询 || 2.按电话查询 || 3.返回 )---->[ ]\b\b");
      scanf("%d",&flag);
      getchar();

      if(flag==1)                  
      {
        printf("输入姓名查询:");
        gets(ch);
        total=0;
        while(fread(&XX,sizeof(XX),1,fp)==1)
           if(strcmp(ch,XX.xingming)==0)
             {
               total++;
               if(total==1)
      {
                  printf("\n姓名:%s",XX.xingming);
             printf("姓别:%s\n",XX.xingbie);
                  printf("电话:%s\n",XX.dianhua);
                    printf("QQ  :%s\n",XX.qq);
                     printf("E-M :%s\n\n",XX.email);
            }
               else if(total!=1)
               {
                   system("cls");
                   printf("找不到!重新查找吗?----->1.好    2.不   3.退出\n选择:");
                   scanf("%d",&a);
                   if(a==1)
                   {
                       cx();
                   }
                   if(a==2)
                   {
                       main();
                   }       
                   if(a==3)
                   {
                       exit(1);
                   }
               }
           }
               printf("1.继续||2.返回||3.退出-->[ ]\b\b");
               scanf("%d",&b);
               if(b==1)
               {
                 cx();
                 }
                 else if(b==2)
                 {
                 system("cls");
                 main();
                 }
                 else
                 {
                    exit(1);
                 }
                 while(b<0||b>3)
                 {
                     printf("请正确输入!");
                     getch();
                     cx();
                 }    
           }
       if(flag==2)                  
         {
           printf("输入电话查询:");
           gets(ch);
           total=0;
           while(fread(&XX,sizeof(XX),1,fp)==1)
             if(strcmp(ch,XX.dianhua)==0)
             {
               total++;
               if(total==1)
               {
                    printf("\n姓名:%s\n",XX.xingming);
                    printf("姓别:%s\n",XX.xingbie);
                       printf("电话:%s\n",XX.dianhua);
                    printf("QQ  :%s\n",XX.qq);
                     printf("E-M :%s\n\n",XX.email);
               }
               }

               else
               {
               printf("\n请正确选择!\n\n");
               getch();
               cx();
               }
               fclose(fp);
                 }
       }
搜索更多相关主题的帖子: 函数 
2008-09-09 09:52
maiiho
Rank: 1
等 级:新手上路
帖 子:94
专家分:7
注 册:2008-7-17
收藏
得分:0 
如果错误的话,那么正确的方式是什么样的?
我这样做,带来的不好的影响是什么  
 = =
2008-09-09 09:53
forever74
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:CC
等 级:版主
威 望:58
帖 子:1696
专家分:4287
注 册:2007-12-27
收藏
得分:0 
影响就是不能通过编译。

就你的上下文,回到主函数应该使用return
重新查询应该使用循环
2008-09-09 10:32
maiiho
Rank: 1
等 级:新手上路
帖 子:94
专家分:7
注 册:2008-7-17
收藏
得分:0 
重新查询应该使用循环?
怎么做?
不是在调用一次那个函数么?
但是我这样写通过编译了哦!
2008-09-09 10:40
forever74
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:CC
等 级:版主
威 望:58
帖 子:1696
专家分:4287
注 册:2007-12-27
收藏
得分:0 
你的编译器好神奇哦
2008-09-09 10:44
maiiho
Rank: 1
等 级:新手上路
帖 子:94
专家分:7
注 册:2008-7-17
收藏
得分:0 
saa~
我用VC6.0++
还发现,8对半大括号也能编译通过.
正好借助这个机会,谁帮我测试一下.
我把我的C上传了
2008-09-09 10:45
maiiho
Rank: 1
等 级:新手上路
帖 子:94
专家分:7
注 册:2008-7-17
收藏
得分:0 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct xx                                
{
   char xingming[15];
   char xingbie[5];
   char dianhua[13];
   char qq[13];
   char email[30];
};

int printf1()                              /*菜单函数*/
{
   int i;
   system("cls");
   printf("\t\t\t\t  通讯录\n");
   printf("--------------------------------------------------------------------------------");
   printf("1.浏览    ");
   printf("2.添加    ");
   printf("3.修改    ");
   printf("4.查找    ");
   printf("5.删除    ");
   printf("6.退出");
   printf("\n--------------------------------------------------------------------------------");
   printf("我要选择:");
   scanf("%d",&i);
   return i;
}
tj()                                       /*添加函数*/
{
   int a;
   struct xx XX;
   FILE *fp;
   if((fp=fopen("xinxi.txt","ab"))==NULL)
   {
     printf("错误!不能打开记录文件(xinxi.txt)!\n");
     getch();
     main();
     }
   system("cls");
   getchar();   /*用来接收'\n'*/
   printf("输入姓名:");
   gets(XX.xingming);
   printf("输入性别:");
   gets(XX.xingbie);
   printf("输入电话:");
   gets(XX.dianhua);
   printf("输入QQ:");
   gets(XX.qq);
   printf("输入E-mail:");
   gets(XX.email);
   fwrite(&XX,sizeof(XX),1,fp);
   fclose(fp);
   printf("\n1.返回--2.继续--3.退出-->[ ]\b\b");
   scanf("%d",&a);

   if(a==1)
   {
     return main();
     }
     else if(a==2)
     {
   return tj();
   }
   else if(a==3)
   {
      exit(0);
      }
   else
   {
    printf("错误,请按套路来!\n");
    getch();
    return main();
    }
   }
xs()                                 /*显示函数*/
{
    FILE *fp;
    int i;
    struct xx XX;
    if((fp=fopen("xinxi.txt","rb"))==NULL)
      {
        printf("错误!不能打开记录文件(xinxi.txt)!");
        getch();
        main();
        }
     while(fread(&XX,sizeof(XX),1,fp)==1)
       {
          printf("姓名: %s\n",XX.xingming);
          printf("性别: %s\n",XX.xingbie);
          printf("电话: %s\n",XX.dianhua);
          printf("QQ  : %s\n",XX.qq);
          printf("E-m : %s\n\n",XX.email);
          }
     fclose(fp);
     printf("(1.返回||2.退出)-->[ ]\b\b");
     scanf("%d",&i);
     if(i==1)
     {
         system("cls");
        return main();
        }
        else if(i==2)
        {
          exit(1);
          }
          else
          printf("错误,请按套路来!");
          getch();
          system("cls");
          xs();
     }
cx()                               /*查询函数*/
{
    struct xx XX;
    FILE *fp;
    int flag;
    int total;
    char ch[20];
    int b;
    int a;

    if((fp=fopen("xinxi.txt","rb"))==NULL)
      {
         printf("错误!不能打开记录文件(xinxi.txt)!");
         getch();
         main();
         }

      rewind(fp);                 //使指针返回文件首端
      system("cls");
      printf("( 1.按姓名查询 || 2.按电话查询 || 3.返回 )---->[ ]\b\b");
      scanf("%d",&flag);
      getchar();                  //接受%d

      if(flag==1)                  
      {
        printf("输入姓名查询:");
        gets(ch);
        total=0;
        while(fread(&XX,sizeof(XX),1,fp)==1)
           if(strcmp(ch,XX.xingming)==0)
             {
               total++;
               if(total==1)
               {
                       printf("\n姓名:%s",XX.xingming);
                    printf("姓别:%s\n",XX.xingbie);
                       printf("电话:%s\n",XX.dianhua);
                    printf("QQ  :%s\n",XX.qq);
                     printf("E-M :%s\n\n",XX.email);
                }
               else
               {
                   system("cls");
                   printf("找不到!重新查找吗?----->1.好    2.不   3.退出\n选择:");
                   scanf("%d",&a);
                   if(a==1)
                       system("cls");
                       cx();
                   if(a==2)
                       system("cls");
                       main();       
                   if(a==3)
                       return main();
               }
           }
               printf("1.继续||2.返回||3.退出-->[ ]\b\b");
               scanf("%d",&b);
               if(b==1)
               {
                 cx();
                 }
                 else if(b==2)
                 {
                 system("cls");
                 return main();
                 }
                 else
                 {
                    exit(1);
                 }
                 while(b<0||b>3)
                 {
                     printf("请正确输入!");
                     getch();
                     cx();
                 }    
           }
       if(flag==2)                  
         {
           printf("输入电话查询:");
           gets(ch);
           total=0;
           while(fread(&XX,sizeof(XX),1,fp)==1)
             if(strcmp(ch,XX.dianhua)==0)
             {
               total++;
               if(total==1)
               {
                    printf("\n姓名:%s\n",XX.xingming);
                    printf("姓别:%s\n",XX.xingbie);
                       printf("电话:%s\n",XX.dianhua);
                    printf("QQ  :%s\n",XX.qq);
                     printf("E-M :%s\n\n",XX.email);
               }
               }

               else
               {
               printf("\n请正确选择!\n\n");
               getch();
               cx();
               }
               fclose(fp);
                 }
       }
               

xg()                                 /*修改函数*/
{
   struct xx XX;
   FILE *fp1,*fp2;
   int flag;
   char ch[15];
   int a;
   if((fp1=fopen("xinxi.txt","rb"))==NULL)
   {
        printf("错误!不能打开记录文件(xinxi.txt)!");
        getch();
        main();
        }
    if((fp2=fopen("temp.txt","wb"))==NULL)
    {
       printf("错误!不能打开记录文件(temp.txt)!");
        getch();
        main();
        }
        printf("要修改谁(输入名字):");
        gets(ch);
        getchar();
        flag=0;
        while(fread(&XX,sizeof(XX),1,fp1)==1)
        {
          if(strcmp(ch,XX.xingming)==0)
          {
            printf("\n姓名:%s\n",XX.xingming);
            printf("性别 :%s\n",XX.xingbie);
            printf("电话 :%s\n",XX.dianhua);
            printf("QQ :%s\n",XX.qq);
            printf("E-M :%s",XX.email);
            printf("\n\n\n按任意键,输入新记录!\n");
            getch();
            printf("姓名:");
            gets(XX.xingming);
            printf("性别:");
            gets(XX.xingbie);
            printf("电话:");
            gets(XX.dianhua);
            printf("QQ:");
            gets(XX.qq);
            printf("E-mail:");
            gets(XX.email);
            flag=1;
            }
            fwrite(&XX,sizeof(XX),1,fp2);
            }
            fclose(fp1);
            fclose(fp2);
            if(flag==1)
            {
              printf("Modify sucess!\n");
              remove("xinxi.txt");
              rename("temp.txt","xinxi.txt");
              }
              else
              printf("找不到这个记录!\n");
              getch();
              printf("1.继续||2.返回||3.退出-->[ ]\b\b");
              scanf("%d",&a);
              if(a==1)
              {
            system("cls");
              xg();
              }
              else if(a==2)
              {
                 main();
                 }
                 else if(a==3)
                 {
                   exit(0);
                   }
              }

main()
{
   switch(printf1())
   {
     case 1:system("cls");xs();break;
     case 2:system("cls");tj();break;
     case 3:system("cls");xg();break;
     case 4:system("cls");cx();break;
     case 5:printf("Deng Dai!");getch();main();break;
     case 6:exit(0);break;
     default:printf("\n\t\t错误!请输入1~6!\n\t\t按任意键重新输入!");getch();system("cls");main();break;
     }
          }
2008-09-09 10:46
maiiho
Rank: 1
等 级:新手上路
帖 子:94
专家分:7
注 册:2008-7-17
收藏
得分:0 
其中修改和删除函数,虽然写出来了,编译也通过,但不能用,问题也不知道出在哪里了..
2008-09-09 10:47
p1s
Rank: 4
等 级:贵宾
威 望:10
帖 子:454
专家分:3
注 册:2005-11-4
收藏
得分:0 
主函数不能被其他函数调用,应该由系统负责调用的吧
2008-09-09 10:58
maiiho
Rank: 1
等 级:新手上路
帖 子:94
专家分:7
注 册:2008-7-17
收藏
得分:0 
那我要返回到主函数的时候,应该怎么做?
return main();???
2008-09-09 11:03
快速回复:这样调用函数正确么?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038523 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved