报数游戏
#define N 100
int main()
{int a[N], n, m, k;
int i, count, j;
scanf(“%d%d%d”, &n, &k, &m);
for(i = 1; i <= n; i++)
a[i] = i;
j = n; //j:圈中剩余小孩数
count = 0; //为报数做准备,以便第一个小孩报1
i = 1; //从数组的1号元素开始报数
while(j > m){
if(a[i] != 0){ //若小孩未退出圈子
count++; //参加报数
if(count == k){ //若报到k
a[i] = 0; //退出圈子
count = 0; //清零,以便重新从1开始报数
j--; //剩余人数减1
}
}
i++; //下一个小孩准备报数
if(i > n) //若越过最后小孩
i = 1; //转到第一个小孩
}
for(i = 1; i <= n; i++)
if(a[i] != 0)
printf(“%4d”, a[i]);
getch();
return 0;
}
时钟程序
struct time{
unsigned char ti_min; //分
unsigned char ti_hour; //小时
unsigned char ti_hund; //百分秒
unsigned char ti_sec; //秒
};
struct date{
int da_year; //自1900年以来的年数
char da_day; //天数
char da_mon; //月数
};
long time(long *p);
int stime(long *p);
void gettime(struct time *timep);
settime(struct time *timep);
#include <dos.h>
#include <conio.h>
int main()
{struct time curtime;
float cur_hour, cur_min, cur_sec;
clrscr();
do{
printf("The current time is:\n");
gettime(&curtime); //得到当前时间
if(curtime.ti_hour <= 12)
printf("AM ");
else{
printf("PM ");
curtime.ti_hour -= 12;
}
if(curtime.ti_hour < 10)
printf("0");
printf("%d:", curtime.ti_hour);
if(curtime.ti_min < 10)
printf("0");
printf("%d:", curtime.ti_min);
if(curtime.ti_sec < 10)
printf("0");
printf("%d", curtime.ti_sec);
sleep(1); //需要conio.h头文件
clrscr();
}while(!kbhit());
return 0;
}
简单的计算器(一)
#include "stdio.h"
#include "stdlib.h"
float get_num(char **);
int main()
{char a[80];
int sign = 1; //对于表达式中的 ‘+’,sign = 1,对于‘-’,sign = -1
float result = 0;
char *p; //用来指向要处理的字符
clrscr();
gets(a);
p = a;
if(*p == '-') { //表达式第一个字符若是‘-’
sign = -1; //存储符号
p++;
}
while(*p != 0) { //没有遇到空字符,则循环
float m; //m用来存储某块的计算结果
m = sign*get_num(&p); //get_num执行过程中,p会向后移动
while( *p == '*' || *p == '/' ){ //该循环求解某块的结果
if( *p == '*' ) {
p++;
m *= get_num(&p);
}
else {
int div;
p++;
div = get_num(&p);
if( div == 0 ) {
printf("错误!除数为0!\n");
exit(1);
}
else
m /= div;
}
}
if(*p != '+' && *p != '-' && *p != 0){
printf("错误!遇到非法字符:%c\n", *p);
exit(0);
}
result += m; //将上面计算出的某块的结果累加到result中
if(*p == '+'){
sign = 1;
p++;
}
else if(*p == '-') {
sign = -1;
p++;
}
else //遇到空字符,退出循环
break;
}
printf("The result is %f\n", result);
getch();
return 0;
}
float get_num(char **pp) //pp指向p
{float x = 0, m = 10;
if(**pp != '.' && (**pp <'0' || **pp >'9') ) {
printf("错误!运算符后面不是一个有效的数据\n");
exit(1);
}
while(**pp >= '0' && **pp <= '9') {
x = x*10 + **pp - 48;
(*pp)++;
}
if(**pp == '.') {
(*pp)++;
while(**pp >= '0' && **pp <= '9') {
x += (**pp - 48)/m;
(*pp)++;
m *= 10;
}
}
return x;
}
简单的计算器(二)
#include "stdio.h"
#include "stdlib.h"
float get_num(char **);
float calculate(char **);
int main()
{char a[80];
float result;
char *p;
clrscr();
gets(a);
p = a;
result = calculate(&p);
printf("%f\n", result);
getch();
return 0;
}
float calculate(char **p)
{float result = 0;
int sign = 1;
if(**p == '-') {
sign = -1;
(*p)++;
}
while(**p != 0 && **p != ')' ) {
float m;
if(**p == '(' ) {
(*p)++;
m = sign*calculate(p); //遇到括号调用自己计算括号中的值
}
else
m = sign*get_num(p);
while(**p == '*' || **p == '/') {
if(**p == '*') {
(*p)++;
if(**p == '(') {
(*p)++;
m *= calculate(p); //遇到括号调用自己计算括号中的值
}
else
m *= get_num(p);
}
else {
int div;
(*p)++;
if(**p == '(') {
(*p)++;
div = calculate(p); //遇到括号调用自己计算括号中的值
}
else
div = get_num(p);
if(div == 0) {
printf("diveide by 0!\n");
exit(1);
}
else
m /= div;
}
}
if(**p != '+' && **p != '-' && **p != 0 && **p != ')'){
printf("illege character: %c\n",**p);
exit(0);
}
result += m;
if(**p == '+') {
sign = 1;
(*p)++;
}
else
if(**p == '-' ) {
sign = -1;
(*p)++;
}
else
if(**p == ')' ) { //遇到右括号,跳过并退出循环返回结果
(*p)++;
break;
}
else
break;
}
return result;
}
float get_num(char **p)
{float n = 0, m = 10;
if(**p != '.' && (**p <'0' || **p >'9')) {
printf("get num error!\n");
exit(1);
}
while(**p >= '0' && **p <= '9') {
n = n*10 + **p - 48;
(*p)++;
}
if(**p == '.') {
(*p)++;
while(**p >= '0' && **p <= '9') {
n += (**p - 48)/m;
(*p)++;
m *= 10;
}
}
return n;
}
本文出自 “成鹏致远” 博客,请务必保留此出处http://infohacker.blog.51cto.com/6751239/1171349
1192



被折叠的 条评论
为什么被折叠?



