C语言编写函数求表达式

C语言编写函数求表达式,第1张

#include<stdioh>

int main(){

int i,n;

double s=00;

scanf("%d",&n);

for(i=1;i<=n;i++)

if(i%2)s+=1/(double)(ii);

else s-=1/(double)(ii);

printf("%lf",s);

return 0;}

5种方法如下:

(1) 待定系数法:已知函数类型,可用待定系数法求解,先设出f (x),再利用题目中给的已知条件,列出关于待定系数的方程组,进而求出待定的系数;

(2)换元法:主要用于解决已知复合函数f [g (x)]的表达式求f (x)的解析式的问题,令g(x)=t,解出x,然后代入f [g (x)]中即可求得f(t),从而求得f (x),要注意新元的取值范围;

(3)配凑法:配凑法是将f [g (x)]右端的代数式配凑成关于g (x)的形式,进而求出f (x)的解析式;

(4)构造方程组法(消元法) :主要解决已知抽象函数关系式求解函数解析式的问题方法是根据不同的变量之间的关系,利用变换形式构造不同的等式,通过解方程组求解。

(5)赋值法:在求某些函数的表达式或求某些函数值时,有时把已知条件中的某些变量赋值,使问题简单明了,从而易于求出函数的表达式。

这个是个简单的学生管理系统,我们学校的程序大作业就是做这个呵呵,希望能帮到你

#include<stdioh> /引用库函数/

#include<stdlibh>

#include<ctypeh>

#include<stringh>

typedef struct /定义结构体数组/

{

char num[10]; /学号/

char name[20]; /姓名/

int score; /成绩/

}Student;

Student stu[80]; /结构体数组变量/

int menu_select() /菜单函数/

{

char c;

do{

system("cls"); /运行前清屏/

printf("\t\tStudents' Grade Management System\n"); /菜单选择/

printf("\t\t | 1 Input Records |\n");

printf("\t\t | 2 Display All Records |\n");

printf("\t\t | 3 Sort |\n");

printf("\t\t | 4 Insert a Record |\n");

printf("\t\t | 5 Delete a Record |\n");

printf("\t\t | 6 Query |\n");

printf("\t\t | 7 Statistic |\n");

printf("\t\t | 8 Add Records from a Text File|\n");

printf("\t\t | 9 Write to a Text file |\n");

printf("\t\t | 0 Quit |\n");

printf("\t\t\n");

printf("\t\t\tGive your Choice(0-9):");

c=getchar(); /读入选择/

}while(c<'0'||c>'9');

return(c-'0'); /返回选择/

}

int Input(Student stud[],int n) /输入若干条记录/

{int i=0;

char sign,x[10]; /x[10]为清除多余的数据所用/

while(sign!='n'&&sign!='N') /判断/

{ printf("\t\t\tstudent's num:"); /交互输入/

scanf("\t\t\t%s",stud[n+i]num);

printf("\t\t\tstudent's name:");

scanf("\t\t\t%s",stud[n+i]name);

printf("\t\t\tstudent's score:");

scanf("\t\t\t%d",&stud[n+i]score);

gets(x); /清除多余的输入/

printf("\t\t\tany more records(Y/N)");

scanf("\t\t\t%c",&sign); /输入判断/

i++;

}

return(n+i);

}

void Display(Student stud[],int n) /显示所有记录/

{

int i;

printf("\t\t\t-----------------------------------\n"); /格式头/

printf("\t\t\tnumber name score\n");

printf("\t\t\t-----------------------------------\n");

for(i=1;i<n+1;i++) /循环输入/

{

printf("\t\t\t%-16s%-15s%d\n",stud[i-1]num,stud[i-1]name,stud[i-1]score);

if(i>1&&i%10==0) /每十个暂停/

{printf("\t\t\t-----------------------------------\n"); /格式/

printf("\t\t\t");

system("pause");

printf("\t\t\t-----------------------------------\n");

}

}

printf("\t\t\t");

system("pause");

}

