9
# include <stdio.h>
void showmenu ( void ) ;
int getchoice ( int low, int high) ;
int main ( void )
{
showmenu ( ) ;
printf ( "您的选择是%d" , getchoice ( 1 , 4 ) ) ;
}
void showmenu ( void ) {
printf ( "Please choose one of the followings:\n" ) ;
printf ( "1)copy files 2)move files\n" ) ;
printf ( "3)remove files 4)quit\n" ) ;
printf ( "Enter the number of your choice:\n" ) ;
}
int getchoice ( int low, int high) {
int ans;
int good;
good= scanf ( "%d" , & ans) ;
while ( good== 1 && ( ans< low|| ans> high) ) {
printf ( "UNVALID!\n" ) ;
showmenu ( ) ;
scanf ( "%d" , & ans) ;
}
if ( good!= 1 )
ans= 4 ;
return ans;
}
1
# include <stdio.h>
double min ( double a, double b) ;
int main ( void )
{ double x, y;
printf ( "请输入两个小数:" ) ;
scanf ( "%lf %lf" , & x, & y) ;
printf ( "其中较小的数值为:%lf" , min ( x, y) ) ;
}
double min ( double a, double b) {
if ( a> b)
return b;
else
return a;
}
2
# include <stdio.h>
void chline ( char ch, int i, int j) ;
const int col= 10 ;
const int row= 10 ;
int main ( void )
{
char ch;
int a;
int b;
printf ( "请输入一个字符:\n" ) ;
scanf ( "%c" , & ch) ;
printf ( "请输入你想要显示字符的行数:\n" ) ;
scanf ( "%d" , & a) ;
printf ( "请输入你想要显示字符的列数:\n" ) ;
scanf ( "%d" , & b) ;
chline ( ch, a, b) ;
}
void chline ( char ch, int i, int j) {
char x= '*' ;
int u, v;
for ( u= 0 ; u< row; u++ ) {
for ( v= 0 ; v< col; v++ ) {
if ( i== u&& j== v)
printf ( "%c" , ch) ;
else
printf ( "%c" , x) ;
}
printf ( "\n" ) ;
}
}
3
# include <stdio.h>
void showchar ( char ch, int row, int col) ;
int main ( void )
{ char ch;
int a, b;
printf ( "请输入一个字符:\n" ) ;
scanf ( "%c" , & ch) ;
printf ( "请输入显示行数:\n" ) ;
scanf ( "%d" , & a) ;
printf ( "请输入显示列数:\n" ) ;
scanf ( "%d" , & b) ;
showchar ( ch, a, b) ;
}
void showchar ( char ch, int row, int col) {
int i, j;
for ( i= 0 ; i< row; i++ ) {
for ( j= 0 ; j< col; j++ ) {
printf ( "%c" , ch) ;
}
printf ( "\n" ) ;
}
}
4
# include <stdio.h>
double play ( double x, double y) ;
int main ( void )
{
double a, b;
printf ( "请输入两个小数:\n" ) ;
scanf ( "%lf %lf" , & a, & b) ;
printf ( "这两个数的谐均值为:%lf" , play ( a, b) ) ;
}
double play ( double x, double y) {
double average;
x= 1 / x;
y= 1 / y;
average= ( x+ y) / 2 ;
return 1 / average;
}
5
# include <stdio.h>
void large_of ( double * a, double * b) ;
int main ( void )
{ double x, y;
printf ( "请输入两个小数:" ) ;
scanf ( "%lf %lf" , & x, & y) ;
large_of ( & x, & y) ;
printf ( "现在两个小数值为:%.2lf %.2lf" , x, y) ;
}
void large_of ( double * a, double * b) {
if ( * a> * b)
* b= * a;
else
* a= * b;
}
6
# include <stdio.h>
# include <ctype.h>
int test ( char ch) ;
int main ( void )
{
char a;
int testnum;
printf ( "请输入一个字符:\n" ) ;
a= getchar ( ) ;
while ( a!= EOF ) {
getchar ( ) ;
testnum= test ( a) ;
if ( testnum== - 1 )
printf ( "该字符不是一个字母\n" ) ;
else
printf ( "该字符在字母表中的数值位置为%d\n" , testnum) ;
printf ( "请输入一个字符:\n" ) ;
a= getchar ( ) ;
}
}
int test ( char ch) {
int testnum;
if ( isalpha ( ch) )
{ if ( islower ( ch) )
testnum= ch- 'a' + 1 ;
else
testnum= ch- 'A' + 1 ;
}
else
testnum= - 1 ;
return testnum;
}
7
# include <stdio.h>
double power ( double n, int p) ;
int main ( void )
{
double a;
int n;
printf ( "请输入一个小数:\n" ) ;
scanf ( "%lf" , & a) ;
printf ( "请输入你想计算的幂数:\n" ) ;
scanf ( "%d" , & n) ;
printf ( "则%.2lf的%d次幂为:%.2lf\n" , a, n, power ( a, n) ) ;
}
double power ( double n, int p) {
double pow= 1 ;
int i;
if ( p> 0 ) {
for ( i= 1 ; i<= p; i++ ) {
pow*= n;
}
}
if ( p< 0 ) {
for ( i= 1 ; i<= - p; i++ ) {
pow*= n;
}
pow= 1 / pow;
}
if ( p= 0 )
pow= 1 ;
return pow;
}
8
# include <stdio.h>
double power ( double n, int p) ;
int main ( void )
{
double a;
int n;
printf ( "请输入一个小数:\n" ) ;
scanf ( "%lf" , & a) ;
printf ( "请输入你想计算的幂数:\n" ) ;
scanf ( "%d" , & n) ;
printf ( "则%.2lf的%d次幂为:%.2lf\n" , a, n, power ( a, n) ) ;
}
double power ( double n, int p) {
double pow= 1 ;
int i;
if ( p> 0 ) {
pow= n* power ( n, p- 1 ) ;
}
if ( p< 0 ) {
pow= 1 / n* power ( n, p+ 1 ) ;
}
if ( p= 0 )
pow= 1 ;
return pow;
}
9
# include <stdio.h>
void to_base ( unsigned long n, int x) ;
int main ( void )
{
unsigned long number;
int z;
printf ( "请输入整数(q退出):\n" ) ;
while ( scanf ( "%lu" , & number) == 1 ) {
printf ( "请输入您想转化的进制(2-8):\n" ) ;
if ( scanf ( "%d" , & z) == 1 && ( z>= 2 && z<= 10 ) ) {
printf ( "则%lu的%d进制为:" , number, z) ;
to_base ( number, z) ;
putchar ( '\n' ) ;
printf ( "请输入整数(q退出):\n" ) ;
}
}
}
void to_base ( unsigned long n, int x) {
int r;
r= n% x;
if ( n>= x)
to_base ( n/ x, x) ;
putchar ( '0' + r) ;
return ;
}
10
# include <stdio.h>
long Fibonnacci ( int n) ;
int main ( void )
{
int n;
printf ( "您想要费那波契数列的第几项:" ) ;
scanf ( "%d" , & n) ;
printf ( "此项为%d" , Fibonnacci ( n) ) ;
}
long Fibonnacci ( int n) {
int i;
long sum;
int a= 0 ;
int b= 1 ;
sum= 1 ;
if ( n== 1 )
return 0 ;
if ( n== 2 )
return 1 ;
for ( i= 3 ; i<= n; i++ ) {
sum= a+ b;
a= b;
b= sum;
}
return sum;
}