I have a small problem with the functions inside calculator02.cpp
there is an error when compile the code
warning: control reaches end of non-void function [-Wreturn-type]130 | }
Yes I know that in any case function must return the value what can I do to solve this problem? in this case I do not have any more cases apart from that ones inside the function itself, same with the get function;
Token Token_stream::get()
{
if (full) { // do we already have a Token ready?
// remove token from buffer
full = false;
return buffer;
}
char ch;
cin >> ch; // note that >> skips whitespace (space, newline, tab, etc.)
switch (ch) {
case ';': // for "print"
case 'q': // for "quit"
case '(': case ')': case '+': case '-': case '*': case '/': case '}': case '{':
return Token(ch); // let each character represent itself
case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '9':
{
cin.putback(ch); // put digit back into the input stream
double val;
cin >> val; // read a floating-point number
return Token('8', val); // let '8' represent "a number"
}
default:
error("Bad token");
}
}
double primary()
{
Token t = ts.get();
switch (t.kind) {
case '(': // handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != ')') error("')' expected");
return d;
}
case '{':
{
double d = expression();
t = ts.get();
if(t.kind != '}') error("'}' Expected");
return d;
}
case '8': // we use '8' to represent a number
return t.value; // return the number's value
default:
error("primary expected");
}
}
Related
I have MySQL database, QSqlTableModel and QTableView with few checkbox columns. It works, but the last click on checkbox does not cause changes in database. It means that if I launch a program, click on some checkbox once and close program, no changes in database will be made. If I'll change the state of several checkboxes, the last change will not be shown in database. Maybe there's something wrong in my setData method?
bool PartyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
QString h=headerData(index.column(),Qt::Horizontal).toString();
QVariant v=value;
switch(role)
{
case Qt::CheckStateRole:
if(h=="One" || h=="Two" || h=="Three" || h=="Four")
{
if(value.toInt()==Qt::Unchecked) v=0;
else v=1;
bool ret = QSqlTableModel::setData(index,v,Qt::EditRole);
if(ret) emit dataChanged(index,index);
return ret;
}
break;
case Qt::DisplayRole:
case Qt::EditRole:
.......
break;
default:
break;
}
return QSqlTableModel::setData(index,v,role);
}
QVariant PartyModel::data(const QModelIndex &idx, int role) const
{
QString h=headerData(idx.column(),Qt::Horizontal).toString();
QVariant v=QSqlTableModel::data(idx,role);
switch(role)
{
case Qt::CheckStateRole:
if(h=="One" || h=="Two" || h=="Three" || h=="Four")
v = (QSqlTableModel::data(idx,Qt::DisplayRole).toInt()==0 ? Qt::Unchecked : Qt::Checked);
break;
case Qt::DisplayRole:
if(h=="One" || h=="Two" || h=="Three" || h=="Four")
v="";
break;
default:
break;
}
return v;
}
Qt::ItemFlags PartyModel::flags(const QModelIndex &index) const
{
QString h=headerData(index.column(),Qt::Horizontal).toString();
Qt::ItemFlags f=QSqlQueryModel::flags(index);
if(h=="One" || h=="Two" || h=="Three" || h=="Four")
{
f |= Qt::ItemIsUserCheckable;
f &= ~Qt::ItemIsEditable;
}
return f;
}
The default "edit strategy" of QSqlTabelModel is OnRowChange, which means that changes are only submitted, as the name suggests, when the selected row changes. To submit changes to the database at other times, you need to either change the edit strategy to OnFieldChange, or manually call submit() or submitAll() at appropriate times.
Is it valid to put expressions in a case statement? I have this switch case statement.
var switchValue:String = StatusUpdateErrorEvent.UPDATE_ERROR;
switch (switchValue) {
case caseValue: { // it's implied here that (switchValue==caseValue)
}
case StatusUpdateErrorEvent.UPDATE_ERROR: {
}
case event is StatusUpdateErrorEvent && StatusUpdateErrorEvent.UPDATE_ERROR: {
}
// is this what I should do if I add my own expression?
case event is StatusUpdateErrorEvent && StatusUpdateErrorEvent.UPDATE_ERROR==type: {
}
}
It doesn't throw any errors when I add an expression is the switchValue==caseValue expression thrown out?
switch (switchValue) {
case caseValue:
//1
break;
case StatusUpdateErrorEvent.UPDATE_ERROR:
//2
break;
case event is StatusUpdateErrorEvent && StatusUpdateErrorEvent.UPDATE_ERROR:
//3
break;
case event is StatusUpdateErrorEvent && StatusUpdateErrorEvent.UPDATE_ERROR == type:
//4
break;
}
You need to use "break;" after every case. If not, all other breaks after will be executed. It can e also "return;" if you just want to exit function after break.
Other thing is that you use "case" in a very weird way, just like they are if's. Don't put boolean comparison there like that. It's place for values.
I have a String fetched from a Database and send from a backend using Servlet. The servlet constructs the JSON and send it to the client
I use this function to escape the JSON
public static String toHTML(String string){
StringBuffer sb = new StringBuffer();
for(int i=0;i<string.length();i++){
char ch=string.charAt(i);
switch(ch){
case '"':
sb.append("\\\"");
break;
case '\\':
sb.append("\\\\");
break;
case '\b':
sb.append("\\b");
break;
case '\f':
sb.append("\\f");
break;
case '\n':
sb.append("\\n");
break;
case '\r':
sb.append("\\r");
break;
case '\t':
sb.append("\\t");
break;
case '/':
sb.append("\\/");
break;
default:
//Reference: http://www.unicode.org/versions/Unicode5.1.0/
if((ch>='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){
String ss=Integer.toHexString(ch);
sb.append("\\u");
for(int k=0;k<4-ss.length();k++){
sb.append('0');
}
sb.append(ss.toUpperCase());
}
else{
sb.append(ch);
}
}
}//for
return sb.toString();
}
But, once displayed, I see the escaped characters. Example "something \r\n".
Any suggestions ?
JavaScript function to replace newline chars
var nl2br = function(str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}
console.log(nl2br('asdf', false)); // asdf
console.log(nl2br('as\r\ndf', false)); // as<br>
//
// df
console.log(nl2br('as\n\rdf', true)); // as<br />
//
// df
\r\n is obivously nothing a browser can interpret. Replace \r\n with <br> and you got your line break in HTML. Replace the other tags accordingly.
I want a function to be able to take in various types. AS3 doesn't support overloading directly... so I can't do the following:
//THIS ISN'T SUPPORTED BY AS3
function someFunction(xx:int, yy:int, someBoolean:Boolean = true){
//blah blah blah
}
function someFunction(arr:Array, someBoolean:Boolean = true){
someFunction(arr[0], arr[1], someBoolean);
}
How can I work around it and still have a function that is able to take arguments of various types?
If you just want to be able to accept any type, you can use * to allow any type:
function someFunction( xx:*, yy:*, flag:Boolean = true )
{
if (xx is Number) {
...do stuff...
} else if (xx is String) {
...do stuff...
} else {
...do stuff...
}
}
If you have a large number of various parameters where order is unimportant, use an options object:
function someFunction( options:Object )
{
if (options.foo) doFoo();
if (options.bar) doBar();
baz = options.baz || 15;
...etc...
}
If you have a variable number of parameters, you can use the ... (rest) parameter:
function someFunction( ... args)
{
switch (args.length)
{
case 2:
arr = args[0];
someBool = args[1];
xx = arr[0];
yy = arr[1];
break;
case 3:
xx = args[0];
yy = args[1];
someBool = args[2];
break;
default:
throw ...whatever...
}
...do more stuff...
}
For cases where you need to call a common function to a number of classes, you should specify the interface common to each class:
function foo( bar:IBazable, flag:Boolean )
{
...do stuff...
baz = bar.baz()
...do more stuff...
}
Could just have:
function something(...args):void
{
trace(args[0], args[1]);
}
This way you can easily loop through your arguments and such too (and even check the argument type):
function something(...args):void
{
for each(var i:Object in args)
{
trace(typeof(i) + ": " + i);
}
}
something("hello", 4, new Sprite()); // string: hello
// number: 4
// object: [object Sprite]
This AS3 function works for normal methods and getter methods:
public function MyClassTestAPI(functionName:String, ...rest):* {
var value:*;
try {
switch(rest.length) {
case 0:
value = myObj[functionName];
break;
case 1:
value = myObj[functionName].call(functionName, rest[0]);
break;
case 2:
value = myObj[functionName].call(functionName, rest[0],rest[1]);
break;
default:
throw("Cannot pass more than 2 parameters (passed " + rest.length + ")");
}
}
return value;
}
Sample usage:
this.MyClassTestAPI("Foo", "arg1"); // tests function Foo(arg1:String):String
this.MyClassTestAPI("MyProperty"); // tests function get MyProperty():String
this.MyClassTestAPI("MyProperty", "new value");// tests function set MyProperty(val:String):void
The third call does not work (throws exception).
How can I make it work for setter methods as well?
Thanks!
edit:
This is a version that works, except with getter and setter that have additional parameters.
It is ok for my needs:
public function MyClassTestAPI(functionName:String, ...rest):* {
var value:*;
try {
if (typeof(this.mediaPlayer[functionName]) == 'function') {
switch(rest.length) {
case 0:
value = myObj[functionName].call(functionName);
break;
case 1:
value = myObj[functionName].call(functionName, rest[0]);
break;
case 2:
value = myObj[functionName].call(functionName, rest[0],rest[1]);
break;
default:
throw("Cannot pass more than 2 parameters (passed " + rest.length + ")");
}
} else {
switch(rest.length) {
case 0:
value = myObj[functionName];
break;
case 1:
myObj[functionName] = rest[0];
break;
default:
throw("Cannot pass parameter to getter or more than one parameter to setter (passed " + rest.length + ")");
}
}
}
return value;
}
Setter functions works as variables, so you can't use it in this way:
myProperty.call( "new value" );
Your function for variables is pointless, because you just have to do a value assignment:
myProperty = "new value";
By the way you can include it in your function in two ways:
create a third parameter what tells your function it is a function or variable
create the value assignment in the catch section
You are currently passing only one string with value "new value"
This should do the trick:
this.MyClassTestAPI("MyProperty", "new","value");
For more information on this matter check the Adobe LiveDocs at:
http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_19.html
Cheers