Thursday, January 28, 2010

OOP Challenge

The first official challenge of this semester
http://zenit.senecac.on.ca/wiki/index.php/OOP344#Challenge

Basic idea is to convert the integer into a string and then use bio_putstr(const char*) to display it.

Here is the solution:

138 void bio_putint(int val){
139 char temp[20] = "";
140 int i = 0;
141 int j = 0;
142 int length = 0;
143 int scale = 1;
144 if (val == 0)
145 bio_putch('0');
146 else{
147 for (i = 1, length = 0; val / i != 0 ; i *= 10, length++);
148 if (val > 0) {
149 for (i = 0; i < length; i++){
150 scale = 1;
151 for (j = 0; length - j > i + 1; j++)
152 scale *= 10;
153 temp[i] = (val / scale) % 10 + 48;
154 }
155 temp[i] = '\0';
156 }
157 else {
158 val *= -1;
159 temp[0] = '-';
160 for (i = 0; i < length; i++){
161 scale = 1;
162 for (j = 0; length - j > i + 1; j++)
163 scale *= 10;
164 temp[i+1] = (val / scale) % 10 + 48;
165 }
166 temp[i+1] = '\0';
167 }
168 }
169 bio_putstr(temp);
170 }


Tested on matrix. I strongly believe it can work on the other three platform. :-]

No comments:

Post a Comment