How to divide a character with a string in Ada - type-conversion

I'm trying to figure out how to divide a character with a string and get a float as the quota. I've done the following in my code:
Procedure extwo is
function "-"(Strang: in String;
Char: in Character) return Float is
F: Float := Float'Value(Strang);
begin
return Float(Character'Pos(Char)-48) - Float'Value(Strang);
end "-";
Differensen: Float;
Char: Character;
Strang: String(1.
Begin
Put("Mata in ett tecken: ");
Get(Char);
Put("Mata in en sträng med exakt 3 tecken: ");
Get(Strang);
Differensen:= Char-Strang;
Put("Du matade in tecknet: ");
Put(Char);
Put(" och strängen: ");
Put(Strang);
Put(" och differensen blev ");
Put(Differensen, Fore=> 0);
end extwo;
With this I get the error messages: " invalid operand types for operator "-" ", " left operand has type "Standard.Character" " and " right operand has subtype of "Standard.String" defined at line 59 "
all on line 95:22 which is the line where it says "Differensen:= Char-Strang;"

It’s easier with operators to use the names L for the parameter that goes on the left of the operator, R for the parameter that goes on the right.
Which, in order to match your usage (Char - Strang) and your implementation would be
function "-" (L : Character; R : String) return Float;

Related

Removing char from string in pascal cause question marks in console pascal

I am trying write simple program that will remove all 'o' letters from the string.
Example :
I love cats
Output:
I lve cats
I wrote following code :
var
x:integer;
text:string;
text_no_o:string;
begin
text:='I love cats';
for x := 0 to Length(text) do
//writeln(Ord(text[6]));
if(Ord(text[x])=111) then
else
text_no_o[x]:=text[x];
write(text_no_o);
end.
begin
end;
end.
When text is in English program works fine .
But if i change it to Russian . It returns we question marks in console.
Code with small modifications for Russian language.
var
x:integer;
text:string;
text_no_o:string;
begin
text:='Русский язык мой родной';
for x := 0 to Length(text) do
//writeln(Ord(text[6]));
if(Ord(text[x])=190) then
else
text_no_o[x]:=text[x];
write(text_no_o);
end.
begin
end;
end.
And result in console that i receive is :
Русский язык м�й р�дн�й
I expect receive
Русский язык мй рднй
As I got the problem can be caused incorrect encoding settings in console, so i should force pascal to use CP1252 instead ANSI .
I am using Free Pascal Compiler version 3.2.0+dfsg-12 for Linux .
P.S I am not allowed to use StringReplace or Pos
Simple solution:
function Simple_StripO (Text : String) : String;
var
i : integer;
Text2 : string;
begin
Text2 := '';
for i := 1 to Length(Text) do
if Text[i] <> 'o' then
Text2 := Text2 + Text[i];
Result := Text2; // Or Simple_StripO := Text2;
end;
The string is likely to be UTF8 encoded. So the cyrillic o is encoded as two chars $d0 $be. Here you replace one $be (=190). You need to replace both chars, though you cannot just test for the value of the char, because their meaning depends of surrounding chars.
Here is a way, remembering the current state (outside of letter or after first byte)
var
c: char;
text: string;
state: (sOutside, sAfterD0);
begin
text:= 'Русский язык мой родной';
state:= sOutside;
for c in text do
begin
if state = sOutside then
begin
if c = #$D0 then // may be the start of the letter
state := sAfterD0
else
write(c); // output this char because not part of letter
end
else if state = sAfterD0 then
begin
if c = #$BE then state := sOutside // finished skipping
else
begin
// chars do not form letter so output skipped char
write(#$D0, c);
state := sOutside;
end;
end
end;
writeln;
end.

Usage of `? :` in Arduino's QR code generator

I'm trying to create a QR code generator using Arduino. but there is a line I cannot understand. any one can help me.My code is down below.
#include "qrcode.h"
void setup() {
Serial.begin(115200);
// Start time
uint32_t dt = millis();
const char* number = "NUMBER";
// Create the QR code
QRCode qrcode;
uint8_t qrcodeData[qrcode_getBufferSize(3)];
qrcode_initText(&qrcode, qrcodeData, 3, 3, number);
// Delta time
dt = millis() - dt;
Serial.print("QR Code Generation Time: ");
Serial.print(dt);
Serial.print("\n");
// Top quiet zone
Serial.print("\n\n\n\n");
for (uint8_t y = 0; y < qrcode.size; y++) {
// Left quiet zone
Serial.print(" ");
// Each horizontal module
for (uint8_t x = 0; x < qrcode.size; x++) {
// Print each module (UTF-8 \u2588 is a solid block)
Serial.print(qrcode_getModule(&qrcode, x, y) ? "\u2588\u2588": " ");
}
Serial.print("\n");
}
// Bottom quiet zone
Serial.print("\n\n\n\n");
}
void loop() {
}
I cant understand Serial.print(qrcode_getModule(&qrcode, x, y) ? "\u2588\u2588": " "); this line. what is the meaning of this part. "\u2588\u2588": " ". I know \u2588 is a block symbol is in unicode. but what is the use of two double commas after the block symbols???
I assume you don't understand the ternary operator ?:.
The construction qrcode_getModule(&qrcode, x, y) ? "\u2588\u2588": " " means:
if qrcode_getModule(&qrcode, x, y) can evaluate to the boolean "true" (here a non-null byte) then use the part before the colon : "\u2588\u2588" (██) (plain block)
else (qrcode_getModule(&qrcode, x, y) == 0) then use the part after the colon : " " (empty block)

Is there an uppercase letter that has two lowercase alternatives?

Are there any two characters ch1, ch2, ch1 <> ch2, and ch1 and ch2 are lowercase letters, where uppercase(ch1) == uppercase(ch2)? Is there actually such a character in Unicode?
A follow-up question is: For any ch that's a lowercase letter, is the following expression always true?
ch == lowercase(uppercase(ch))
Did a quick test in Java:
public static void main(String[] args) {
for (char ch1 = 0; ch1 < 65534; ch1++) {
if (!isLetter(ch1) || !isLowerCase(ch1)) {
continue;
}
String s1 = "" + ch1;
for (char ch2 = (char) (ch1 + 1); ch2 < 65535; ch2++) {
if (!isLetter(ch2) || !isLowerCase(ch2)) {
continue;
}
String s2 = "" + ch2;
if (s1.toUpperCase(Locale.US).equals(s2.toUpperCase(Locale.US))) {
System.out.println("ch1=" + ch1 + " (" + (int) ch1 + "), ch2=" + ch2 + " (" + (int) ch2 + ")");
}
}
}
}
It prints:
ch1=i (105), ch2=ı (305)
ch1=s (115), ch2=ſ (383)
ch1=µ (181), ch2=μ (956)
ch1=ΐ (912), ch2=ΐ (8147)
ch1=ΰ (944), ch2=ΰ (8163)
ch1=β (946), ch2=ϐ (976)
ch1=ε (949), ch2=ϵ (1013)
ch1=θ (952), ch2=ϑ (977)
ch1=ι (953), ch2=ι (8126)
ch1=κ (954), ch2=ϰ (1008)
ch1=π (960), ch2=ϖ (982)
ch1=ρ (961), ch2=ϱ (1009)
ch1=ς (962), ch2=σ (963)
ch1=φ (966), ch2=ϕ (981)
ch1=в (1074), ch2=ᲀ (7296)
ch1=д (1076), ch2=ᲁ (7297)
ch1=о (1086), ch2=ᲂ (7298)
ch1=с (1089), ch2=ᲃ (7299)
ch1=т (1090), ch2=ᲄ (7300)
ch1=т (1090), ch2=ᲅ (7301)
ch1=ъ (1098), ch2=ᲆ (7302)
ch1=ѣ (1123), ch2=ᲇ (7303)
ch1=ᲄ (7300), ch2=ᲅ (7301)
ch1=ᲈ (7304), ch2=ꙋ (42571)
ch1=ṡ (7777), ch2=ẛ (7835)
ch1=ſt (64261), ch2=st (64262)
So the answer is: yes, there are distinct characters that have the same upper-case representation.
Upper, title, and lower cases are locale-specific, and so in different locales you may have different lower case letter (e.g. French upper cases may lose accents).
But Unicode defines also a standard way to convert to upper case or to lower case, and with an exception for Turkish languages, which may have different rules (marked with T in the CaseFolding.txt Unicode database, and further special cases for Turkish, Greek, and Lithuanian, in SpecialCasing.txt).
For most cases, you have a unique way to convert lower to upper (and the contrary), but see SIGN KELVIN which maps with K and other signs which use the same glyphs as other letters (that should go away, if you remove compatibility characters with a normalization).
One case is the Greek Sigma letter. There is only one in upper case, but you may use two different in lower case, depending on whether it is at the end of a word.
You will find more information in the Unicode document about Unicode database: http://www.unicode.org/reports/tr44/#Casemapping and in the Unicode standard (linked in the document, as well the two files I named above).
Note: some characters increase the number of code points, so when converting back, one should check the longest match.

QBASIC Decimal to Binary conversion

I have converted a decimal number to binary using STR$() in QBASIC. But I need a way to convert decimal number to binary without using string functions. Thanks.
My Code :
CLS
INPUT N
WHILE N <> 0
E = N MOD 2
B$ = STR$(E)
N = FIX(N / 2)
C$ = B$ + C$
WEND
PRINT "Output "; C$
END
This code sample converts a numeric value to a binary string in Basic.
PRINT "Enter value";
INPUT Temp#
Out3$ = ""
IF Temp# >= False THEN
Digits = False
DO
IF 2 ^ (Digits + 1) > Temp# THEN
EXIT DO
END IF
Digits = Digits + 1
LOOP
FOR Power = Digits TO 0 STEP -1
IF Temp# - 2 ^ Power >= False THEN
Temp# = Temp# - 2 ^ Power
Out3$ = Out3$ + "1"
ELSE
Out3$ = Out3$ + "0"
END IF
NEXT
END IF
PRINT Out3$
END
When you want to display an integer value as binary, it seems logical to me to store it in a string variable, because it's only for display. So I'm not really sure what you are trying to do here.
Maybe you were looking for LTRIM$ so you would get outputs like 11010 instead of 1 1 0 1 0 ?
You could store it in an integer value like in the code below. But, although the integer value will look the same as the string variable, it will in fact be a completely different value.
CLS
INPUT "Type a decimal number:", N
S$ = ""
I = 0
P = 1
WHILE (N <> 0)
' get right most bit and shift right
E = N AND 1
N = INT(N / 2) ' bit shift right
' format for dsplay
S$ = LTRIM$(STR$(E)) + S$
I = I + (E * P)
P = P * 10
WEND
PRINT "Binary as string="; S$
PRINT "Binary as int="; I
END

Error Catching Newline in Lex

I have been debugging for a couple of days now and can't seem to fix this bug. The following code is a Lex file for C language grammar.
I have two problems I haven't been able to spot yet.
This first is newline detection. It seems to leave the newline char in the output and doesn't increment the line var (to keep track of line, column for error feedback).
I have every combination of newline escape sequence but can't seem catch it.
The second is detecting a single quote followed by a newline, which should produce an
illegal character error.
What am I not seeing? Thanks in advance!
%{
int col = 1;
int line = 1;
int aux_col = 0;
int err = 0;
%}
%x C_COMMENT
/* Identifiers */
ids [a-zA-Z_]([a-zA-Z0-9_])*
/* Integers */
inteiros 0|[1-9]([0-9])*
/* Chars or any Escape Sequence */
chrlit (\'[^\n\'\\]?\')|(\'\\.\')
/* Strings */
strlit (\"[^\n\"\\]?+\")
/* Char Error: Multi Char Constant */
chr_multi (\'[^\n\']{2,}\')
/* Char Error: Non-terminated Char Constant */
chr_nonterm (\'[^\n\']{1,})
/* String Error: Non-terminated String Constant */
strerr (\"[^\n\"]?+)
%%
"/*" {BEGIN(C_COMMENT); if(col==0) {aux_col=3;} else {aux_col=2;}}
<C_COMMENT>"*/" {col+=aux_col+2; aux_col=0; BEGIN(INITIAL);}
<C_COMMENT><<EOF>> {printf("Line %d, col %d: unterminated comment\n",line, col); BEGIN(INITIAL);}
<C_COMMENT>. {aux_col++;}
"do"|"struct"|"auto"|"long"|"switch"|"break"|"enum"|"register"|"typedef"
"case"|"extern"|"union"|"float"|"short"|"unsigned"|"const"|"for"|"signed" "void"|"continue"|"goto"|"sizeof"|"volatile"|"default"|"static"
{col+=yyleng; printf("RESERVED\n");}
return {col+=yyleng; printf("RETURN\n");}
while {col+=yyleng; printf("WHILE\n");}
printf {col+=yyleng; printf("PRINTF\n");}
atoi {col+=yyleng; printf("ATOI\n");}
if {col+=yyleng; printf("IF\n");}
int {col+=yyleng; printf("INT\n");}
itoa {col+=yyleng; printf("ITOA\n");}
char {col+=yyleng; printf("CHAR\n");}
"&&" {col+=yyleng; printf("AND\n");}
"&" {col+=yyleng; printf("AMP\n");}
"=" {col+=yyleng; printf("ASSIGN\n");}
"*" {col+=yyleng; printf("AST\n");}
"," {col+=yyleng; printf("COMMA\n");}
"/" {col+=yyleng; printf("DIV\n");}
"==" {col+=yyleng; printf("EQ\n");}
">=" {col+=yyleng; printf("GE\n");}
">" {col+=yyleng; printf("GT\n");}
"{" {col+=yyleng; printf("LBRACE\n");}
"<=" {col+=yyleng; printf("LE\n");}
"(" {col+=yyleng; printf("LPAR\n");}
"[" {col+=yyleng; printf("LSQ\n");}
"<" {col+=yyleng; printf("LT\n");}
"-" {col+=yyleng; printf("MINUS\n");}
"%" {col+=yyleng; printf("MOD\n");}
"!=" {col+=yyleng; printf("NE\n");}
"!" {col+=yyleng; printf("NOT\n");}
"+" {col+=yyleng; printf("PLUS\n");}
"}" {col+=yyleng; printf("RBRACE\n");}
")" {col+=yyleng; printf("RPAR\n");}
"]" {col+=yyleng; printf("RSQ\n");}
";" {col+=yyleng; printf("SEMI\n");}
\|\| {col+=yyleng; printf("OR\n");}
{ids} {col+=yyleng; printf("ID(%s)\n",yytext);}
{inteiros} {col+=yyleng; printf("INTLIT(%s)\n", yytext);}
{strlit} {col+=yyleng; printf("STRLIT(%s)\n", yytext);}
{chrlit} {col+=yyleng; printf("CHRLIT(%s)\n", yytext);}
{chr_multi} {if(col==0) {col++;err=1;}
printf("Line %d, col %d: multi-character char constant\n", line, col);
col+=yyleng;
if(err == 1) {col--;err=0;}}
{chr_nonterm} {if(col==0) {col++;err=1;}
printf("Line %d, col %d: unterminated char constant\n", line, col);
col+=yyleng;if(err == 1) {col--;err=0;}}
{strerr} {if(col==0) {col++;err=1;}
printf("Line %d, col %d: unterminated string constant\n", line,col);
col+=yyleng;
if(err == 1) {col--;err=0;}}
" " {col++;};
'\n' {col=1;line++;printf("BAR N\n");}
"\\n" {col=1;line++;printf("BAR N2\n");}
\n {col=1;line++;printf("BAR N3\n");}
.|\' {printf("Line %d, col %d: illegal character ('%s')\n", line, col, yytext); col++;}
%%
int main () {
yylex();
return 0;
}
int yywrap(){
return 1;
}
You are not matching newlines in comments. . matches any character but newline.
After fixing the rule to match keywords (adding a couple of | and putting it all on one line) the program did report an illegal character on a single quote followed by a newline. So please give the exact input that give unexpected results.
Best regards from a fellow Bryan.