我想让用户给我可变数量的字符串(作为virables)。 例子:
开始 cout('你好').('').('世界') 结束。
这将打印:“Hello world” 我知道我只能让他输入一个字符串,但我想让这段代码起作用...... 我认为记录会有所帮助,但我不知道如何 谢谢你
我不确定你写的代码示例是什么......但我尽力帮助你。
Program test;
uses crt;
var string1,string2,string3:string;
begin
write("Write to first string : "); readln(string1);
write("Write to second string: "); readln(string2);
write("Write to third string : "); readln(string3);
String2:= string1 +" "+ string3; // it will add first with third string to one with one space
String3:= string2 +" "+ string1;// also 2.+ 1.
clrscr; //clearscreen (in CRT)
writeln("1. = 1.");
writeln("2. = 1. + 3.");
writeln("3. = 2. + 1.");
writeln;
writeln(string1);
writelm(string2);
writeln(string3);
我还没有测试过这个(我现在已经为你写了)我想你会阅读它并学习如何上瘾或如何用字符串做简单的事情。
真正的可变例程在 Pascal 中是不可能的。 它要么是 one 参数,其类型可以同时容纳多个值,要么是 one 参数 对应于 one 参数(如第 § 例程重载中所示)。
Pascal 由 ISO 标准 7185(“标准 Pascal”)和 10206(“扩展 Pascal”)标准化,定义了“一致数组参数”。 下面显示了接受任意非零数量的
procedure
值的 char
。
program variadicRoutineWorkaround(output);
const
width = 80;
procedure writeLnCentered(
message: array[firstCharacterIndex‥lastCharacterIndex: integer] of char
);
var
messageLength, messageIndex: integer;
begin
messageLength ≔ lastCharacterIndex − firstCharacterIndex;
{ Padding }
for messageIndex ≔ (width − messageLength) div 2 downto 1 do
begin
write(' ')
end;
{ Write the message }
for messageIndex ≔ firstCharacterIndex to lastCharacterIndex do
begin
write(message[messageIndex])
end;
writeLn
end;
begin
writeLnCentered('Pascal is a');
writeLnCentered('supercalifragilisticexpialidocious');
writeLnCentered('programming language.')
end.
Pascal 的 delphi 方言发明了一种称为 开放数组 的非标准扩展。 一个例程最多可以有一个“开放数组”参数,并且它必须是形式参数列表中的最后一个:
procedure writeLines(line: array of string);
var
lineIndex: ALUSInt;
begin
for lineIndex := low(line) to high(line) do
begin
writeLn(line[lineIndex]);
end;
end;
begin
writeLines(['Hello', 'world', '!']);
end.
请注意,您仍在传递 one 值,一个动态数组值(
[…, …, …]
是一个值)。
无法删除 [
和 ]
。
如果您想传递混合数据类型,您可以使用Delphi的
array of const
构造:
{$mode objFPC}{ compiler directive for the FreePascal Compiler }
{ $mode Delphi and $mode DelphiUnicode are valid, too. }
procedure writeLines(line: array of const);
var
lineIndex: ALUSInt;
begin
for lineIndex := low(line) to high(line) do
begin
with line[lineIndex] do begin
case vType of
vtInteger:
begin
writeLn(vInteger);
end;
vtBoolean:
begin
writeLn(vBoolean);
end;
vtChar:
begin
writeLn(vChar);
end;
vtWideChar:
begin
writeLn(vWideChar);
end;
vtExtended:
begin
writeLn(vExtended^);
end;
vtString:
begin
writeLn(vString^);
end;
vtPointer:
begin
writeLn(sysBacktraceStr(vPointer));
end;
vtPChar:
begin
if vPChar <> nil then
begin
writeLn(vPChar^);
end;
end;
vtPWideChar:
begin
if vPWideChar <> nil then
begin
writeLn(vPWideChar^);
end;
end;
vtAnsiString:
begin
writeLn(ansiString(vAnsiString));
end;
vtCurrency:
begin
writeLn(vCurrency^);
end;
vtWideString:
begin
writeLn(wideString(vWideString));
end;
vtInt64:
begin
writeLn(vInt64^);
end;
vtQWord:
begin
writeLn(vQWord^);
end;
otherwise
begin
writeLn(stdErr, 'Error: ', {$include %currentRoutine%}, ' cannot print values of type ', vType, '.');
halt(1);
end;
end;
end;
end;
end;
begin
writeLines(['first', 'second', 'third']);
end.
array of const
本质上是array of tVarRec
开放数组的特例,但实际参数列表会自动转换为tVarRec
值。
虽然 const
可能表明只允许常量文字,但也允许变量。
对于数量非常有限的参数,您可以使用例程重载。 许多方言(包括 turbo-pascal、delphi、freepascal 和 pascalabc.net)允许再次使用相同的标识符,前提是同一例程的其他定义不具有相同的参数数据类型序列。
program overloadingDemo(input, output);
procedure writeLines(line: string);
begin
writeLn(line);
end;
procedure writeLines(line0, line1: string);
begin
writeLn(line0);
writeLn(line1);
end;
procedure writeLines(line0, line1, line2: string);
begin
writeLines(line0, line1);
writeLn(line2);
end;
procedure writeLines(line0, line1, line2, line3: string);
begin
writeLines(line0, line1);
writeLines(line2, line3);
end;
begin
writeLines('Hello', 'around', 'the', 'globe!');
end.
显然这写起来非常乏味,并且要求您可以预测需要支持的最大参数数量。