C Primer Plus 第五版 课后练习

             C Primer Plus 
                        ——普拉塔(第五版)


             
第一章 概述


1.就编程而言,可移植表示什么?
  答:程序可以在不同的系统中运行。
2 解释源代码,目标代码和可执行代码的区别?
 答:源代码:用各种编程语言编写的的程序
     目标代码:编译器将源代码变成机器语言即目标代码
     可执行代码:链接将库代码和启动代码与目标代码合成可执行代码。
3  编程的七个步骤
  答:设计程序目标,程序设计,编写代码,编译,运行,测试和调试,维护和修改。
4  编译器的任务:将源代码变成目标代码
5  链接器任务:将库代码,启动代码与目标代码合成可执行代码。


目标:将英寸转化为厘米;
步骤:1.英寸与厘米公式:1英寸=2.54cm 2.输入英寸a 3.厘米=2.54*a 4.输出厘米


           第二章 C语言概述


 1.C程序的基本模块为 函数
 2.语法错误 I is a simple computer! (int a,)
 3.语义错误 (int n,n2,n3;n2=n*n;n3=n2*n2;n3!=n*n*n;)
 4.#include<stdio.h>
   int main(void)
   { 
    int s;
    s=56;
    printf("There are %d weeks in a year.",s);
    return 0;
}
5.输出结果
 a. Baa Baa Black Sheep.Have you any wool?
 b.Begon!
   0 creature of lard!
 c.What?
   No/nBonzo?
 d.2 + 2 = 4


6.关键字 int char
7.printf("There are %d words and %d lines\n",words,lines);
8.b=5;a=5;
编程练习
1.#include<stdio.h>
  int main(void)  
  {
    printf("Shen Youli\n");
    printf("Shen\nYouli\n");
    printf("Shen");
    printf("Youli");
    return 0;
   }




2.#include<stdio.h>
  int main(void)
  {
   printf("Shen,anhui");
   return 0;
   }




3.#include<stdio.h>
   int main(void)
   {
     int age;
     printf("Please enter your age.\n");
     scanf("%d",&age);
     printf("%d year is %d days.",age,365*age);
     return 0;
    }


4.#include<stdio.h>
  void first(void);
  void secand(void);
  int main(void)
  {
   printf("For he's a jolly good fellow!\n");
   printf("For he's a jolly good fellow!\n");
   first();
   secand();
   return 0;
}


void first(void)
{
 printf("For he's a jolly good fellow!\n");
}

void secand(void)
{
 printf("For he's a jolly good fellow!");
}


5.#include<stdio.h>
int main(void)
{
 int toes=10;
 printf("toes=%d,toes squared=%d,toes cubed=%d",toes,toes*toes,toes*toes*toes);
 return 0;
}


6.#include<stdio.h>
  void Smile(void);
  int main(void)
  {
   Smile();
   Smile();
   Smile();
   printf("\n");
   printf("Smile!Smile!\n");
  Smile();
  return 0;
  }

void Smile(void)
{
 printf("Smile!");
}


7.#include<stdio.h>
  void one_three(void);
  void two(void);
  int main(void)
{
 printf("starting now:\n");
 one_three();
 two();
return 0;
}


void one_three(void)
{
 printf("one\n");
}
void two(void)
{
 printf("two\nthree\ndone!");
}


              第三章 数据和C


1. a.int b.float c.char d.int


2.有的系统long 比 int存储等大的数, 需要处理更      大的整数


3.   int32_t,至少是32位 int_least32_t 计算速度最快int_fast32_t


4.'\b'转义字符 退格;1066 整型;99.44 浮点型 0XAA 十六进制整型  2.0e30 浮点数的指数形式


5.#include<stdio.h>
  int main(void)
  {
    float g,h;
    float tax,rate;
    g=1.0e21;
    tax=rate*g;//g 没有赋值


  }




6.12 int %d;0x3 int %#x;'C' char %c;2.34E07 double %e;'\040' char %c; 7.0 float %f;6L long %ld;6.0f float %f


7.012 int %#o;2.9e05l float %e;'s' char %c;100000 long %ld;'\n' char %c; 20.0f float %f;0x44 int %#x


8. #include<stdio.h>
   int main(void)
  {
  int imate=2;
  long shot=53456;
  char grade='A';
  float log=2.71828;
  printf("The odds against the %d were %d to 1.\n",imate,short);
 printf("A score of %f is not an %c grade.\n",log,grade);
  return 0;
}


9.#include<stdio.h>
  int main(void)
  {
   char ch;
   ch='\r';
   ch=13;
   ch='\015';
   ch='\xd';
}


10.#include<stdio.h>
   int main(void)
  {
   
}


11. \n 换行符,\\ 反斜杠, \"双引号  \t 水平制表符


编程练习
1. #include<stdio.h>
   int main(void)
  {
   int num=32768;
   float mnum=123456789123454.98;
   float mnum1;
   mnum1=-mnum;
   printf("%d\n %f\n %f",num,mnum,mnum1);
   return 0;
}


2.#include<stdio.h>
  int main(void)
  {
    int num;
    printf("Please enter ASCII num\n");
   scanf("%d",&num);
   printf("ASCII num is %c",num);
   return 0;
 
  }


