| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3210 人关注过本帖
标题:如何在已知程序中输入字符串执行新功能
只看楼主 加入收藏
滕文生
Rank: 2
等 级:论坛游民
帖 子:20
专家分:10
注 册:2019-1-14
结帖率:100%
收藏
 问题点数:0 回复次数:11 
如何在已知程序中输入字符串执行新功能
如何在已知程序中输入字符串执行新功能 且不影响原功能
而且此字符串不输入也对程序没影响
/*    this is a small game that Mike Ce Shi implemented for his dearest
    students who attend his C programing lecture. he tried to use
    as few contents students dont know yet as possible. so the code
    can be improved as students learn more things. he hopes the
    students will find fun in both the game and programing, and also
    make the game complete when they finish C programing studying.
    2017-10-23        Mike Ce Shi
*/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
{
  char n1,n2,n3,n4;//numbers to guess
  char i1,i2,i3,i4,ch[100];//numbers user guesses
  int a,b,i;       //b:number correct a:position correct
  int guess = 10,invalid;

  srand(time(NULL));//set the seed for random number

  //generate 4 different digits
  n1 = rand() % 10 + '0';
  n2 = rand() % 10 + '0';

  if (n2 == n1)
    if( n2 == '9')
      n2 = '0';
    else
      n2++;
  n3 = rand() % 10 + '0';
  while(n3 == n1 || n3 == n2)
    n3 = rand() % 10 + '0';
  n4 = rand() % 10 + '0';
  while(n4 == n1 || n4 == n2 || n4 == n3)
    n4 = rand() % 10 + '0';
  //n1='9';n2='5';n3='2';n4='7';//testing numbers

  //game starts
  printf("add game descriptions and instructions here:\n");


  for(i=0;i<guess;i++)
  {

    invalid = 0;
 

    //get number input
    do
    {
      if( invalid == 1)
        printf("Input numbers are invalid! They should be NUMBERS!\n");
      else if( invalid == 2)
        printf("Input numbers are invalid! They should be Different Numbers!\n");
      printf("Please guess and type 4 different digits here: \n");

      scanf("%c%c%c%c",&i1,&i2,&i3,&i4);
      invalid = 0;
      //printf("%c%c%c%c",i1,i2,i3,i4); // for debuging
      //getchar(); //try to remove enter
      clear(); // clear the buffer

      //check if the number is valid
      if( i1<'0' || i1>'9' ||i2<'0' || i2>'9' ||i3<'0' || i3>'9' ||i4<'0' || i4>'9')
        invalid = 1;
      if( i1 == i2 || i1 == i3 || i1 == i4 || i2 == i3 || i2 == i4 || i3 == i4)
        invalid = 2;
    }while(invalid);

    //check number & position


    a=0,b=0;
    if(i1 == n1)
      a++;
    else if(i1 == n2)
      b++;
    else if(i1 == n3)
      b++;
    else if(i1 == n4)
      b++;

    if(i2 == n2)
      a++;
    else if(i2 == n1)
      b++;
    else if(i2 == n3)
      b++;
    else if(i2 == n4)
      b++;

    if(i3 == n3)
      a++;
    else if(i3 == n2)
      b++;
    else if(i3 == n1)
      b++;
    else if(i3 == n4)
      b++;

    if(i4 == n4)
      a++;
    else if(i4 == n2)
      b++;
    else if(i4 == n3)
      b++;
    else if(i4 == n1)
      b++;

    //result
    printf("    %dA%dB\n",a,b);


    //game finished
    if(a == 4)
      break;

  }
  if(i < guess)
    printf("Congratulations!\n");
  else
    printf("Game Over! The answer is %c%c%c%c!\n",n1,n2,n3,n4);

  return 0;
}


void clear()
{
    char c;
    while ((c = getchar()) != '\n' && c != EOF) { }

}
这是源代码
搜索更多相关主题的帖子: the invalid if || printf 
2019-01-21 16:56
滕文生
Rank: 2
等 级:论坛游民
帖 子:20
专家分:10
注 册:2019-1-14
收藏
得分:0 
比如说输入一串特定字符,则打印正确答案
2019-01-21 17:00
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
没看明白,密码吗

