1.判断字符类型
for(1 -eq 1){
[char]$ch=read-Host "请输入一个字符"
$a="\d{1}"
$b="[a-z]{1}"
$c="[A-Z]{1}"
if($ch -cmatch $a){
Write-Output($ch+":是一个数字")
}elseif($ch -cmatch $b){
Write-Output($ch+":是小写字母")
}elseif($ch -cmatch $c){
Write-Output($ch+":是大写字母")
}else{
Write-Output($ch+":是符号")
}
return
}
While(1 -eq 1){
[char]$str = Read-Host "请输入1个字符"
[int]$asc = [char]$str
if($asc -ge 48 -and $asc -le 57){
Write-Output ($str + ":这个字符是数字")
}
elseif($asc -ge 65 -and $asc -le 90){
Write-Output ($str + ":这个字符是大写字母")
}
elseif($asc -ge 97 -and $asc -le 122){
Write-Output ($str + ":这个字符是小写字母")
}
else{
Write-Output ($str + ":这个字符是符号")
}
break
}
[string]$str = Read-Host "请输入1个字符串"
[char[]]$ascii = $str.ToCharArray()
$a,$b,$c,$d = 0,0,0,0
for($i = 0 ;$i -lt $ascii.Length; $i++){
[int]$num = [int]$ascii[$i]
if(($num -ge 48) -and ($num -le 57)){
$a++
}elseif(($num -ge 65) -and ($num -le 90)){
$b++
}elseif(($num -ge 97) -and ($num -le 122)){
$c++
}else{
$d++
}
}
write-output ("$str `n数字:$a 个;大写字母:$b 个;小写字母:$c 个;字符:$d 个。" )
[string]$str = Read-Host "请输入1个字符串"
[char[]]$ascii = $str.ToCharArray()
$a,$b,$c,$d = 0,0,0,0
for($i = 0 ;$i -lt $str.Length; $i++){
if($str[$i] -match "[0-9]"){
$a++
}elseif($str[$i] -cmatch "[A-Z]"){
$b++
}elseif($str[$i] -cmatch "[a-z]"){
$c++
}else{
$d++
}
}
write-output ("$str `n数字:$a 个;大写字母:$b 个;小写字母:$c 个;字符:$d 个。" )
[string]$str = Read-Host "请输入一个字符串"
[int]$a = $str.Length
for($i = 1;$i -le $a;$i++){
[string]$char_tem = $str.Substring($i-1,1)
if($char_tem -notmatch "[0-9.]"){
Write-Output "你输入的字符串不可以转换为一个有效的数字。"
break;
}elseif ( $i -eq $a){
Write-Output "你输入的字符串可以转换为一个有效的数字"
}
}
[string]$str_1 = "网络空间安全"
for($i = 0;$i -lt $str_1.Length-1;$i++){
$char_2 = $str_1.substring($i,2)
Write-Output ("{0}" -f $char_2)
}
2.常见数学模型
水仙花数
for($i = 100;$i -lt 999; [int]$i = $i +1){
[string]$num = $i
[int]$num_1 = $num.Substring(0,1)
[int]$num_2 = $num.Substring(1,1)
[int]$num_3 = $num.Substring(2)
$tem_sum = ($num_1*$num_1*$num_1)+($num_2*$num_2*$num_2)+($num_3*$num_3*$num_3)
if($tem_sum -eq $num){
Write-Output ("水仙花数:"+$num)
}
}
完全平方数
for($i = 1;$i -lt 10000;$i++){
$sum_1 = $i + 100
$sum_2 = $i + 268
for($j = 1;$j -le 100;$j++){
$square_1 = ($j * $j)
if($sum_1 -eq $square_1){
for($k = 1;$k -le 100;$k++){
$square_2 = ($k * $k)
}if($sum_2 -eq $square_2){
Write-Output "该数是 $i"
}
}
}
}
质数
- 因此判断一个整数m是否是素数,只需把 m 被 2 ~ m-1 之间的每一个整数去除,如果都不能被整除,那么 m 就是一个素数。
- 另外判断方法还可以简化。m 不必被 2 ~ m-1 之间的每一个整数去除,只需被 2 ~ 之间的每一个整数去除就可以了。如果 m 不能被 2 ~ 间任一整数整除,m 必定是素数。例如判别 17 是是否为素数,只需使 17 被 2~4 之间的每一个整数去除,由于都不能整除,可以判定 17 是素数。
- 原因:因为如果 m 能被 2 ~ m-1 之间任一整数整除,其二个因子必定有一个小于或等于 ,另一个大于或等于 。例如 16 能被 2、4、8 整除,16=28,2 小于 4,8 大于 4,16=44,4=√16,因此只需判定在 2~4 之间有无因子即可。
for($i = 2;$i -lt 1000;$i++){
for($j = 2;$j -le $i;$j++){
[int]$remainder = $i % $j
if($remainder -eq 0){
if($i -ne $j){
break;
}else{
Write-Output "$i"
}
}
}
}
3.打印乘法表、三角形
for($i = 1; $i -le 9; $i++){
for($j = 1; $j -le $i; $j++){
Write-Host $("{0}x{1}={2} " -f $i,$j,($i*$j)) -NoNewline
}
Write-Host
}
for($i = 0;$i -lt 6;$i++){
for($j = 6;$j -gt $i;$j--){
Write-Host "*"-NoNewline
}
Write-Host ""
}
for($i=1;$i -le 5;$i++){
for($j=1;$j-le 5-$i;$j++){
Write-Host (" ")-NoNewline
}
for($j=1;$j-le (2*$i-1);$j++){
Write-Host ("*")-NoNewline
}
Write-Host ("")
}
4.常见算法
冒泡排序
$array_num = 3,65,22,102,4
$x_num = @()
for($i = 0;$i -lt $array_num.Length-1;$i++){
for($j = 0;$j -lt $array_num.Length-$i-1;$j++){
if($array_num[$j] -gt $array_num[$j+1]){
$x_num = $array_num[$j]
$array_num[$j] = $array_num[$j+1]
$array_num[$j+1] = $x_num
}
}
}
foreach($index in $array_num){
Write-Host $index
}