מבוא לתכנות ולמדעי המחשב בשפת C/תרגיל 3: הבדלים בין גרסאות בדף

תוכן שנמחק תוכן שנוסף
imported>איתמר לנדסמן
שורה 315:
</center>
 
 
== פתרון ==
 
=== q1.c ===
<syntaxhighlight>
#include <stdio.h>
#include <math.h>
 
int mySign(double x) {
// Returns 1 if x is a positive number, -1 if it is negative and 0 if it is zero.
if(x < 0)
return -1;
else
if(x>0)
return 1;
return 0;
}
 
double myAbs(double x) {
// Returns the absolute value of x
return mySign(x)*x;
}
 
void count(int from, int to) {
// Prints the numbers between 'from' and 'to' .
// if to > from, it prints from, from+1, from+2, .. , to
// if to <= from, it prints from, from-1, from-2, .., to
int i;
int d = mySign(to-from);
for(i=from; i*d <= to *d; i+=d) {
printf("%d\n",i);
}
 
}
 
int myRound(double x) {
// Round x to the nearest integer. If there are two with the same distance, assume
// x had some more decimal digits that are not zero
 
double fx = floor(x);
double d = x -fx ; // d in [0,1)
if(x < 0)
if(d <= 0.5)
return fx;
else
return fx+1;
else
if(d >= 0.5)
return fx+1;
else
return fx;
}
 
 
int main() {
printf("mySign(5.5) = %d\n",mySign(5.5));
printf("mySign(-5.5) = %d\n",mySign(-5.5));
 
printf("myAbs(5.5) = %lf\n",myAbs(5.5));
printf("myAbs(-5.5) = %lf\n",myAbs(-5.5));
 
printf("counting from 1 to 10:\n");
count(1,10);
 
printf("counting from 10 to 1:\n");
count(10,1);
 
double checks[] = {0,5,5.4,5.5,5.6};
int n = sizeof(checks)/sizeof(double),i;
 
for(i=0; i<n; ++i) {
double x = checks[i];
printf("myRound(%lf) = %d\n", x, myRound(x));
printf("myRound(%lf) = %d\n", -x, myRound(-x));
 
}
 
return 0;
}
 
 
</syntaxhighlight>
=== q2.c ===
<syntaxhighlight>
#include <stdio.h>
#include <math.h>
 
int ROWS = 30, COLS = 70;
int board[30][70];
void plot(int r, int c, char symbol){
if(r>=0 && r<ROWS && c >=0 && c < COLS)
board[r][c] = symbol;
}
 
void initBoard() {
int i,j;
for(i=0; i<ROWS; ++i)
for(j=0; j<COLS; ++j)
board[i][j] = '.';
}
 
void printBoard() {
int i,j;
for(i=0; i<ROWS; ++i) {
for(j=0; j<COLS; ++j)
printf("%c",board[i][j]);
printf("\n");
}
}
 
int mySign(double x) {
if(x<0)
return -1;
if(x>0)
return 1;
return 0;
}
 
double myAbs(double x) {
if(x>=0)
return x;
return -x;
}
 
void line(int r1, int c1, int r2, int c2) {
// Draw a straight line from (r1,c1) to (r2,c2)
// r1 - the row of the start point
// c1 - the column of the start point
// r2 - the row of the end point
// c2 - the column of the end point
 
 
double dr = r2-r1, dc = c2-c1;
double m = dr/dc;
 
if(myAbs(m) <= 1) {
int x;
int d = mySign (dc);
for(x = c1; x*d <= c2*d; x += d) {
double y = r1+(x-c1)*m;
plot(round(y),x,'@');
}
} else {
int y;
int d = mySign (dr);
for(y = r1; y*d <= r2*d; y += d) {
double x = c1+(y-r1)*(1/m);
plot(y,round(x),'@');
}
}
}
 
int main() {
initBoard();
int ry = ROWS/2, rx = COLS/2, y = ROWS/2, x = COLS/2;
double alpha;
 
for(alpha = 0; alpha < 2*M_PI; alpha += .5) {
int y1 = round( y+sin(alpha)*ry );
int x1 = round( x+cos(alpha)*rx );
line(y, x, y1, x1);
}
 
printBoard();
return 0;
}
 
 
</syntaxhighlight>
=== q3.c ===
<syntaxhighlight>
 
#include <math.h>
#include <stdlib.h>
 
int mySign(double x) {
if(x<0)
return -1;
if(x>0)
return 1;
return 0;
}
 
double myAbs(double x) {
if(x>=0)
return x;
return -x;
}
 
void line(int x1, int y1, int x2, int y2, char c) {
// draw a stright line from (x1,y1) to (x2,y2). c is its color
double dy = y2-y1, dx = x2-x1;
double m = dy/dx;
 
if(myAbs(m) <= 1) {
int x;
int d = mySign (dx);
for(x = x1; x*d <= x2*d; x += d) {
double y = y1+(x-x1)*m;
point(x,round(y),c);
}
} else {
int y;
int d = mySign (dy);
for(y = y1; y*d <= y2*d; y += d) {
double x = x1+(y-y1)*(1/m);
point(round(x),y,c);
}
}
}
 
void recrangle(int x, int y, int width, int height, char c) {
line(x,y,x+width,y,c);
line(x,y,x,y+height,c);
line(x+width,y,x+width,y+height,c);
line(x,y+height,x+width,y+height,c);
}
 
void square(int x, int y, int size, char c) {
recrangle(x,y,size,size,c);
}
 
void triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c) {
line(x1,y1,x2,y2,c);
line(x2,y2,x3,y3,c);
line(x3,y3,x1,y1,c);
}
 
void house(int x,int y, int size, char c) {
triangle(x,y,x-size,y+size,x+size,y+size,c);
recrangle(x-(.7)*size,y+size,(1.4)*size,(1.5)*size,c);
recrangle(x-size/3,y+1.6*size, size/3, .9*size,c);
square(x+0.3*size,y+1.2*size,size/3,c);
}
 
void tree(int x, int y, int size, char c) {
recrangle (x,y,2,size,c);
int i;
for(i=0; i<size/3; ++i) {
line(x,y+i*2,x-size/4,y+i*2+size/4,c);
line(x,y+i*2,x+size/4,y+i*2+size/4,c);
}
}
 
void drawSun(int x, int y, int r, char c1, char c2) {
// Draw a "Sun"
// (x,y) - location of the sun's center
// r - radius of the sun
// c1,c2 - alternating colors
double alpha;
char c = c1;
for(alpha=0; alpha < 2*M_PI; alpha += 0.1) {
line(x,y,x+cos(alpha)*r,y+sin(alpha)*r,c);
if (c==c1)
c = c2;
else
c = c1;
}
}
 
char colors[] = {'r','b','g','l'};
 
void drawStreet(int x, int y, int size, int n) {
// Draw a street of alternate colors of trees and houses
// (x,y) - location of the top of the leftmost house
// size - of houses
// n - number of house and trees
// The size of the trees varies randomly withing certain range.
int c = 0,i;
double g = 3;
for(i=0; i<n; ++i) {
house(x+size*g*i,y,size,colors[c]);
double rnd = (random()%20)/10.0-1;
double treeSize = (2+rnd)*size;
tree(x+size*g*i+size*1.5, y+(2.5)*size-treeSize, treeSize, colors[c]);
c = (c+1)%4;
}
}
 
 
void myFunc() {
 
drawStreet(30,60,15,6);
drawStreet(55,120,30,4);
drawStreet(70,220,50,3);
 
drawSun(40,40,30,'r','g');
}
 
</syntaxhighlight>
 
<center>