How to do it in the simplest method?
How to do it in the simplest method?
sprintf
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
thank Salem!!!
I have a problem about precision multiplication. The code I wrote below.
How to do a action like "a = precMult(b, c, lenB, lenC, &lenA)". a, b, c are array. lenA, lenB, lenC are int.Code:int charToInt(char x) { return (int)(x-'0'); } char intToChar(int x) { return (char)(x+'0'); } void reverse(char *x, int lenX) { int i; char t; for (i = 0; i < (int)(lenX/2); i++) { t = x[i]; x[i] = x[lenX-i-1]; x[lenX-i-1] = t; } for (i = 0; i < lenX; i++) printf("%c", x[i]); printf("\n"); } char *precMult(char *a, char *b, int *lenC, int lenA, int lenB) { int i, j, lenMax, lenMin; int *tmp; char *c; lenMax = MAX(lenA, lenB); lenMin = MIN(lenA, lenB); tmp = malloc((lenA+lenB+1) * sizeof *tmp); c = malloc((lenA+lenB+1) * sizeof *tmp); reverse(a, lenA); reverse(b, lenB); for (i = 0; i < lenMax; i++) tmp[i] = 0; for (i = 0; i < lenMin; i++) { for (j = 0; j < lenMax; j++) { tmp[i+j] += charToInt(a[j])*charToInt(b[i]); if (tmp[i+j] >= 10) { tmp[i+j+1] += (int)tmp[i+j]/10; tmp[i+j] %= 10; } } } *lenC = lenA+lenB-1; if (tmp[lenA+lenB-1] > 0) (*lenC)++; for (i = *lenC-1; i >= 0; i--) c[i] = intToChar(tmp[i]); free(tmp); return c; }
Umm . . . change the order of the arguments?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
yes, dwks. I wanna change the order of arguments.
You're asking how you do that?
->Code:printf("Hello, %s. You are %d years old.\n", name, age);
Code:printf("Hello, %d-year-old %s.\n", age, name);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.