第九章 函数 编程练习个人答案

// 第八章 字符输入,输出和输入验证.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
int i = 0;
char ch = '\0';
void introduction();
double min_process(double i,double j);
void mind_min();
void chine(char ch, int i, int j);
void func_3(char ch, int i, int j);
double harmonic_mean(double harmonic_mean_1, double harmonic_mean_2);
double func_5(double m, double n);
double* func_6(double m1, double m2, double m3);
double* input_func_6(double m1,double m2,double m3); 
double func_8();
int *func_7( int num[]);
//练习1
//练习2,5
int m=0, n = 0;
//练习3
double harmonic_mean_1 = 0, harmonic_mean_2 = 0;
//练习6
double m1 = 0, m2 = 0, m3 = 0;
double mm[3]; 
//练习7
int num[100] = {'\0'};
void func_11();

void main()
{
introduction();
while (scanf("%d", &i) == 1)
{
getchar();
switch (i)
{
case 1:system("cls"); mind_min(); system("cls"); break;
case 2:system("cls"); chine(ch, m, n); system("cls"); break;
case 3:system("cls"); func_3(ch, m, n); system("cls"); break;
case 4:system("cls"); printf("%3.3lf", harmonic_mean(harmonic_mean_1, harmonic_mean_2)); system("pause"); system("cls"); break;
case 5:system("cls"); printf("%3.3lf", func_5(n, m)); system("pause"); system("cls"); break;
case 6:system("cls"); input_func_6(m1, m2, m3); func_6(mm[0], mm[1], mm[2]); printf("%2lf %2lf %2lf\n", mm[0], mm[1], mm[2]); system("pause"); system("cls"); break;
case 7:system("cls"); func_7(num);
for (i = 0; i < 100; i++) 
{
if (num[i] != '\0') { printf("NO.%d:%d \n",i+1, num[i]); }
} system("pause"); system("cls"); break;
case 8:system("cls"); printf("%3.3lf\n", func_8()); system("pause"); system("cls"); break;
case 11:system("cls"); func_11(); system("cls"); break;
default:printf("erroe number,please reinput\n"); system("pause");
}
introduction();
}


}




