C#练习题答案: Esolang口译#1 - 介绍Esolangs和我的第一个解释(MiniStringFuck)【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

Esolang口译#1 - 介绍Esolangs和我的第一个解释(MiniStringFuck)【难度:2级】:

答案1:

public class Kata
{
  public static string MyFirstInterpreter(string code)
  {
    byte memory = 0;
    string stdout = "";
    
    foreach (char c in code)
    {
      if (c == '+') ++memory;
      if (c == '.') stdout += (char)memory;
    }
    return stdout;
  }
}

答案2:

using System.Text;

public class Kata
{
  public static string MyFirstInterpreter(string code)
  {
    var output = new StringBuilder();
    byte memory = 0;
    
    foreach (char c in code)
    {
      switch (c)
      {
        case '+':
          ++memory;
          break;
        case '.':
          output.Append((char)memory);
          break;
      }
    }
    
    return output.ToString();
  }
}

答案3:

using System;
public class Kata
{
  public static string MyFirstInterpreter(string code)
  {
            string output = "";
            byte buffer = 0;

            foreach(char c in code.ToCharArray())
            {
                switch (c)
                {
                    case '+':
                        try
                        {
                            buffer++;
                        }
                        catch (Exception)
                        {
                            buffer = 0;
                        }
                        break;
                    case '.':
                        output += (char)buffer;
                        break;
                    default:
                        break;
                }
            }

            return output;

  }
}

答案4:

public class Kata
{
  public static string MyFirstInterpreter(string code)
  {
    var flag = 0;
    var result = string.Empty;
    foreach (var c in code)
    {
        switch (c)
        {
            case '+':
                flag++;
                if (flag == 256) flag = 0;
                break;
            case '.':
                result += ((char)flag).ToString();
                break;
            default:
                break;
        }
    }
    return result;
  }
}

答案5:

using System.Linq;
using System;

public class Kata
{
  public static string MyFirstInterpreter(string code)
  {
    byte memory = 0;
    return String.Join("", code.Select(x=>x.ToString()).Select(x=>{if(x=="+"){memory++;}else if(x=="."){return ((char)memory).ToString(); } return "";}));
  }
}

答案6:

using System.Collections.Generic;
using System.Linq;
public class Kata
{
  public static string MyFirstInterpreter(string code) => new string(MiniStringFuck(code).ToArray());
  
  private static IEnumerable<char> MiniStringFuck(string code)
  {
    byte cell = 0;
    foreach (var instruction in code)
    {
      if (instruction == '+') cell++;
      if (instruction == '.') yield return (char)cell;
    }
  }
}

答案7:

public class Kata
{
  public static string MyFirstInterpreter(string code)
  {
    byte cell = default(byte);
    string output = string.Empty;
    foreach(char command in code)
    {
      switch(command) //not 'if' because with 'switch' you can easier add new features to this code
      {
        case '+': 
          unchecked { cell++; }
          break;
        case '.':
          output += (char)cell;
          break;
      }
    }
    return output;
  }
}

答案8:

using System;

public class Kata
{
  public static string MyFirstInterpreter(string code)
  {
    code = code.Replace(((char)13).ToString(), "");
    char[] chars = code.ToCharArray();
    
    int cell=0;
    string output="";
    foreach(char c in chars)
    {
      if(c=='+'){
        if(cell==255) cell=0;
        else cell++;
        }
      else if(c=='.') output+=Convert.ToChar(cell);
    }
    
    return output;
  }
}

答案9:

using System;
using System.Collections.Generic;

public class Kata {
        public static string MyFirstInterpreter(string code) {
            string result = "";
            int cell = 0;
            foreach (char character in code) {
                if(character == '+') {
                    if(cell == 255) {
                        cell = 0;
                    }
                    else {
                        ++cell;
                    }
                }
                else {
                    if(character == '.') {
                        result = String.Concat(result + (char)cell);
                    }
                }
            }
            return result;
        }
    }

答案10:

using System;
using System.Text;

public class Kata
{
  public static string MyFirstInterpreter(string code)
  {
    var builder = new StringBuilder();
    byte memory = 0;
    foreach(var c in code)
    {
      if(c == '+')
        ++memory;
      else if(c == '.')
        builder.Append(Convert.ToChar(memory));   
    }
    return builder.ToString();
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值