标题:结构体问题 函数调用找不到结构体 只看case12,还有这种情况free()函数应该 ...
只看楼主
呼延
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2017-2-15
结帖率:100%
已结贴  问题点数:20 回复次数:2 
结构体问题 函数调用找不到结构体 只看case12,还有这种情况free()函数应该用在哪?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <malloc.h>
#define N 10
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct student
{
    int id;
    char name[20];
    int age;
    struct student *next;
};
/*void Menu();
float Radius_Area(float Radius);
void Diamond(n);
void Hunderd();
void Prime(int n);
void Bubble(int arr[]);
void add(char arr[]);
char change(char x);
void plan();
float add1(int n);
void ra();
void ad(int a[4][4],int b[4][4]);*/
void add2(student *stu);
void add3(student *stu);
void show();
student head ={0};
student *p;
int main(int argc, char *argv[]) {
    float Radius;int x,n,i,j;int arr[N],a[4][4],b[4][4];char x1,m,arr1[50];m=1;
    while(m)
    {
   
     Menu();
   printf("\n*****************\n");
   printf("input number please!\n");
    scanf("%d",&x);
    switch(x)
     {
       /*case  1: printf("input : Radius\n");scanf("%f",&Radius);
        printf("圆的面积为;%.2f\n",Radius_Area(Radius));break;
       case  2: printf("input :n\n");scanf("%d",&n);Diamond(n);break;
       case  3: Hunderd();break;
       case  4: printf("please input :n\n");scanf("%d",&n);Prime(n);break;
       case  5: for(i=0;i<N;i++)
                {
                    printf("please input :arr[%d]\n",i);
                    scanf("%d",&arr[i]);
                }
                Bubble(arr);break;
        case  6: printf("please input:arr1\n");gets(arr1);add(arr1);break;
        case  7: printf("please input:x1\n");scanf("%c",&x1);printf("%c转换后:%c",x1,change(x1));break;   
        case  8: plan(); break;
        case  9: printf("please input :n\n");scanf("%d",&n);printf("sum=%.2f",add1(n));break;
        case 10: ra();break;
        case 11: for(i=0;i<4;i++)
                 for(j=0;j<4;j++)
                 {
                     printf("input :a[%d][%d]",i,j);
                     scanf("%d",&a[i][j]);
                 }
                 for(i=0;i<4;i++)
                 for(j=0;j<4;j++)
                 {
                    printf("input :b[%d][%d]",i,j);
                    scanf("%d",&b[i][j]);
                 }
                 ad(a,b);break;*/
        case 12: student *stu=(stu*)malloc (sizeof(student));
                     stu->id=12;
                     strcpy(stu->name,"zzz");
                     stu->age=18;
                     add2(stu);
                 student *stu1=(stu*)malloc (sizeof(student));
                     stu1->id=15;
                     strcpy(stu1->name,"xxx");
                     stu1->age=18;
                      add2(stu1);
                 student *stu2=(stu*)malloc (sizeof(student));
                     stu2->id=17;
                     strcpy(stu2->name,"sss");
                     stu2->age=20;
                      add2(stu2);
                 student *stu3=(stu*)malloc (sizeof(student));
                     stu3->id=16;
                     strcpy(stu3->name,"yyy");
                     stu3->age=22;
                      add2(stu3);
                 show();      
                 student *stu=(stu*)malloc (sizeof(student));
                     stu5->id=18;
                     strcpy(stu5->name,"zjc");
                     stu5->age=20;
                     add3(stu5);
                 show();break;                                                      
        }

}
    return 0;
}
void Menu()    //菜单函数//
{  
   printf("\n 1 随机输入r,求圆的面积");
   printf("\n 2 打印菱形");
   printf("\n 3 百钱买百鸡问题");
   printf("\n 4 素数问题");
   printf("\n 5 数组冒泡排序");
   printf("\n 6 随机输入字符串求其中数字、大写字母、空格出现的次数,以问号结束");
   printf("\n 7 大小写字母转换");
   printf("\n 8 计算π的值");
   printf("\n 9 计算1~n的k次方的累计值");
   printf("\n 10 随机出10道算术题");
   printf("\n 11 矩阵相加");
   printf("\n 12 链表的插入");
}
/*float Radius_Area(float Radius)     //圆的面积//
{   float Radius_Area;
  Radius_Area=3.14*Radius*Radius;
  return (Radius_Area);
}
void Diamond(n)    //打印菱形//
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n-i;j++) printf(" ");
        for(j=1;j<=2*i-1;j++) printf("*");
        printf("\n");   
    }
    for(i=n-1;i>=1;i--)
    {
        for(j=1;j<=n-i;j++) printf(" ");
        for(j=1;j<=2*i-1;j++) printf("*");
        printf("\n");   
    }
   
}
void Hunderd()        //百钱买百鸡问题//
{   int Male,Trigamous,Small;float t;t=0;
    for(Male=1;Male<=19;Male++)
    {
        for(Trigamous=1;Trigamous<=32;Trigamous++)
        {
            Small=100-Male-Trigamous;
            t=(float)Small/3;
            if(5*Male+3*Trigamous+t==100)
            printf("公鸡=%d,母鸡=%d,小鸡=%d\n",Male,Trigamous,Small);
        }
    }
}
void Prime(int n)    //素数问题//
{
    int i,m;m=0;
    for(i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            printf("n不是素数\n");
            m=1;
            break;
        }
    }
    if(m==0)printf("n是素数\n");
}
void Bubble(int ar[])    //冒泡排序//
{
    int i,j,t;t=0;
    for(i=0;i<N-1;i++)
        for(j=0;j<N-1-i;j++)
            if(ar[j]>ar[j+1])
            {
                t=ar[j];ar[j]=ar[j+1];ar[j+1]=t;
            }
    for(i=0;i<N;i++)printf("%d\n",ar[i]);        
}
void add(char arr[50])       //字符串统计问题//
{
    int k,j,i,n;i=0;j=0;n=0;k=0;
    while(arr[i]!='?');
    {
        if(arr[i]==' ')k++;
        if(arr[i]>='0'&&arr[i]<='9')j++;
        if(arr[i]>='A'&&arr[i]<='z')n++;
        i++;
     }
     printf("数字:%d,字母:%d,空格:%d\n",j,n,k);
}
char change(char x)    //字母转换问题//
{
    char y;
    if(x>='a'&&x<='z')y=y-32;
    if(x>='A'&&x<='Z')y=y+32;
    return(y);
}
void plan()   //计算π的值//
{
    float x,y,d,t,sum;sum=1;x=1;y=3;d=1;
    while(d>1e-6)
    {
        d=d*x/y;
        sum=sum+d;
        x++;
        y+=2;
    }
    t=2*sum;
    printf("π=%.2f",t);
}
float add1(int n)    //计算1~n的k次方的累计值//
{
    float sum;int i,k;sum=0;
    printf("input :k\n");
    scanf("%d",&k);
    for(i=1;i<=n;i++)
    sum=sum+pow(i,k);
    return(sum);
}
void ra()    //随机出10道数学题//
{
    int i;
    srand(time(NULL));
    for(i=0;i<10;i++)
    {
        printf("%d+%d=?\n",rand()%50,rand()%50);
    }
}
void ad(int a[4][4],int b[4][4])   //矩阵的加法//
{
    int i,j,c[4][4];
    for(i=0;i<4;i++)
    for(j=0;j<4;j++)c[i][j]=a[i][j]+b[i][j];
    printf("onput:a\n");  //输出a矩阵//
    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)printf("%d\t",a[i][j]);
        printf("\n");
    }
        printf("onput:b\n");  //输出b矩阵//
    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)printf("%d\t",b[i][j]);
        printf("\n");
    }
        printf("onput:c\n");  //输出c矩阵//
    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)printf("%d\t",c[i][j]);
        printf("\n");
    }
} */
void add2(student *stu)   //动态链表的创建//
{
    stu->=head.next;
    head.next= stu;
}
void add3(student *stu)    //插入链表//
{
    stu *c;
    p=head.next;
    while(p)
    {
        if(p->id==17)
        {
            c=p->next;
            p->next=stu;
            stu->next=c;
            break;
        }
        p=p->next;
    }
}
void show()   //输出链表//
{
    p=head.next;
    wgile(p)
    {
        printf("ID: %d,name: %s,age: %d\n",p->id,p->name,p->age);
        p=p->next;
    }
}   
搜索更多相关主题的帖子: student int void printf for 
2018-06-10 13:20
liaohs
Rank: 4
等 级:业余侠客
威 望:7
帖 子:61
专家分:292
注 册:2017-11-26
得分:20 
程序错误很多,懂得原理才能纠错。
case 12中,(stu*)应为(student*)
2018-06-10 15:15
呼延
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2017-2-15
得分:0 
回复 2楼 liaohs
还是不行   提示错误  [Error] unknown type name 'student'
2018-06-10 16:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-487776-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014517 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved