#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct message
{
char name[10];//用戶
char use[50];//用途
double income;//收入
double spend;//支出
char time[10];//使用時間
double sum;//余額
struct message *next;
};
struct message *Findfile(struct message *head1)//將檔案資訊存到鏈表中
{
struct message *head=NULL;
FILE *fp1;
char c;
struct message *p1,*p2;
char name[10];//用戶
char use[50];//用途
double income;//收入
double spend;//支出
char time[10];//使用時間
double sum;//余額
if((fp1=fopen("money.in","r"))==NULL)
{
printf("cannot open file\n");
return 0;
}
while((c=fgetc(fp1))!=EOF)
{
fseek(fp1,-1,SEEK_CUR);
p1=(struct message* )malloc(sizeof(struct message));
fscanf(fp1,"%s%lf%lf%lf%s%s\n",name,&income,&spend,&sum,use,time);
strcpy(p1->name,name);
strcpy(p1->use,use);
p1->income=income;
p1->spend=spend;
strcpy(p1->time,time);
p1->sum=sum;
p1->next=NULL;
if(head==NULL) head=p1;
else p2->next=p1;
p2=p1;
}
return head1;
}
void Correct(struct message *head3)
{
struct message *p31=head3,*p32=p31->next;
while(p31&&p32)
{
p32->sum=(p31->sum)+(p32->income)-(p32->spend);
p31=p31->next;
p32=p31->next;
}
}
void Change(struct message *head2)
{
int k=0,choice;
struct message *p21,*p22=head2,*p23,*p24=head2;
p21=head2;
char name[10];
printf("請輸入要修改的人的名字:");
scanf("%s",name);
while(p21)
{
if(strcmp(p21->name,name)==0)
{
k++;
printf("[%d] %s %.2f %.2f %.2f %s %s\n",k,p21->name,p21->income,p21->spend,p21->sum,p21->use,p21->time);
}
p21=p21->next;
}
if(k!=0)
{
printf("選擇要修改的賬戶資訊的序號:");
scanf("%d",&k);
while(k)
{
if(strcmp(p22->name,name)==0)
{
k--;
p23=p22;
p22=p22->next;
}
else
{
p23=p22;
p22=p22->next;
}
}
printf("選擇要修改的賬戶資訊的專案序號:");
scanf("%d",&choice);
while(choice)
{
switch(choice)
{
case 1:
{
printf("請輸入更正后的姓名:");
scanf("%s",p23->name);
printf("修改成功");
break;
}
case 2:
{
printf("請輸入更正后的收入金額:");
scanf("%lf",&p23->income);
printf("修改成功");
break;
}
case 3:
{
printf("請輸入更正后的支出金額:");
scanf("%lf",&p23->spend);
printf("修改成功");
break;
}
case 4:
{
printf("請輸入更正后的用途:");
scanf("%s",p23->use);
printf("修改成功");
break;
}
case 5:
{
printf("請輸入更正后的使用時間:");
scanf("%s",p23->time);
printf("修改成功");
break;
}
case 0:break;
default:
printf("請輸入正確的序號:");
break;
}
if(choice==2||choice==3)
{
if(p24==p23)
{
p24->sum=p23->income-p23->spend;
}
else
{
while(p24->next!=p23)
{
p24=p24->next;
}
Correct(p24);
}
}
}
}
else
{
printf("查無此人!");
}
}
int main()
{
struct message *head=NULL;
head=Findfile(head);
int power=1;
if(power)
{
Change(head);
}
return 0;
}
uj5u.com熱心網友回復:
資料結構對單鏈表進行資料排序 http://bbs.csdn.net/topics/392201633uj5u.com熱心網友回復:
供參考:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct message
{
char name[10];//用戶
char use[50];//用途
double income;//收入
double spend;//支出
char time[10];//使用時間
double sum;//余額
struct message *next;
};
struct message *Findfile(struct message *head1)//將檔案資訊存到鏈表中
{
struct message *head;
FILE *fp1;
struct message *p1,*p2;
if((fp1=fopen("money.in","r"))==NULL)
{
printf("cannot open file\n");
return 0;
}
if (head1 == NULL){
head1=(struct message* )malloc(sizeof(struct message));//生成頭節點
head = head1;
head1->next=NULL;
}else{
for(head=head1;head->next!=NULL;head=head->next);//找到節點尾部
}
p2 = head;
while(1){
p1=(struct message* )malloc(sizeof(struct message));
if(fscanf(fp1,"%s%lf%lf%lf%s%s\n",p1->name,&p1->income,
&p1->spend,&p1->sum,p1->use,p1->time)!=6)break;
p1->next=NULL;
if(head==NULL) head->next=p1;
else p2->next=p1;
p2=p1;
}
return head1;
}
void Correct(struct message *head3)
{
struct message *p31=head3,*p32=p31->next;
while(p31&&p32)
{
p32->sum=(p31->sum)+(p32->income)-(p32->spend);
p31=p31->next;
p32=p31->next;
}
}
void Change(struct message *head2)
{
int k=0,choice;
struct message *p21,*p22=head2,*p23,*p24=head2;
p21=head2;
char name[10];
printf("請輸入要修改的人的名字:");
scanf("%s",name);
while(p21)
{
if(strcmp(p21->name,name)==0)
{
k++;
printf("[%d] %s %.2f %.2f %.2f %s %s\n",k,p21->name,
p21->income,p21->spend,p21->sum,p21->use,p21->time);
}
p21=p21->next;
}
if(k!=0)
{
printf("選擇要修改的賬戶資訊的序號:");
scanf("%d",&k);
while(k)
{
if(strcmp(p22->name,name)==0)
{
k--;
p23=p22;
p22=p22->next;
}
else
{
p23=p22;
p22=p22->next;
}
}
printf("選擇要修改的賬戶資訊的專案序號:");
scanf("%d",&choice);
while(choice)
{
switch(choice)
{
case 1:
{
printf("請輸入更正后的姓名:");
scanf("%s",p23->name);
printf("修改成功");
break;
}
case 2:
{
printf("請輸入更正后的收入金額:");
scanf("%lf",&p23->income);
printf("修改成功");
break;
}
case 3:
{
printf("請輸入更正后的支出金額:");
scanf("%lf",&p23->spend);
printf("修改成功");
break;
}
case 4:
{
printf("請輸入更正后的用途:");
scanf("%s",p23->use);
printf("修改成功");
break;
}
case 5:
{
printf("請輸入更正后的使用時間:");
scanf("%s",p23->time);
printf("修改成功");
break;
}
case 0:break;
default:
printf("請輸入正確的序號:");
break;
}
if(choice==2||choice==3)
{
if(p24==p23)
{
p24->sum=p23->income-p23->spend;
}
else
{
while(p24->next!=p23)
{
p24=p24->next;
}
Correct(p24);
}
}
}
}
else
{
printf("查無此人!");
}
}
int main()
{
struct message *head=NULL;
head = Findfile(head);
int power=1;
if(power)
{
Change(head);
}
return 0;
}
uj5u.com熱心網友回復:
修正#2樓的,第43,44行之間插入一行:fclose(fp1);(得把打開的檔案關閉了)
uj5u.com熱心網友回復:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct message
{
char name[10];//用戶
char use[50];//用途
double income;//收入
double spend;//支出
char time[10];//使用時間
double sum;//余額
struct message *next;
};
struct message *Findfile(struct message *head1)//將檔案資訊存到鏈表中
{
struct message *head=NULL;
FILE *fp1;
//char c;
int c;
struct message *p1,*p2;
char name[10];//用戶
char use[50];//用途
double income;//收入
double spend;//支出
char time[10];//使用時間
double sum;//余額
if((fp1=fopen("money.in","r"))==NULL)
{
printf("cannot open file\n");
return 0;
}
//while((c=fgetc(fp1))!=EOF)
while((c=fgetc(fp1))!=EOF) //c表示不了EOF,因此要用int定義c
{
fseek(fp1,-1,SEEK_CUR); //這兒的操作似乎沒有意義~
p1=(struct message* )malloc(sizeof(struct message));
fscanf(fp1,"%s%lf%lf%lf%s%s\n",name,&income,&spend,&sum,use,time);
strcpy(p1->name,name);
strcpy(p1->use,use);
p1->income=income;
p1->spend=spend;
strcpy(p1->time,time);
p1->sum=sum;
p1->next=NULL;
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
}
fclose(fp1);
return head1;
}
void Correct(struct message *head3)
{
struct message *p31=head3,*p32=p31->next;
while(p31&&p32)
{
p32->sum=(p31->sum)+(p32->income)-(p32->spend);
p31=p31->next;
p32=p31->next;
}
}
void Change(struct message *head2)
{
int k=0,choice;
struct message *p21,*p22=head2,*p23,*p24=head2;
p21=head2;
char name[10];
printf("請輸入要修改的人的名字:");
scanf("%s",name);
while(p21)
{
if(strcmp(p21->name,name)==0)
{
k++;
printf("[%d] %s %.2f %.2f %.2f %s %s\n",k,p21->name,p21->income,p21->spend,p21->sum,p21->use,p21->time);
break;//這兒是不是應該跳出
}
p21=p21->next;
}
if(k!=0)
{
printf("選擇要修改的賬戶資訊的序號:");
scanf("%d",&k);
while(k)
{
//if(strcmp(p22->name,name)==0) //比較有什么意義?if和else執行了基本上相同的邏輯除了k--
if(strcmp(p22->name,name)==0) //比較有什么意義?if和else執行了基本上相同的邏輯除了k--
{
k--;
p23=p22;
p22=p22->next;
}
else
{
p23=p22;
p22=p22->next;
}
}
printf("選擇要修改的賬戶資訊的專案序號:");
scanf("%d",&choice);
while(choice)
{
switch(choice)
{
case 1:
{
printf("請輸入更正后的姓名:");
scanf("%s",p23->name); //此時的p23是否為你要找的節點,我看不一定,可能是NULL
printf("修改成功");
break;
}
case 2:
{
printf("請輸入更正后的收入金額:");
scanf("%lf",&p23->income);
printf("修改成功");
break;
}
case 3:
{
printf("請輸入更正后的支出金額:");
scanf("%lf",&p23->spend);
printf("修改成功");
break;
}
case 4:
{
printf("請輸入更正后的用途:");
scanf("%s",p23->use);
printf("修改成功");
break;
}
case 5:
{
printf("請輸入更正后的使用時間:");
scanf("%s",p23->time);
printf("修改成功");
break;
}
case 0:break;
default:
printf("請輸入正確的序號:");
break;
}
if(choice==2||choice==3)
{
if(p24==p23)
{
p24->sum=p23->income-p23->spend;
}
else
{
while(p24->next!=p23)
{
p24=p24->next;
}
Correct(p24);
}
}
}
}
else
{
printf("查無此人!");
}
}
int main()
{
struct message *head=NULL;
head=Findfile(head);
int power=1;
if(power)
{
Change(head);
}
return 0;
}
問題比較多,樓主的問題只是其中一個。建議樓主在Findfile函式執行完把創建的鏈表列印出來,確保進入Change函式里的鏈表是正常的。
uj5u.com熱心網友回復:
調通代碼,但修改函式Change()的功能沒實作目標呢,似乎樓主考慮得太復雜了,其他問題見代碼,供參考:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct message
{
char name[10];//用戶
char use[50];//用途
double income;//收入
double spend;//支出
char time[10];//使用時間
double sum;//余額
struct message *next;
};
struct message *Findfile(struct message *head1)//將檔案資訊存到鏈表中
{
struct message *head;
FILE *fp1;
struct message *p1,*p2;
if((fp1=fopen("money.in","r"))==NULL){
printf("cannot open file\n");
return 0;
}
if (head1 == NULL){
head1=(struct message* )malloc(sizeof(struct message));//生成頭節點
head = head1;
head1->next=NULL;
}else{
for(head=head1;head->next!=NULL;head=head->next);//找到節點尾部
}
p2 = head;
while(1){
p1=(struct message* )malloc(sizeof(struct message));
if(fscanf(fp1,"%s %s %lf %lf %s %lf\n",p1->name,p1->use,&p1->income,
&p1->spend,p1->time,&p1->sum)!=6)break;
p1->next=NULL; //從檔案里讀入資訊的各引數順序和結構體里定義的順序一樣
if(head==NULL) head->next=p1; //檔案保存時也必須是按此順序排列。
else p2->next=p1;
p2=p1;
}
fclose(fp1);
return head1;
}
void Correct(struct message *head3)
{
struct message *p31=head3,*p32=p31->next;
while(p31&&p32)
{
p32->sum=(p31->sum)+(p32->income)-(p32->spend);
p31=p31->next;
p32=p31->next;
}
}
void Change(struct message *head2) //修改
{
int k=0,choice;
struct message *p21,*p22=head2,*p23,*p24=head2;
p21=head2;
char name[10];
printf("請輸入要修改的人的名字:");
scanf("%s",name);
while(p21)
{
if(strcmp(p21->name,name)==0)
{
k++;
printf("[%d] %s %.2f %.2f %.2f %s %s\n",k,p21->name,
p21->income,p21->spend,p21->sum,p21->use,p21->time);
}
p21=p21->next;
}
if(k!=0)
{
printf("選擇要修改的賬戶資訊的序號:");
scanf("%d",&k);
while(k) //這段的演算法沒成功,就是不能實作下面的對資訊的修改定位
{
if(strcmp(p22->name,name)==0)
{
k--;
p23=p22;
p22=p22->next;
}
else
{
p23=p22;
p22=p22->next;
}
}
printf("選擇要修改的賬戶資訊的專案序號(1:姓名,2:收入金額,3:支出金額,4:用途,5:使用時間):");
scanf("%d",&choice);
//while(choice) //這里的while多余了,造成死回圈
//{
switch(choice)
{
case 1:
{
printf("請輸入更正后的姓名:");
scanf("%s",p23->name);
printf("修改成功");
break;
}
case 2:
{
printf("請輸入更正后的收入金額:");
scanf("%lf",&p23->income);
printf("修改成功");
break;
}
case 3:
{
printf("請輸入更正后的支出金額:");
scanf("%lf",&p23->spend);
printf("修改成功");
break;
}
case 4:
{
printf("請輸入更正后的用途:");
scanf("%s",p23->use);
printf("修改成功");
break;
}
case 5:
{
printf("請輸入更正后的使用時間:");
scanf("%s",p23->time);
printf("修改成功");
break;
}
case 0:break;
default:
printf("請輸入正確的序號:");
break;
}
if(choice==2||choice==3) //這段的演算法和上面同樣問題
{
if(p24==p23)
{
p24->sum=p23->income-p23->spend;
}
else
{
while(p24->next!=p23)
{
p24=p24->next;
}
Correct(p24);
}
}
//}
}
else
{
printf("查無此人!");
}
}
int main()
{
struct message *head=NULL,*p;
head = Findfile(head);
for(p = head->next;p!= NULL; p=p->next) //這里加兩句,輸出看看檔案讀入的情況,除錯好可以洗掉
printf("%s %s %f %f %s %f\n",p->name,p->use,p->income,p->spend,p->time,p->sum);
int power=1;
if(power)
{
Change(head);
}
system("pause");
return 0;
}
//下面是試運行的結果:
//1111 gz 300.000000 100.000000 20210316 200.000000
//1122 gz 300.000000 100.000000 20210316 200.000000
//1133 gz 300.000000 100.000000 20210316 200.000000
//1144 gz 300.000000 100.000000 20210316 200.000000
//請輸入要修改的人的名字:1111
//[1] 1111 300.00 100.00 200.00 gz 20210316
//選擇要修改的賬戶資訊的序號:1
//選擇要修改的賬戶資訊的專案序號(1:姓名,2:收入金額,3:支出金額,4:用途,5:使用時間):1
//請輸入更正后的姓名:2211
//修改成功請按任意鍵繼續. . . 這里運行時雖然顯示修改成功,但再次讀入時沒有變化
uj5u.com熱心網友回復:
修正5#樓的錯誤,change()函式還是實作修改功能的,我忘了修改后是沒保存的
,見代碼:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct message
{
char name[10];//用戶
char use[50];//用途
double income;//收入
double spend;//支出
char time[10];//使用時間
double sum;//余額
struct message *next;
};
struct message *Findfile(struct message *head1)//將檔案資訊存到鏈表中
{
struct message *head;
FILE *fp1;
struct message *p1,*p2;
if((fp1=fopen("money.in","r"))==NULL){
printf("cannot open file\n");
return 0;
}
if (head1 == NULL){
head1=(struct message* )malloc(sizeof(struct message));//生成頭節點
head = head1;
head1->next=NULL;
}else{
for(head=head1;head->next!=NULL;head=head->next);//找到節點尾部
}
p2 = head;
while(1){
p1=(struct message* )malloc(sizeof(struct message));
if(fscanf(fp1,"%s %s %lf %lf %s %lf\n",p1->name,p1->use,&p1->income,
&p1->spend,p1->time,&p1->sum)!=6)break;
p1->next=NULL; //從檔案里讀入資訊的各引數順序和結構體里定義的順序一樣
if(head==NULL) head->next=p1; //檔案保存時也必須是按此順序排列。
else p2->next=p1;
p2=p1;
}
fclose(fp1);
return head1;
}
void Correct(struct message *head3)
{
struct message *p31=head3,*p32=p31->next;
while(p31&&p32)
{
p32->sum=(p31->sum)+(p32->income)-(p32->spend);
p31=p31->next;
p32=p31->next;
}
}
void Change(struct message *head2) //修改
{
int k=0,choice;
struct message *p21,*p22=head2,*p23,*p24=head2;
p21=head2;
char name[10];
printf("請輸入要修改的人的名字:");
scanf("%s",name);
while(p21)
{
if(strcmp(p21->name,name)==0)
{
k++;
printf("[%d] %s %.2f %.2f %.2f %s %s\n",k,p21->name,
p21->income,p21->spend,p21->sum,p21->use,p21->time);
}
p21=p21->next;
}
if(k!=0)
{
printf("選擇要修改的賬戶資訊的序號:");
scanf("%d",&k);
while(k)
{
if(strcmp(p22->name,name)==0)
{
k--;
p23=p22;
p22=p22->next;
}
else
{
p23=p22;
p22=p22->next;
}
}
printf("選擇要修改的賬戶資訊的專案序號(1:姓名,2:收入金額,3:支出金額,4:用途,5:使用時間):");
scanf("%d",&choice);
//while(choice) //這里的while造成死回圈,這里是想實作多專案多次的修改嗎 ?
//{
switch(choice)
{
case 1:
{
printf("請輸入更正后的姓名:");
scanf("%s",p23->name);
printf("修改成功\n");
break;
}
case 2:
{
printf("請輸入更正后的收入金額:");
scanf("%lf",&p23->income);
printf("修改成功\n");
break;
}
case 3:
{
printf("請輸入更正后的支出金額:");
scanf("%lf",&p23->spend);
printf("修改成功\n");
break;
}
case 4:
{
printf("請輸入更正后的用途:");
scanf("%s",p23->use);
printf("修改成功\n");
break;
}
case 5:
{
printf("請輸入更正后的使用時間:");
scanf("%s",p23->time);
printf("修改成功\n");
break;
}
case 0:break;
default:
printf("請輸入正確的序號:");
break;
}
if(choice==2||choice==3)
{
if(p24==p23)
{
p24->sum=p23->income-p23->spend;
}
else
{
while(p24->next!=p23)
{
p24=p24->next;
}
Correct(p24);
}
}
//}
}
else
{
printf("查無此人!");
}
}
int main()
{
struct message *head=NULL,*p;
head = Findfile(head);
for(p = head->next;p!= NULL; p=p->next) //這里加兩句,輸出看看檔案讀入的情況,除錯好可以洗掉
printf("%s %s %f %f %s %f\n",p->name,p->use,p->income,p->spend,p->time,p->sum);
int power=1;
if(power)
{
Change(head);
}
for(p = head->next;p!= NULL; p=p->next)
printf("%s %s %f %f %s %f\n",p->name,p->use,p->income,p->spend,p->time,p->sum);
system("pause");
return 0;
}
//1111 gz 300.000000 100.000000 20210316 200.000000
//1122 gz 300.000000 100.000000 20210316 200.000000
//1133 gz 300.000000 100.000000 20210316 200.000000
//1144 gz 300.000000 100.000000 20210316 200.000000
//請輸入要修改的人的名字:1111
//[1] 1111 300.00 100.00 200.00 gz 20210316
//選擇要修改的賬戶資訊的序號:1
//選擇要修改的賬戶資訊的專案序號(1:姓名,2:收入金額,3:支出金額,4:用途,5:使用時間):1
//請輸入更正后的姓名:5555
//修改成功
//5555 gz 300.000000 100.000000 20210316 200.000000
//1122 gz 300.000000 100.000000 20210316 200.000000
//1133 gz 300.000000 100.000000 20210316 200.000000
//1144 gz 300.000000 100.000000 20210316 200.000000
//請按任意鍵繼續. . .
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/268520.html
標籤:C語言
上一篇:Qt表示檔案路徑的問題
