标题:如何在已知程序中输入字符串执行新功能
取消只看楼主
滕文生
Rank: 2
等 级:论坛游民
帖 子:20
专家分:10
注 册:2019-1-14
结帖率:100%
 问题点数:0 回复次数:7 
如何在已知程序中输入字符串执行新功能
如何在已知程序中输入字符串执行新功能 且不影响原功能
而且此字符串不输入也对程序没影响
/*    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
滕文生
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
滕文生
Rank: 2
等 级:论坛游民
帖 子:20
专家分:10
注 册:2019-1-14
得分:0 
输入 "show me the money "显示正确答案怎么改??

2019-01-21 17:22
滕文生
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
滕文生
Rank: 2
等 级:论坛游民
帖 子:20
专家分:10
注 册:2019-1-14
得分:0 
回复 8楼 wp231957
????答案是以时间为种子随即的  如果想知道可以打印出来  我想加一个条件 就是输入特定字符就可以打印答案 不输入没影响
2019-01-21 17:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-492781-1-1.html




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

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