
一、题目:家庭财务管理小程序
2.程序源代码:
/*money management system*/
#include "stdio.h"
#include "dos.h"
main()
{
FILE *fp
struct date d
float sum,chm=0.0
int len,i,j=0
int c
char ch[4]="",ch1[16]="",chtime[12]="",chshop[16],chmoney[8]
pp: clrscr()
sum=0.0
gotoxy(1,1)printf("|----------------------------------------------------|")
gotoxy(1,2)printf("| money management system(C1.0) 2000.03 |")
gotoxy(1,3)printf("|----------------------------------------------------|")
gotoxy(1,4)printf("| -- money records -- | -- today cost list -- |")
gotoxy(1,5)printf("销孝卜| ------------------------ |-----------------------------|")
gotoxy(1,6)printf("| date: -------------- | |")
gotoxy(1,7)printf("| | | | |")
gotoxy(1,8)printf("| -------------- | |")
gotoxy(1,9)printf("| thgs: ------------------ | |")
gotoxy(1,10)printf("| | | | |")
gotoxy(1,11)printf("| ------------------ | |")
gotoxy(1,12)printf("| cost: ---------- | |")
gotoxy(1,13)printf("| | | | |")
gotoxy(1,14)printf("慎局| ---------- | |")
gotoxy(1,15)printf("| | |")
gotoxy(1,16)printf("| | |")
gotoxy(1,17)printf("| | |")
gotoxy(1,18)printf("| | |")
gotoxy(1,19)printf("| | |")
gotoxy(1,20)printf("| | |")
gotoxy(1,21)printf("| | |")
gotoxy(1,22)printf("| | |")
gotoxy(1,23)printf("|--------------------------------------------------|")
i=0
getdate(&d)
sprintf(chtime,"%4d.%02d.%02d",d.da_year,d.da_mon,d.da_day)
for()
{
gotoxy(3,24)printf(" Tab __browse cost list Esc __quit")
gotoxy(13,10)printf(" ")
gotoxy(13,13)printf(" ")
gotoxy(13,7)printf("%s",chtime)
j=18
ch[0 ]=getch()
if(ch[0]==27)
break
strcpy (chshop,"")
strcpy(chmoney,"")
if(ch[0]==9)
{
mm:i=0
fp=fopen("home.dat","r+")
gotoxy(3,24)printf(" ")
gotoxy(6,4)printf(" list records ")
gotoxy(1,5)printf("|-------------------------------------|")
gotoxy(41,4)printf(" ")
gotoxy(41,5)printf(" |")
while(fscanf(fp,"%10s%14s%f\n",chtime,chshop,&chm)!=EOF)
{
if(i==36)
{
getch()
i=0
}
if ((i%36)<17)
{
gotoxy(4,6+i)
printf(" ")
gotoxy(4,6+i)
}
else
if((i%36)>16)
{
gotoxy(41,4+i-17)
printf(" ")
gotoxy(42,4+i-17)
}
i++
sum=sum+chm
printf("%10s %-14s %6.1f\n",chtime,chshop,chm)}
gotoxy(1,23)printf("|----------------------------------------------|")
gotoxy(1,24)printf("| |")
gotoxy(1,25)printf("|----------------------------------------------|")
gotoxy(10,24)printf("total is %8.1f$",sum)
fclose(fp)
gotoxy(49,24)printf("press any key to.....")getch()goto pp
}
else
{
while(ch[0]!='\r')
{
if(j<10)
{
strncat(chtime,ch,1)
j++
}
if(ch[0]==8)
{
len=strlen(chtime)-1
if(j>15)
{
len=len+1
j=11
}
strcpy(ch1,"")
j=j-2
strncat(ch1,chtime,len)
strcpy(chtime,"")
strncat(chtime,ch1,len-1)
gotoxy(13,7)printf(" ")
}
gotoxy(13,7)printf("%s",chtime)ch[0]=getch()
if(ch[0]==9)
goto mm
if(ch[0]==27)
exit(1)
}
gotoxy(3,24)printf(" ")
gotoxy(13,10)
j=0
ch[0]=getch()
while(ch[0]!='\r')
{
if (j<14)
{
strncat(chshop,ch,1)
j++
}
if(ch[0]==8)
{
len=strlen(chshop)-1
strcpy(ch1,"")
j=j-2
strncat(ch1,chshop,len)
strcpy(chshop,"")
strncat(chshop,ch1,len-1)
gotoxy(13,10)printf(" ")
}
gotoxy(13,10)printf("%s",chshop)ch[0]=getch()}
gotoxy(13,13)
j=0
ch[0]=getch()
while(ch[0]!='\r')
{
if (j<6)
{
strncat(chmoney,ch,1)
j++
}
if(ch[0]==8)
{
len=strlen(chmoney)-1
strcpy(ch1,"")
j=j-2
strncat(ch1,chmoney,len)
strcpy(chmoney,"")
strncat(chmoney,ch1,len-1)
gotoxy(13,13)printf(" ")
}
gotoxy(13,13)printf("%s",chmoney)ch[0]=getch()
}
if((strlen(chshop)==0)||(strlen(chmoney)==0))
continue
if((fp=fopen("home.dat","a+"))!=NULL)
fprintf(fp,"%10s%14s%6s",chtime,chshop,chmoney)
fputc('\n',fp)
fclose(fp)
i++
gotoxy(41,5+i)
printf("%10s %-14s %-6s",chtime,chshop,chmoney)
}
}
}
二、万年历
#include"iostream.h"
#include"iomanip.h"
#include"stdlib.h"
leapyear(int a)/*是否是闰年*/
{
if((a%4==0&&a%100!=0)||(a%100==0&&a%400==0))
return (1)
else return (0)
}
int aa[12]={31,28,31,30,31,30,31,31,30,31,30,31}
int cc[12]={31,29,31,30,31,30,31,31,30,31,30,31}
char bb[8][8]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}
ww(int a,int b,int c)
{
int n=0,m=0,i,j,k=0
for(i=1i<ci++)
{
if(leapyear(i)==1)
m=m+366
else m=m+365
}
for(j=1j<bj++)
{if(leapyear(c)==1) k=k+cc[j-1]
else k=k+aa[j-1]
}
n=(m+k+a)%7
return n
}
void yuefen(int m)
{
cout<<"最大天数是:"
if(m==2)
cout<<aa[m-1]+1
else cout<<aa[m-1]<<endl
}
void nianfen(int n)/*打印年份的日历*/
{
int i,j,k
if(leapyear(n)==1)
{
for(j=1j<=12j++)
{
cout<<j
cout<<"月份"<<endl
cout<<" 日"<<" 一"<<" 二"<<" 三"<<" 四"<<" 五"<<" 六"<<endl
for(i=0i<ww(1,j,n)i++)
{
cout<<setw(4)<<""
}
for(k=1k<=cc[j-1]k++)
{
cout<<setw(4)<<k
if((ww(1,j,n)+k)%7==0)
cout<<endl
}
cout<<endl
}
}
else
{
for(j=1j<=12j++)
{
cout<<j
cout<<"月份"<<endl
cout<<ww(1,j,n)<<endl
cout<<endl<<" 日"<<" 一"<<" 二"<<" 三"<<" 四"<<" 五"<<" 六"<<endl
for(i=0i<ww(1,j,n)i++)
{
cout<<setw(4)<<""
}
for(k=1k<=aa[j-1]k++)
{
cout<<setw(4)<<k
if((ww(1,j,n)+k)%7==0)
cout<<endl
}
cout<<endl
}
}
}
void nianyue(int n,int y)/*打印某年某月的月历*/
{
int i,k
if(leapyear(n)==1)
{
cout<<" 日"<<" 一"<<" 二"<<" 三"<<" 四"<<" 五"<<" 六"<<endl
for(i=0i<ww(1,y,n)i++)
{
cout<<setw(4)<<""
}
for(k=1k<=cc[y-1]k++)
{
cout<<setw(4)<<k
if((ww(1,y,n)+k)%7==0)
cout<<endl
}
cout<<endl
}
else
{ cout<<endl<<" 日"<<" 一"<<" 二"<<" 三"<<" 四"<<" 五"<<" 六"<<endl
for(i=0i<ww(1,y,n)i++)
{
cout<<setw(4)<<""
}
for(k=1k<=aa[y-1]k++)
{
cout<<setw(4)<<k
if((ww(1,y,n)+k)%7==0)
cout<<endl
}
cout<<endl
}
}
void main()
{
int n,m
int day,month ,year
while(1)
{
cout<<"***********************************:"<<endl
cout<<"1查询某年某月某日是星期几"<<endl
<<"2是否为闰年"<<endl
<<"3查询某月的最大天数"<<endl
<<"4打印某年的全年日历"<<endl
<<"5打印某年某月的月历"<<endl
<<"6exit"<<endl
<<"***********************************:"<<endl
cout<<"请输入要 *** 作的指令:"<<endl
cin>>n
switch(n)
{
case 1:cout<<"请输入要查询的日期:"
cout<<"year:"
cin>>year
cout<<endl<<"month:"
cin>>month
cout<<endl<<"day:"
cin>>day
cout<<"星期是:"
cout<<bb[ww(day,month,year)]<<endlbreak
switch(ww(day,month,year))
{
case 1:cout<<"星期一"break
case 2:cout<<"星期二"break
case 3:cout<<"星期三"break
case 4:cout<<"星期四"break
case 5:cout<<"星期五"break
case 6:cout<<"星期六"break
case 7:cout<<"星期日"break
}
cout<<endlbreak
case 2:cout<<"请输入要查询的年份:"
cin>>m
if(leapyear(m)==1) cout<<"是闰年"<<endl
else cout<<"不是闰年,请返回重新输入"<<endl
break
case 3:cout<<"请输入月份:"
cin>>m
yuefen(m)break
case 4:cout<<"请输入所要打印年份:"
cin>>m
nianfen(m)break
case 5:cout<<"请输入年份:"
cin>>n
cout<<endl<<"请输入月份:"
cin>>m
cout<<endl
nianyue(n,m)break
case 6:exit(0)
}
}
}
//=================================[说明]=======================================*///学生成绩管理
//文件名:橘链main.cpp
//------!!!!!!---------BOF-[程序代码开始]-------------------
#include<iostream>
#include<string>
using namespace std
//=============<开始定义结构体>===================================================
struct combox
{
int num
int mark
string name
combox *next
}
//=============<结束定义结构体>===================================================
//=============<开始定义Commonbox类>==============================================
//-----类体开始------------------------
class Commonbox
{
private:
combox *head
void Swap(combox *,combox *)//交换两个combox变量的数据域
void Print(combox *)//输出一combox指定的记录
combox *Find(int)//查找条例条件的记录,并返回该记录的指针宽伍虚
public:
Commonbox()
{
head=NULL
}
int ListCount()//统计当前链表的记录总数,返回一个整数
void AddItem(int num, string name, int mark)//添加一条记录到表尾
void RemoveItem(int)//删除一条指定的记录
void List()//列出当前链表中的所有记录
void Sort()//对当前链表进行排序
void Search(int)//在当前链表查找指定记录并输出
float Average()//计算平均成绩
}
//-----类体结束------------------------
//-----类成员函数开始----------------------------------
int Commonbox::ListCount() //统计当前链表的慎燃记录总数,返回一个整数
{
if (! head)return 0
combox *p=head
int n=0
while (p)
{
n++
p=p->next
}
return n
}
void Commonbox::AddItem(int num, string name, int mark) //添加一条记录到表尾
{
if (! head)
{
head=new combox
head->mark=mark
head->num=num
head->name=name
head->next=NULL
return
}
combox *t=head
while (t &&t->num!=num)
t=t->next
if (t)
{
cout<<" *** 作失败:学号为"<<num<<"的记录已经存在!"<<endl
return
}
combox *p=head
while (p->next)p=p->next
combox *p1=new combox
p1->num=num
p1->mark=mark
p1->name=name
p1->next=NULL
p->next=p1
return
}
void Commonbox::RemoveItem(int num) //删除一条指定的记录
{
combox *t=Find(num)
if (! t)return
combox *p=head
//如果要删除的记录位于表头
if (head==t)
{
head=head->next
delete p
cout <<"成功删除学号为 "<<num<<" 的记录!"<<endl<<endl
return
}
while (p->next!=t)p=p->next
combox *p1=p->next
p->next=p1->next
delete p1
cout <<"成功删除学号为 "<<num<<" 的记录!"<<endl<<endl
return
}
void Commonbox::Print(combox *p) //输出一combox指定的记录
{
cout<<p->num<<"\t\t"
cout<<p->name<<"\t\t"
cout<<p->mark<<endl
return
}
void Commonbox::List() //列出当前链表中的所有记录
{
if (ListCount()==0)
{
cout <<"错误:当前的列表为空!"<<endl
return
}
combox *p=head
cout<<"共有记录:"<<ListCount()<<endl
cout<<"学号\t\t姓名\t\t分数"<<endl
while (p)
{
Print(p)
p=p->next
}
cout <<endl
return
}
void Commonbox::Search(int num) //在当前链表查找指定记录并输出
{
cout <<"Searching...."<<endl
combox *p=Find(num)
if (p)
{
cout<<"学号\t\t姓名\t\t分数"<<endl
Print(p)
}
cout <<endl
}
combox *Commonbox::Find(int num)
{
if (ListCount()==0)
{
cout <<"错误:当前的列表为空!"<<endl
return NULL
}
combox *p=head
while (p)
{
if (p->num==num)break
p=p->next
}
if (! p)
{
cout <<"错误:找不到该记录!\n"
return NULL
}
return p
}
void Commonbox::Swap(combox *p1, combox *p2) //交换两个combox变量的数据域
{
combox *temp=new combox
temp->num=p1->num
temp->mark=p1->mark
temp->name=p1->name
p1->num=p2->num
p1->mark=p2->mark
p1->name=p2->name
p2->num=temp->num
p2->mark=temp->mark
p2->name=temp->name
}
void Commonbox::Sort() //对当前链表进行排序
{
cout <<"Sorting..."<<endl
if (ListCount()<2) return
combox *temp=NULL,*p=NULL,*p1=NULL,*p2=NULL,*k=NULL
int n=ListCount(),i,j
p=head
for (i=1i<ni++)
{
k=p
p1=p->next
for (j=0j<n-ij++)
{
if (k->num >p1->num)
{
k=p1
}
p1=p1->next
}
if (p!=k)Swap(k,p)
p=p->next
}
cout <<"Complete successfully!"<<endl<<endl
return
}
float Commonbox::Average() //计算平均成绩
{
if (ListCount()==0)
{
cout <<"错误:当前的列表为空!"<<endl
return -1
}
int sum=0,n=0
combox *p=head
while (p)
{
sum += p->mark
p=p->next
n++
}
return float(sum)/n
}
//-----类成员函数结束----------------------------------
//=============<结束定义Commonbox类>==============================================
Commonbox student//定义全局变量
int Menu()
{
cout <<"===========[主选单:]==========="<<endl
int n=1,select=-1
cout <<n++<<".输入学生成绩"<<endl<<endl
cout <<n++<<".按学号排序"<<endl<<endl
cout <<n++<<".按学号查找记录"<<endl<<endl
cout <<n++<<".删除由学号指定的记录"<<endl<<endl
cout <<n++<<".列出所有记录"<<endl<<endl
cout <<n++<<".计算平均成绩"<<endl<<endl
cout <<"0.退出"<<endl<<endl
cout <<"[请选择(输入相应数字)]:"
cin >>select
return select
}
char Exit() //返回一个字符患,用于确认退出
{
char s
cout<<"确定要退出程序吗?[Y/N]:"
cin >>s
return s
}
void Input(int *num, string *name, int *mark) //输入学生信息
{
cout <<"请输入 学号 姓名 分数:"
cin >>*num
if (*num==-1)return
cin >>*name>>*mark
return
}
void AddNew() //增加记录
{
int num=0,mark=0
string name=""
cout<<endl<<"当输入的学号为-1时表示结束输入."<<endl
Input(&num, &name, &mark)
while (num!=-1)
{
student.AddItem(num,name,mark)
Input(&num, &name, &mark)
}
return
}
void DoFind() //按学号查找
{
int num
cout<<endl<<"当输入的学号为-1时表示结束输入."<<endl
do
{
cout <<"请输入要查找的学生的学号: "
cin>>num
if (num==-1)continue
student.Search(num)
}
while (num!=-1)
return
}
void DoDelete() //删除记录
{
cout<<endl<<"当输入的学号为-1时表示结束输入."<<endl
int num
do
{
cout <<"请输入要删除的学生的学号:"
cin>>num
if (num==-1)continue
student.RemoveItem(num)
}
while (num!=-1)
return
}
void ShowAverage() //输出平均数
{
float avr=student.Average()
if (avr>0)
{
cout<<"共有记录:\t"<<student.ListCount()<<endl<<endl
cout<<"平均成绩:\t"<<avr<<endl<<endl
}
return
}
//-------******<主函数开始>******-------
int main()
{
cout<<"Welcome!\n学生成绩管理系统\nVer 1.01\nBy FondBoy\n\n"
int select
char s
while (1)
{
select=Menu()
switch (select)
{
case 0: //退出程序
s=Exit()
if (s=='y' || s=='Y')return 0
break
case 1: //输入学生成绩
AddNew()
break
case 2: //按学号排序
student.Sort()
break
case 3: //按学号查找记录
DoFind()
break
case 4: //删除由学号指定的记录
DoDelete()
break
case 5: //列出所有记录
student.List()
break
case 6: //输出平均成绩
ShowAverage()
break
default:
cout<<"无效输入!"<<endl
}
}
return 0
}
//-------******<主函数结束>******-------
//------!!!!!!---------EOF-[程序代码结束]-------------------
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)