c语言做一个小项目(或称大作业),解决一个实际问题。
发布网友
发布时间:2022-04-27 05:54
我来回答
共2个回答
热心网友
时间:2022-06-27 05:34
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void xieruwenjian(struct txgl stu[],int count,char wenjian[]);
void huanyuan(struct txgl stu[],char wenjian[]);
void txxxlr(struct txgl stu[],int &count);
void xianshi(struct txgl stu[],int count);
void chaxun(struct txgl stu[],int count);
struct txgl
{
char xinming[10];
char di[30];
int dianhua;
int youbian;
};
void main()
{
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\t ----------------------------------------------------- \n");
printf("\t ----------------------------------------------------- \n");
printf("\n");
printf("\t \n");
printf("\t \n");
printf("\t 欢迎进入通讯管理系统 \n");
printf("\t \n");
printf("\t \n");
printf("\t ----------------------------------------------------- \n");
printf("\t ----------------------------------------------------- \n");
printf("\n");
printf("\n");
printf("\n");
system("PAUSE"); //清屏操作
system("CLS");
int a; //选择使用
int count=0; //学生的个数
char wenjian[]={"stu.txt"};
struct txgl stu[100];
//huanyuan(stu,wenjian);
while(1)
{
printf("1 通讯的信息录入\n");
printf("2 写入文件\n");
printf("3 显示所有信息\n");
printf("4 查询\n");
printf("0 退出\n");
printf("请输入你的选择:");
scanf("%d",&a);
switch(a)
{
case 1:txxxlr(stu,count);break;
case 2:xieruwenjian(stu,count,wenjian);break;
case 3:xianshi(stu,count);break;
case 4:chaxun(stu,count);break;
case 0: exit(0);
default:{
printf("\n你输入的选项有误!请按照提示输入\n");
printf("\n---------------------------------------------------\n");
printf("\n请选择....\n");
printf("\n---------------------------------------------------\n");
}
}
}
}
void xieruwenjian(struct txgl stu[],int count,char wenjian[])
{
FILE *fp;
if(count<=0)
{
printf("\n没有任何信息无法写入文件\n");
printf("\n---------------------------------------------------\n");
printf("\n请选择....\n");
printf("\n---------------------------------------------------\n");
return;
}
fp=fopen(wenjian,"w");
for(int i=0;i<count;i++)
{
fprintf(fp,"%s","姓名:");
fprintf(fp,"%s",stu[i].xinming);
fprintf(fp,"%s","地址:");
fprintf(fp,"%s",stu[i].di);
fprintf(fp,"%s","电话:");
fprintf(fp,"%d",stu[i].dianhua);
fprintf(fp,"%s","邮编:");
fprintf(fp,"%d",stu[i].youbian);
fprintf(fp,"\n");
}
fclose(fp);
printf("\n文件写入成功\n");
printf("\n---------------------------------------------------\n");
printf("\n请选择....\n");
printf("\n---------------------------------------------------\n");
}
void huanyuan(struct txgl stu[],char wenjian[])
{
int i=0;
FILE *fp;
fp=fopen(wenjian,"r");
if(fp==NULL)
printf("没有任何信息读取\n");
else
{
while(!feof(fp)) //feof是文件结束标志的文件,feof(fp)有两个返回值:如果遇到文件结束,函数feof(fp)的值为1,否则为0
{
//fscanf(fp,"%s","姓名:");
fscanf(fp,"%s",stu[i].xinming);
printf("%s\n",stu[i].xinming);
//fscanf(fp,"%s","地址:");
fscanf(fp,"%s",stu[i].di);
printf("%s\n",stu[i].di);
//fscanf(fp,"%s","电话:");
fscanf(fp,"%d",&stu[i].dianhua);
printf("%d\n",stu[i].dianhua);
//fscanf(fp,"%s","邮编:");
fscanf(fp,"%d",&stu[i].youbian);
printf("%d\n",stu[i].youbian);
i++;
//fprintf(fp,"\n");
printf("文件读出成功\n");
}
}
fclose(fp);
}
void txxxlr(struct txgl stu[],int &count)
{
char a;//选择退出使用
printf("\n录入的通讯信息,包括,姓名,地址,电话,邮编\n");
do{
printf("\n请输入%d个人的姓名:",count+1);
scanf("%s",stu[count].xinming);
printf("\n请输入%d个人的地址:",count+1);
scanf("%s",stu[count].di);
printf("\n请输入%d个人的电话:",count+1);
scanf("%d",&stu[count].dianhua);
printf("\n请输入%d个人的邮编:",count+1);
scanf("%d",&stu[count].youbian);
count++;
printf("\n请问你还要输入吗?按y确认,按n取消\n");
scanf("%s",&a);
}while(a=='y');
printf("\n---------------------------------------------------\n");
printf("\n请选择....\n");
printf("\n---------------------------------------------------\n");
}
void xianshi(struct txgl stu[],int count)
{
int i;
if(count<=0)
{
printf("没有任何数据不能显示\n");
printf("\n---------------------------------------------------\n");
printf("\n请选择....\n");
printf("\n---------------------------------------------------\n");
return;
}
for(i=0;i<count;i++)
{
printf("\n\n\n");
printf("第%d个人的资料\n",i);
printf("姓名:");
printf("%s\n",stu[i].xinming);
printf("地址:");
printf("%s\n",stu[i].di);
printf("电话:");
printf("%d\n",stu[i].dianhua);
printf("邮编:");
printf("%d\n",stu[i].youbian);
printf("\n");
}
printf("\n---------------------------------------------------\n");
printf("\n请选择....\n");
printf("\n---------------------------------------------------\n");
}
void chaxun(struct txgl stu[],int count)
{
char xm[10];
int i;
if(count<=0)
{
printf("没有任何信息不能实现查询\n");
printf("\n---------------------------------------------------\n");
printf("\n请选择....\n");
printf("\n---------------------------------------------------\n");
return;
}
printf("请按姓名查询,请输入姓名:\n");
scanf("%s",xm);
for(i=0;i<count;i++)
{
if(strcmp(xm,stu[i].xinming)==NULL) break;
}
printf("\n查询到%s的信息如下:\n",xm);
printf("姓名:%s\n",stu[i].xinming);
printf("地址:%s\n",stu[i].di);
printf("电话:%d\n",stu[i].dianhua);
printf("邮编:%d\n",stu[i].youbian);
printf("\n---------------------------------------------------\n");
printf("\n请选择....\n");
printf("\n---------------------------------------------------\n");
}
热心网友
时间:2022-06-27 05:34
//实际问题如计算一个数的阶乘
/*
要求用到:盘文件输入输出、数组、循环、选择、除主函数外至少要用到3个自定义函数。
要有项目简介、运行示例和程序清单,程序要有适当的注释
*/
//计算阶乘的函数
//参数 Num : 待计算阶乘的数字
//返回 : 此数字计算的结果
int JsJC(int Num)
{
int returnValue = Num;
while ( Num > 1 )//循环
{
returnValue *= (--Num);
}
return returnValue;
}
int main(void)
{
int Number = 0;
printf("输入一个整数,将计算其阶乘 = ");
scanf("%d",&Number);
//0的阶乘为1,就不用计算直接返回了
if ( Number == 0 )//选择
{
printf(" %d ! = 1\n",Number);
}
else
{
//调用计算阶乘的函数JsJC()来计算Number的阶乘
printf(" %d ! = %d\n",Number,JsJC(Number));
}
fflush(stdin);//让屏幕暂停
getchar();//让屏幕暂停
return 0;
}