| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1142 人关注过本帖
标题:本人自学写的代码,遇到一些问题,请会的帮我解释下(小白别进)
只看楼主 加入收藏
枯_草
Rank: 2
等 级:论坛游民
帖 子:17
专家分:44
注 册:2016-11-12
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:4 
本人自学写的代码,遇到一些问题,请会的帮我解释下(小白别进)
//怎样限制只获取一个字符,scanf()的缓冲输入要如何不干扰数据;
//各函数是怎样传递返回值的;
//bank();用清除缓冲输入则无法计算值,怎样解决;
//请会的为我详细解答,非常感谢;详见代码(具体问题已标记):
#include<stdio.h>
#include<windows.h>
#include<stdint.h>
#include<time.h>
#define cls system("cls")
#define loa "loading..."
int bag();
int shop();
int killer();
int gold_add();//控制一秒加一的函数。
int gw();
int menu();//主菜单
int bank();
int exp=100,level=1,gold=100;//全局变量
int main()
{
    cls;
    system("color 0A");
    int sp=0,rp=0;
    char ar;

    system("title [arbit]");
    while(sp<60){
     sp++;printf("*");
    }
    printf("\nG:OPEN\n");
    printf("enter the ch_t for app\n");
    while(rp<60){
      rp++;printf("*");
     }
    printf("\n\n\n");
    while(1){
      ar=getchar();
      if(ar=='g'||ar=='G')
      {printf("%s",loa);menu();}
      else
      {
         printf("\nagain\n");main();
       }     
    }
}
int menu()
{
    cls;
    system("color 0A");
    int sp=0,rp=0;
    char ar;
     while(sp<60){
      sp++;printf("*");
    }
    printf("\nB:BAG-----------S:SHOP-----------E:KILLER\n");
    printf("\nD:BANK---------------------------Q:QUIT\n");
    printf("enter the ch_t for app(q to back)\n");

    while(rp<60){
      rp++;printf("*");
    }
    puts("\n\n");

    puts("tip: if you lose, you can look up the code");
    printf("\n\n\n");
    printf("\nYour gold:%d\n-----Your exp:%d\n",gold,exp);

    while(1){
    ar=getchar();
    if(ar=='b'||ar=='B'){
     printf("%s",loa);bag();
     }
    else if(ar=='s'||ar=='S'){
      printf("%s",loa);shop();
     }
    else if(ar=='e'||ar=='E'){
      printf("%s",loa);killer();
     }
    else if(ar=='d'||ar=='D'){
     printf("%s",loa);bank();
     }
    else if(ar=='q'||ar=='Q')
    main();

    else{
      printf("error\n");menu();
     }        
    }
}
int bag()
{
      cls;
      system("color 0B");
      int as=0,ap=0;
      char qw;

      while(as<60){
        as++;printf("*");
       }

      printf("\nYour gold is:%d\n",gold+gold_add());
      gold=gold+gold_add();

      while(ap<60){
       ap++;printf("*");
       }

      printf("\nenter R to menu\n");
      qw=getchar();
      if(qw=='r'||qw=='R')
      main();   
}
int gold_add()
{
    int qq=0;
   while(1){
     qq++;
     Sleep(1000);
     break;
    }
return qq;
}
int shop()
{
  cls;
  system("color 0C");
  int as=0,ap=0;
  char qa;

  while(as<60){
   as++;printf("*");
   }
   printf("\nYour exp is:%d\n",exp);
   while(ap<60){
   ap++;printf("*");
   }

  printf("\nenter R to menu\n");
  qa=getchar();
  if(qa=='r'||qa=='R')
  menu();   
}
int killer()
{
  cls;
  system("color 0A");
  int as=0,ap=0,gk;

  while(as<60){
  as++;printf("*");
  }
  printf("\nYour level is %d \nyou kill gw is %d\n",level,gw);
  while(ap<60){
   ap++;printf("*");
   }

  printf("\nenter R to menu\n");
  gk=getchar();
  if(gk=='r'||gk=='R')
  menu();   
}
int gw()
{
    int qq=0;
   while(1){
     qq++;
     Sleep(1000);
     break;
    }
    return qq;     
}
int bank()
{
    cls;
    int ar=0,pr=0;
   static int money=0;
   static int save=0;//存储输入值的变量
    int s_mon,t_mon;
    char as;

    system("color 1A");

    while(ar<60){
    printf("*");ar++;
    }
    printf("\nYour bank have:%d\n",money+save);
    puts("enter A|B to money(R to quit)");

    while(pr<60){
    printf("*");pr++;
    }
    printf("\n\n\n");
 
    if(gold){
      while(1){
        as=getchar();
        if(as=='a'||as=='A'){
         printf("\n\nIn put your money to save:");
         scanf("%d",&s_mon);
         if (gold>=s_mon){
          gold-=s_mon;
          save+=s_mon;
          money=save;
         }//已解决迭代两次问题,但数值依旧计算两次
         else{
             printf("your money not en_e\n");
             bank();
             }
           }
         else if(as=='b'||as=='B'){
          printf("\n\nIn put your money to take:");
          scanf("%d",&t_mon);//第三次调用这个函数,则会变成加且输出错误提示
          if(money<t_mon){
            printf("you can't take:%d\n",t_mon);
            Sleep(1000);
            bank();
           }
          else{
          gold+=t_mon;
          save-=t_mon;
          money-=save;
          }
         }
          else if(as=='r'||as=='R'){
            printf("BYE\n");menu();
          }
          else
          {
           printf("Again!!!\n");
           bank();
           }//第三次调用函数则输出这句话。为何??
          }
    }
}