void introduction()
{
printf("please input the number before the sentence to run the progress if you want\n""press any other key to quit\n");
printf(
"1:传入两个double数据,比较两个double 类型数据,返回最小值\n"
"2:传入字符c,int类型 i,j,返回打印c字符i行j列\n"
"3:传入字符c,int类型 i,j,返回打印c字符i次j行\n"
"4:传入两个double类型数据,返回调和平均数(取两数倒数平均值,其倒数是调和平均数)\n"
"5:传入2个double值,将大值赋给两个double值,传出\n"
"6:传入三个double值,有小而大排序\n"
"7:输入一段字符,判断是否字符,如果是,传出其在字母表中位置\n"
"8:输入数字和幂指数,输出结果\n"
"9:略\n"
"10:略\n"
"11:斐波那契数列前100个\n");
}
double min_process(double m, double n)
{

if (m < n) {printf("%lf, %lf", m, n); return m; }
else { printf("%lf, %lf", m, n); return n; }
}
void mind_min()
{
double m = 0, n = 0;
printf("please input two number,press any othe key instead of number to quit\n");

while (1)
{
if ((scanf("%lf", &n)) != 1) { getchar(); break; }  if ((scanf("%lf", &m) != 1)) { getchar(); break; }
getchar();
rewind(stdin);
printf("the min:%3.3lf\n", min_process(n, m));
printf("Are you want to quit?(press 'n' to continue,press any other key to quit)\n");
if ((ch = getchar()) == 'n') { continue; }
else { break; }
}


}
void chine(char ch, int i, int j)
{
printf("please input two interger and a character,first for row,next for colunm\nformat:row column char,press any othe key instead of requiration to quit\n");


while (1)
{
if ((scanf("%d", &i) != 1)){getchar();break;} getchar();
if ((scanf("%d", &j) != 1)){getchar();break;} getchar();  
if ((scanf("%c", &ch) != 1)){getchar();break;}getchar();
rewind(stdin);//确保清空缓存区

for (int row = 0; row < i; row++)
{
for (int column = 0; column < j; column++)
{
printf("%c", ch);
}printf("\n");
}
printf("Are you want to quit?(press 'n' to continue,press any other key to quit)\n");
(ch = getchar()); getchar();
if (ch =='n') {  continue; }
else {  break; }
}
}
void func_3(char ch, int i, int j)
{
printf("please input two interger and a character,first for print time,next for print colunm\nformat:row column char,press any othe key instead of requiration to quit\n");




while (1)
{
if ((scanf("%d", &i) != 1)) { getchar(); break; } getchar();
if ((scanf("%d", &j) != 1)) { getchar(); break; } getchar();
if ((scanf("%c", &ch) != 1)) { getchar(); break; }getchar();
rewind(stdin);//确保清空缓存区


for (int time = 0; time < i; time++)
{
for (int row = 0; row < j; row++)
{
printf("%c\n", ch);
}printf("\n");
}
printf("Are you want to quit?(press 'n' to continue,press any other key to quit)\n");
(ch = getchar()); getchar();
if (ch == 'n') { continue; }
else { break; }
}m = 0, n = 0;
}
double harmonic_mean(double harmonic_mean_1, double harmonic_mean_2)
{double harmonic_mean_number = 0;
printf("please input two number,press any othe key instead of number to quit\n");


while (1)
{
if ((scanf("%lf", &harmonic_mean_1)) != 1) { getchar(); break; }  
if ((scanf("%lf", &harmonic_mean_2) != 1)) { getchar(); break; }
getchar();
harmonic_mean_number = 1.0 / ((1.0 / harmonic_mean_1 + 1.0 / harmonic_mean_2) / 2.0);
return harmonic_mean_number;

}




}
double func_5(double m, double n)
{
m = 0, n = 0;
printf("please input two number\n");



scanf("%lf", &n); getchar(); scanf("%lf", &m); getchar(); 

rewind(stdin);
if (n > m) { m = n; return n; }
else { n = m; return m; }


}
double *func_6(double m1, double m2,double m3)
{
int temp = 0;
if (mm[0] > mm[1]) {  temp=mm[0]; mm[0] = mm[1]; mm[1] = temp; }
if (mm[0] > mm[2]) { temp = mm[0]; mm[0] = mm[2]; mm[2] = temp; }
if (mm[1] > mm[2]) {  temp =mm[1];  mm[1] = mm[2]; mm[2] = temp; }
return mm;


}
double* input_func_6(double m1, double m2, double m3)
{
printf("please input the three real\n");
while (1)
{
printf("press any key instead of required to quit\n""format:1 2 3\n");
if (scanf("%lf", &m1) != 1) { getchar(); break; }getchar();
if (scanf("%lf", &m2) != 1) { getchar(); break; }getchar();
if (scanf("%lf", &m3) != 1) { getchar(); break; }getchar();
printf("the real:%3.3lf %3.3lf %3.3lf\n", m1, m2, m3);
mm[0] = m1;
mm[1] = m2;
mm[2] = m3;
}
return mm;
}
int *func_7(int num[])
{
char str[100];
printf("please input a string\n");
gets_s(str);
int times = strlen(str);
for (i = 0; i <= times; i++)
{
if((str[i]<='z'&&str[i]>='a'))
{
num[i] = (int )(str[i] - 'a'+1);
}
if ((str[i] <= 'Z'&&str[i] >= 'A'))
{
num[i] = (int)(str[i] - 'A'+1);
}
}
str[i + 1] = '\n';
return num;
}
double func_8()
{
double a = 0, b = 0;
double number = 0;
printf("please input n and m,then it out put n^m\n""format:n m\n");
scanf("%lf %lf", &a, &b); getchar();

number = pow(a, b);
return number;


}
void func_11()
{
unsigned long num_11[100] = { '\0' };
num_11[0] = num_11[1] = 1;
printf("%u %u ", num_11[0], num_11[1]);
for (i = 2; i < 100; i++)
{
num_11[i] = num_11[i - 1] + num_11[i - 2];
printf("%u ", num_11[i]);
if (i %10 == 0) { printf("\n"); }
}system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值