3.#include<stdio.h>
  int main(void)
  {
  printf("%c",'\a');
  printf("Startled by the sudden sound,Sally shouted,"By the Great Pumpkin, what was that!"");
 return 0;
}


4.#include<stdio.h>
  int main(void)
  {
   float num;
   num=1234.45;
  printf("num is %f and %e",num,num);
  return 0;
}


5.#include<stdio.h>
  int main(void)
  {
   int age;
   long double s;
   printf("Please enter your age\n");
   scanf("%d",&age);
   s=age*3.156*e7;
   printf("Your age %d is %e s",age,s);
   return 0;
}


6.#include<stdio.h>
  int main(void)
 {
   int kt;
   long long num;
   printf("Please enter kuat num\n");
   scanf("%d",kt);
   num=kt/(3.0*e-23);
   printf("%d kuat has %ld num",kt,num);
  return 0;
   
}
7.#include<stdio.h>
  int main(void)
  {
    int g;
    float yc;
   printf("Please enter your g\n");
   scanf("%d",&g);
   yc=g/2.54;
   printf("%d is %f",g,yc); 
   return 0;
}
            第四章字符串和格式化输入\输出


1.姓并没有输入,因为scanf("%s",&name)遇空白就停止


2.a.234.50 b.Hi! c.His Hamlet was funny without being vulgar     d.1.20e03 1201.00
3./"/"
4.#include<stdio.h>
  #define B booboo
  #define X 10
  int main(void)
  {
   int xp,age;
   char name[40];
   printf("Please enter your first name. ");
   scanf("%s",name);
   printf("All right, %s,what's your age?\n",name);
   scanf("%d",&age);
   xp=age+X;
   printf("That's a %s! You must be at least %d.\n",B,xp);
    return 0;
   }


5. #include<stdio.h>
  #define BOOK "War and peace"
  int main(void)
  {
  float cost=12.99;
  float percent =80.0;
  printf("This copy of /"BOOK/" sells for $%f.",cost);
  printf("That is %0.0f%% of list");
  return 0;
}


6 a.%d b.%4x c.%10.3f d.%12.2e e.%-30s
7 a.%15lu b.%4#x c.%12.2E d.%+10.3f e.%8.8s
8 a.%6.4d  b.%*#o  c.%2c   d.%+*.*f  e.%-7.5s
9 int num=101;scanf("%d",&num);
  float a,b;  scanf("%f %f",&a,&b);
  char cha[40]; scanf("%s",cha);
  char cha[10]; int num;scanf("%s %d",&cha,&num);
                        scanf("%d",&num);
10  空白字符:空格 换行符 制表符
11.所有的括号都将替换


程序练习
 
1
#include<stdio.h>
 int main(void)
 {
  char name[18];
  char name1[10];
  printf("Please enter your name");
  scanf("%s,%s",name,name1);
  printf("Your name is %s,%s",name,name1);
  return 0;
}


2.
 #include<stdio.h>
 #include<string.h>
 int main(void)
 {
  char name[40];
  int width;
  printf("Please enter your name\n");
  scanf("%s",name);
  printf("\"%s\"\n",name);
  printf("\"%20s\"\n",name);
  printf("\"%-20s\"\n",name);
  width=3+strlen(name);
  printf("%*s",width,name);
  return 0;
}


3.
 #include<stdio.h>
 int main(void)
 {
  float num;
  printf("Please enter number\n");
  scanf("%f",&num);
  printf("The input is %.1f or %.1e\n",num,num);
  printf("The input is %.3f or %.3e",num,num); 
  return 0;
}


4.
#include<stdio.h>
 int main(void)
 {
 float tall;
 char name[40];
 printf("Please enter your name and your tall");
 scanf("%f %s",&tall,name);
 printf("%s,you are %f feet tall\n",name,tall);
 printf("%s,you are %f feet tall",name,tall*2.54);
 return 0;


}


5.
#include<stdio.h>
#include<string.h>
 int main(void)
{
 char name[10];
 char name1[10];
 int num,num1;
 printf("Please enter your name\n");
 scanf("%s %s",name,name1);
 printf("%s %s",name,name1);
 num=strlen(name);
 num1=strlen(name1);
 printf("%*d %*d",num,num,num1,num1);
 printf("%s %s",nume,nume1);
 printf("%*d %*d",num,num,num+1,num1);
 return 0;
}


6
#include<stdio.h>
#include<float.h>
int main(void)
{
 float num=1.0/3.0;
 double num1=1.0/3.0;
 printf("%.4f or %.12f or %.16f\n",num,num,num);
 printf("%.4lf or %.12lf or %.16lf\n",num1,num1,num1);
 printf("%lf or %lf",FLT_DIG,DBL_DIG);
 return 0;
}

#include<stdio.h>
int main(void)
{
  const float k1=3.785;
  const float k2=1.609;
  float yl,jl;
  printf("Please enter \n");
 scanf("%f%f",&yl,&jl);
  printf("%.1f\n",yl/jl);
 printf("%.1f",yi*1.609/(jl*3.785));
 return 0;
}





































  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值