2つの数値を入力し、その最小公倍数を計算します。
lcm.psc
/*
最大公約数を計算 for PasC
*/
function int start(){
int a,b,c;
do {
output "a (> 0) = ";
input a;
} while (a <= 0);
do {
output "b (> 0) = ";
input b;
} while (b <= 0);
c := lcm(a,b);
output "最大公約数は";
output c;
output "です。\n";
return 0;
}
// 最大公約数を計算
function int lcm(int a,int b){
int temp;
while (a >< b){
if (a < b){ // Swap(a,b)
temp := a;
a := b;
b := temp;
}
a := a - b;
}
return a;
}
コマンドプロンプト
C:\sample>pascc lcm
lcm.psc - エラー 0、警告 0
C:\sample>pasc lcm
実行結果
a (> 0) = 24
b (> 0) = 18
最大公約数は6です。
円盤の数を入力し、ハノイの塔問題の解答を表示します。
hanoi.psc
/*
ハノイの塔 for PasC
*/
function int start(){
int n := 0;
while (n < 1){
output "Number of disks > ";
input n;
}
hanoi(n,1,2,3); //円盤nを棒1から3に移動する
return 0;
}
// ハノイの塔を再帰的に解く
function void hanoi(int n,int a,int b,int c){
if (n = 1)
MethodOut(1,a,c); //円盤1を移動
else{
hanoi(n-1,a,c,b); //円盤n-1までを別の場所に移動
MethodOut(n,a,c); //円盤nを移動
hanoi(n-1,b,a,c); //円盤n-1までを円盤nの上に移動
}
}
//移動方法を出力する
function void MethodOut(int disc_no,int from,int to){
output "move the disc ";
output disc_no;
output " from ";
if (from = 1)
output "A";
else if (from = 2)
output "B";
else
output "C";
output " to ";
if (to = 1)
output "A";
else if (to = 2)
output "B";
else
output "C";
output "\n";
}
コマンドプロンプト
C:\sample>pascc hanoi
hanoi.psc - エラー 0、警告 0
C:\sample>pasc hanoi
実行結果
Number of disks > 4
move the disc 1 from A to B
move the disc 2 from A to C
move the disc 1 from B to C
move the disc 3 from A to B
move the disc 1 from C to A
move the disc 2 from C to B
move the disc 1 from A to B
move the disc 4 from A to C
move the disc 1 from B to C
move the disc 2 from B to A
move the disc 1 from C to A
move the disc 3 from B to C
move the disc 1 from A to B
move the disc 2 from A to C
move the disc 1 from B to C
1から100までの数値を素因数分解し、結果を表示します。
soinsu.psc
/*
素因数分解 for PasC
http://www.sra.co.jp/people/miyata/algorithm/factoriz.txt 参照
*/
function void factorize(int x){
int d, q;
output x;
output " = ";
while (x >= 4 and x mod 2 = 0) {
output"2 * ";
x := x div 2;
}
d := 3;
q := x div d;
while (q >= d) {
if (x mod d = 0) {
output d;
output " * ";
x := q;
} else
d := d + 2;
q := x div d;
}
output x;
output "\n";
}
function int start(){
int i;
for (i := 1; i <= 100; i := i + 1)
factorize(i);
return 0;
}
コマンドプロンプト
C:\sample>pascc soinsu
soinsu.psc - エラー 0、警告 0
C:\sample>pasc soinsu > result.txt
実行結果(result.txt)
1 = 1
2 = 2
3 = 3
4 = 2 * 2
5 = 5
6 = 2 * 3
7 = 7
8 = 2 * 2 * 2
9 = 3 * 3
10 = 2 * 5
11 = 11
12 = 2 * 2 * 3
13 = 13
14 = 2 * 7
15 = 3 * 5
16 = 2 * 2 * 2 * 2
17 = 17
18 = 2 * 3 * 3
19 = 19
20 = 2 * 2 * 5
21 = 3 * 7
22 = 2 * 11
23 = 23
24 = 2 * 2 * 2 * 3
25 = 5 * 5
26 = 2 * 13
27 = 3 * 3 * 3
28 = 2 * 2 * 7
29 = 29
30 = 2 * 3 * 5
31 = 31
32 = 2 * 2 * 2 * 2 * 2
33 = 3 * 11
34 = 2 * 17
35 = 5 * 7
36 = 2 * 2 * 3 * 3
37 = 37
38 = 2 * 19
39 = 3 * 13
40 = 2 * 2 * 2 * 5
41 = 41
42 = 2 * 3 * 7
43 = 43
44 = 2 * 2 * 11
45 = 3 * 3 * 5
46 = 2 * 23
47 = 47
48 = 2 * 2 * 2 * 2 * 3
49 = 7 * 7
50 = 2 * 5 * 5
51 = 3 * 17
52 = 2 * 2 * 13
53 = 53
54 = 2 * 3 * 3 * 3
55 = 5 * 11
56 = 2 * 2 * 2 * 7
57 = 3 * 19
58 = 2 * 29
59 = 59
60 = 2 * 2 * 3 * 5
61 = 61
62 = 2 * 31
63 = 3 * 3 * 7
64 = 2 * 2 * 2 * 2 * 2 * 2
65 = 5 * 13
66 = 2 * 3 * 11
67 = 67
68 = 2 * 2 * 17
69 = 3 * 23
70 = 2 * 5 * 7
71 = 71
72 = 2 * 2 * 2 * 3 * 3
73 = 73
74 = 2 * 37
75 = 3 * 5 * 5
76 = 2 * 2 * 19
77 = 7 * 11
78 = 2 * 3 * 13
79 = 79
80 = 2 * 2 * 2 * 2 * 5
81 = 3 * 3 * 3 * 3
82 = 2 * 41
83 = 83
84 = 2 * 2 * 3 * 7
85 = 5 * 17
86 = 2 * 43
87 = 3 * 29
88 = 2 * 2 * 2 * 11
89 = 89
90 = 2 * 3 * 3 * 5
91 = 7 * 13
92 = 2 * 2 * 23
93 = 3 * 31
94 = 2 * 47
95 = 5 * 19
96 = 2 * 2 * 2 * 2 * 2 * 3
97 = 97
98 = 2 * 7 * 7
99 = 3 * 3 * 11
100 = 2 * 2 * 5 * 5
円周率πを計算します。
pi.psc
int RADIX:=100;
function int start(){
int arctan1[101],arctan2[101],pai[101],a[101];
int i,j,k,l;
int remainder,val,carry,borrow;
//arctan1の初期化
arctan1[0]:=1;
for(i:=1;i<=100;i:=i+1){
arctan1[i]:=0;
}
//arctan2の初期化
arctan2[0]:=1;
for(i:=1;i<=100;i:=i+1){
arctan2[i]:=0;
}
//paiの初期化
pai[0]:=1;
for(i:=1;i<=100;i:=i+1){
pai[i]:=0;
}
//16*arctan(1/5)を求める
i:=0;
do{
//aの初期化
a[0]:=1;
for(k:=1;k<=100;k:=k+1){
a[k]:=0;
}
a[1]:=16;
for(j:=1;j<=2*i+1;j:=j+1){
//a:=a div 5
remainder:=0;
for(k:=1;k<=100;k:=k+1){
val:=remainder*RADIX+a[k];
a[k]:=val div 5;
remainder:=val mod 5;
}
}
if(i mod 2 >< 0)
a[0]:=-1;
//a:=a div (2*i+1)
remainder:=0;
for(k:=1;k<=100;k:=k+1){
val:=remainder*RADIX+a[k];
a[k]:=val div (2*i+1);
remainder:=val mod (2*i+1);
}
//arctan1:=arctan1+a
if(arctan1[0]=a[0]){
carry:=0;
for(k:=100;k>0;k:=k-1){
arctan1[k]:=arctan1[k]+a[k]+carry;
if(arctan1[k]0;k:=k-1){
arctan1[k]:=arctan1[k]-a[k]-borrow;
if(arctan1[k] >= 0){
borrow:=0;
}
else{
borrow:=1;
arctan1[k]:=arctan1[k]+RADIX;
}
}
}
i:=i+1;
}while(i<=100);
//4*arctan(1/239)を求める
i:=0;
do{
//aの初期化
a[0]:=1;
for(k:=1;k<=100;k:=k+1){
a[k]:=0;
}
a[1]:=4;
for(j:=1;j<=2*i+1;j:=j+1){
//a:=a div 239
remainder:=0;
for(k:=1;k<=100;k:=k+1){
val:=remainder*RADIX+a[k];
a[k]:=val div 239;
remainder:=val mod 239;
}
}
if(i mod 2 >< 0)
a[0]:=-1;
//a:=a div (2*i+1)
remainder:=0;
for(k:=1;k<=100;k:=k+1){
val:=remainder*RADIX+a[k];
a[k]:=val div (2*i+1);
remainder:=val mod (2*i+1);
}
//arctan2:=arctan1+a
if(arctan2[0]=a[0]){
carry:=0;
for(k:=100;k>0;k:=k-1){
arctan2[k]:=arctan2[k]+a[k]+carry;
if(arctan2[k]0;k:=k-1){
arctan2[k]:=arctan2[k]-a[k]-borrow;
if(arctan2[k] >= 0){
borrow:=0;
}
else{
borrow:=1;
arctan2[k]:=arctan2[k]+RADIX;
}
}
}
i:=i+1;
}while(i<=100);
//πを求める
//pai:=arctan1-arctan2
arctan2[0]:=-arctan2[0];
borrow:=0;
for(k:=100;k>0;k:=k-1){
pai[k]:=arctan1[k]-arctan2[k]-borrow;
if(pai[k]>=0){
borrow:=0;
}
else{
borrow:=1;
pai[k]:=pai[k]+RADIX;
}
}
//πの出力
output pai[1];
output ".";
for(k:=2;k<=100;k:=k+1){
l:=RADIX;
do{
l:=l mod 10;
if(pai[k]>=l){
output pai[k];
}
else{
output "0";
}
}while(l>1 and pai[k]
コマンドプロンプト
C:\sample>pascc pi
pi.psc - エラー 0、警告 0
C:\sample>pasc pi
実行結果
3.141592653589793238462643383279502884197169399375105820974944592378164628628998
62834825342117679821480865132823066479384460955058223172536915449587196225462543
99125346231340359642843665442
Floyd Warshallのアルゴリズムを使い、グラフの最短路を求めます。
floyd.psc
/*
最短路問題 for PasC
Floyd Warshallのアルゴリズムを使います。
∞の距離を入力したいときは、負数を入力してください。
入力データサンプルをfloyd.txtに用意したので、
pasc floyd.psc < floyd.txt
で、確認してください。
*/
const int SIZE := 10; // 配列の大きさ
const int HUGE_VAL := 10000; // 最大数
int D[100]; // SIZE × SIZE
int P[100]; // SIZE × SIZE
function int start(){
int n := 0,i,j;
do{
output "Input matrix W (<= 10) size : ";
input n;
} while(n <= 0 or n > 10);
for (i := 0;i < n;i := i + 1)
for (j := 0;j < n;j := j + 1){
output "Input d(";
output i + 1;
output ",";
output j + 1;
output ") : ";
input D[i * SIZE + j];
if (D[i * SIZE + j] < 0)
D[i * SIZE + j] := HUGE_VAL;
}
output "\n";
Floyd_Warshall(n);
PrintLoad(n);
return 0;
}
//--------------Floyd_Warshallの計算----------------------------
function int Floyd_Warshall(int n){
int i,j,k;
for (i := 0;i < n;i := i + 1) //p(0)を定義に基づいて計算する
for (j := 0;j < n;j := j + 1){
if (i = j)
P[i * SIZE + j] := HUGE_VAL;
else if (D[i * SIZE + j] < HUGE_VAL)
P[i * SIZE + j] := i + 1;
else
P[i * SIZE + j] := HUGE_VAL;
}
PrintMatrix(n,0);
for(k := 0;k < n;k := k + 1){
for (i := 0;i < n;i := i + 1)
for (j := 0;j < n;j := j + 1){
//D[i][j] <= D[i][k] + D[k][j]の時は計算せず引き継ぐ
if (D[i * SIZE + j] > (D[i * SIZE + k] + D[k * SIZE + j])){
D[i * SIZE + j] := D[i * SIZE + k] + D[k * SIZE + j];
P[i * SIZE + j] := P[k * SIZE + j];
}
}
PrintMatrix(n,k + 1);
}
return 0;
}
//--------------最短路を表示----------------------------------
function int Print(int i,int j){
if (P[i * SIZE + j] < HUGE_VAL){
Print(i,P[i * SIZE + j] - 1);
output "->";
format_output (2,j + 1);
}
else if (i = j)
format_output (2,i + 1);
else
output " There is NOT a Load.";
return 0;
}
function int PrintLoad(int size){
int i,j;
output " A -> B : dis.: Load\n";
for (i := 0;i < size;i := i + 1)
for(j := i + 1;j < size;j := j + 1){
format_output (2,i + 1);
output " -> ";
format_output (2,j + 1);
output " :";
if (D[i * SIZE + j] < HUGE_VAL){
output " ";
format_output (2,D[i * SIZE + j]);
output " : ";
}
else
output " Inf.:";
Print(i,j);
output "\n";
}
return 0;
}
//--------------D(n),P(n)を表示----------------------------------
function int PrintMatrix(int size,int n){
int i,j;
output "D(";
output n;
output ")";
for (i := 0;i < size;i := i + 1)
output " ";
output "P(";
output n;
output ")\n";
for (i := 0;i < size;i := i + 1){
for (j := 0;j < size;j := j + 1){
if (D[i * SIZE + j] < HUGE_VAL)
format_output (3,D[i * SIZE + j]);
else
output " I";
}
output " ";
for (j := 0;j < size;j := j + 1){
if (P[i * SIZE + j] < HUGE_VAL)
format_output (3,P[i * SIZE + j]);
else
output " N";
}
output "\n";
}
for(i := 0;i < size;i := i + 1)
output "--------";
output "\n";
return 0;
}
//------- printf("%nd",x)をサポート -------------
function void format_output(int n,int x){
int keta := 1,i;
for (i := x div 10;i > 0;i := i div 10)
keta := keta + 1;
for (i := n - keta;i > 0;i := i - 1)
output " ";
output x;
}
コマンドプロンプト
C:\sample>pascc floyd
floyd.psc - エラー 0、警告 0
C:\sample>pasc floyd < floyd.txt > result.txt
floyd.txt
7
0 4 5 2 -1 -1 -1
-1 0 -1 -1 1 -1 -1
-1 -1 0 -1 -1 2 -1
-1 1 2 0 -1 1 -1
-1 -1 -1 -1 0 -1 2
-1 -1 -1 -1 1 0 -1
-1 -1 -1 -1 -1 1 0
実行結果(result.txt)
Input matrix W (<= 10) size : Input d(1,1) : Input d(1,2) : Input d(1,3) : Input d(1,4) : Input d(1,5) : Input d(1,6) : Input d(1,7) : Input d(2,1) : Input d(2,2) : Input d(2,3) : Input d(2,4) : Input d(2,5) : Input d(2,6) : Input d(2,7) : Input d(3,1) : Input d(3,2) : Input d(3,3) : Input d(3,4) : Input d(3,5) : Input d(3,6) : Input d(3,7) : Input d(4,1) : Input d(4,2) : Input d(4,3) : Input d(4,4) : Input d(4,5) : Input d(4,6) : Input d(4,7) : Input d(5,1) : Input d(5,2) : Input d(5,3) : Input d(5,4) : Input d(5,5) : Input d(5,6) : Input d(5,7) : Input d(6,1) : Input d(6,2) : Input d(6,3) : Input d(6,4) : Input d(6,5) : Input d(6,6) : Input d(6,7) : Input d(7,1) : Input d(7,2) : Input d(7,3) : Input d(7,4) : Input d(7,5) : Input d(7,6) : Input d(7,7) :
D(0) P(0)
0 4 5 2 I I I N 1 1 1 N N N
I 0 I I 1 I I N N N N 2 N N
I I 0 I I 2 I N N N N N 3 N
I 1 2 0 I 1 I N 4 4 N N 4 N
I I I I 0 I 2 N N N N N N 5
I I I I 1 0 I N N N N 6 N N
I I I I I 1 0 N N N N N 7 N
--------------------------------------------------------
D(1) P(1)
0 4 5 2 I I I N 1 1 1 N N N
I 0 I I 1 I I N N N N 2 N N
I I 0 I I 2 I N N N N N 3 N
I 1 2 0 I 1 I N 4 4 N N 4 N
I I I I 0 I 2 N N N N N N 5
I I I I 1 0 I N N N N 6 N N
I I I I I 1 0 N N N N N 7 N
--------------------------------------------------------
D(2) P(2)
0 4 5 2 5 I I N 1 1 1 2 N N
I 0 I I 1 I I N N N N 2 N N
I I 0 I I 2 I N N N N N 3 N
I 1 2 0 2 1 I N 4 4 N 2 4 N
I I I I 0 I 2 N N N N N N 5
I I I I 1 0 I N N N N 6 N N
I I I I I 1 0 N N N N N 7 N
--------------------------------------------------------
D(3) P(3)
0 4 5 2 5 7 I N 1 1 1 2 3 N
I 0 I I 1 I I N N N N 2 N N
I I 0 I I 2 I N N N N N 3 N
I 1 2 0 2 1 I N 4 4 N 2 4 N
I I I I 0 I 2 N N N N N N 5
I I I I 1 0 I N N N N 6 N N
I I I I I 1 0 N N N N N 7 N
--------------------------------------------------------
D(4) P(4)
0 3 4 2 4 3 I N 4 4 1 2 4 N
I 0 I I 1 I I N N N N 2 N N
I I 0 I I 2 I N N N N N 3 N
I 1 2 0 2 1 I N 4 4 N 2 4 N
I I I I 0 I 2 N N N N N N 5
I I I I 1 0 I N N N N 6 N N
I I I I I 1 0 N N N N N 7 N
--------------------------------------------------------
D(5) P(5)
0 3 4 2 4 3 6 N 4 4 1 2 4 5
I 0 I I 1 I 3 N N N N 2 N 5
I I 0 I I 2 I N N N N N 3 N
I 1 2 0 2 1 4 N 4 4 N 2 4 5
I I I I 0 I 2 N N N N N N 5
I I I I 1 0 3 N N N N 6 N 5
I I I I I 1 0 N N N N N 7 N
--------------------------------------------------------
D(6) P(6)
0 3 4 2 4 3 6 N 4 4 1 2 4 5
I 0 I I 1 I 3 N N N N 2 N 5
I I 0 I 3 2 5 N N N N 6 3 5
I 1 2 0 2 1 4 N 4 4 N 2 4 5
I I I I 0 I 2 N N N N N N 5
I I I I 1 0 3 N N N N 6 N 5
I I I I 2 1 0 N N N N 6 7 N
--------------------------------------------------------
D(7) P(7)
0 3 4 2 4 3 6 N 4 4 1 2 4 5
I 0 I I 1 4 3 N N N N 2 7 5
I I 0 I 3 2 5 N N N N 6 3 5
I 1 2 0 2 1 4 N 4 4 N 2 4 5
I I I I 0 3 2 N N N N N 7 5
I I I I 1 0 3 N N N N 6 N 5
I I I I 2 1 0 N N N N 6 7 N
--------------------------------------------------------
A -> B : dis.: Load
1 -> 2 : 3 : 1-> 4-> 2
1 -> 3 : 4 : 1-> 4-> 3
1 -> 4 : 2 : 1-> 4
1 -> 5 : 4 : 1-> 4-> 2-> 5
1 -> 6 : 3 : 1-> 4-> 6
1 -> 7 : 6 : 1-> 4-> 2-> 5-> 7
2 -> 3 : Inf.: There is NOT a Load.
2 -> 4 : Inf.: There is NOT a Load.
2 -> 5 : 1 : 2-> 5
2 -> 6 : 4 : 2-> 5-> 7-> 6
2 -> 7 : 3 : 2-> 5-> 7
3 -> 4 : Inf.: There is NOT a Load.
3 -> 5 : 3 : 3-> 6-> 5
3 -> 6 : 2 : 3-> 6
3 -> 7 : 5 : 3-> 6-> 5-> 7
4 -> 5 : 2 : 4-> 2-> 5
4 -> 6 : 1 : 4-> 6
4 -> 7 : 4 : 4-> 2-> 5-> 7
5 -> 6 : 3 : 5-> 7-> 6
5 -> 7 : 2 : 5-> 7
6 -> 7 : 3 : 6-> 5-> 7
error.psc
function int start(){
int a:=5;
const int b:=true;//型の違う代入
int b;//同名の変数宣言
bool c;
if(a=d){//宣言されていない変数
a:= 5+true;//int型でない+演算
}
while(a=true){}//型の違う=演算
do{}while(a+8);//条件式がbool型ではない
func2(false);//引数の違う関数呼び出し
input b;//const変数への代入
input c;//int以外の変数への入力
xyz;//構文エラー
return 0;
}
function int func2(int a){
break;//不正なbreak
return false;//型の違う返り値
}
function bool func(){
return;//返り値がない
}
function int func(bool a){}//同名の関数
コマンドプロンプト
C:\sample>pascc error
エラー(15): bison: statementを無視します
エラー(25): define_function: 関数'func'はすでに定義されています
エラー(3): declare_valiable: bool型をint型に代入することはできません
エラー(4): declare_valiable: 変数'b'はすでに宣言されています
エラー(7): exp_variable: 変数'd'は宣言されていません
エラー(8): expression: int型でない項で+演算してはいけません
エラー(10): expression: 異なる型の項で=演算してはいけません
エラー(11): while_statement: 条件式がbool型ではありません
エラー(12): func_call: 引数の数や型が定義と一致しません
エラー(13): input_statement: const宣言された変数に代入してはいけません
エラー(14): input_statement: int型以外の変数に代入することはできません
エラー(17): statement_s: 構文木が正しく生成されていません
警告(17): MapManager: 変数'b'は使用されていません
警告(17): MapManager: 変数'c'は使用されていません
エラー(17): define_function: 関数'start'に値を返さないコントロールパスがあります
エラー(19): break_statement: 不正な'break'です
エラー(20): return_statement: 返り値の型が違います
警告(21): MapManager: 変数'a'は使用されていません
エラー(23): return_statement: 返り値がありません
エラー(24): define_function: 関数'func'に値を返さないコントロールパスがあります
error.psc - エラー 17、警告 3