返回列表 發帖

C# 7 109 字元處理

TQC+ 物件導向程式語言
最新一次更新時間:2024-01-05 11:48:41

1. 題目說明:
請新增一個主控台應用程式,加入C:\ANS.CSF\CS01資料夾中的CSD01.cs進行編寫。依下列題意進行作答:將輸入的字串進行字元修改後再輸出,使輸出值符合題意要求。檔案名稱請另存新檔為CSA01.cs,儲存於C:\ANS.CSF\CS01資料夾,再進行評分。

2. 設計說明:
請撰寫程式,讓使用者輸入一個英、數字的字串。
請修改第一個及最後一個字元後輸出。若字串長度只有一個字元,則修改該字元後並輸出。
字元修改方式為:
a. 英文字:大小寫互換且往後移動一個字母,例如A改為b、B改為c、Z改為{、a改為B,b改為C,z改為[。
b. 數字0-9:往後移動一個字母,並循環,例如0改為1、1改為2、9改為0。
如輸入符號、文字等,導致第一個或最後一個字元無法處理者,顯示【error】。
*提示:0-9的ASCII值為48-57,A-Z的ASCII值為65-90,a-z的ASCII值為97-122。
3. 輸入輸出:
輸入說明
一個字串(英、數字)

輸出說明
修改後結果(輸出最後一行後不自動換行)

範例輸入1
thebookthief
範例輸出1
UhebookthieG

範例輸入2
!!!@@@
範例輸出2
error

範例輸入3
0
範例輸出3
1

4. 評分項目:
(1) 符合設計說明輸出正確格式配分        10
May

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;

  7. namespace CSA01
  8. {
  9.     class CSA01
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             try
  14.             {
  15.                 string str = Console.ReadLine();
  16.                 if (str.Trim() == "") { throw new Exception(); }

  17.                 if (str.Length == 1)
  18.                 {
  19.                     str = encrypt(str[0]).ToString();
  20.                 }
  21.                 else
  22.                 {
  23.                     str = encrypt(str[0])
  24.                         + str.Substring(1, str.Length - 2)
  25.                         + encrypt(str[str.Length - 1]);
  26.                 }
  27.                 Console.Write(str);
  28.             }
  29.             catch
  30.             {
  31.                 Console.Write("error");
  32.             }

  33.             Console.ReadKey();
  34.         }

  35.         static char encrypt(char ch)
  36.         {
  37.             if ('0' <= ch && ch <= '8')
  38.             {
  39.                 ch++;
  40.             }
  41.             else if (ch == '9')
  42.             {
  43.                 ch = '0';
  44.             }
  45.             else if (Char.IsUpper(ch))
  46.             {
  47.                 ch = (char)(Char.ToLower(ch) + 1);
  48.             }
  49.             else if (Char.IsLower(ch))
  50.             {
  51.                 ch = (char)(Char.ToUpper(ch) + 1);
  52.             }
  53.             else
  54.             {
  55.                 throw new Exception("unexpected char: " + ch);
  56.             }
  57.             return ch;
  58.         }

  59.     }
  60. }
複製代碼
May

TOP

  1. using ConsoleApp1;
  2. using System;//程式庫呼叫
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq.Expressions;
  5. using ABC.qq;


  6. class Program//負責一部分工作的人
  7. {
  8.     static void Main()//method ..Entry Point 程式進入點
  9.     {
  10.       
  11.         try
  12.         {
  13.             string str=Console.ReadLine();
  14.             if(str.Trim()=="")
  15.             {
  16.                 throw new Exception();
  17.             }
  18.             if (str.Length == 1)
  19.                 str = encrypt(str[0]).ToString();
  20.             else
  21.             {
  22.                 str = encrypt(str[0]) + str.Substring(1, str.Length - 2) + encrypt(str[str.Length-1]);
  23.             }
  24.             Console.WriteLine(str);
  25.         }
  26.         catch
  27.         {
  28.           Console.WriteLine("error");
  29.         }
  30.     }
  31.     static char encrypt(char ch)
  32.     {
  33.         if (ch >= '0' && ch <= '8')
  34.             ch++;
  35.         else if (ch == '9')
  36.             ch = '0';
  37.         else if (Char.IsUpper(ch))//判斷是否為大寫
  38.         {
  39.             ch=(char)(Char.ToLower(ch)+1);//轉小寫後加1
  40.         }
  41.         else if (Char.IsLower(ch))//判斷是否為小寫
  42.         {
  43.              ch=(char)(Char.ToUpper(ch)+1);//轉大寫後加1
  44.         }
  45.         else
  46.         {
  47.             throw new Exception("unexpected char: " + ch);
  48.         }
  49.         return ch;
  50.     }
  51. }
複製代碼
istak.teach2@gmail.com

TOP

返回列表