花了一下午时间,用VS2015将C Peimer 第九章的编程习题做了一遍,即熟悉了VS,又回忆了C语言,记录一下。
// Chapter09_2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#define DL "************"
double minMy(double x, double y) { return x > y ? y : x; };
void chLine(char ch, int i, int j);
void showLetter(char ch,int i,int j);
double harmonicAverage(double x,double y);
void largerOf(double *x, double *y);
double powerMy(double x,int n);
double powerRe(double x, int n);
int to_base_n(int num,int n);
int Fibonacci(int n);
int main()
{
double result;
//1
printf("\n1.%s%s\n", DL, DL);//分割线!**************
result = minMy(1.5,2.6);
printf("result is %lf",result);
printf("\n2.%s%s\n", DL, DL);//分割线!**************
//2
chLine('a', 4, 9);
printf("\n3.%s%s\n", DL, DL);//分割线!**************
//3
showLetter('h', 5, 7);
printf("\n4.%s%s\n", DL, DL);//分割线!**************
//4
printf("this harmonic average is %lf", harmonicAverage(4, 4));
printf("\n5.%s%s\n", DL, DL);//分割线!**************
//5
double x5 = 5.9;
double y5 = 8.7;
largerOf(&x5,&y5);
printf("x5 is %lf,y5 is %lf", x5, y5);
printf("\n6.%s%s\n", DL, DL);//分割线!**************
//6
char temp;
while ((temp = getchar())!=EOF) {
if ((temp<='Z'&&temp >= 'A')>0) {
printf("%d",temp-'A'+1);
}
else if ((temp <= 'z'&&temp >= 'a')>0) {
printf("%d", temp - 'a' + 1);
}
}
printf("\n7.%s%s\n", DL, DL);//分割线!**************
//7
printf("%lf", powerMy(-3, 3));
printf("\n8.%s%s\n", DL, DL);//分割线!**************
//8
printf("recursion method computing is %lf", powerRe(3, 3));
printf("\n9.%s%s\n", DL, DL);//分割线!**************
//9
int temp2;
printf("the result is :");
temp2 = to_base_n(8,5);
printf("final result is %d",temp2);
printf("\n9.%s%s\n", DL, DL);//分割线!**************
//10
temp2 = Fibonacci(8);
printf("Fibonacci is %d",temp2);
return 0;
}
void chLine(char ch, int i, int j)
{
int counter;
for (counter = 0; counter < j; counter++) {
if (counter<i) {
printf(" ");
}
else {
printf("%c", ch);
}
}
}
void showLetter(char ch, int i, int j)
{
int row, col;
for (row = 0; row < j; row++) {
for (col = 0; col < i; col++) {
printf("%c", ch);
}
printf("\n");
}
}
double harmonicAverage(double x, double y)
{
x = 1 / x;
y = 1 / y;
x = (x + y) / 2;
x = 1 / x;
return x;
}
void largerOf(double *x, double *y)
{
double temp = *x > *y ? *x : *y;
*x = temp;
*y = temp;
}
double powerMy(double x,int n)
{
int counter;
double total = 1;
if (x != 0) {
for (counter = 1; counter <= n; counter++) {
total *= x;
}
}
else {
total == 0;
}
return total;
}
double powerRe(double x, int n)
{
double result = x;
if (n==1) {
result = x;
}
else {
result *= powerRe(x, n - 1);
}
return result;
}
int to_base_n(int num, int n)
{
int r;
r = num % n;
if (num >= n) {
r += 10 * to_base_n(num / n, n);
}
//putchar('0'+r);
return r;
}
int Fibonacci(int n)
{//斐波那契数列
int temp;
if (n==1) {
temp = 1;
}
else if (n==2) {
temp = 1;
}
else {
temp = Fibonacci(n - 1) + Fibonacci(n - 2);
}
return temp;
}