Pascal 中的小问题,你能帮忙吗?

发布于 2024-10-11 14:41:49 字数 1920 浏览 3 评论 0原文

我用 pascal 编写这个程序 要求用户输入两个数组和常数值 K 该程序将 K 与数组相乘。 然后将答案保存在新数组中 并在新数组中进行一些操作 加法<<工作顺利 减法<<也工作 但是 Multi 中的问题 <<我试图要求用户输入一个新数组并执行 Muti 但仍然存在问题。 还 我希望重复这些操作,直到用户按下退出<<<我无法执行此选项,因为我对 pascal 并不完美。

如果您能帮助我,我将不胜感激

,这是我的代码

program BST6;

const maxN=100;maxM=100;
type mat=array[1..maxN,1..maxM]of integer;
var A,B,c:mat;
    n,m,l,s,i,j,k:integer;
    ch : char;

procedure readMat(var A:mat;var m,n:integer);
begin
for i:=1 to m do
    for j:=1 to n do
        begin
        write('mat[',i,',',j,']=');
        readln(A[i,j]);
        end;
end;

procedure writeMat(A:mat;m,n:integer);
begin
for i:=1 to m do
    begin
    for j:=1 to n do
        write(a[i,j]:4);
    writeln;
    end;
end;

function multK(A:mat;k:integer):mat;
begin
for i:=1 to n do
    for j:=1 to m do
        begin
        B[i,j]:= K*A[i,j];
        end;
multK:=B;
end;

function minus(A,B:mat):mat;
begin
for i:=1 to m do
    for j:=1 to n do
        C[i,j]:=A[i,j]-B[i,j];
minus:=C;
end;

function plus(A,B:mat):mat;
begin
for i:=1 to m do
    for j:=1 to n do
        C[i,j]:=A[i,j]+B[i,j];
plus:=C;
end;

function mult(A,B:mat;m,l,n:integer):mat;
begin
for i:=1 to m do
    for j:=1 to n do
        for k:=1 to l do
            c[i,j]:=c[i,j]+A[i,k]*B[k,j];
mult:=C;
end;

begin
write('input m<=',maxM,'.. m=' );readln(m);
write('input n<=',maxN,'.. n=');readln(n);
readMat(A,m,n);
writeln('input the const K');readln(k);
B:=multK(A,K);
writeln('The matrix A : ');
writeMat(A,m,n);
writeln('The matrix B=K*A : ');
writeMat(B,m,n);
writeln('choose the operation + , - or * ');
readln(ch);


case ch of
'+' : c:=plus(A,B);
'-' : c:=minus(A,B);
'*' : begin
      writeln('input m<=',maxM,'input l<=',maxN);readln(m,l);readMat(A,m,l);
      writeln('input l<=',maxN);readln(n);readMat(B,l,n);
      c:=mult(A,B,m,l,n);
      end;

end;
writeMat(c,m,n);
readln;
end.

I write this program with pascal
which ask the user to enter two arrays and constant value which is K
the program muli the K with arrays .
and then save the answer in new array
and do some operation in new array
addition << work well
Subtraction << also work
BUT the problem in Multi << I am trying to ask the user to enter a new array and do Muti but still there is a problem.
ALSO
I want these operation repeated until the user press exit <<< I could not do this options because i am not perfect with pascal .

I would be grateful if you could help me

This is My Code

program BST6;

const maxN=100;maxM=100;
type mat=array[1..maxN,1..maxM]of integer;
var A,B,c:mat;
    n,m,l,s,i,j,k:integer;
    ch : char;

procedure readMat(var A:mat;var m,n:integer);
begin
for i:=1 to m do
    for j:=1 to n do
        begin
        write('mat[',i,',',j,']=');
        readln(A[i,j]);
        end;
end;

procedure writeMat(A:mat;m,n:integer);
begin
for i:=1 to m do
    begin
    for j:=1 to n do
        write(a[i,j]:4);
    writeln;
    end;
end;

function multK(A:mat;k:integer):mat;
begin
for i:=1 to n do
    for j:=1 to m do
        begin
        B[i,j]:= K*A[i,j];
        end;
multK:=B;
end;

function minus(A,B:mat):mat;
begin
for i:=1 to m do
    for j:=1 to n do
        C[i,j]:=A[i,j]-B[i,j];
minus:=C;
end;

function plus(A,B:mat):mat;
begin
for i:=1 to m do
    for j:=1 to n do
        C[i,j]:=A[i,j]+B[i,j];
plus:=C;
end;

function mult(A,B:mat;m,l,n:integer):mat;
begin
for i:=1 to m do
    for j:=1 to n do
        for k:=1 to l do
            c[i,j]:=c[i,j]+A[i,k]*B[k,j];
mult:=C;
end;

begin
write('input m<=',maxM,'.. m=' );readln(m);
write('input n<=',maxN,'.. n=');readln(n);
readMat(A,m,n);
writeln('input the const K');readln(k);
B:=multK(A,K);
writeln('The matrix A : ');
writeMat(A,m,n);
writeln('The matrix B=K*A : ');
writeMat(B,m,n);
writeln('choose the operation + , - or * ');
readln(ch);


case ch of
'+' : c:=plus(A,B);
'-' : c:=minus(A,B);
'*' : begin
      writeln('input m<=',maxM,'input l<=',maxN);readln(m,l);readMat(A,m,l);
      writeln('input l<=',maxN);readln(n);readMat(B,l,n);
      c:=mult(A,B,m,l,n);
      end;

end;
writeMat(c,m,n);
readln;
end.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拒绝两难 2024-10-18 14:41:50

首先,拥有与同名函数参数发生冲突的全局单字母变量是疯狂的。

为什么 multK 会修改全局变量 B 作为副作用?
为什么 minus 会修改全局变量 C 作为副作用?
为什么索引变量使用全局整数?
mult 更糟糕:它不仅会修改 C 作为副作用,而且它还假设 C 事先包含有意义的值。我认为它需要事先将 C 初始化为全零。

我的猜测是你的一些副作用会以奇怪的方式干扰。但我不想仔细考虑。首先重构你的代码。特别是了解如何以及何时使用局部变量。

First of all having global one letter variables which collide with function parameters with the same name is insane.

Why does multK modify the global variable B as a sideeffect?
Why does minus modify the global variable C as a sideeffect?
Why global integers as for index variables?
And mult is even worse: It doesn't only modify C as a sideeffect, but it assumes C contains meaningful values beforehand. I think it needs to initialize C to all zeros beforehand.

My guess is some of your side effects interfere in strange ways. But I don't want to think it through. Refactor your code first. In particular learn how and when to use local variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文