课程设计

来源:互联网 发布:特朗普蔡英文通话 知乎 编辑:程序博客网 时间:2024/05/22 05:00

没错,还是课程设计,帮同学写的。。。。。

# include<stdio.h># include<string.h># include<stdlib.h>//====================全局变量=============================//===================1=============================int Arr[5] = {-1,-1,-1,-1,-1};//第一题要用到的全局变量,最大数字为65536,//所以数最多保存5位数字int reArr[5] = { -1, -1, -1, -1, -1 };//反转后的数组 int reNum = 0;//反转后的数组转化的数字 int iPali;//回文数//====================函数声明================================//=======================1================================void numToArr(int a);//将数字的每一位上的数字存入数组void reverseArr();//将数组Arr1中的内容反转后//赋值给Arr2void arrToNum();//将数组转化为数字int isPaliNum(int n, int *p);//判断是否是回文数//palindromic 回文//=======================2================================int isLeapYear(int year);int daysOfYear(int year);int daysOfMonth(int year, int month);int isFishing(int year, int month,int day);//=====================3=======================typedef struct _node{    char name[40];//姓名     char unit[60];//单位     char no[6];//房间号     struct _node *link;//指向下一节点的指针 }node;void create(node **head);void num(node*head);void delet(node **head, char *p);void print(node *head);int main(){    //================定义变量=================    int iSelect;    //===================1=================    int i;    //===================2=================    int year, month, day;    int i_2;    //===================3=================    struct node *head;//定义头指针     char str[40], *Pstr = str;//定义一个指针数组     printf("请选择要进行的项目,输入对应数字后回车\n");    printf("1:回文数\n2:打鱼还是晒网\n3:参会人员管理\n");    scanf("%d", &iSelect);    getchar();    switch (iSelect)    {    case 1:        printf("以下这些数的平方都是回文数:\n");        for (i = 0; i<4; i++)            printf("\t%d\n", i);        for (i = 10; i<256; i++)        {            if (1 == isPaliNum(i, &iPali))                printf("\t%d\n", iPali);        }        break;    case 2:        printf("某从1990年1月1日开始\"三天打鱼,两天晒网\",请你输入在这之后的某个日期:\n");        printf("年:");        scanf("%d", &year);        printf("月:");        scanf("%d", &month);        printf("日:");        scanf("%d", &day);        //注视部分做测试用的        /*for (i_2= 1; i_2 < 20; i_2++)        {            if (isFishing(year, month, i_2))                printf("这天该人是打鱼\n");            else printf("这天该人是晒网\n");        }*/        if (isFishing(year, month, day))            printf("这天该人是打鱼\n");        else printf("这天该人是晒网\n");        break;    case 3:        head = NULL;//建一个空表         create(&head);//调用creat函数         num(head);//调用num函数计算单位人数         print(head);//输出链表         printf("Please input delete no:");        gets(Pstr);//输入要删除的名字         delet(&head, Pstr);//调用删除函数删除要删除的人         print(head);//再次输出链表         break;    }    return 0;}//=============================函数定义=============================//=======================1================================void numToArr(int a)//将数字的每一位上的数字存入数组{    int i,j;    int k = 10000;//保存除数,刚开始为100000    i = 0; j = 0;    for (; i<5; )    {        //如果数组元素默认为0的话:        //比如数字14对应数组{1,4,0,0,0}        //反转就是{0,0.0,4,1}        //对应反过来的数字就是41        //但是如果换成140呢{1,4,0,0,0}    显然是错误的        //所以0有误导性,设为-1较好        if (a / k == 0&&i==0)//如果没有执行if后面的代码,说明数字比如4前几位都是0(即00004)        {            Arr[i + j] = -1;            j++;            k /= 10;            continue;            //这里循环了j次        }        else{            Arr[i+j] = a / k;            a = a - Arr[i+j] * k;            k /= 10;            i++;            //这里循环了i次            if ((i + j) == 5)                return;        }    }}void reverseArr(){    int i = 0;    for (; i < 5;i++)        reArr[i] = Arr[4 - i];}void arrToNum(){    int i;    int m = 10000;    int j=0;//标记-1的个数,为了将m设为合适的大小,比如,数字10,数组就是-1,-1,-1,1,0    //反转就是0,1,-1,-1,-1,所以-1有3个,m应除以103次,就是10,然后前面的数字一次乘以m,m/10...    reNum = 0;//没词都要清零,防止其他函数改变reNum干扰    for (i = 4; i >= 0; i--)//从后面开始比较好转换    {        if (reArr[i] == -1)        {            m /= 10;            j++;        }    }    for (i = 0; i < 5 - j; i++)    {        reNum += reArr[i] * m;        m /= 10;    }}int isPaliNum(int i, int *p)//判断是否是回文数{    int a = i*i;    numToArr(a);    reverseArr();    arrToNum();    if (a == reNum)    {        *p = i;        return 1;    }    else    {        *p = 0;        return 0;    }}//=======================2===============================int isLeapYear(int year)//是否是闰年{    if ((year % 100 == 0) && (year / 400 == 0))        return 1;    else return 0;}int daysOfYear(int year){    if (isLeapYear)        return 366;//是闰年一年就是366天    else return 365;//平年365天}int daysOfMonth(int year,int month){    if (isLeapYear(year) && month == 2)        return 29;    else if (month == 2)        return 28;    else     switch (month)    {    case 1:    case 3:    case 5:    case 7:    case 8:    case 10:    case 12:        return 31;    case 4:    case 6:    case 9:    case 11:        return 30;    }}int isFishing(int year, int month, int day){    int totalDays=0;//int足够了    int i = 0;    int totalOfMon = 0;//剩下的1月1日到month月day日的天数    //先加上1990年1月1号到year年1月1号的天数    for (i=1990; i <=year; i++)    {        if (year == 1990)            totalDays += 0;        else        totalDays += daysOfYear(year-1);//比如1990年就不加,1991年就        //加上1990年的天数    }    for (i = 1; i <= month; i++)    {        if (month == 1)            totalOfMon += 0;        else            totalOfMon += daysOfMonth(year,month-1);//比如1月到2月,天数就是1月的天数        //这里不用考虑12月到1月份的天数(就是12月的天数),    }    totalDays = totalDays + totalOfMon + day;    if ((totalDays % 5 == 4) || (totalDays%5== 0))        return 0;    else return 1;}//=======================3================================/*********创建链表*********/void create(node **head){    node *p1, *p2;    p1 = p2 = (node*)malloc(sizeof(node));//申请新结点     printf("Please input the name:");    gets(p1->name);//输入名字     printf("Please input the unit:");    gets(p1->unit);//输入单位     printf("Please input the no:");    gets(p1->no);//输入房间号     p1->link = NULL;//将新结点的指针赋为空     while (strlen(p1->name)>0)//当输入name为空时结束     {        if (*head == NULL)//接入表头             *head = p1;        else//接到表尾             p2->link = p1;        p2 = p1;        p1 = (node *)malloc(sizeof( node));//申请一个新结点         printf("Please input the name:");        gets(p1->name);        printf("Please input the unit:");        gets(p1->unit);        printf("Please input the no:");        gets(p1->no);        p1->link = NULL;    }}/******计算单位人员数******/void num(node *head){    node *temp;    char str_1[60];//定义一个字符数组     int n = 0, k = 0;    temp = head;    printf("Please input unit:");    gets(str_1);//输入单位名     while (temp!=NULL)//当单位是所要查询的单位时开始统计     {        k = strcmp(temp->unit, str_1);//判断是否为所需查询的单位         if (k == 0)            n++;        temp = temp->link;//指向下一个结点     }    printf("There are %d participants in the unit.\n", n);}/*********删除节点*********/void delet(node **head, char *Pstr){ node *p, *temp;    temp = *head;    if (*head == NULL)//如果原链表是空表,则输出表为空,结束         printf("\nList is null!\n");    else    {        temp = *head;        if (strcmp((*head)->no,Pstr)==0)//如果temp指向的是头结点,就把第二个结点的地址给head         {            *head = (*head)->link;            free(temp);        }        else         {while (strcmp(temp->link->no, Pstr) != 0)//当temp结点的下一结点不是要删除的结点         {            temp = temp->link;//temp后移一个结点         }        if (strcmp(temp->link->no, Pstr) == 0)//如果找到了要删除的结点的前一节点        {            p=temp->link;//p保存要删除的结点            temp->link=p->link;            printf("delete string:%s\n", p->no);            free(p);        }        else//找不到要删除的结点             printf("\nno find string!\n");        }    }}/*********输出链表*********/void print(node *head){    node *temp;    temp = head;//取得链表的头指针     printf("\noutput strings:\n");    printf("      name              unit              no\n");    while (temp != NULL)//只要不是空表     {        printf("\n%10s       %10s       %10s\n", temp->name, temp->unit, temp->no);//输出链表的值         printf("---------------------------------------------------\n");        temp = temp->link;//移向下一个链表     }}
0 0
原创粉丝点击