Here's the pseudocode for what I need to write using the preprocessor directives:
(IF VAR == NOT DEFINED) OR (VAR == DEFINED AND VAR == 0) THEN
{a few lines of code}
How can I write that logical expression in one line?
I tried this:
#if (defined(VAR) == 0) || ((defined(VAR) == 1) && (VAR == "0"))
but it didn't work. It says:
Undeclared identifier: "VAR".
Your code seems reasonable. I would have expected it to work too.
Anyway, your code gives an impression that "0" should be a default value of the VAR. So this should do:
#ifndef VAR
#define VAR "0"
#endif
#if VAR == "0"
{a few lines of code}
#endif
Related
I have written the following code:
MODULE main
VAR
status:{empty, no_empty};
x : 0..3;
ASSIGN
init(status):= empty;
init(x):=0;
next(status):= case
(status = empty): no_empty;
(status = no_empty) & (x=0): empty;
TRUE: status;
esac;
next(x):= case
(status = empty): x+3;
(status = no_empty) & (x>0): x-1;
TRUE: x;
esac;
However, when I execute the command "flatten_hierarchy" I get the following error: "x-1" undefined
I don't know why x-1 is undefined.
This is a known issue.
The parser is confusing x-1 for an identifier, when it is supposed to be an expression.
Replace
x-1
with
x - 1
I'm trying to make a calculator in Haxe, it is almost done but have a bug. The bug is happening every time that some part of the equation result in 0.
This is how I concatenate the numbers and put i the array number, the cn is the variable used to receive the digit and transform in a number, the ci is a specific counter to make the while work well and the c is the basic counter that is increased to a background while used to read the array (input) items:
var cn = '';
var ci = c;
if (input[c] == '-') {
number.push('+');
cn = '-';
ci ++;
}
while (input[ci] == '0' || input[ci] == '1' || input[ci] == '2' || input[ci] == '3' || input[ci] == '4' || input[ci] == '5' || input[ci] == '6' || input[ci] == '7' || input[ci] == '8' || input[ci] == '9' || input[ci] == '.') {
if(ci == input.length) {
break;
}
cn += input[ci];
ci++;
}
number.push(cn);
c += cn.length;
This is the part of the code used to calculate the addition and subtraction
for (i in 0 ... number.length) { trace(number); if (number[c] == '+') { number[c-1] = ''+(Std.parseFloat(number[c-1])+Std.parseFloat(number[c+1])); number.remove(number[c+1]); number.remove(number[c]); }
else {
c++;
}
}
Example:
12+13-25+1: When my code read this input, it transform in a array ([1,2,+,1,3,-,2,5,+,1]), then the code concatenate the numbers ([12,+,13,-,25,+,1]) and for lastly it seeks for the operators(+,-,* and /) to make the operation (ex: 12+13), substituting "12" for the result of the operation (25) and removing the "+" and the "13". This part works well and then the code does 25-25=0.
The problem starts here because the equation then becomes 0+1 and when the code process that what repend is that the 0 vanish and the 1 is removed and the output is "+" when the expected is "1".
remove in this case uses indexOf and is not ideal, suggest using splice instead.
number.splice(c,1);
number.splice(c,1);
https://try.haxe.org/#D3E38
I am trying to add an if condition; Idea is If I enter the input as print or text with 4 in it, else block should be called.
if ((turnContext.activity.text.indexOf('print') == -1) || (turnContext.activity.text.indexOf('4') == -1))
Now, it goes into the if block as opposed to else.
You have to use: &&. Otherwise when the text is print, the text is not 4 entering the if block.
if ((turnContext.activity.text.indexOf('print') == -1)
&& (turnContext.activity.text.indexOf('4') == -1)) {
// not 4 nor print
} else {
// print or 4
}
The execution goes into else block if text is 'print' or string contains 4 in it.
if (turnContext.activity.text !== 'print' && turnContext.activity.text.indexOf('4') === -1) {
}
This is my code on Q&A game on Visual C++
if (this->answer->Text == "2"){
this->question->Text = "2+2?";
}
else if (this->answer->Text == "4"){
this->question->Text = "3+3?";
}
else if (this->answer->Text == "6"){
this->question->Text = "4+4?";
}
else if (this->answer->Text == "8"){
this->question->Text = "Finished!";
}
else {
MessageBox::Show("Wrong!!");
}
Is there any to shorten this code? consider using arrays?
I am not a Windows programmer, don't know what the type of your
this->question->Text is, you didn't tell us, but if it is std::string or something which can be converted to char *, then this should work:
std::string t = this->answer->Text;
this->question->Text = t == "2" ? "2+2?"
: t == "4" ? "3+3?"
: t == "6" ? "4+4?"
: t == "8" ? "Finished"
: "";
if (this->question->Text = "") MessageBox::Show("Wrong!!");
You're repeating if (this->answer->Text == and this->question->Text =. Write those only once and keep the condition and the answer in a std::map.
Update:
#include <map>
#include <string>
...
std::map<std::string,std::string> answers;
answers["2"]="2+2"; // "configuration
answers["4"]="3+3?";
//and so on
std::string text=this->answer->Text;
// instead of if ...
std::map<std::string,std::string>::const_iterator found=answers.find(text);
if (found!=answers.end())
this->question->Text = answers[text]; //once. Even better found->second
else
MessageBox::Show("Wrong!!");
Try to use a switch case statement.
I'm wondering how (and in which way it's best to do it) to split a string with a unknown number of spaces as separator in C++/CLI?
Edit: The problem is that the space number is unknown, so when I try to use the split method like this:
String^ line;
StreamReader^ SCR = gcnew StreamReader("input.txt");
while ((line = SCR->ReadLine()) != nullptr && line != nullptr)
{
if (line->IndexOf(' ') != -1)
for each (String^ SCS in line->Split(nullptr, 2))
{
//Load the lines...
}
}
And this is a example how Input.txt look:
ThisISSomeTxt<space><space><space><tab>PartNumberTwo<space>PartNumber3
When I then try to run the program the first line that is loaded is "ThisISSomeTxt" the second line that is loaded is "" (nothing), the third line that is loaded is also "" (nothing), the fourth line is also "" nothing, the fifth line that is loaded is " PartNumberTwo" and the sixth line is PartNumber3.
I only want ThisISSomeTxt and PartNumberTwo to be loaded :? How can I do this?
Why not just using System::String::Split(..)?
The following code example taken from http://msdn.microsoft.com/en-us/library/b873y76a(v=vs.80).aspx#Y0 , demonstrates how you can tokenize a string with the Split method.
using namespace System;
using namespace System::Collections;
int main()
{
String^ words = "this is a list of words, with: a bit of punctuation.";
array<Char>^chars = {' ',',','->',':'};
array<String^>^split = words->Split( chars );
IEnumerator^ myEnum = split->GetEnumerator();
while ( myEnum->MoveNext() )
{
String^ s = safe_cast<String^>(myEnum->Current);
if ( !s->Trim()->Equals( "" ) )
Console::WriteLine( s );
}
}
I think you can do what you need to do with the String.Split method.
First, I think you're expecting the 'count' parameter to work differently: You're passing in 2, and expecting the first and second results to be returned, and the third result to be thrown out. What it actually return is the first result, and the second & third results concatenated into one string. If all you want is ThisISSomeTxt and PartNumberTwo, you'll want to manually throw away results after the first 2.
As far as I can tell, you don't want any whitespace included in your return strings. If that's the case, I think this is what you want:
String^ line = "ThisISSomeTxt \tPartNumberTwo PartNumber3";
array<String^>^ split = line->Split((array<String^>^)nullptr, StringSplitOptions::RemoveEmptyEntries);
for(int i = 0; i < split->Length && i < 2; i++)
{
Debug::WriteLine("{0}: '{1}'", i, split[i]);
}
Results:
0: 'ThisISSomeTxt'
1: 'PartNumberTwo'