本文地址:http://www.cnblogs.com/archimedes/p/programming-abstractions-in-c-2.html,转载请注明源地址。

2、按照规定求圆柱的表面积和体积

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#define PI 3.1415926
void input(double *r, double *h)
{
double a, b;
printf("请输入半径和高度:");
scanf("%lf %lf", &a, &b);
*r = a;
*h = b;
}
void count(double r, double h, double *s, double *v)
{
*s = ( * PI * r) * h;
*v = PI * r * r * h;
}
void output(double s, double v)
{
printf("圆柱的表面积:%lf\n", s);
printf("圆柱的体积:%lf\n", v);
}
int main()
{
double r, h, s, v;
input(&r, &h);
count(r, h, &s, &v);
output(s, v);
return ;
}

3、按照规则计算平均值

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#define MaxJudges 100
double Mean(double array[], int n)
{
int i;
double total, max, min;
max = min = array[];
total = 0.0;
for(i = ; i < n; i++) {
total += array[i];
if(max < array[i])
max = array[i];
if(min > array[i])
min = array[i];
}
return ((total - min - max) / n);
}
void ReadScore(double scores[], int n)
{
int i;
for(i = ; i < n; i++)
scanf("%lf", &scores[i]);
}
int main()
{
int n;
double array[MaxJudges];
scanf("%d", &n);
ReadScore(array, n);
printf("平均值为:%lf\n", Mean(array, n));
return ;
}

4、判断一个数列是否升序

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
bool IsSorted(int *a, int n)
{
int i;
for(i = ; i <= n - ; i++) {
if(a[i] < a[i-])
return false;
}
return true;
}
int main()
{
int n, array[];
scanf("%d", &n);
for(int i = ; i < n; i++)
scanf("%d", &array[i]);
if(IsSorted(array, n))
printf("TRUE\n");
else
printf("FALSE\n");
return ;
}

5、埃拉托斯特尼筛生成2~1000之间的素数

#include<stdio.h>
#include<stdbool.h>
#include<math.h>
#define N 1000000
bool isprime[N];
void initPrime()
{
int n, i, j;
int k;
for(k = ; k < N; k++)
isprime[k] = true;
n = (int)sqrt(N);
for(i = ; i <= n; i++) {
if(isprime[i]) {
j = i * i;
while(j <= n) {
isprime[j] = false;
j += i;
}
}
}
}
int main()
{
int i = ;
initPrime();
while() {
if(i > ) break;
if(isprime[i])
printf("%d\n", i);
i++;
}
return ;
}

6、按照格式输出一组数据的柱状图

#include<stdio.h>
#include<stdbool.h>
#include<math.h>
void printStar(int n)
{
for(int i = ; i <= n; i++)
putchar('*');
printf("\n");
}
int main()
{
int a[], b[];
int n, t, i;
scanf("%d", &n);
for(i = ; i < ; i++)
a[i] = ;
for(i =; i < n; i++) {
scanf("%d", &b[i]);
t = b[i] / ;
switch(t) {
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
default: break;
}
}
for(i = ; i < ; i++) {
printf("%3d: ", i * );
printStar(a[i]);
}
return ;
}

8、扫描数组,出去数组中含0的元素,返回0的数目