void Sort_by_num(Student stud[],int n) /按学号排序/

{ int i,j,p,q,s;

char t[10];

for(i=0;i<n-1;i++) /冒泡法排序/

for(j=0;j<n-1-i;j++)

if(strcmp(stud[j]num,stud[j+1]num)>0)

{strcpy(t,stud[j+1]num);

strcpy(stud[j+1]num,stud[j]num);

strcpy(stud[j]num,t);

strcpy(t,stud[j+1]name);

strcpy(stud[j+1]name,stud[j]name);

strcpy(stud[j]name,t);

p=&stud[j+1]score;

q=&stud[j]score;

s=p;

p=q;

q=s;

}

}

int Insert_a_record(Student stud[],int n) /插入一条记录/

{char x[10]; /清除多余输入所用/

printf("\t\t\tstudent's num:"); /交互式输入/

scanf("\t\t\t%s",stud[n]num);

printf("\t\t\tstudent's name:");

scanf("\t\t\t%s",stud[n]name);

printf("\t\t\tstudent's score:");

scanf("\t\t\t%d",&stud[n]score);

gets(x);

n++;

Sort_by_num(stud,n); /调用排序函数/

printf("\t\t\tInsert Successed!\n"); /返回成功信息/

return(n);

}

int Delete_a_record(Student stud[],int n) /按姓名查找,删除一条记录/

{ char s[20];

int i=0,j;

printf("\t\t\ttell me his(her) name:"); /交互式问寻/

scanf("%s",s);

while(strcmp(stud[i]name,s)!=0&&i<n) i++; /查找判断/

if(i==n)

{ printf("\t\t\tnot find!\n"); /返回失败信息/

return(n);

}

for(j=i;j<n-1;j++) /删除 *** 作/

{

strcpy(stud[j]num,stud[j+1]num);

strcpy(stud[j]name,stud[j+1]name);

stud[j]score=stud[j+1]score;

}

printf("\t\t\tDelete Successed!\n"); /返回成功信息/

return(n-1);

}

void Query_a_record(Student stud[],int n) /查找并显示一个记录/

{ char s[20];

int i=0;

printf("\t\t\tinput his(her) name:"); /交互式输入/

scanf("\t\t\t%s",s);

while(strcmp(stud[i]name,s)!=0&&i<n) i++; /查找判断/

if(i==n)

{ printf("\t\t\tnot find!\n"); /输入失败信息/

return;

}

printf("\t\t\this(her) number:%s\n",stud[i]num); /输出该学生信息/

printf("\t\t\this(her) score:%d\n",stud[i]score);

}

void Statistic(Student stud[],int n) /新增功能,输出统计信息/

{ int i,j=0,k=0,sum=0;

float aver; /成绩平均值/

for(i=0;i<n;i++) /循环输入判断/

{

sum+=stud[i]score;

if(stud[j]score>stud[i]score) j=i;

if(stud[k]score<stud[i]score) k=i;

}

aver=10sum/n;

printf("\t\t\tthere are %d records\n",n); /总共记录数/

printf("\t\t\tthe hignest score:\n"); /最高分/

printf("\t\t\tnumber:%s name:%s score:%d\n",stud[j]num,stud[j]name,stud[j]score);

printf("\t\t\tthe lowest score:\n"); /最低分/

printf("\t\t\tnumber:%s name:%s score:%d\n",stud[k]num,stud[k]name,stud[k]score);

printf("\t\t\tthe average score is %52f\n",aver); /平均分/

}

int AddfromText(Student stud[],int n) /从文件中读入数据/