DO IT YOURSELF !
2019-01-21 17:03
滕文生
Rank: 2
等 级:论坛游民
帖 子:20
专家分:10
注 册:2019-1-14
收藏
得分:0 
回复 3楼 wp231957
猜数字
猜数字游戏规则:由计算机随机产生4位不重复的数字,玩家需要在n次之内猜出这个数字。每次猜测计算机会给出一个xAyB结果,A代表数字和位置都正确的数字的个数,B代表数字正确但位置不正确的个数。那么结果为4A0B即为猜测正确。

2019-01-21 17:12
滕文生
Rank: 2
等 级:论坛游民
帖 子:20
专家分:10
注 册:2019-1-14
收藏
得分:0 
想加一个类似作弊码的东西 输入一串字符 直接打印coungrelations
2019-01-21 17:14
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 

DO IT YOURSELF !
2019-01-21 17:16
滕文生
Rank: 2
等 级:论坛游民
帖 子:20
专家分:10
注 册:2019-1-14
收藏
得分:0 
输入 "show me the money "显示正确答案怎么改??

2019-01-21 17:22
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
使用字符串比较函数,另,你得知道正确答案

DO IT YOURSELF !
2019-01-21 17:25
滕文生
Rank: 2
等 级:论坛游民
帖 子:20
专家分:10
注 册:2019-1-14
收藏
得分:0 

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
{
  char n1,n2,n3,n4;
  char i1,i2,i3,i4;
  int a,b,i;      
  int guess = 10,invalid;
  srand(time(NULL));
  n1 = rand() % 10 + '0';
  n2 = rand() % 10 + '0';
  if (n2 == n1)
    if( n2 == '9')
      n2 = '0';
    else
      n2++;
  n3 = rand() % 10 + '0';
  while(n3 == n1 || n3 == n2)
    n3 = rand() % 10 + '0';
  n4 = rand() % 10 + '0';
  while(n4 == n1 || n4 == n2 || n4 == n3)
    n4 = rand() % 10 + '0';
  printf("add game descriptions and instructions here:\n");
  for(i=0;i<guess;i++)
  {
    invalid = 0;
    do
    {
      if( invalid == 1)
        printf("Input numbers are invalid! They should be NUMBERS!\n");
      else if( invalid == 2)
        printf("Input numbers are invalid! They should be Different Numbers!\n");
      printf("Please guess and type 4 different digits here: \n");
      scanf("%c%c%c%c",&i1,&i2,&i3,&i4);
      invalid = 0;
      clear();
      if( i1<'0' || i1>'9' ||i2<'0' || i2>'9' ||i3<'0' || i3>'9' ||i4<'0' || i4>'9')
        invalid = 1;
      if( i1 == i2 || i1 == i3 || i1 == i4 || i2 == i3 || i2 == i4 || i3 == i4)
        invalid = 2;
    }while(invalid);
    a=0,b=0;
    if(i1 == n1)
      a++;
    else if(i1 == n2)
      b++;
    else if(i1 == n3)
      b++;
    else if(i1 == n4)
      b++;
    if(i2 == n2)
      a++;
    else if(i2 == n1)
      b++;
    else if(i2 == n3)
      b++;
    else if(i2 == n4)
      b++;
    if(i3 == n3)
      a++;
    else if(i3 == n2)
      b++;
    else if(i3 == n1)
      b++;
    else if(i3 == n4)
      b++;
    if(i4 == n4)
      a++;
    else if(i4 == n2)
      b++;
    else if(i4 == n3)
      b++;
    else if(i4 == n1)
      b++;
    printf("    %dA%dB\n",a,b);
    if(a == 4)
      break;
  }
  if(i < guess)
    printf("Congratulations!\n");
  else
    printf("Game Over! The answer is %c%c%c%c!\n",n1,n2,n3,n4);
  return 0;
}


void clear()
{
    char c;
    while ((c = getchar()) != '\n' && c != EOF) { }

}
简化后的代码
2019-01-21 17:28
滕文生
Rank: 2
等 级:论坛游民
帖 子:20
专家分:10
注 册:2019-1-14
收藏
得分:0 
如果把char型改成int 应该更加不好改
2019-01-21 17:29
快速回复:如何在已知程序中输入字符串执行新功能
数据加载中...
 
   



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

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