Why it is not passing if this if statement - ios4

I am making a calculator, and I defined all functions SUM SUB DIV MUL
Then when I typed this if statement is pass for only the first one
although the condition is wrong
So what I have is 2 text fields
4 actions for each button
2 actions : result and clear
and a label for the operation which will take + or - or * or ÷ whatever ...
this label called Operation
I typed this if statement in the calculate action, but I don't know what is the wrong.
When I tap calculate the operation will be the first if operation which is +
and when I press 1 -4 it shows 1 + 4 = 5
//in -(IBAction)calculate :
-(IBAction)Calculate:(id)sender{
if (operation.text=#"+"){
//sum
a =num1.text.integerValue;
b= num2.text.integerValue;
c=num1.text.integerValue+num2.text.integerValue;
printf("%i >> %i",b,num2.text.integerValue);
[num1 resignFirstResponder];
[num2 resignFirstResponder];
result.text =[NSString stringWithFormat:#"%i + %i = %i",a,b,c];}
else if (operation.text=#"-"){
//sub
a =num1.text.integerValue;
b= num2.text.integerValue;
c=num1.text.integerValue-num2.text.integerValue;
printf("%i >> %i",b,num2.text.integerValue);
[num1 resignFirstResponder];
[num2 resignFirstResponder];
result.text =[NSString stringWithFormat:#"%i - %i = %i",a,b,c];}
else if (operation.text=#"*"){
//mul
a =num1.text.integerValue;
b= num2.text.integerValue;
c=num1.text.integerValue*num2.text.integerValue;
printf("%i >> %i",b,num2.text.integerValue);
[num1 resignFirstResponder];
[num2 resignFirstResponder];
result.text =[NSString stringWithFormat:#"%i * %i = %i",a,b,c];
}
//div
else if (operation.text=#"÷"){
a =num1.text.integerValue;
b= num2.text.integerValue;
c=num1.text.integerValue/num2.text.integerValue;
printf("%i >> %i",b,num2.text.integerValue);
[num1 resignFirstResponder];
[num2 resignFirstResponder];
result.text =[NSString stringWithFormat:#"%i ÷ %i = %i",a,b,c];
}
}

= performs assignment, so if (operation.text=#"+") actually assigns #"+" to operation.text, it does not compare for equality. The assigned value then is treated as true by the if statement.

Related

Two questions with base64 encoding

I confused how to convert const char * to base64 with 2 Questions:
Question #1 how do I defined the length of output string that would perfectly match the length of output base64?I have found a code which from apple opensource,the code in below http://www.opensource.apple.com/source/QuickTimeStreamingServer/QuickTimeStreamingServer-452/CommonUtilitiesLib/base64.c
or I could directly use "atlenc.h" in VC++.if the length of coded_dst which I have defined is smaller than the actually,the program may crashed
int Base64encode(char *coded_dst, const char *plain_src, int len_plain_src)
{
const char basis_64[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int i;
char *p;
p = coded_dst;
for (i = 0; i < len_plain_src - 2; i += 3) {
*p++ = basis_64[(plain_src[i] >> 2) & 0x3F];
*p++ = basis_64[((plain_src[i] & 0x3) << 4) |
((int) (plain_src[i + 1] & 0xF0) >> 4)];
*p++ = basis_64[((plain_src[i + 1] & 0xF) << 2) |
((int) (plain_src[i + 2] & 0xC0) >> 6)];
*p++ = basis_64[plain_src[i + 2] & 0x3F];
}
if (i < len_plain_src) {
*p++ = basis_64[(plain_src[i] >> 2) & 0x3F];
if (i == (len_plain_src - 1)) {
*p++ = basis_64[((plain_src[i] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = basis_64[((plain_src[i] & 0x3) << 4) |
((int) (plain_src[i + 1] & 0xF0) >> 4)];
*p++ = basis_64[((plain_src[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}
*p++ = '\0';
return p - coded_dst;
}
Question #2 as we all well know that the type of byte in C++ is unsigned char,how do I convert the char * to unsigned char *?
thanks
regards
Ken
The design of your function, based on the signature, tells me it's up to the caller to provide a sufficient buffer for output. This would be unsafe in your example because the caller isn't informing the function how large that buffer is. Your function has no chance to limit output to coded_dst to the buffer provided, so you should add, at the least, a parameter for that.
As such, you would need to check as you loop to be sure p, a pointer into coded_dst, stays within that limit, returning an error to the caller if there's insufficient room.
That said, notice how many increments of p occur for every 3 source items processed. The ratio is 3/4...for every 3 that go into that loop, 4 come out. So, to start the calculation of the required length, begin with
( len_plain_src / 3 ) * 4;
Now, consider r = len_plain_src % 3; If r is zero, your algorithm adds 2 more bytes. If r has a remainder, your algorithm adds 3 more bytes.
After that, you append a zero terminator.
Look carefully, I've not clearly analyzed this, but you may have an error in the closing '=' appended at the tail for the case where (i<len_plain_src) - you may have added two of them instead of just one.
Now, to handle the unsigned char, you could change the declaration and initial assignment of p with,
unsigned char * p = (unsigned char *) coded_dst;
At which point it would be more convenient for you if you declare basis_64 to be unsigned char

Notepad can not find symbol for Average, max, min, etc

First, this is a homework assignment. I am supposed to create a program using a switch command. It asks the user to input 3 integers, then input an integer of 1-5 for the five cases average, max, min, total, and exit. I have pretty much completed this program, first having a user input 3 integers, then creating the switch "menu" structure, and inside each case, I attempted to create the mathematics for each of the arithmetic functions.
My error message is "cannot find symbol", which is a broad and vague error message to post about. Everything I have Googled, or found on this site seems to be unrelated to the specific symbols that my program is missing, and/or can be typographical in nature. So, unfortunately, I've had to create another thread about this.
My program can not find the symbols average, max, min, and et cetera. I believe that I may have done my arithmetic algorithms wrong, and they are either not initialized properly or at all, or I need to set them to zero before the switch command is ever used.
I'll need to post the code so you can see what I have done:
>public static void main( String args[] )
{
// Attempting to initialize the 3 integers AND the user-input early on
int n1, n2, n3, n4;
Scanner sc = new Scanner( System.in);
while ( true )
{
System.out.println ( "Please give three integers separated by spaces: " );
n1 = sc.nextInt();
n2 = sc.nextInt();
n3 = sc.nextInt();
// building a menu - hoping integers aboive will work in Switch command
System.out.println( "\n****** Analysis Menu ******" );
System.out.println( "1: Average" );
System.out.println( "2: Maximum" );
System.out.println( "3: Minimum" );
System.out.println( "4: Total" );
System.out.println( "5: Exit" );
System.out.println( "*******************\n" );
n4 = sc.nextInt(); // get the user selection
if ( n4 == 1 )
{
Average = n1 + n2 + n3 / 3;
System.out.print( "Average = " + Average );
}
else if ( n4 == 2 )
{
max = n1;
if ( max < n2 ) max = n2;
if ( max < n3 ) max = n3;
System.out.println( "Maximum = " + max );
}
else if ( n4 == 3 )
{
min = n1;
if ( min > n2) min = n3;
if ( min > n3) min = n2;
System.out.print( "Minimum = " + min );
}
else if ( n4 == 4 )
{
total = sum;
System.out.print( "total = " + total );
}
else if ( n4 == 5 )
{
System.out.println( "You have selected Exit." );
break;
}
else
{
System.out.println( "No such selection exists." );
The error messages say they cannot find every total, min, max, average, and sum in this. So I am not sure if I need to initialize them somehow, set them to 0, or even how to do that in this context.
There are multiple issues in above code:
1. Average, min, max variable not defined.
2. You average logic is not right.
3. You are missing end brace.
I tried to modify these and this is what i came up with.
public static void main( String args[] )
{
// Attempting to initialize the 3 integers AND the user-input early on
int n1, n2, n3, n4;
int Average, max, min, total;
Scanner sc = new Scanner( System.in);
while ( true )
{
System.out.println ( "Please give three integers separated by spaces: " );
n1 = sc.nextInt();
n2 = sc.nextInt();
n3 = sc.nextInt();
// building a menu - hoping integers aboive will work in Switch command
System.out.println( "\n****** Analysis Menu ******" );
System.out.println( "1: Average" );
System.out.println( "2: Maximum" );
System.out.println( "3: Minimum" );
System.out.println( "4: Total" );
System.out.println( "5: Exit" );
System.out.println( "*******************\n" );
n4 = sc.nextInt(); // get the user selection
if ( n4 == 1 )
{
Average = (n1 + n2 + n3) / 3;
System.out.print( "Average = " + Average );
}
else if ( n4 == 2 )
{
max = n1;
if ( max < n2 ) max = n2;
if ( max < n3 ) max = n3;
System.out.println( "Maximum = " + max );
}
else if ( n4 == 3 )
{
min = n1;
if ( min > n2) min = n3;
if ( min > n3) min = n2;
System.out.print( "Minimum = " + min );
}
else if ( n4 == 4 )
{
total = n1 + n2 + n3;
System.out.print( "total = " + total );
}
else if ( n4 == 5 )
{
System.out.println( "You have selected Exit." );
break;
}
else
{
System.out.println( "No such selection exists." );
}
}
}
You need to define Average,min,max,total as a variable just like n1,..,n4. I mean
decimal Average;
BTW it's better to avoid naming variable like this for example minValue or maxValue are better in naming.

Why the float cannot give exactly 0 value?

I have a question. Below is my code. I am wonder why, my output for "err3" cannot give a value of 0 ? is it because of the datatype is float?
The output are as below:
the value of err1 is -7.03125e-06
the value of err2 is 7.03125e-06
the value of err3 is -4.54747e-13
the operation between err1 and err2 is + and thus, the value for err3 should be 0.
can anyone help me to explain and solve this problem? I have google but still did not get the result.
Thanks in advance :)
void calnormal()
{
long numcal;
float indexNC0,indexNC1;
float error;
float aa0 = 0, ab0 = 0, ac0 = 0, ad0 = 0;
float bb0 = 0, bc0 = 0, bd0 = 0;
float cc0 = 0, cd0 = 0;
float dd0 = 0;
float aa, ab, ac, ad;
float bb, bc, bd;
float cc, cd;
float dd;
for(int i=0;i<noofvert;i++)
{
numcal = vlist[i].returnsizef();
for(int j=0;j<numcal;j=j+2)
{
indexNC0 = vlist[i].returnindexf(j);
u.ax = vlist[i].returnx() - vlist[indexNC0].returnx();
u.ay = vlist[i].returny() - vlist[indexNC0].returny();
u.az = vlist[i].returnz() - vlist[indexNC0].returnz();
if(j == 0){v0 = u;}
indexNC1 = vlist[i].returnindexf(j+1);
v.ax = vlist[i].returnx() - vlist[indexNC1].returnx();
v.ay = vlist[i].returny() - vlist[indexNC1].returny();
v.az = vlist[i].returnz() - vlist[indexNC1].returnz();
normal.ax = u.ay * v.az - u.az * v.ay;
normal.ay = u.az * v.ax - u.ax * v.az;
normal.az = u.ax * v.ay - u.ay * v.ax;
normal.D = - vlist[i].returnx() * normal.ax - vlist[i].returny() * normal.ay - vlist[i].returnz() * normal.az;
aa = normal.ax * normal.ax;
ab = normal.ax * normal.ay;
ac = normal.ax * normal.az;
ad = normal.ax * normal.D;
bb = normal.ay * normal.ay;
bc = normal.ay * normal.az;
bd = normal.ay * normal.D;
cc = normal.az * normal.az;
cd = normal.az * normal.D;
dd = normal.D * normal.D;
aa0 = aa0 + aa;
ab0 = ab0 + ab; bb0 = bb0 + bb;
ac0 = ac0 + ac; bc0 = bc0 + bc; cc0 = cc0 + cc;
ad0 = ad0 + ad; bd0 = bd0 + bd; cd0 = cd0 + cd; dd0 = dd0 + dd;
}
double err1,err2,err3;
**err1 = cd0 * vlist[i].returnz();
err2 = dd0 ;
err3 = err2 + err1;
cout << err1 << " " << err2 << " " << err3 << endl;**
cout << endl;
}
cout << endl;
}
err3 is very close to zero, 13 zeros. err1 and err2 have 6 zeros.
A float can store about 7 decimal digits of precision.
If you had printed out more decimals of err1 and err2 you probably see they are not exactly the same. Because err1 and err2 is float they store 7 digits of precision. They probably differ a little bit in the last digit (0.5zeros+7digits) and substracting them give a result with 0.000..00x (13 zeros).
A double have roughly a maximum of 16 decimal digits of precision. When you do computations, you lose a little precision for every operation because every result is truncated to about 16 decimal digits. If you in the end end up with 13 digits of precision it is fully normal.

xprop setting multiple fields of atom property

I searched internet and found examples with setting only one field of property:
xprop -id "$windowid" -f _NET_WM_STATE 32a -set _NET_WM_STATE _NET_WM_STATE_ABOVE
but how can I set multiple fields?
I tried:
xprop -id "$windowid" -f _NET_WM_STATE 32a -set _NET_WM_STATE '_NET_WM_STATE_ABOVE, _NET_WM_STATE_SKIP_TASKBAR'
and
xprop -id "$windowid" -f _NET_WM_STATE 32aa -set _NET_WM_STATE _NET_WM_STATE_ABOVE,_NET_WM_STATE_SKIP_TASKBAR
and many other variants with no luck.
Is it possible ? :)
Ok....
I wrote a patch for xprop to fix this, and It works, but don't know is it correct.
Thanks to #MichałGórny.
(xprop.c,v 1.6)
--- xprop.c 2012-07-31 11:24:01.178117974 +0400
+++ xprop.mod 2012-07-31 11:23:19.434784430 +0400
## -1487,11 +1487,20 ##
break;
}
case 'a': {
- static Atom avalue;
- avalue = Parse_Atom(value, False);
- type = XA_ATOM;
- data = (unsigned char *) &avalue;
- nelements = 1;
+ static unsigned long data32[MAXELEMENTS];
+ char * value2 = strdup(value);
+ char * tmp = strtok(value2,",");
+ nelements = 0;
+ while( NULL != tmp ){
+ data32[nelements] = Parse_Atom(tmp, False);
+ nelements +=1;
+ if(nelements >= MAXELEMENTS)
+ break;
+ tmp = strtok(NULL,",");
+ }
+ type = XA_ATOM;
+ data = (unsigned char *) data32;
+ free(value2);
break;
}
case 'm':
Looking at the xprop's code, it's not possible.
case 'a': {
static Atom avalue;
avalue = Parse_Atom(value, False);
type = XA_ATOM;
data = (unsigned char *) &avalue;
nelements = 1;
break;
}
This is the code parsing the value to -set.
static Atom
Parse_Atom (const char *name, int only_if_exists)
{
/* may return None = 0 */
return XInternAtom(dpy, name, only_if_exists);
}
So it parses only a single atom.
I've also opened a bug for it; maybe they'll add this.

Sum of even integers problem

I need to create a program that displays the sum of the even integers between and including two numbers entered by the user.
This is what I have so far and it's not working!?
So point me in the right direction please!
//Advanced30.cpp - displays the sum of the even integers between and
//including two numbers entered by the user
//Created/revised by <your name> on <current date>
#
include <iostream>
using namespace std;
int main()
{
// declare variables
int num1 = 0;
int num2 = 0;
int sum= 0;
cout << "Enter the First Number:" << endl;
cin >> num1;
cout << "Enter the Second Number:" << endl;
cin >> num2;
for (num2 = num1; num1 <= num2; num1 += 2) sum += num1;
num1 = num1 % 2 == 0 ? num1 : num1+1;
num2 = num2 % 2 == 0 ? num2 : num2-1;
return 0;
Try to do EXACTLY what your computer is doing when it's doing the loop. Do it on a paper. Keep track of num2, num1 and their value.
You'll see very quickly where the problem is.
try the Loop
for(; num1<=num2;num1++)
{
if(num1%2==0)
sum=sum+num1
}
for (num2 = num1; num1 <= num2; num1 += 2) sum += num1;
You've overwritten your stop-point. :)
I would also suggest more meaningful names:
int start=0;
int real_start=0;
int stop=0;
int sum=0;
/* ... */
real_start = (start % 2) ? start+1 : start;
for (int i = real_start; i <= stop; i+=2) sum += i;
/* ... */

Resources