一道c语言 题目望大虾帮助
发布网友
发布时间:2022-04-27 12:58
我来回答
共2个回答
热心网友
时间:2023-09-21 10:53
我的课程设计和你的类似,但我用的是文本形式,没关系,你只需要修改存储类型就可以了.放心绝对可以运行!
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include<iostream.h>
#include <string.h>
#define PF printf
#define SIZE 20/*成员人数上限*/
#include <stdlib.h>
int count=0;/*用于存储已有的成员个数*/
int num=0;/*用于存储此时登陆成员的编号*/
struct family/*定义结构体*/
{ int user;/*账号*/
char mima[10];/*密码*/
char name[20];/*姓名*/
int income;/*收入*/
int payout;/*支出*/
int total;/*余额*/
}people[SIZE];
void menu()/*普通成员菜单*/
{
PF(" ******************************%s ,欢迎您登陆******************************\n",people[num].name);
PF(" 1. 创建新用户 \n");
PF(" 2. 查询本人收支情况 \n");
PF(" 3. 查询其他成员收支情况 \n");
PF(" 4. 查询收支排行 \n");
PF(" 5. 修改自己的信息 \n");
PF(" 6. 修改密码 \n");
PF(" 0. 返回登陆界面 \n");
}
void MAXmenu()/*最高特权者菜单*/
{
PF(" ******************************%s,欢迎您登陆******************************\n",people[num].name);
PF(" 1. 创建新用户 \n");
PF(" 2. 查询本人收支情况 \n");
PF(" 3. 查询其他成员收支情况 \n");
PF(" 4. 查询收支排行 \n");
PF(" 5. 修改自己的信息 \n");
PF(" 6. 修改密码 \n");
PF(" 7. 删除用户(只能删除成员) \n");
PF(" 0. 返回登陆界面 \n");
}
void openf()/*打开文件,并以结构体存在内存中*/
{ count=0;
int c;
FILE *fp;
fp=fopen("family_list5.txt","r");
PF("已存在账号 密码 姓名\n");
while( !feof(fp) )/*输出已存在账号和密码,姓名*/
{
fscanf(fp,"%d %s %s %d %d %d",&people[count].user,people[count].mima,people[count].name,&people[count].income,&people[count].payout,&people[count].total);
printf("%10d%15s%15s\n",people[count].user,people[count].mima,people[count].name);
count++;
}fclose(fp);
printf("count为:%d",count);/*用于测试count是否正确*/
}
void save()/*存储成文件*/
{
FILE *fp;
int i=0;
if((fp=fopen("family_list5.txt","w"))==NULL)
{
printf("不能打开文件\n");
return;
}
for(i=0;i<count-1;i++)
{ fprintf(fp,"%d %s %s %d %d %d\n",people[i].user,people[i].mima,people[i].name,people[i].income,people[i].payout,people[i].income-people[i].payout);}
fprintf(fp,"%d %s %s %d %d %d",people[i].user,people[i].mima,people[i].name,people[i].income,people[i].payout,people[i].income-people[i].payout);
/*由于循环最后有‘\n’不能将最后一行也置于循环中,否则文件将多一行乱码*/
fclose(fp);/*关闭文件*/
}
void newuser()/*创建新用户函数*/
{ FILE *fp;
int i=count; PF("请输入新用户信息:");
if((fp=fopen("family_list5.txt","a"))==NULL)/*以追加的形式打开文件*/
{
printf("不能打开文件\n");
return;
}
PF("请输入账号名:");
scanf("%d",&people[i].user);
PF("请输入密码:"); fflush(stdin);/*清除键盘缓冲区*/
scanf("%s",people[i].mima);
PF("请输入您的姓名:"); fflush(stdin);/*同上*/
scanf("%s",people[i].name);
PF("请输入您的月收入:"); fflush(stdin);
scanf("%d",&people[i].income);
PF("请输入您的月支出:"); fflush(stdin);
scanf("%d",&people[i].payout);
fprintf(fp,"\n%d %s %s %d %d %d",people[i].user,people[i].mima,people[i].name,people[i].income,people[i].payout,people[i].income-people[i].payout);
fclose(fp);
PF("恭喜您,创建成功!\n");
}
void user()/*输入账号函数*/
{
int i,b;
PF("请输入账号:");
scanf("%d",&b);
for(i=0;i<count;i++)
if(b==people[i].user) {num=i;}/*核心算法,顺序查找符合输入值的账号*/
}
int password()/*密码输入函数*/
{ char c;
char mima[10];PF("%s",people[num].mima);PF("请输入密码(以回车结束):");
loop: int i=0;
while((c=getch())!='\r') /*输入值只要不为回车则循环*/
{
if(c =='\b' && i==0)/*如果按BACKSPACE并且之前没有输入任何值*/
{
putchar(7);/*发出鸣叫*/
}
else if(c == '\b')/*如果按BACKSPACE*/
{ mima[--i]=c; PF("\b \b"); }//覆盖上一个‘*’
else if(i<10)//密码范围小于10则输出‘*’
{putchar('*');
mima[i]=c;
i++;
}
if(i>=10)
{PF("\n密码输入超出范围,请重新输入:");
goto loop;/*重新输入,返回此函数开头*/
}
} mima[i]='\0';/*定义密码结束标准*/
if(strcmp(mima,people[num].mima)==0) {return(1);}/*如果密码正确返回函数值1*/
else/*否则重新输入密码*/
{system("cls");/*清屏*/
PF("\n密码错误!请重新输入:");
goto loop;
return(0);
}
}
void PXTO()/*对余额排序*/
{
int n=count;/*n赋值为结构体个数*/
struct family *p;/*定义结构体指针*/
struct family t;/*定义另一个结构体,用于暂时存储数据,起到交换目的*/
openf();/*打开文件*/
system("cls");/*清屏*/
p=people;
int i=0,j,k;
while(i<n)/*当i<结构体个数时执行循环*/
{
for(j=i+1;j<n;j++)/*用选择法对余额排序*/
{
if(((p+j)->total)>((p+i)->total))
{t.user=(p+i)->user;strcpy(t.mima,(p+i)->mima);strcpy(t.name,(p+i)->name);t.income=(p+i)->income;t.payout=(p+i)->payout;t.total=(p+i)->total;
(p+i)->user=(p+j)->user;strcpy((p+i)->mima,(p+j)->mima);strcpy((p+i)->name,(p+j)->name);(p+i)->income=(p+j)->income;(p+i)->payout=(p+j)->payout;(p+i)->total=(p+j)->total;
(p+j)->user=t.user;strcpy((p+j)->mima,t.mima);strcpy((p+j)->name,t.name);(p+j)->income=t.income;(p+j)->payout=t.payout;(p+j)->total=t.total;
}
}
i++;
}
PF("******************************节余排行榜**********************************\n");
PF("名次 姓名 节余\n");
if(num==0||num==1)/*如果是最高特权者,则能看见全部信息*/
{for(k=0;k<n;k++)
PF("\n%5d%10s%15d\n",k+1,(p+k)->name,(p+k)->total);
}else/*如果不是最高特权者,只能看见成员信息*/
{
for(k=2;k<n;k++)/*最高特权者是0和1号分别是爸爸妈妈*/
PF("\n%5d%10s%15d\n",k-1,(p+k)->name,(p+k)->total);
}
}
void PXIN()/*对收入排序*/
{
int n=count;/*n赋值为结构体个数*/
struct family *p;/*定义结构体指针*/
struct family t;/*定义另一个结构体,用于暂时存储数据,起到交换目的*/
openf();/*打开文件*/
system("cls");/*清屏*/
p=people;int i=0,j,k;
while(i<n)/*当i<结构体个数时执行循环*/
{
for(j=i+1;j<n;j++)/*用选择法对收入排序*/
{
if(((p+j)->income)>((p+i)->income))
{t.user=(p+i)->user;strcpy(t.mima,(p+i)->mima);strcpy(t.name,(p+i)->name);t.income=(p+i)->income;t.payout=(p+i)->payout;t.total=(p+i)->total;
(p+i)->user=(p+j)->user;strcpy((p+i)->mima,(p+j)->mima);strcpy((p+i)->name,(p+j)->name);(p+i)->income=(p+j)->income;(p+i)->payout=(p+j)->payout;(p+i)->total=(p+j)->total;
(p+j)->user=t.user;strcpy((p+j)->mima,t.mima);strcpy((p+j)->name,t.name);(p+j)->income=t.income;(p+j)->payout=t.payout;(p+j)->total=t.total;
}
}
i++;
}
PF("******************************收入排行榜**********************************\n");
PF("名次 姓名 收入 \n");
if(num==0||num==1)/*如果是最高特权者,则能看见全部信息*/
{
for(k=0;k<n;k++)
PF("\n%5d%10s%15d\n",k+1,(p+k)->name,(p+k)->income,(p+k)->payout);
}
else
{for(k=2;k<n;k++)/*如果不是最高特权者,只能看见成员信息*/
PF("\n%5d%10s%15d\n",k-1,(p+k)->name,(p+k)->income,(p+k)->payout);
}/*最高特权者是0和1号分别是爸爸妈妈*/
}
void PXPAY()/*对支出排序*/
{
int n=count;/*一下同理*/
struct family *p;
struct family t;
openf();
system("cls");
p=people;int i=0,j,k;
while(i<n)
{
for(j=i+1;j<n;j++)
{
if(((p+j)->payout)>((p+i)->payout))
{t.user=(p+i)->user;strcpy(t.mima,(p+i)->mima);strcpy(t.name,(p+i)->name);t.income=(p+i)->income;t.payout=(p+i)->payout;t.total=(p+i)->total;
(p+i)->user=(p+j)->user;strcpy((p+i)->mima,(p+j)->mima);strcpy((p+i)->name,(p+j)->name);(p+i)->income=(p+j)->income;(p+i)->payout=(p+j)->payout;(p+i)->total=(p+j)->total;
(p+j)->user=t.user;strcpy((p+j)->mima,t.mima);strcpy((p+j)->name,t.name);(p+j)->income=t.income;(p+j)->payout=t.payout;(p+j)->total=t.total;
}
}
i++;
}
PF("******************************支出排行榜**********************************\n");
PF(" 名次 姓名 支出\n");
if(num==0||num==1)
{for(k=0;k<n;k++)
PF("\n%5d%10s%15d\n",k+1,(p+k)->name,(p+k)->payout);
}
else
{for(k=2;k<n;k++)
PF("\n%5d%10s%15d\n",k-1,(p+k)->name,(p+k)->payout);
}
}
int returnm()/*返回主菜单函数*/
{
PF("按0返回主菜单");
char c;
c=getch();/*从键盘得到一个字符*/
if(c=='0') {return(1);}/*如果是0则返回1*/
else
return(0);/*否则返回0*/
}
void phmemu()/*排序菜单*/
{
char c;
loop:PF(" 1.查询收入排行 \n");
PF(" 2.查询支出排行 \n");
PF(" 3.查询余额排行 \n");
PF(" 0.返回上级菜单 \n");
c=getch();
switch(c)/*选择功能*/
{
case '1': system("cls");PXIN();if(returnm()==1){ system("cls");goto loop;}else break;
/*清屏,调用收入排序函数*/
case '2': system("cls");PXPAY();if(returnm()==1){ system("cls");goto loop;}else break;
/*清屏,调用支出排序函数*/
case '3': system("cls");PXTO();if(returnm()==1){ system("cls");goto loop;}else break;
/*清屏,调用余额排序函数*/
case '0': break;
default: break;
}
}
void look()/*查询其他成员的情况*/
{int k;
openf();/*打开文件*/
system("cls");
struct family *p;/*定义结构体指针方便打字*/
p=people;
PF("账号 姓名 收入 支出 节余\n");
if(num==0||num==1)/*如果是最高特权者则可以看见所以信息*/
{
for(k=0;k<count;k++)
PF("\n%5d%10s%15d%15d%15d\n",k+1,(p+k)->name,(p+k)->income,(p+k)->payout,(p+k)->total);
}
else/*如果是普通成员,只能看见普通成员的信息*/
{
for(k=2;k<count;k++)
PF("\n%5d%10s%15d%15d%15d\n",k-1,(p+k)->name,(p+k)->income,(p+k)->payout,(p+k)->total);
}
}
void lookmy()/*查询自己的信息*/
{system("cls");
PF("账号 姓名 收入 支出 节余\n");
PF("%5d%10s%15d%15d%15d\n",people[num].user,people[num].name,people[num].income,people[num].payout,people[num].total);
}
void changemima()/*修改密码*/
{
char xmima[10];char c;
//一下这段是调用变*用法
PF("请输入新密码:");
loop: int i=0;
while((c=getch())!='\r')
{
if(c =='\b' && i==0)
{
putchar(7);
}
else if(c == '\b')
{ xmima[--i]=c; PF("\b \b"); }
else if(i<10)
{putchar('*');
xmima[i]=c;
i++;
}
if(i>=10)
{PF("\n密码输入超出范围,请重新输入:");
goto loop;
}
} xmima[i]='\0';
//变*用法结束
strcpy(people[num].mima,xmima);/*将新密码赋值给内存中当前用户的密码*/
save();/*此函数需要修改*/
PF("\n密码修改成功!\n");
}
void changeinfromation()/*修改个人信息*/
{
lop:PF(" 1.修改收入 \n");
PF(" 2.修改支出 \n");
PF(" 3.修改姓名 \n");
PF(" 0.返回主菜单 \n");
int in,out;
char xname[20];
char c;
c=getch();
switch(c)
{
case'1':system("cls");PF(" 1.请输入收入(按回车键结束):");scanf("%d",&in);people[num].income=in;save();system("cls");goto lop;
case'2':system("cls");PF(" 1.请输入支出(按回车键结束):");scanf("%d",&out);people[num].payout=out;save();system("cls");goto lop;
case'3':system("cls");PF(" 1.请输入姓名(按回车键结束):");scanf("%s",xname);strcpy(people[num].name,xname);save();system("cls");goto lop;
/* case'0':returnm()*/
}
}
void deletezh()/*删除用户*/
{
int zh,i,j;FILE *fp;
PF("请输入需要删除的账户名:");
scanf("%d",&zh);
for(i=0;i<count;i++)
{ if(zh==people[i].user && i<count-1 && i>1)
{
fp=fopen("family_list5.txt","w");
for(j=0;j<i;j++)
{fprintf(fp,"%d %s %s %d %d %d\n",people[j].user,people[j].mima,people[j].name,people[j].income,people[j].payout,people[j].income-people[j].payout);}
for(j=j+1;j<count-1;j++)
{fprintf(fp,"%d %s %s %d %d %d\n",people[j].user,people[j].mima,people[j].name,people[j].income,people[j].payout,people[j].income-people[j].payout);}
fprintf(fp,"%d %s %s %d %d %d",people[j].user,people[j].mima,people[j].name,people[j].income,people[j].payout,people[j].income-people[j].payout);/*将最后一行单独赋值,以免多一个\n*/
}
else if(zh==people[i].user && i==count-1)
{
fp=fopen("family_list5.txt","w");
for(j=0;j<i-1;j++)
{fprintf(fp,"%d %s %s %d %d %d\n",people[j].user,people[j].mima,people[j].name,people[j].income,people[j].payout,people[j].income-people[j].payout);}
fprintf(fp,"%d %s %s %d %d %d",people[j].user,people[j].mima,people[j].name,people[j].income,people[j].payout,people[j].income-people[j].payout);/*将最后一行单独赋值,以免多一个\n*/
}else {PF("删除对象错误!不能删除特权用户!");goto lp;}
}PF("删除成功!");
lp: fclose(fp);
}
void main()
{ char c;
openf();
lop:user();
if(password()==1&&num>1)
{system("cls");menu();
loop:c=getch();
switch(c)
{
case '0':system("cls");goto lop;
case '1':system("cls");newuser();if(returnm()==1){ system("cls");menu();goto loop;}
case '2':lookmy();if(returnm()==1){ system("cls");menu();goto loop;}else break;
case '3':system("cls");look();if(returnm()==1){ system("cls");menu();goto loop;}else break;
case '4':openf();system("cls");phmemu(); system("cls");menu();goto loop;
case '5':system("cls");changeinfromation();system("cls");menu();goto loop;
case '6':system("cls");changemima();if(returnm()==1){ system("cls");menu();goto loop;}else break;
default: break;
}
}
else
{system("cls");MAXmenu();
lp:c=getch();
switch(c)
{
case '0':system("cls");goto lop;
case '1':system("cls");newuser();if(returnm()==1){ system("cls");menu();goto lp;}
case '2':lookmy();if(returnm()==1){ system("cls");menu();goto lp;}else break;
case '3':system("cls");look();if(returnm()==1){ system("cls");menu();goto lp;}else break;
case '4':openf();system("cls");phmemu(); system("cls");menu();goto lp;
case '5':system("cls");changeinfromation();system("cls");menu();goto lp;
case '6':system("cls");changemima();if(returnm()==1){ system("cls");menu();goto lp;}else break;
case '7':system("cls");MAXmenu();deletezh();
default: break;
}
}
}
热心网友
时间:2023-09-21 10:54
//vc通过..tc没试
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <conio.h>
typedef struct account{
char accountid[10];
char customerid[30];
char name[20];
char address[30];
float balance;
}c_account;
typedef struct Node
{
c_account* data;
struct Node *prev;
struct Node *next;
}Lnode;
enum{MAINMENU=0,SUBMENU=1};
typedef struct List
{
Lnode *HeadNode;
Lnode *LastNode;
int m_count;
}CList;
CList* InitList(void)
{
CList* t_list=(CList*)malloc(sizeof(CList));
Lnode* t_node=(Lnode*)malloc(sizeof(Lnode));
t_list->HeadNode=t_node;
t_list->LastNode=t_node;
t_list->m_count=0;
t_node->data=NULL;
t_node->prev=t_node;
t_node->next=t_node;
return t_list;
}
void AddAccount(CList* t_list,c_account* t_data)
{
Lnode* t_node=t_list->LastNode;
t_list->m_count=t_list->m_count+1;
t_node->data=t_data;
Lnode* newnode=(Lnode*)malloc(sizeof(Lnode));
newnode->data=NULL;
newnode->next=NULL;
newnode->prev=t_node;
t_node->next=newnode;
t_list->LastNode=newnode;
}
c_account* Search(CList* t_list,char t_accountid[])
{
if(t_list->m_count==0)
{
printf("无用户!\n");
}else
{
Lnode* t_node=t_list->HeadNode;
while(t_node!=NULL)
{
if(t_node->data!=NULL)
{
if(strcmp(t_node->data->accountid,t_accountid)==0)
return t_node->data;
}
t_node=t_node->next;
}
}
return NULL;
}
void DeleteAccount(CList* t_list,char t_accountid[])
{
if(t_list->m_count==0)
{
printf("无用户!\n");
}else
{
Lnode* t_node=t_list->HeadNode;
while(t_node!=NULL)
{
if(strcmp(t_node->data->accountid,t_accountid)==0)
{
t_node->prev->next=t_node->next;
t_node->next->prev=t_node->prev;
if(t_list->HeadNode==t_node)
t_list->HeadNode=t_node->next;
free(t_node->data);
free(t_node);
t_list->m_count=t_list->m_count-1;
return;
}
t_node=t_node->next;
}
}
}
void DestoryList(CList* t_list)
{
Lnode* t_node=t_list->HeadNode;
while(t_node!=NULL)
{
t_list->HeadNode=t_node->next;
if(t_node->data!=NULL)
free(t_node->data);
free(t_node);
t_node=t_list->HeadNode;
}
t_list->m_count=0;
t_list->HeadNode=NULL;
t_list->LastNode=NULL;
free(t_list);
}
c_account* CreateAccount()
{
system("cls");
c_account* t_account=(c_account*)malloc(sizeof(c_account));
printf("\t帐号(1~10): ");
scanf("%s",t_account->accountid);
printf("\t身份证号码(1~30): ");
scanf("%s",t_account->customerid);
printf("\t用户姓名(1~20): ");
scanf("%s",t_account->name);
printf("\t用户地址(1~30): ");
scanf("%s",t_account->address);
printf("\t帐户金额: ");
scanf("%f",&(t_account->balance));
return t_account;
}
void playAccount(c_account* t_account)
{
system("cls");
printf("\t帐号: %s\n",t_account->accountid);
printf("\t身份证号码: %s\n",t_account->customerid);
printf("\t用户姓名: %s\n",t_account->name);
printf("\t用户地址: %s\n",t_account->address);
printf("\t帐户金额: %f\n\t",t_account->balance);
}
void playmenu(int Select=MAINMENU)
{
system("cls");
if(Select==MAINMENU)
{
printf("\t主菜单\n");
printf("\t1.新用户\n");
printf("\t2.查询用户\n");
printf("\t3.修改用户\n");
printf("\t4.删除用户\n");
printf("\t0.退出\n");
printf("\t选择(0~4)");
}
if(Select==SUBMENU)
{
printf("\t修改选项\n");
printf("\t1.用户名\n");
printf("\t2.身份证号码\n");
printf("\t3.用户地址\n");
printf("\t4.帐户金额\n");
printf("\t0.返回\n");
printf("\t选择(0~4)");
}
}
bool ReadAccountInformation(CList* t_list)
{
FILE* f_file;
f_file=fopen("c:\\back.dat","ab+");
if(f_file==NULL)
{
printf("file read fail\n");
system("pause");
return false;
}
//采用feof将产生无效用户(文件结束符)
fseek(f_file,0,SEEK_END);
int file_length=ftell(f_file);
if(file_length>=sizeof(c_account))
{
fseek(f_file,0,SEEK_SET);
while(true)
{
c_account* t_account=(c_account*)malloc(sizeof(c_account));
fread(t_account,sizeof(c_account),1,f_file);
AddAccount(t_list,t_account);
if(file_length-ftell(f_file)<sizeof(c_account))
break;
}
}
fclose(f_file);
return true;
}
bool SaveAccountInformation(CList* t_list)
{
FILE* f_file;
f_file=fopen("c:\\back.dat","wb");
if(f_file==NULL)
{
printf("file write fail\n");
system("pause");
return false;
}
Lnode* t_node=t_list->HeadNode;
while(t_node!=NULL)
{
if(t_list->m_count!=0&&t_node->data!=NULL)
fwrite(t_node->data,sizeof(c_account),1,f_file);
t_node=t_node->next;
}
fclose(f_file);
return true;
}
void main()
{
CList* t_list=InitList();
if(!ReadAccountInformation(t_list))
{
DestoryList(t_list);
exit(0);
}
char ch;
while(true)
{
playmenu();
ch=getchar();
char t_accountid[10];
c_account* t_account=NULL;
switch(ch)
{
case '1':
AddAccount(t_list,CreateAccount());
break;
case '2':
system("cls");
printf("\t帐号(1~10): ");
scanf("%s",&t_accountid);
t_account=Search(t_list,t_accountid);
if(t_account!=NULL)
playAccount(t_account);
else
printf("\t无该用户.\n\t");
system("pause");
break;
case '3':
system("cls");
printf("\t帐号(1~10): ");
scanf("%s",&t_accountid);
t_account=Search(t_list,t_accountid);
if(t_account==NULL)
{
printf("\t无该用户.\n\t");
system("pause");
break;
}
while(true)
{
char ch1;
playmenu(SUBMENU);
ch1=getchar();
switch(ch1)
{
case '1':
printf("\t用户姓名(1~20): ");
scanf("%s",t_account->name);
break;
case '2':
printf("\t身份证号码(1~30): ");
scanf("%s",t_account->customerid);
break;
case '3':
printf("\t用户地址(1~30): ");
scanf("%s",t_account->address);
break;
case '4':
printf("\t帐户金额: ");
scanf("%f",&(t_account->balance));
break;
case '0':
break;
}
if(ch1=='0')
break;
}
break;
case '4':
system("cls");
printf("\t帐号(1~10): ");
scanf("%s",&t_accountid);
t_account=Search(t_list,t_accountid);
if(t_account==NULL)
{
printf("\t无该用户.\n\t");
system("pause");
break;
}
playAccount(t_account);
char ch2;
printf("确认删除(Y/N)?");
ch2=getch();
putchar(ch2);
getch();
if(ch2=='Y'||ch2=='y')
DeleteAccount(t_list,t_accountid);
break;
case '0':
SaveAccountInformation(t_list);
DestoryList(t_list);
exit(0);
}
}
}