[此贴子已经被作者于2016-11-13 01:39编辑过]

搜索更多相关主题的帖子: 如何 system include loading 
2016-11-12 18:29
枯_草
Rank: 2
等 级:论坛游民
帖 子:17
专家分:44
注 册:2016-11-12
收藏
得分:0 
这个代码是我看cpp函数这一章想出来的,但我用函数传递参数时链接器出错;
至于时间函数我没有看懂,就照例子打上代码,结果一秒钟增加值到int 最大;
网页搜索不到类似问题,请为我解答;
2016-11-12 18:35
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:20 
代码有看没有懂,不知道你想做什么。所谓的控制程序一秒增加一是什么意思,不懂,我知道Sleep(1000)暂停1秒钟。
怎样限制只获取一个字符;这个问的就蒙了,getchar(),getch(),scanf("%c")不都是取一个字符吗。
在bank();中输入值会被计算两次,为何;你自己写的递归调用你不知道啊,static对变量的影响你不知道吗.
这书写风格也是日了狗了。


2016-11-12 22:04
枯_草
Rank: 2
等 级:论坛游民
帖 子:17
专家分:44
注 册:2016-11-12
收藏
得分:0 
回复 3楼 linlulu001
控制gold_add();中的变量一秒加一。另外用scanf()获取数字可以但是获取完缓冲输入还在呀。我再返回这个函数时就会乱加减数值。。sleep不知道怎么加,我加上去编译器报错。至于你说的迭代我已经解决了,然后我是直接Ctrl+C把代码复制过来的,不是原风格。
2016-11-12 22:40
枯_草
Rank: 2
等 级:论坛游民
帖 子:17
专家分:44
注 册:2016-11-12
收藏
得分:0 
回复 3楼 linlulu001
原谅我是自学,许多东西没看懂。而且我目前也只知道它们怎么用,其它问题没注意。
才学两个月想形成好的代码书写风格难(问题是我都是在纸上写的代码,一星期用一次电脑)
2016-11-12 22:44
快速回复:本人自学写的代码,遇到一些问题,请会的帮我解释下(小白别进)
数据加载中...
 
   



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

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