9.
#include <stdio.h>
int menu();
int showNum(int n1, int n2);
int main (void)
{
int num;
do {
menu();
} while((num = showNum(1,100)) == 0);
printf("%d",num);
return 0;
}
int menu()
{
int code,status;
printf("Please choose one of the following:\n");
printf("1)copy files 2)move files\n");
printf("3)remove files 4)quit\n");
printf("Enter the number of your choice:");
while((status = scanf("%d", &code)) != 1 || code < 1 || code > 4)
{ //如果输入的参数错误类型,比如字符串,跳过后面的字符
if(status != 1)
scanf("%*s");
printf("Enter a integer from 1 to 5,please.\n");
}
return code;
}
int showNum(int n1, int n2)
{
int num;
printf("Please enter a integer:");
while(scanf("%d", &num) != 1) {
scanf("%*s");
printf("Please enter a integer like 2,-20,30");
}
return num >= n1 && num <= n2 ? num : 0;
}
1.
#include <stdio.h>
double min (double, double);
int main (void)
{
double d1,d2;
//默认输入q退出
printf("请输入2个数字(输入q退出):\n");
while(scanf("%lf,%lf", &d1,&d2) == 2)
{
printf("两个数字中较小的一个是%lf",min(d1,d2));
}
return 0;
}
double min (double d1, double d2)
{
return d1 <= d2 ? d1 : d2;
}
2.
#include <stdio.h>
void chline(char,int,int);
int main (void)
{
int i1,i2;
char c1;
//默认输入q退出
printf("请输入一个字符和2个数字(输入q退出):\n");
while(scanf("%c,%d,%d", &c1,&i1,&i2) == 3)
{
chline(c1,i1,i2);
}
return 0;
}
void chline(char ch,int i,int j)
{
int line;
for(line = 1; line <= j; line++)
{
if(line >= i)
{
putchar(ch);
}
putchar('\n');
}
}
3.
#include <stdio.h>
void chline(char,int,int);
int main (void)
{
int i1,i2;
char c1;
//默认输入q退出
printf("请输入一个字符和2个数字(输入q退出):\n");
while(scanf("%c,%d,%d", &c1,&i1,&i2) == 3)
{
chline(c1,i1,i2);
}
return 0;
}
void chline(char ch,int r,int l)
{
int lines;
for(;r > 0; r--)
{
for(lines=1;lines<=l;lines++)
{
putchar(ch);
}
putchar('\n');
}
}
4,
#include <stdio.h>
double harmmean(double,double);
int main (void)
{
double d1,d2;
//默认输入q退出
printf("请输入2个数字(输入q退出):\n");
while(scanf("%lf,%lf", &d1,&d2) == 2)
{
printf("谐均值为:%lf",harmmean(d1,d2));
}
return 0;
}
double harmmean(double d1,double d2)
{
return 1/(((1/d1)+(1/d2))/2);
}
5.
#include <stdio.h>
void larger_of(double *,double *);
int main (void)
{
double d1,d2;
//默认输入q退出
do
{
printf("请输入2个数字(输入q退出):\n");
}
while(scanf("%lf,%lf", &d1,&d2) != 2);
larger_of(&d1,&d2);
printf("d1=%lf,d2=%lf",d1,d2);
return 0;
}
void larger_of(double *d1,double *d2)
{
if(*d1 >= *d2)
{
*d2 = *d1;
}
else
{
*d1 = *d2;
}
}
6.
#include <stdio.h>
int check_letter(ch);
int main (void)
{
char ch;
printf("请输入字符(输入q退出):\n");
while(scanf("%c",&ch) != EOF)
{
if(ch != '\n')
{
printf("%c = %d\n",ch, check_letter(ch));
}
}
return 0;
}
int check_letter(ch)
{
int num = -1;
if(ch >= 'a' && ch <= 'z')
{
num = ch - 'a' + 1;
}
else if(ch >= 'A' && ch <= 'Z')
{
num = ch - 'A' + 1;
}
return num;
}
7.
#include <stdio.h>
double power(double n, int p);
int main (void)
{
double x,xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while(scanf("%lf,%d",&x,&exp) == 2)
{
xpow = power(x,exp);
printf("%.3g to the power %d is %.5g\n",x,exp,xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n,int p)
{
double pow = 1;
if(n == 0 || p == 0)
{
return 1;
}
if(p < 0)
{
n = 1/n;
p = -p;
}
for(; p>0; p--)
{
pow *= n;
}
return pow;
}
8.
#include <stdio.h>
double power(double n, int p);
int main (void)
{
double x,xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while(scanf("%lf,%d",&x,&exp) == 2)
{
xpow = power(x,exp);
printf("%.3g to the power %d is %.5g\n",x,exp,xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n,int p)
{
if(n == 0 || p == 0)
{
return 1;
}
if(p < 0)
{
n = 1/n;
p = -p;
}
if(p > 1)
return n * power(n,--p);
return n;
}
9.
#include <stdio.h>
//void to_binary(unsigned long n);
void to_base_n(unsigned long n, int m);
int main (void)
{
unsigned long number;
int m,cou;
printf("Enter an integer(q to quit);\n");
//%ul不行
while(scanf("%u,%d",&number,&m) == 2 && m >= 2 && m<=10)
{
printf("Binary equivalent:");
//to_binary(number);
to_base_n(number,m);
putchar('\n');
printf("Enter an integer(q to quit);\n");
}
printf("Done.\n");
return 0;
}
/*
void to_binary(unsigned long n)
{
int r;
r = n % 2;
if(n >= 2)
to_binary(n/2);
putchar('0' + r);
}
*/
//m接收2-10间的数字
void to_base_n(unsigned long n, int m)
{
int r;
r = n % m;
if(n >= m)
to_base_n(n/m,m);
putchar('0' + r);
}
10.
#include <stdio.h>
long Fibonacci(int n);
int main (void)
{
int m;
printf("Enter an integer(q to quit);\n");
while(scanf("%ld",&m) == 1)
{
printf("Fibonacci:%ld\n",Fibonacci(m));
printf("Enter an integer(q to quit);\n");
}
printf("Done.\n");
return 0;
}
/*
long Fibonacci(int n)
{
if(n >2)
return Fibonacci(n-1) + Fibonacci(n-2);
else
return 1;
}
*/
long Fibonacci(int n)
{
int i,p1,p2,tmp;
p1 = p2 = 1;
if(n <= 2)
{
return 1;
}
for(i = 2; i<=n; i++)
{
tmp = p2;
p2 = p1+p2;
p1 = tmp;
}
return p2;
}