#include<stdio.h>
#include<stdbool.h>
#include<math.h>
void swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
int RemoveZeroElements(int a[], int n)
{
int num, i, j;
num = ;
if(a[]) num++;
for(i = ; i < n; i++) {
if(!a[i] && i < n - ) {
for(j = i + ; j < n; j++) {
if(a[j]) {
num++;
swap(&a[i], &a[j]);
break;
}
}
}
}
return n - num;
}
void printArray(int *a, int n)
{
int i;
for(i = ; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
int main()
{
int a[] = {,,,,,,,,,,,,};
int n, zeronum;
n = sizeof(a) / sizeof(a[]);
printArray(a, n);
zeronum = RemoveZeroElements(a, n);
printArray(a, n - zeronum);
printf("zreo numbers: %d\n", zeronum);
return ;
}

10、找出一组输入数字中的最小值与最大值

#include<stdio.h>
#include<stdbool.h>
#include<math.h>
void solve()
{
double n, min, max;
printf("Enter the elements of the array, one per line.\n");
printf("use -1 to sigal the end of the list.\n");
printf("? ");
scanf("%lf", &n);
max = min = n;
printf("? ");
while(~scanf("%lf", &n) && (n != -)) {
if(n < min)
min = n;
if(max < n)
max = n;
printf("? ");
}
printf("The range of value is %lf-%lf\n", min, max); }
int main()
{
solve();
return ;
}

11、按要求动态分配一个整型数组并赋值

#include<stdio.h>
#include<stdbool.h>
#include<math.h>
#include<stdlib.h>
int *indexArray(int n)
{
int *p = (int *)malloc(n * sizeof(int));
for(int i = ; i < n; i++)
p[i] = i;
return p;
}
int main()
{
int *ip, n;
scanf("%d", &n);
ip = indexArray(n);
for(int i = ; i < n; i++)
printf("%d ", ip[i]);
printf("\n");
free(ip);
return ;
}

《C程序设计的抽象思维》2.10编程练习(未完)的更多相关文章

  1. Python之路第一课Day7--随堂笔记(面向对象编程进阶...未完待续 )

    本节内容: 面向对象高级语法部分 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 作业:开发一个支持多用户在线的FTP程序 面向对象高级语法部分 一.静态方法 通过@s ...

  2. 使用 Python 进行并发编程 -- asyncio (未完)

    参考地址 参考地址 参考地址 Python 2 时代, 高性能的网络编程主要是使用 Twisted, Tornado, Gevent 这三个库. 但是他们的异步代码相互之间不兼容越不能移植. asyn ...

  3. Siki_Unity_3-8_Lua编程(未完)

    Unity 3-8 Lua编程 任务1&2&3:前言 课程内容: Lua从入门到掌握 为之后的xLua和其他热更新方案打下基础 任务4:Lua简介 Lua是轻量小巧的脚本语言--无需编 ...

  4. 10.31-11.1Test(未完)

    10.31-11.1Test 题目 描述 做法 \(BSOJ5177\) 求在\(n\)个数里选\(K\)个的所有方案的异或和之和 按位讨论,组合数算 \(BSOJ5178\) 化简\(\displa ...

  5. C#中正则表达式编程(未完,待补充)

    对于只存储一个匹配,可用Match类: 一般模式: Regex reg = new Regex(string pattern); string str = "###############& ...

  6. 20172319 《Java程序设计教程》 第10周学习总结

    20172319 2018.05.09-05.21 <Java程序设计教程>第10周学习总结 目录 教材学习内容总结 教材学习中的问题和解决过程 代码调试中的问题和解决过程 代码托管 上周 ...

  7. 已看1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架、多线程(并发编程)、I/O(NIO)、Socket、JDBC、XML、反射等。[泛型]\

    1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架.多线程(并发编程).I/O(NIO).Socket.JDBC.XML.反射等.[泛型]\1* ...

  8. 20155324 《Java程序设计》实验五 网络编程与安全

    20155324 <Java程序设计>实验五 网络编程与安全 实验内容 任务一 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上面功能中获取的表达式中 ...

  9. Fedora 10编程开发工具

    1请问Fedora 10编程开发工具有什么 编辑器就用vim,编译用gcc,当然个人爱好随意 IDE的话推荐eclipse,如果做C/C++的,用codeblocks也是个不错的选择 输入gcc -v ...

  10. 20155326 《Java程序设计》实验五网络编程与安全实验报告

    20155326 <Java程序设计>实验五网络编程与安全实验报告 实验内容 任务一 1.两人一组结对编程: 参考http://www.cnblogs.com/rocedu/p/67667 ...

随机推荐

  1. 我是如何用Go语言搭建自己的博客的

    前言: 话说,已经很久没有在博客园更新博客了,之前写的关于go语言的系列学习文章<让我们一起Go>也由于种种原因一度中断.但是,正如我之前在文章中所写,可以慢慢来,但是对于Go语言的学习却 ...

  2. 资源 | 数十种TensorFlow实现案例汇集:代码+笔记

    选自 Github 机器之心编译 参与:吴攀.李亚洲 这是使用 TensorFlow 实现流行的机器学习算法的教程汇集.本汇集的目标是让读者可以轻松通过案例深入 TensorFlow. 这些案例适合那 ...

  3. Connecting my Particle Photon Internet of Things device to the Azure IoT Hub(Translation)

    原文: http://www.hanselman.com/blog/ConnectingMyParticlePhotonInternetOfThingsDeviceToTheAzureIoTHub.a ...

  4. [OpenCV] IplImage and Functions

    In this chapter, APIs will make U crazy. Good luck! Next, Review Linear Algebra.  Ref: http://blog.c ...

  5. 清除linux缓存命令

    命令 #sync #echo 3 > /proc/sys/vm/drop_caches 查看内存情况: # more /proc/meminfo Kernels 2.6.16 and newer ...

  6. Cocos2dx-3.0版本 从开发环境搭建(Win32)到项目移植Android平台过程详解

    作为重量级的跨平台开发的游戏引擎,Cocos2d-x在现今的手游开发领域占有重要地位.那么问题来了,作为Cocos2dx的学习者,它的可移植特性我们就需要掌握,要不然总觉得少一门技能.然而这个时候各种 ...

  7. 配置内存中OLTP文件组提高性能

    在今天的文章里,我想谈下使用内存中OLTP的内存优化文件组来获得持久性,还有如何配置它来获得高性能.在进入正题前,我想简单介绍下使用你数据库里这个特定文件组,内存OLTP是如何获得持久性的. 内存中O ...

  8. JS魔法堂:关于元素位置和鼠标位置的属性

    一.关于鼠标位置的属性   1. 触发鼠标事件的区域 盒子模型中的border,padding,content区域会触发鼠标事件,点击margin区域将不触发鼠标事件.   2. 鼠标事件对象Mous ...

  9. Excel中显示长数字的方法

    主要有以下三种方法: 1.先设置为文本格式,再粘贴2.在另一列输入=CONCATENATE(A1),双击此格右下角得到全部数值,再格式化为文本粘贴回去3.选中数据列,点数据-分列,下一步-下一步,选中 ...

  10. 简单介绍.Net3.0 中跨线程访问控件

    这两天用WPF做一个项目的UI部分时,发现跨线程地访问了UI控件,自然地报异常了.当时找了半天也没在控件中找到InvokeRequired属性和Invoke方法,郁闷之极.....最后发现在.net3 ...