C语言程序设计50例(三)(经典收藏)
【程序31】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续
判断第二个字母。
1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
void main()
{
char letter;
printf("please input the first letter of someday\n");
while((letter=getch())!='Y')/*当所按字母为Y时才结束*/
{
switch (letter)
{
case 'S':printf("please input second letter\n");
if((letter=getch())=='a')
printf("saturday\n");
else if ((letter=getch())=='u')
printf("sunday\n");
else printf("data error\n");
break;
case 'F':printf("friday\n");break;
case 'M':printf("monday\n");break;
case 'T':printf("please input second letter\n");
if((letter=getch())=='u')
printf("tuesday\n");
else if ((letter=getch())=='h')
printf("thursday\n");
else printf("data error\n");
break;
case 'W':printf("wednesday\n");break;
default: printf("data error\n");
}
}
getch();
}
【程序32】
题目:Press any key to change color, do you want to try it. Please hurry up!
1.程序分析:
2.程序源代码:
#include "conio.h"
#include "stdio.h"
void main(void)
{
int color;
for (color = ; color < ; color++)
{
textbackground(color);/*设置文本的背景颜色*/
cprintf("This is color %d\r\n", color);
cprintf("Press any key to continue\r\n");
getch();/*输入字符看不见*/
}
}
【程序33】
题目:学习gotoxy()与clrscr()函数
1.程序分析:
2.程序源代码:
#include "conio.h"
#include "stdio.h"
void main(void)
{
clrscr();/*清屏函数*/
textbackground();
gotoxy(, );/*定位函数*/
cprintf("Output at row 5 column 1\n");
textbackground();
gotoxy(, );
cprintf("Output at row 10 column 20\n");
getch();
}
【程序34】
题目:练习函数调用
1. 程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
void hello_world(void)
{
printf("Hello, world!\n");
}
void three_hellos(void)
{
int counter;
for (counter = ; counter <= ; counter++)
hello_world();/*调用此函数*/
}
void main(void)
{
three_hellos();/*调用此函数*/
getch();
}
【程序35】
题目:文本颜色设置
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
void main(void)
{
int color;
for (color = ; color < ; color++)
{
textcolor(color);/*设置文本颜色*/
cprintf("This is color %d\r\n", color);
}
textcolor( + );
cprintf("This is blinking\r\n");
getch();
}
【程序36】
题目:求100之内的素数
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "math.h"
#define N 101
main()
{
int i,j,line,a[N];
for(i=;i<N;i++) a=i;
for(i=;i<sqrt(N);i++)
for(j=i+;j<N;j++)
{
if(a!=&&a[j]!=)
if(a[j]%a==)
a[j]=;
}
printf("\n");
for(i=,line=;i<N;i++)
{
if(a!=)
{
printf("%5d",a);
line++;
}
if(line==)
{
printf("\n");
line=;
}
}
getch();
}
【程序37】
题目:对10个数进行排序
1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,
下次类推,即用第二个元素与后8个进行比较,并进行交换。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
#define N 10
main()
{
int i,j,min,tem,a[N];
/*input data*/
printf("please input ten num:\n");
for(i=;i<N;i++)
{
printf("a[%d]=",i);
scanf("%d",&a);
}
printf("\n");
for(i=;i<N;i++)
printf("%5d",a);
printf("\n");
/*sort ten num*/
for(i=;i<N-;i++)
{
min=i;
for(j=i+;j<N;j++)
if(a[min]>a[j])
min=j;
tem=a;
a=a[min];
a[min]=tem;
}
/*output data*/
printf("After sorted \n");
for(i=;i<N;i++)
printf("%5d",a);
getch();
}
【程序38】
题目:求一个3*3矩阵对角线元素之和
1.程序分析:利用双重for循环控制输入二维数组,再将a累加后输出。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
/* 如果使用的是TC系列编译器则可能需要添加下句 */
static void dummyfloat(float *x){ float y; dummyfloat(&y);}
main()
{
float a[][],sum=;
int i,j;
printf("please input rectangle element:\n");
for(i=;i<;i++)
for(j=;j<;j++)
scanf("%f",&a[j]);
for(i=;i<;i++)
sum=sum+a;
printf("duijiaoxian he is %6.2f",sum);
getch();
}
【程序39】
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
1. 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后
此元素之后的数,依次后移一个位置。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
int a[]={,,,,,,,,,};
int temp1,temp2,number,end,i,j;
printf("original array is:\n");
for(i=;i<;i++)
printf("%5d",a);
printf("\n");
printf("insert a new number:");
scanf("%d",&number);
end=a[];
if(number>end)
a[]=number;
else
{
for(i=;i<;i++)
{
if(a>number)
{
temp1=a;
a=number;
for(j=i+;j<;j++)
{
temp2=a[j];
a[j]=temp1;
temp1=temp2;
}
break;
}
}
}
for(i=;i<;i++)
printf("%6d",a);
getch();
}
【程序40】
题目:将一个数组逆序输出。
1.程序分析:用第一个与最后一个交换。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
#define N 5
main()
{
int a[N]={,,,,},i,temp;
printf("\n original array:\n");
for(i=;i<N;i++)
printf("%4d",a);
for(i=;i<N/;i++)
{
temp=a;
a=a[N-i-];
a[N-i-]=temp;
}
printf("\n sorted array:\n");
for(i=;i<N;i++)
printf("%4d",a);
getch();
}
【程序41】
题目:学习static定义静态变量的用法
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
varfunc()
{
int var=;
static int static_var=;
printf("\40:var equal %d \n",var);
printf("\40:static var equal %d \n",static_var);
printf("\n");
var++;
static_var++;
}
void main()
{
int i;
for(i=;i<;i++)
varfunc();
getch();
}
【程序42】
题目:学习使用auto定义变量的用法
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
int i,num;
num=;
for(i=;i<;i++)
{
printf("\40: The num equal %d \n",num);
num++;
{
auto int num=;
printf("\40: The internal block num equal %d \n",num);
num++;
}
}
getch();
}
【程序43】
题目:学习使用static的另一用法。
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
int i,num;
num=;
for(i=;i<;i++)
{
printf("\40: The num equal %d \n",num);
num++;
{
static int num=;
printf("\40:The internal block num equal %d\n",num);
num++;
}
}
getch();
}
【程序44】
题目:学习使用external的用法。
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
int a,b,c;
void add()
{
int a;
a=;
c=a+b;
}
void main()
{
a=b=;
add();
printf("The value of c is equal to %d\n",c);
getch();
}
【程序45】
题目:学习使用register定义变量的方法。
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
void main()
{
register int i;
int tmp=;
for(i=;i<=;i++)
tmp+=i;
printf("The sum is %d\n",tmp);
getch();
}
【程序46】
题目:宏#define命令练习(1)
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
#define TRUE 1
#define FALSE 0
#define SQ(x) (x)*(x)
void main()
{
int num;
int again=;
printf("\40: Program will stop if input value less than 50.\n");
while(again)
{
printf("\40:Please input number==>");
scanf("%d",&num);
printf("\40:The square for this number is %d \n",SQ(num));
if(num>=)
again=TRUE;
else
again=FALSE;
}
getch();
}
【程序47】
题目:宏#define命令练习(2)
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
/*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"*/
#define exchange(a,b) { \
int t;\
t=a;\
a=b;\
b=t;\
}
void main(void)
{
int x=;
int y=;
printf("x=%d; y=%d\n",x,y);
exchange(x,y);
printf("x=%d; y=%d\n",x,y);
getch();
}
【程序48】
题目:宏#define命令练习(3)
1.程序分析:
2.程序源代码:
#define LAG >
#define SMA <
#define EQ ==
#include "stdio.h"
#include "conio.h"
void main()
{
int i=;
int j=;
if(i LAG j)
printf("\40: %d larger than %d \n",i,j);
else if(i EQ j)
printf("\40: %d equal to %d \n",i,j);
else if(i SMA j)
printf("\40:%d smaller than %d \n",i,j);
else
printf("\40: No such value.\n");
getch();
}
【程序49】
题目:#if #ifdef和#ifndef的综合应用。
1. 程序分析:
2.程序源代码:
#include "stdio.h"
#include "conio.h"
#define MAX
#define MAXIMUM(x,y) (x>y)?x:y
#define MINIMUM(x,y) (x>y)?y:x
void main()
{
int a=,b=;
#ifdef MAX
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#else
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#ifndef MIN
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#endif
#undef MAX
#ifdef MAX
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#else
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#define MIN
#ifndef MIN
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#endif
getch();
}
【程序50】
题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。
1. 程序分析:例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
2.程序源代码:
#include <iostream>
#include <Cmath>
using namespace std;
/*
求100-999之间的水仙花数
*/
int main()
{
int number,hun,ten,gw,sum;
for (number=;number<;++number){
hun=number/;
ten=number%/;
gw=number%;
sum=pow(hun,)+pow(ten,)+pow(gw,);
if(sum==number)
{
//是水仙花数
cout<<number<<"是水仙花数"<<endl; }
}
return ;
}
C语言程序设计50例(三)(经典收藏)的更多相关文章
- C语言程序设计50例(经典收藏)
本篇文章是对C语言程序设计的50个小案例进行了详细的分析介绍,需要的朋友参考下 [程序1]题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位.十 ...
- C语言程序设计50例(二)(经典收藏)
[程序11]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月 后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序分析: 兔子的规律为数列1,1 ...
- C语言程序设计50例(一)(经典收藏)
[程序1]题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. # ...
- C语言程序设计50例(经典收藏)之1
题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. #includ ...
- C语言程序设计I—第三周教学
由于本课程是从教学周的第二周开始上课,所以第二次授课是发生在第三周,为了让PTA.云班课和博客能统一,所以将教学周作为随笔的标题.本周由于处理外聘教师随意退课等事情,总结有些延后了. 第三周教学安排 ...
- 黑马程序员——经典C语言程序设计100例
1.数字排列 2.奖金分配问题 3.已知条件求解整数 4.输入日期判断第几天 5.输入整数进行排序 6.用*号显示字母C的图案 7.显示特殊图案 8.打印九九口诀 9.输出国际象棋棋盘 10.打印楼梯 ...
- C语言程序设计100例之(9):生理周期
例9 生理周期 问题描述 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为 23 天.28 天和33 天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如 ...
- C语言程序设计100例之(14):丑数
例14 丑数 问题描述 丑数是其质因子只可能是2,3或5的数.前10个丑数分别为1, 2, 3, 4, 5, 6, 8, 9, 10, 12.输入一个正整数n,求第n个丑数. 输入格式 每行为一个 ...
- C语言程序设计100例之(25):确定进制
例25 确定进制 问题描述 6*9 = 42 对于十进制来说是错误的,但是对于13进制来说是正确的.即 6(13)* 9(13)= 42(13),因为,在十三进制中,42 = 4 * 13 + ...
随机推荐
- shell中颜色的设置
linux启动后环境变量加载的顺序为:etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ~/.bashrc → [/etc/bashrc] 想 ...
- MultiImageSelector 仿微信选择多张图片回调
项目可以去github下载 : https://github.com/lovetuzitong/MultiImageSelector 第0步 把模块 multi-image-selector 作为你的 ...
- :before与::before的区别
相同点 都可以用来表示伪类对象,用来设置对象前的内容 :befor和::before写法是等效的 不同点 :befor是Css2的写法,::before是Css3的写法 :before的兼容性要比: ...
- XSS 攻击的防御
xss攻击预防,网上有很多介绍,发现很多都是只能预防GET方式请求的xss攻击,并不能预防POST方式的xss攻击.主要是由于POST方式的参数只能用流的方式读取,且只能读取一次,经过多次尝试,自己总 ...
- (四)创建ROS程序包(就是软件包)
你的 ROS 程序包都放到下面这个目录里, 切换到这个目录: $ cd ~/catkin_ws/src 使用下面的命令: 创建一个 ROS 程序包 名字就叫:beginner_tutorials $ ...
- [z]重建索引
https://blog.csdn.net/funnyfu0101/article/details/52961485 所有执行的结果是脚本命令集合,可以用来创建索引: a)在plsql中使用execu ...
- python collections 里面的Counter 统计所有出现的字符数量
from collections import Counter c_num = Counter('Hello world') # 统计出现的每个字符数量print(c_num) for key, va ...
- Linux移植之子目录下的built-in.o生成过程分析
在Linux移植之make uImage编译过程分析中罗列出了最后链接生成vmlinux的过程.可以看到在每个子目录下都有一个built-in.o文件.对于此产生了疑问built-in.o文件是根据什 ...
- Linux移植之配置过程分析
在Linux移植之移植步骤中已经将Linux移植的过程罗列出来了,现在分析一下Linux的配置过程,将分析以下两个配置过程: 1.make s3c2410_defconfig分析 2.make men ...
- socket网络编程扫盲篇
socket 是“套接字”的意思,是计算机之间进行通信的一种约定,也可以认为是一种技术.通过 socket 这种约定,一台计算机可以接收其他计算机的数据,也可以向其他计算机发送数据. socket 的 ...