Em xin chào mọi người ạ . Em đang gặp khó khăn về phần gọi phương trình từ một hàm khác vào hàm giải pt để giải pt ạ .
Nguyên văn của đề như sau ạ:
- File DAYSO.IN có thể sẽ bị giảng viên bắt bẻ bằng cách thêm phần tử ạ(thêm thủ công trong file DAYSO.IN)
- Hiện tại câu em đã làm xong câu a và câu b. Câu c em đã giải được pt bằng pp dây cung ở một chương trình riêng, nhưng phương trình đó là do em nhập sẵn, chỉ thay đổi được bằng cách sửa code. Code của câu c em đã để riêng ra một ct và đã giải được, nó đây ạ
//phuongphapdaycung
#include<conio.h>
#include<stdio.h>
#include<math.h>
#define epsi 0.00001
int main()
{
float a,b,ga,gb,dx,x;
float g(float);
printf("Tim nghiem bang phuong phap day cung\n");
printf("Cho cac gia tri a, b \n");
do
{
printf("Cho gia tri cua a = ");
scanf("%f",&a);
printf("Cho gia tri cua b = ");
scanf("%f",&b);
}
while (g(a)*g(b) > 0 );
ga=g(a);
gb=g(b);
dx = ga*(b-a)/(ga-gb);
while( fabs(dx) > epsi)
{
x=a+dx;
ga=g(x);
if( (ga*gb) <= 0 )
a=x;
else
b=x;
ga=g(a);
gb=g(b);
dx = ga*(b-a)/(ga-gb);
}
printf("Nghiem x = %f",x);
getch();
}
float g(float x)
{
float e;
e = -12 + 11*x + 9*x*x + 7*x*x*x + 5*x*x*x*x + 3*x*x*x*x*x + x*x*x*x*x*x;
return e;
}
// Nghiem cua pt trên la:
// 0.5921746962
// -2.096592022
Dưới đây là code của câu a và câu b ạ:
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
struct Node
{
float data;
struct Node *next;
};
struct List
{
struct Node *head;
struct Node *tail;
};
// khoi tao stack
void Init(struct List *s)
{
s->head = s->tail = NULL;
}
// kiem tra stack co rong khong
int isEmpty(struct List *s)
{
if (s->head == NULL)
return 1;
else
return 0;
}
// tao Node
struct Node *creat_Node(float x)
{
struct Node *p = (struct Node*) malloc( sizeof(struct Node) );
p->data = x;
p->next = NULL;
return p;
}
// Push vao stack
void push(struct List *s, float x)
{
struct Node *p = creat_Node(x);
if (isEmpty(s))
{
s->head = s->tail = p; // neu stack rong thi ta gan p la phan tu dau va cuoi
}
else
{
p->next = s->head; // cho phan tu ke tiep bang phan tu dau
s->head = p; // cap nhat phan tu dau la phan tu moi them vao
}
}
// Pop phan tu ra khoi stack
void pop(struct List *s)
{
struct Node *p = s->head;
if (!isEmpty(s))
{
s->head = p->next;
free(p);
}
else
{
printf("\n danh sach dang rong");
}
}
// tim kiem phan tu trong stack
struct Node *search_Node(struct List s, int x)
{
struct Node *q = s.head;
while (q != NULL)
{
if (q->data == x)
{
return q;
}
else
{
q = q->next;
}
}
return NULL;
}
// Nhap du lieu cho Stack
void xuat(struct List s)
{
struct Node *p = s.head;
if (p==NULL)
{
printf("\n danh sach rong");
return ;
}
while (p != NULL)
{
printf("\t%.2f",p->data);
p = p->next;
}
}
void Doc_file(struct List s[],FILE *f,int n,int m)
{
f=fopen("DAYSO.IN","r");
fscanf(f,"\n%d",&n);
fscanf(f,"\n%d",&m);
int i,j;
for(i=1;i<=n;i++)
{
printf("danh sach thu %2d:\n",i);
Init(&s[i]);// khoi tao stack
for(j=1;j<=m;j++)
{
struct Node *p;
// struct Node *p = creat_struct Node(x);
float x;
fscanf(f,"%f ",&x);
push(&s[i], x);
}
xuat(s[i]);
printf("\n");
}
fclose(f);
}
void xoaphantu(struct List s[])
{
pop(&s[1]);
xuat (s[1]);
printf("\n");
}
void xuat_file(struct List s[],FILE *f)
{
// FILE *f;
f=fopen("RESULT1.OUT","w");
int i,j;
int n;
for (i=1;i<=1;++i)
{
struct Node *p = s[i].head;
while (p != NULL)
{
fprintf(f,"\n %.2f ",p->data);
// printf("%.2f ",p->data);
p = p->next;
}
fprintf(f,"\n");
printf("\n");
}
fclose(f);
}
int main()
{
int n,m;
struct List s[10];
FILE*f;
Doc_file(s,f,n,m) ;
xoaphantu(s);
xuat_file(s,f);
}
- Hai file DAYSO.IN và file RESULT1.OUT của em đây ạ:
- file DAYSO.IN có nội dung như sau:
1 8
1 3 5 7 9 11 -12 15
- File RESULT1.OUT có nội dung như sau:
-12.00
11.00
9.00
7.00
5.00
3.00
1.00
- Bây giờ em muốn nhập hai chương trình này thành 1 nhưng vẫn không thành ạ, đây là code của em khi sáp nhập hai chương trình của câu a,b và câu c ạ:
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
struct Node
{
float data;
struct Node *next;
};
struct List
{
struct Node *head;
struct Node *tail;
};
// khoi tao stack
void Init(struct List *s)
{
s->head = s->tail = NULL;
}
// kiem tra stack co rong khong
int isEmpty(struct List *s)
{
if (s->head == NULL)
return 1;
else
return 0;
}
// tao Node
struct Node *creat_Node(float x)
{
struct Node *p = (struct Node*) malloc( sizeof(struct Node) );
p->data = x;
p->next = NULL;
return p;
}
// Push vao stack
void push(struct List *s, float x)
{
struct Node *p = creat_Node(x);
if (isEmpty(s))
{
s->head = s->tail = p; // neu stack rong thi ta gan p la phan tu dau va cuoi
}
else
{
p->next = s->head; // cho phan tu ke tiep bang phan tu dau
s->head = p; // cap nhat phan tu dau la phan tu moi them vao
}
}
// Pop phan tu ra khoi stack
void pop(struct List *s)
{
struct Node *p = s->head;
if (!isEmpty(s))
{
s->head = p->next;
free(p);
}
else
{
printf("\n danh sach dang rong");
}
}
// tim kiem phan tu trong stack
struct Node *search_Node(struct List s, int x)
{
struct Node *q = s.head;
while (q != NULL)
{
if (q->data == x)
{
return q;
}
else
{
q = q->next;
}
}
return NULL;
}
// Nhap du lieu cho Stack
void xuat(struct List s)
{
struct Node *p = s.head;
if (p==NULL)
{
printf("\n danh sach rong");
return ;
}
while (p != NULL)
{
printf("\t%.2f",p->data);
p = p->next;
}
}
void Doc_file(struct List s[],FILE *f,int n,int m)
{
f=fopen("DAYSO.IN","r");
fscanf(f,"\n%d",&n);
fscanf(f,"\n%d",&m);
int i,j;
for(i=1;i<=n;i++)
{
printf("danh sach thu %2d:\n",i);
Init(&s[i]);// khoi tao stack
for(j=1;j<=m;j++)
{
struct Node *p;
// struct Node *p = creat_struct Node(x);
float x;
fscanf(f,"%f ",&x);
push(&s[i], x);
}
xuat(s[i]);
printf("\n");
}
fclose(f);
}
void xoaphantu(struct List s[])
{
pop(&s[1]);
xuat (s[1]);
printf("\n");
}
void xuat_file(struct List s[],FILE *f )
{
// FILE *f;
f=fopen("RESULT1.OUT","w");
int i,j;
int n;
for (i=1;i<=1;++i)
{
struct Node *p = s[i].head;
while (p != NULL)
{
fprintf(f,"\n %.2f ",p->data);
// printf("%.2f ",p->data);
p = p->next;
}
fprintf(f,"\n");
printf("\n");
}
fclose(f);
}
float pt(struct List s[],FILE *f,int m)
{
int i=0,j=0;
float pt = 0;
float a[100][100]={{0}};
struct Node* p =s[1].head;
while (p!=NULL){
a[i][j]=p->data;
p=p->next;
j++;
}
int k, x;
// printf("\n");
for (i=0;i<1;++i)
{
for (k=0;k<j;k++)
{
printf("%f ",a[i][k]);
pt += a[i][k]*pow(x,k);
}
// printf("%f\n", pt);
}
return pt;
}
float g(float x)
{
float e;
int m;
struct List s[10];
FILE*f;
pt(s,f, m);
e = pt(s,f, m);
return e;
}
int giaipt(struct List s[],FILE *f)
{
float a,b,ga,gb,dx,x;
float g(float);
float epsi = 0.00001;
// clrscr();
printf("\nTim nghiem bang phuong phap day cung");
printf("\nCho cac gia tri a, b ");
do
{
printf("\nCho gia tri cua a = ");
scanf("%f",&a);
printf("\nCho gia tri cua b = ");
scanf("%f",&b);
}
while (g(a)*g(b) > 0 );
ga=g(a);
gb=g(b);
dx = ga*(b-a)/(ga-gb);
while( fabs(dx) > epsi)
{
x=a+dx;
ga=g(x);
if( (ga*gb) <= 0 )
a=x;
else
b=x;
ga=g(a);
gb=g(b);
dx = ga*(b-a)/(ga-gb);
}
printf("Nghiem x = %f",x);
f=fopen("RESULT2.OUT","w");
int i,j;
struct Node *p = s[i].head;
while (p != NULL)
{
fprintf(f,"\n %.2f ",x);
// printf("%.2f ",p->data);
p = p->next;
}
fprintf(f,"\n");
printf("\n");
// getch();
}
int main()
{
int n,m;
struct List s[10];
FILE*f;
Doc_file(s,f,n,m);
xoaphantu(s);
xuat_file(s,f);
pt(s,f,m);
giaipt(s,f);
}
- Mong các cao nhân đi trước chỉ ra lỗi sai của em trong quá trình sát nhập code câu a,b và câu c ạ. Em đã cố gắng cả tuần nay nhưng không thành ạ. Em xin chân thành cảm ơn các bác .