C primer 编程练习 (不断更新)
目前在看《C Primer》,以后会经常在这篇博客里更新课后的编程练习题
第二章:编程练习
2.1
#include <stdio.h>
int main(void)
{
printf("Anton Bruckner\n");
printf("Anton\nBruckner\n");
printf("Anton");
printf("Bruckner");
return 0;
}
2.2
#include <stdio.h>
int main(void)
{
printf("姓名:霍义霞\n");
printf("地址:北京电子科技学院\n");
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\n",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!\nFor he's a jolly good fellow!\nFor he's a jolly good fellow!\n");
}
void fellow(void)
{
printf("which nobody can deny\n");
}
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\n",toes,toes_double,toes_square);
return 0;
}
2.6
#include <stdio.h>
void smile_one(void);
int main(void)
{
printf("smile!smile!smile!\n");
printf("smile!smile!\n");
smile_one();
return 0;
}
void smile_one(void)
{
printf("smile!\n");
}
2.7
#include <stdio.h>
void one();
void two();
void three();
int main(void)
{
printf("starting now:\n");
one();
two();
three();
printf("done\n");
return 0;
}
void one()
{
printf("one\n");
}
void two()
{
printf("two\n");
}
void three()
{
printf("three\n");
}
第三章 编程练习
3.1
#include<stdio.h>
int main(void) /*本机使用的是32位系统 */
{
int i=2147483647;
float f_up=1.234567e38;
float f_down=1.234567;
printf("%d\n",i+1); //有符号整型最大表示 2147483647,故溢出
printf("%f\n",f_up*10); //单精度浮点数最大指数为38,故上溢出
printf("%f\n",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\n",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!\"\n" );
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\n",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\n",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\n",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)\n",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\n",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\"\n",name);
printf("\"%20s\"\n",name);
printf("\"%-20s\"\n",name);
printf("\"%s \"\n",name);
return 0;
}
4.3
#include <stdio.h>
int main()
{
float a = 21.29;
printf("the input is %.1f or %0.1e\n",a,a);
printf("the input is +%.3f or %0.3E\n",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\n",name,height);
printf("please input your height(cm) and your name:");
scanf("%f %s",&height,name);
printf("%s,you are %.3f meters tall\n",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\n",f_name,l_name);
f = strlen(f_name);
l = strlen(l_name);
printf("%*d %*d\n",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\n",a,b);
printf("%.12f %.12f\n",a,b);
printf("%.16f %.16f\n",a,b);
printf("%f %lf\n",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\n",m/g);
printf("That is %f litres/hkm\n",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\n",hours,minutes);
printf("again:");
scanf("%ld\n",&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\n",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\n",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\n",centimeters,feet,inches);
printf("Enter a height in centimeters(<=0 to quit):");
scanf("%f",¢imeters);
}
printf("Bye\n");
return 0;
}
5.5
#include<stdio.h>
int main()
{
int count,i = 10,sum;
printf("please input 20 integer:\n");
while(i--)
{
scanf("%d",&count);
sum+=count;
}
printf("the sum of the 20 count is : %d\n",sum);
return 0;
}
5.6
#include<stdio.h>
int main()
{
int count,i = 10,sum;
printf("please input 20 integer:\n");
while(i--)
{
scanf("%d",&count);
count=count*count;
sum+=count;
}
printf("the sum of the 20 count is : %d\n",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*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\n",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("\n");
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("\n");
}
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("\n");
}
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("\n");
}
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:\n");
}
else
{
for (;down_limit<=up_limit;down_limit++)
{
square = down_limit*down_limit;
cube=square*down_limit;
printf("%6d%6d%6d\n",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:\n");
scanf("%s",&word);
n = strlen(word);
for (int i=n-1;i>=0;i--)
{
printf("%c",word[i]);
}
printf("\n");
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\n",(a-b)/(a*b));
printf("input again\n");
}
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\n",test(first,second));
printf("input again\n");
}
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\n",lower*lower,super*super,sum);
sum=0;
printf("Enter next set of limits:");
scanf("%d%d",&lower,&super);
}
printf("Done\n");
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("\n");
return 0;
}
C primer 编程练习 (不断更新)的更多相关文章
- Asky极简教程:零基础1小时学编程,已更新前8节
Asky极简架构 开源Asky极简架构.超轻量级.高并发.水平扩展.微服务架构 <Asky极简教程:零基础1小时学编程>开源教程 零基础入门,从零开始全程演示,如何开发一个大型互联网系统, ...
- Android中多线程编程(三)Handler更新UI的方式
Handler更新UI的方式和原因以及遇到的问题 1.方式: 仅仅能通过Handler来更新UI. 代码例如以下: package com.chengdong.su.handlerdemo; impo ...
- WinForm/Silverlight多线程编程中如何更新UI控件的值
单线程的winfom程序中,设置一个控件的值是很easy的事情,直接 this.TextBox1.value = "Hello World!";就搞定了,但是如果在一个新线程中这么 ...
- Python socket编程之五:更新分时图
f1.py # -*- coding: utf-8 -*- import socket import struct import sqlalchemy import pandas ########## ...
- intellij idea pycharm phpstorm webstorm 使用 FiraCode 作为编程字体,更新后字符乱码问题解决
先说使用下载 传送门 https://pan.baidu.com/s/1OI-novVYy-C74HIUfr9E6w windows: 1.下载后打开ttf文件夹,选择所有右键安装. 2.或者使用ch ...
- 总结切面编程AOP的注解式开发和XML式开发
有段日子没有总结东西了,因为最近确实有点忙,一直在忙于hadoop集群的搭建,磕磕碰碰现在勉强算是能呼吸了,因为这都是在自己的PC上,资源确实有点紧张(搭建过程后期奉上),今天难得大家都有空(哈哈哈~ ...
- 【Socket编程】Java通信是这样炼成的
简介 网络无处不在,移动互联时代也早已到来,单机版程序慢慢的已没有生命力,所有的程序都要能够访问网络,比如 QQ 网络聊天程序.迅雷下载程序等,这些程序都要同网络打交道,本次将与各位小伙伴们分享的就是 ...
- 编程范式:命令式编程(Imperative)、声明式编程(Declarative)和函数式编程(Functional)
主要的编程范式有三种:命令式编程,声明式编程和函数式编程. 命令式编程: 命令式编程的主要思想是关注计算机执行的步骤,即一步一步告诉计算机先做什么再做什么. 比如:如果你想在一个数字集合 collec ...
- [SharePoint][SharePoint Designer 入门经典]Chapter13 客户端Silverlight编程
1.使用Silverlight,CAML和Linq取得数据 2.编程性创建更新删除列表数据项 3.修饰列表和库的配置 4.管理文件和文件夹 5.修改快速启动和顶部导航条 [使用Silverlight, ...
随机推荐
- 如何通过.Net Compact Framework来获得应用程序的当前路径
在Win CE上是没有驱动器的概念的,所以要想使用System.IO.Directory.GetCurrentDirectory()来获得当前路径的话,在CF中会遇到未知错误. 应该使用Path. ...
- 【BZOJ】2819: Nim(树链剖分 / lca+dfs序+树状数组)
题目 传送门:QWQ 分析 先敲了个树链剖分,发现无法AC(其实是自己弱,懒得debug.手写栈) 然后去学了学正解 核心挺好理解的,$ query(a) $是$ a $到根的异或和. 答案就是$ l ...
- Window下MySql 5.6 安装后内存占用很高的问题
Window下MySql 5.6 安装后内存占用很高的问题 刚刚准备玩一把mysql,初学者 环境是window 7和window sever 2008, mysql是最新的5.6, 发现的问题是安装 ...
- C#Remoting
C# Remoting 细细品味C#——.Net Remoting专题 http://www.cnblogs.com/xia520pi/archive/2011/11/02/2233371.htm ...
- RSA_JS_PHP加密解密
root@DESKTOP-I4OIMJC /cygdrive/e/html/RSA_JS_PHP/openssl/bin # ./openssl.exe OpenSSL> genrsa -out ...
- nginx 1.12 配置解析php
server { listen 80; server_name foo.com; root /path; index index.html index.htm index.php; location ...
- sort_region——对区域进行排序
The operator sort_region sorts the regions with respect to their relative position. All sorting meth ...
- django -- url 的 默认值
在urls.py里可以直接向函数传递默认值,看代码: urls.py from django.conf.urls import url from mytest import views urlpatt ...
- select sum也会返回null值
SELECT SUM(detail.VAL) FROM AI_SDP_ORDER_MONTH_DETAIL_201706 detail 如果所有的VAL都是null的话,或者根本就不存在 ...
- hibernate中 criteria.setProjection(Projections.rowCount()).uniqueResult()为null的Bug
在hibernate中,在查询总数时,会使用如下方法; public Integer getCount(final DetachedCriteria detachedCriteria) { ...