{ int i=0,num;

FILE fp; /定义文件指针/

char filename[20]; /定义文件名/

printf("\t\t\tInput the filename:");

scanf("\t\t\t%s",filename); /输入文件名/

if((fp=fopen(filename,"rb"))==NULL) /打开文件/

{ printf("\t\t\tcann't open the file\n"); /打开失败信息/

printf("\t\t\t");

system("pause");

return(n);

}

fscanf(fp,"%d",&num); /读入总记录量/

while(i<num) /循环读入数据/

{

fscanf(fp,"%s%s%d",stud[n+i]num,stud[n+i]name,&stud[n+i]score);

i++;

}

n+=num;

fclose(fp); /关闭文件/

printf("\t\t\tSuccessed!\n");

printf("\t\t\t");

system("pause");

return(n);

}

void WritetoText(Student stud[],int n) /将所有记录写入文件/

{

int i=0;

FILE fp; /定义文件指针/

char filename[20]; /定义文件名/

printf("\t\t\tWrite Records to a Text File\n"); /输入文件名/

printf("\t\t\tInput the filename:");

scanf("\t\t\t%s",filename);

if((fp=fopen(filename,"w"))==NULL) /打开文件/

{

printf("\t\t\tcann't open the file\n");

system("pause");

return;

}

fprintf(fp,"%d\n",n); /循环写入数据/

while(i<n)

{

fprintf(fp,"%-16s%-15s%d\n",stud[i]num,stud[i]name,stud[i]score);

i++;

}

fclose(fp); /关闭文件/

printf("Successed!\n"); /返回成功信息/

}

void main() /主函数/

{

int n=0;

for(;;)

{

switch(menu_select()) /选择判断/

{

case 1:

printf("\t\t\tInput Records\n"); /输入若干条记录/

n=Input(stu,n);

break;

case 2:

printf("\t\t\tDisplay All Records\n"); /显示所有记录/

Display(stu,n);

break;

case 3:

printf("\t\t\tSort\n");

Sort_by_num(stu,n); /按学号排序/

printf("\t\t\tSort Suceessed!\n");

printf("\t\t\t");

system("pause");

break;

case 4:

printf("\t\t\tInsert a Record\n");

n=Insert_a_record(stu,n); /插入一条记录/

printf("\t\t\t");

system("pause");

break;

case 5:

printf("\t\t\tDelete a Record\n");

n=Delete_a_record(stu,n); /按姓名查找,删除一条记录/

printf("\t\t\t");

system("pause");

break;

case 6:

printf("\t\t\tQuery\n");

Query_a_record(stu,n); /查找并显示一个记录/

printf("\t\t\t");

system("pause");

break;

case 7:

printf("\t\t\tStatistic\n");

Statistic(stu,n); /新增功能,输出统计信息/

printf("\t\t\t");

system("pause");

break;

case 8:

printf("\t\t\tAdd Records from a Text File\n");

n=AddfromText(stu,n); /新增功能,输出统计信息/

break;

case 9:

printf("\t\t\tWrite to a Text file\n");

WritetoText(stu,n); /循环写入数据/

printf("\t\t\t");

system("pause");

break;

case 0:

printf("\t\t\tHave a Good Luck,Bye-bye!\n"); /结束程序/

printf("\t\t\t");

system("pause");

exit(0);

}

}

}

无论是你的答案还是正确答案 都有问题

你的答案 问题在于自变量i设置成了double型

浮点型 做自加 *** 作的时候可能导致死循环的 也就是你的程序 如果精度设置的特别高会出问题

所谓的正确答案,没有这个问题 但是不符题意

即某项小于00005时停止迭代

正确答案中是某项小于精度后 把该项累加并在下一项停止迭代

下面是把你的程序改成了完全符合“正确答案”的代码

PS: 题目中也有一个错误 当精度为00005时 输出的是3141106 即保留四位小数位31411

要想输出31416 最低精度为000002

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#include <stdioh>

double fun ( double eps)

{ double pi=1,a=1;

int i;

for(i=1;a>=eps;i++){

a=ai/(2i+1);

pi=pi+a;

}

return 2pi;

}

main( )

{ double x;

printf("Input eps:") ;

scanf("%lf",&x); printf("\neps = %lf, PI=%4lf\n", x, fun(x));//输出4位小数

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/12184849.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存