目前在看《C Primer》,以后会经常在这篇博客里更新课后的编程练习题
第二章:编程练习
2.1
#include <stdio.h>
int main(void)
{
printf("Anton Bruckner
");
printf("Anton
Bruckner
");
printf("Anton");
printf("Bruckner");
return 0;
}
2.2
#include <stdio.h>
int main(void)
{
printf("姓名:霍义霞
");
printf("地址:北京电子科技学院
");
return 0;
}
2.3
#include <stdio.h>
int main(void)
{
int age;
int days;
age = 24;
days = age*365;
printf("my age is %d,that is %d days
",age,days);
return 0;
}
2.4
#include <stdio.h>
void jolly(void);
void fellow(void);
int main(void)
{
jolly();
fellow();
return 0;
}
void jolly(void)
{
printf("For he's a jolly good fellow!
For he's a jolly good fellow!
For he's a jolly good fellow!
");
}
void fellow(void)
{
printf("which nobody can deny
");
}
2.5
#include <stdio.h>
int main(void)
{
int toes=10;
int toes_double;
int toes_square;
toes_double=toes+toes;
toes_square=toes*toes;
printf("toes=%d,toes_double=%d,toes_square=%d
",toes,toes_double,toes_square);
return 0;
}
2.6
#include <stdio.h>
void smile_one(void);
int main(void)
{
printf("smile!smile!smile!
");
printf("smile!smile!
");
smile_one();
return 0;
}
void smile_one(void)
{
printf("smile!
");
}
2.7
#include <stdio.h>
void one();
void two();
void three();
int main(void)
{
printf("starting now:
");
one();
two();
three();
printf("done
");
return 0;
}
void one()
{
printf("one
");
}
void two()
{
printf("two
");
}
void three()
{
printf("three
");
}
第三章 编程练习
3.1
#include<stdio.h>
int main(void) /*本机使用的是32位系统 */
{
int i=2147483647;
float f_up=1.234567e38;
float f_down=1.234567;
printf("%d
",i+1); //有符号整型最大表示 2147483647,故溢出
printf("%f
",f_up*10); //单精度浮点数最大指数为38,故上溢出
printf("%f
",f_down/10); //单精度浮点保留6位有效数字,故下溢出
return 0;
}
3.2
#include<stdio.h>
int main(void) /*ASCII码和字符的转换*/
{
char a;/*保证输入的只有8位*/
printf("Please enter an ASCII number between 0-127:");
scanf("%d",&a);
printf("the ASCII number means:%c
",a);
return 0;
}
3.3
#include<stdio.h>
int main(void)
{
printf("aStartked by the sudden,Sally shouted,"By the Gteat Pumpkin,what was that!"
" );
return 0;
}
3.4
#include<stdio.h>
int main(void)
{
float a;
printf("please input a float number:");
scanf("%f",&a);
printf("the input is %f or %e
",a,a);
return 0;
}
3.5
#include<stdio.h>
int main(void)
{
int a = 3.156e7;
int age;
printf("please input your age:");
scanf("%d",&age);
printf("the age is %d,that is %e seconds
",age,age*a);
return 0;
}
3.6
#include<stdio.h>
int main()
{
float k;
printf("Please input the weight:");
scanf("%f",&k);
printf("It includes %e molecules
",k*950/3.0e-23);
}
3.7
#include<stdio.h>
int main()
{
float height;
printf("Please input your height(cm):");
scanf("%f",&height);
printf("Your height is:%f(inch)
",height/2.54);
}
第四章 编程练习
4.1
#include <stdio.h>
int main()
{
char f_name[20],l_name[20];
printf("please input your first name and last name:");
scanf("%s %s",f_name,l_name);
printf("%s,%s
",l_name,f_name);
return 0;
}
4.2
#include <stdio.h>
int main()
{
char name[40];
printf("please input your name:");
scanf("%s",name);
printf(""%s"
",name);
printf(""%20s"
",name);
printf(""%-20s"
",name);
printf(""%s "
",name);
return 0;
}
4.3
#include <stdio.h>
int main()
{
float a = 21.29;
printf("the input is %.1f or %0.1e
",a,a);
printf("the input is +%.3f or %0.3E
",a,a);
return 0;
}
4.4
#include<stdio.h>
int main() //
{
char name[20];
float height;
printf("please input your height(inch) and your name:");
scanf("%f %s",&height,name);
printf("%s,you are %.3f feet tall
",name,height);
printf("please input your height(cm) and your name:");
scanf("%f %s",&height,name);
printf("%s,you are %.3f meters tall
",name,height/100);
return 0;
}
4.5
#include<stdio.h>
#include "string.h"
int main() //对齐稍有难度,要用到*,*代表宽度,可参考课本p81
{
char f_name[20];
char l_name[20];
short f,l;
printf("please input your first name and last name:");
scanf("%s %s",f_name,l_name);
printf("%s %s
",f_name,l_name);
f = strlen(f_name);
l = strlen(l_name);
printf("%*d %*d
",f,f,l,l);
return 0;
}
4.6
#include<stdio.h>
#include <float.h>
int main()
{
double a = 1.0/3.0;
float b = 1.0/3.0;
printf("%.4f %.4f
",a,b);
printf("%.12f %.12f
",a,b);
printf("%.16f %.16f
",a,b);
printf("%f %lf
",FLT_DIG,DBL_DIG);
return 0;
}
4.7
#include<stdio.h>
#define GALLON_LITRE 3.785
#define MILE_KILOMETRE 1.609
int main()
{
float m,g;
printf("Please input the miles and gallons:");
scanf("%f%f",&m,&g);
printf("That is %f miles/gallon
",m/g);
printf("That is %f litres/hkm
",GALLON_LITRE *g/(MILE_KILOMETRE *m*100));
}
第五章编程练习
5.1
#include <stdio.h>
#define H_PER_M 60
int main()
{
long minutes;
long hours;
printf("please input the current_time in minutes:");
scanf("%ld",&minutes);
while(minutes > 0)
{
hours = minutes / H_PER_M;
minutes = minutes % H_PER_M;
printf("that time is %ld hours and %ld minutes
",hours,minutes);
printf("again:");
scanf("%ld
",&minutes);
}
return 0;
}
5.2
#include <stdio.h>
int main()
{
int a;
int i =10;
printf("please input the number:");
scanf("%d",&a);
while (i--)
printf("%3d",a++);
printf("%3d
",a);
return 0;
}
5.3
#include <stdio.h>
#define week 7
int main()
{
int days1;
int days;
int weeks;
printf("please input the days:");
scanf("%d",&days);
while (days > 0)
{
weeks = days/week;
days1 = days % week;
printf("%d days is %d weeks , %d days
",days,weeks,days1);
printf("input again:");
scanf("%d",&days);
}
return 0;
}
5.4
#include<stdio.h>
#define INCHES_PER_FEET 12
#define INCHES_PER_CM 0.393700
int main()
{
float centimeters,inches;
long feet;
printf("please input a height in centimeters:");
scanf("%f",¢imeters);
while(centimeters>0)
{
inches=centimeters*INCHES_PER_CM;
feet=inches/INCHES_PER_FEET;
inches=inches-feet*INCHES_PER_FEET;
printf("%.1f cm = %ld feet, %.1f inches
",centimeters,feet,inches);
printf("Enter a height in centimeters(<=0 to quit):");
scanf("%f",¢imeters);
}
printf("Bye
");
return 0;
}
5.5
#include<stdio.h>
int main()
{
int count,i = 10,sum;
printf("please input 20 integer:
");
while(i--)
{
scanf("%d",&count);
sum+=count;
}
printf("the sum of the 20 count is : %d
",sum);
return 0;
}
5.6
#include<stdio.h>
int main()
{
int count,i = 10,sum;
printf("please input 20 integer:
");
while(i--)
{
scanf("%d",&count);
count=count*count;
sum+=count;
}
printf("the sum of the 20 count is : %d
",sum);
return 0;
}
5.7
#include<stdio.h>
void cube(float);
int main()
{
float a;
printf("please input a float:");
scanf("%f",&a);
cube(a);
return 0;
}
void cube(float n)
{
printf("the number's cube is %f
",n*n*n);
}
5.8
#include<stdio.h>
void Temperatures(double);
int main()
{
double fahrenheit;
printf("please input the temperatures(fahrenheit)");
while(scanf("%lf",&fahrenheit))
{
Temperatures(fahrenheit);
printf("input again:");
}
return 0;
}
void Temperatures(double fahrenheit )
{
double celsius;
double kelvin;
double C_F_MULT = 1.8;
double C_F_PLUS = 32.0;
double K_C_PLUS = 273.16;
celsius = C_F_MULT*fahrenheit +C_F_PLUS;
kelvin = celsius + K_C_PLUS;
printf("%.2lf fahrenheit is %.2lf celsius or %.2lf kelvin
",fahrenheit,celsius,kelvin);
}
第六章 编程练习
6.1
#include<stdio.h>
int main()
{
int i = 0;
char c ='a';
char letter[26];
printf("please input the 26 letters:");
while(c <= 'z')
{
letter[i] = c;
printf("%c ",c);
i++;
c++;
}
printf("
");
return 0;
}
6.2
#include<stdio.h>
int main()
{
int i,j;
char c = '$';
for (i=0;i< 5;i++)
{
for (j=0;j < i+1;j++)
{
printf("%c",c);
}
printf("
");
}
return 0;
}
6.3
#include<stdio.h>
int main()
{
int i,j;
char c = 'F';
for (i=0;i<6;i++)
{
for (j=0;j<i+1;j++)
{
printf("%c",'F'-j);
}
printf("
");
}
return 0;
}
6.4
#include<stdio.h>
int main()
{
char letter;
int i,space,up,down;
printf("please input a capital letter:");
scanf("%c",&letter);
for (i=0;i<=letter-'A';i++)
{
for (space='A';space<letter-i;space++)
{
printf(" ");
}
for (up='A';up<='A'+i;up++)
{
printf("%c",up);
}
for (down=up-2;down>='A';down--)
{
printf("%c",down);
}
printf("
");
}
return 0;
}
6.5
#include<stdio.h>
int main()
{
int up_limit;
int down_limit;
int square,cube;
printf("enter the down_limitand up_limit:");
scanf("%d %d",&down_limit,&up_limit);
if (down_limit>up_limit)
{
printf("error:
");
}
else
{
for (;down_limit<=up_limit;down_limit++)
{
square = down_limit*down_limit;
cube=square*down_limit;
printf("%6d%6d%6d
",down_limit,square,cube);
}
}
return 0;
}
6.6
#include<stdio.h>
#include <string.h>
int main()
{
char word[20];
int n;
printf("Enter a word:
");
scanf("%s",&word);
n = strlen(word);
for (int i=n-1;i>=0;i--)
{
printf("%c",word[i]);
}
printf("
");
return 0;
}
6.7
#include<stdio.h>
int main()
{
float a,b;
printf("please input two float numbers:");
while (scanf("%f%f",&a,&b)==2)
{
printf("%f
",(a-b)/(a*b));
printf("input again
");
}
return 0;
}
6.8
#include<stdio.h>
float test(float,float);
int main()
{
float first,second;
printf("please input two float numbers:");
while (scanf("%f%f",&first,&second)==2)
{
printf("%f
",test(first,second));
printf("input again
");
}
return 0;
}
float test(float f,float s)
{
return (f-s)/(f*s);
}
6.9
#include<stdio.h>
int main() //用循环求平方的和
{
int super,lower,sum;
printf("Enter lower and upper integer limits:");
scanf("%d%d",&lower,&super);
while(lower<super)
{
for(int i=lower;i<=super;i++)
sum+=i*i;
printf("The sums of the squares "
"from %d to %d is %d
",lower*lower,super*super,sum);
sum=0;
printf("Enter next set of limits:");
scanf("%d%d",&lower,&super);
}
printf("Done
");
return 0;
}
6.10
#include<stdio.h>
#define SIZE 8
int main()
{
int number[SIZE];
printf("please input eight numbers:");
for(int i=0;i<8;i++)
scanf("%d",&number[i]);
for(int j=SIZE-1;j>=0;j--)
printf("%5d",number[j]);
printf("
");
return 0;
}