Code Maintenance & Programming Rules

 This guide outlines essential best practices spanning code style, architectural design, debugging, testing, performance, and portability—all aimed at reducing the long-term cognitive load of code maintenance. 🎨 1. Style Code is written for humans to read, and only incidentally for computers to execute. Variable Naming : Use descriptive names for global variables, and short names for local variables. Precision and Consistency : Use active names for functions (e.g., calculateTotal ). Above all, keep your coding style consistent throughout the project. Structure & Expressions : Use a consistent indentation and brace ( {} ) style to show program structure visually. Use the natural form for expressions. Use parentheses to make the semantics unambiguous. Break up overly complex expressions to keep them clear. Side Effects & Macros : Beware of functions with side effects. Avoid function-like macros; if unavoidable, parenthesize the macro body and arguments carefully. Magic Numbe...

高中生程式解題系統:電話客服中心



題目連結 http://zerojudge.tw/ShowProblem?problemid=a054。此題是a020: 身分證檢驗變化題。因為檢查碼 c = 10 - m ,c 會有十種結果,並以同餘的算法,就可以建立好這十種結果的對照表,程式碼中的 prtResult 是可以換成陣列方式的。

程式碼:
#include <iostream>
using namespace std;

void prtResult(int n)
{
 switch(n) {
  case 10:
   cout << "BNZ" << endl;
   break;
  
  case 1:
   cout << "AMW" << endl;
   break;
  
  case 2:
   cout << "KLY" << endl;
   break;

  case 3:
   cout << "JVX" << endl;
   break;
   
  case 4:
   cout << "HU" << endl;
   break;
   
  case 5:
   cout << "GT" << endl;
   break;
   
  case 6:
   cout << "FS" << endl;
   break;
   
  case 7:
   cout << "ER" << endl;
   break;
   
  case 8:
   cout << "DOQ" << endl;
   break;
   
  case 9:
   cout << "CIP" << endl;
   break;
   
  default:
   cout << "Error" << endl;
   break;  
 }
 
}

int main() {
 string num;
    while(cin >> num)
 {
  int sum = 0;
  for(unsigned int i = 0; i < num.size() - 1; i++)
  {   
   sum += (num[i] - '0') * (8-i);
  }

  sum += (num[num.size() - 1]-'0');  
  int tar = 10 - (sum % 10);
  prtResult(tar);
    }
 
    return 0;
}
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

留言

這個網誌中的熱門文章