DIR Return Create A Forum - Home
---------------------------------------------------------
Class H22
HTML https://classh22.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: exercises
*****************************************************
#Post#: 12--------------------------------------------------
26.2 קוד מקור
DIR By: Noam
Date: March 1, 2014, 9:20 am
---------------------------------------------------------
היות
ותאריך
ההגשה עבר,
אין מניעה
לשחרר
קודים
מקוריים,
למי שזה
יכול לעזור
לו, ללמידה
וכמקור
להשוואה.
אז הנה
הקודים שלי
לפירמידות
ולפלינדרום.
--- Code ---
/*
* Author: Noam Chen
* Date: 26.2.14
*
* This program gets a positive int amount of lines,
* and draws three pyramids.
*/
#include <stdio.h>
void func1(int);
void func2(int);
void func3(int);
void PutMultiChars(char c, int repeats);
int main()
{
double N;
/*gets a positive integer and performs a sanity check*/
do
{
printf("Please enter a positive integer:\n");
if (!scanf("%lf", &N) || N != (int) N)
{
printf("error, only positive integers allowd!\n");
return -1;
}
}while (N <= 0);
putchar('\n');
func1(N);
putchar('\n');
func2(N);
putchar('\n');
func3(N);
return 0;
}
/*this funtion prints out a half -pyramid*/
void func1(int lines)
{
int i,j;
for (i = 0; i < lines; ++i)
{
PutMultiChars('*', i + 1);
putchar('\n');
}
}
/*this function prints out a half-pyramid on top an
upside-down half-pyramid with the same base*/
void func2(int lines)
{
int i,j;
/*print pyramid*/
func1(lines);
/*print upside-down pyramid 1 size smaller*/
for (i = lines - 1; i > 0; --i)
{
PutMultiChars('*', i);
putchar('\n');
}
}
/*this function prints out a full-pyramid*/
void func3(int lines)
{
int i,j;
for (i = 0; i < lines; ++i)
{
PutMultiChars(' ', lines - i);
PutMultiChars('*', i * 2 + 1);
putchar('\n');
}
}
/*this function prints out a given char repeatedly*/
void PutMultiChars(char c, int repeats)
{
int j;
for (j = 0; j < repeats; ++j)
{
putchar(c);
}
}
--- End Code ---
--- Code ---
/*
* Author: Noam Chen
* Date: 26.2.14
*
* palindrom - this program checks whether the input integer
number
* is a palindrome or not. it returns 1 if it is and 0 if it
isn't.
*/
#include <stdio.h>
int main ()
{
int digit, tmp, mosh_rev = 0;
double num;
/*gets a non-negative integer and performs a sanity check*/
do
{
printf("Please enter a non-negative integer:\n");
if (!scanf("%lf", &num) || num != (int) num)
{
printf("error, only non-negative integers allowd!\n");
return -1;
}
}while (num < 0);
/*reverse the number*/
tmp = (int) num;
while (tmp > 0)
{
digit = tmp % 10;
tmp /= 10;
mosh_rev = mosh_rev * 10 + digit;
/*printf("%d\n", mosh_rev);*/
}
if (mosh_rev == (int) num)
{
/*printf("%d\n", mosh_rev);*/
return 1;
}
else
{
return 0;
}
}
--- End Code ---
#Post#: 13--------------------------------------------------
Re: 26.2 קוד מקור
DIR By: SpiderGoat
Date: March 1, 2014, 10:15 am
---------------------------------------------------------
my palindrom:
int palindrom (unsigned int number)
{
int digits=0, counter=1, i;
unsigned int num=number, head, hiHead;
while (num) /* how many digits */
{
num = num/10;
digits++;
}
counter = digits/2; /* checking how many times */
num = number;
while (counter>0)
{
head = num;
for (i=1 ; i<digits ; i++)
{
head = head/10;
} /* first digit */
if ((num%10) == head) /* first == last */
{
hiHead = head;
for (i=1 ; i<digits ; i++)
hiHead = hiHead*10; /* header */
num = (num-(hiHead))/10; /* takes off first and last digits
*/
counter--; /* one less action */
digits = digits-2;
} /* end of if identical */
else
{
return 0; /* not palindrom */
}
} /* end of while checking */
return 1; /* palindrom */
} /* end of palindrom */
*****************************************************
Page 1 of 1