1.
#include <stdio.h>
double funcPi(int);
int main(void)
{
int arg;
printf("Input the argument: ");
//
// 无异常输入
//
while (!scanf("%d", &arg))
{
printf("Check your input and retry: ");
while (getchar()!='\n')
{
continue;
}
}
while (getchar()!='\n')
{
continue;
}
//
// 调用函数,输出结果
//
printf("%lf", funcPi(arg));
return 0;
}
//
// 函数定义,不用太多解释了吧?一个循环解决正数值的累加,另一个是负数值的累加。
//
double funcPi( int n )
{
double back = 0.0;
int count;
for (count = 1; count <= 4*n-1; count+=4)
{
back += (double)1/count;
}
for (count = 3; count <= 4*n-1; count+=4)
{
back -= (double)1/count;
}
return 4*back;
}
2.
#include <stdio.h>
#define LEN 3
#define N 30
//
// 结构定义
//
typedef struct stu
{
char name[N];
float scrOfMth;
float scrOfChn;
float scrOfEng;
float scrOfAll;
}Stu;
void swap( Stu *a, Stu *b );
int main(void)
{
Stu table[LEN]; // 申请一个长度为3的结构数组来存放数据
int count = 0;
char *p[5] = {"姓名","数学成绩","语文成绩","英语成绩","总分"};
//
// 输入数据并计算总分,基本可以实现无异常输入,名字数组长度30,有越界可能
//
for(count = 0; count < LEN; count++)
{
printf("Input student%d's name: ", (count+1));
gets(table[count].name);
printf("Input the score of Math: ");
while(!scanf("%f", &table[count].scrOfMth))
{
printf("Check your input and retry.\n");
while(getchar()!='\n')
{
continue;
}
}
while(getchar()!='\n')
{
continue;
}
printf("Input the score of Chinese: ");
while(!scanf("%f", &table[count].scrOfChn))
{
printf("Check your input and retry.\n");
while(getchar()!='\n')
{
continue;
}
}
while(getchar()!='\n')
{
continue;
}
printf("Input the score of English: ");
while(!scanf("%f", &table[count].scrOfEng))
{
printf("Check your input and retry.\n");
while(getchar()!='\n')
{
continue;
}
}
while(getchar()!='\n')
{
continue;
}
table[count].scrOfAll = table[count].scrOfMth + table[count].scrOfChn
+ table[count].scrOfEng;
}
//
// 因为数组长度只有3,所以用3个if就可以实现排序,没有用排序算法
//
if( table[0].scrOfAll > table[1].scrOfAll )
{
swap(table, table+1);
}
if( table[1].scrOfAll > table[2].scrOfAll )
{
swap(table+1, table+2);
}
if( table[0].scrOfAll > table[1].scrOfAll )
{
swap(table, table+1);
}
//
// 打印表头,然后输出列表
//
for(count = 0; count < 5; count++)
{
printf("%s\t", p[count]);
}
printf("\n");
for(count = 0; count < LEN; count++)
{
printf("%s\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", table[count].name, table[count].scrOfMth, table[count].scrOfChn, table[count].scrOfEng, table[count].scrOfAll);
}
return 0;
}
//
// 函数swap,交换两个结构的数据
//
void swap( Stu *a, Stu *b )
{
Stu temp;
temp = *a;
*a = *b;
*b = temp;
}
3.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX 10000000;
void factory(int *, int, int);
int main(void)
{
int arg = 0; // 阶乘底数
double temp = 0.0; // 临时存储器
int length = 0; // 内存长度
int *pointer = NULL; // 指针
int count = 0; // 计数器
printf("Input the argument(integer): ");
while (!scanf("%d", &arg))
{
printf("Check your input and retry: ");
while (getchar()!='\n')
{
continue;
}
}
while (getchar()!='\n')
{
continue;
}
//
// 计算需要多大的数组(足够,但可能多)
//
for (count = 1; count <= arg; count++)
{
temp += log10((double)arg);
}
length = (int)temp/7 + 1;
pointer = (int *) malloc( length * sizeof(int) );
//
// 调用函数计算阶乘
//
factory( pointer, length, arg );
//
// 找到最后一个不等于0的元素,从该元素开始倒序输出,每个元素输出7位数组,用0补位
//
count = length -1;
while (*(pointer+count) == 0)
{
count--;
}
printf("%d", *(pointer+count--));
for (; count >= 0; count--)
{
printf("%07d", *(pointer+count));
}
return 0;
}
//
// 计算阶乘的函数
//
void factory( int *a, const int l, const int n )
{
int count = 0;// 计数器
int mul = 0;// 乘数
int upper = 0;// 进位计数器
int temp = 0;// 临时存储器
for (count = 0; count < l; count++)
{
*(a+count) = 0;
}
*a = 1;
//
// 数组中每个元素存储7位数字
//
for (mul = 1; mul <= n; mul++)
{
upper = 0;
for (count = 0; count < l; count++)
{
temp = *(a+count) * mul + upper;
*(a+count) = temp % MAX;
upper = temp / MAX;
}
}
}
太多了,错误应该没有,注释可能少了点,实在太多了。自己仔细看看吧。
温馨提示:内容为网友见解,仅供参考