鱼C论坛

 找回密码
 立即注册
查看: 3652|回复: 11

共同体代码求改成 不限人数的

[复制链接]
发表于 2012-8-13 18:30:46 | 显示全部楼层 |阅读模式
3鱼币
#include <stdio.h>
struct
{
      int num;
      char name[10];
      char sex;
      char job;
      union
      {
            int banji;
            char position[10];
      }category;
}person[2];  //为了方便先假设一个学生一个老师。
void main()
{
      int i;
      for(i=0; i < 2; i++)
      {
            printf("Please input the num: ");
            scanf("%d", &person[i].num);
            printf("Please input the name: ");
            scanf("%s", person[i].name);
            fflush(stdin);
            printf("Please input the sex(M/F): ");
            scanf("%c", &person[i].sex);
            fflush(stdin);
            printf("Please input the job(s/t): ");
            scanf("%c", &person[i].job);
            fflush(stdin);
            if( person[i].job == 's' )
            {
                  printf("Please input the class: ");
                  scanf("%d", &person[i].category.banji);
                  fflush(stdin);
            }
            else if( person[i].job == 't' )
            {
                  printf("Please input the position: ");
                  scanf("%s", person[i].category.position);
                  fflush(stdin);
            }
            else
            {
                  printf("Input Error!!\n");
            }            
            
            printf("\n");
      }
      
      // 以下是打印数据……
      printf("No.    name    sex job class/position\n");
      for( i=0; i < 2; i++ )
      {
            if( person[i].job == 's')
            {
                  printf("%-6d%-10s%-3c%-3c%10d\n", person[i].num,
                        person[i].name, person[i].sex, person[i].job,
                        person[i].category.banji);
            }
            else
            {
                  printf("%-6d%-10s%-3c%-3c%10s\n", person[i].num,
                        person[i].name, person[i].sex, person[i].job,
                        person[i].category.position);
            }
      }

最佳答案

查看完整内容

把开头的部分改一下试试 或者把拓展名由.c改成.cpp
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-13 18:30:47 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <malloc.h>//要包含这个头文件

  3. typedef struct
  4. {
  5.        int num;
  6.        char name[10];
  7.        char sex;
  8.        char job;
  9.        union
  10.        {
  11.              int banji;
  12.              char position[10];
  13.        }category;
  14. }persons;  //将这种结构体数据类型定义为persons

  15. int main()
  16. {
  17.        int i;
  18.        int total;//定义总人数
  19.        persons *person;//把这句放到前面
  20.        printf("输入总人数:");
  21.        scanf("%d",&total);
  22.            person = (persons*)malloc(sizeof(persons) * total);//按人数分配堆内存空间
复制代码

把开头的部分改一下试试
或者把拓展名由.c改成.cpp


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-14 00:06:09 | 显示全部楼层
本帖最后由 the_one 于 2012-8-14 00:53 编辑
  1. #include <stdio.h>
  2. typedef struct
  3. {
  4.        int num;
  5.        char name[10];
  6.        char sex;
  7.        char job;
  8.        union
  9.        {
  10.              int banji;
  11.              char position[10];
  12.        }category;
  13. }persons;  //将这种结构体数据类型定义为persons
  14. int main()
  15. {
  16.        int i;
  17.        int total;//定义总人数
  18.        printf("输入总人数:");
  19.        scanf("%d",&total);
  20.        persons person[total];//按人数确定定义结构体个数
  21.       
  22.        for(i=0; i < total; i++)//根据总人数确定循环次数
  23.        {
  24.               
  25.             
  26.                          printf("Please input the num: ");
  27.              scanf("%d", &person[i].num);
  28.              printf("Please input the name: ");
  29.              scanf("%s", person[i].name);
  30.              fflush(stdin);
  31.              printf("Please input the sex(M/F): ");
  32.              scanf("%c", &person[i].sex);
  33.              fflush(stdin);
  34.              person[i].job |= 0x20;//改进了一下,即使输入大写也可以识别
  35.              printf("Please input the job(s/t): ");
  36.              scanf("%c", &person[i].job);
  37.              fflush(stdin);
  38.              if( person[i].job == 's' )
  39.              {
  40.                    printf("Please input the class: ");
  41.                    scanf("%d", &person[i].category.banji);
  42.                    fflush(stdin);
  43.              }
  44.              else if( person[i].job == 't' )
  45.              {
  46.                    printf("Please input the position: ");
  47.                    scanf("%s", person[i].category.position);
  48.                    fflush(stdin);
  49.              }
  50.              else
  51.              {
  52.                    printf("Input Error!!\n");
  53.              }            
  54.             
  55.              printf("\n");
  56.             
  57.        }
  58.       
  59.        // 以下是打印数据……
  60.        printf("No.    name    sex job class/position\n");
  61.        for( i=0; i < total; i++ )//根据总人数确定循环次数
  62.        {
  63.              if( person[i].job == 's')
  64.              {
  65.                    printf("%-6d%-10s%-3c%-3c%10d\n", person[i].num,
  66.                         person[i].name, person[i].sex, person[i].job,
  67.                         person[i].category.banji);
  68.              }
  69.              else
  70.              {
  71.                    printf("%-6d%-10s%-3c%-3c%10s\n", person[i].num,
  72.                         person[i].name, person[i].sex, person[i].job,
  73.                         person[i].category.position);
  74.              }
  75.        }
  76.       
  77.        getchar();       return 0;
  78. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-14 03:46:06 | 显示全部楼层

编译不了  可否改下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-14 04:34:05 | 显示全部楼层

#include <stdio.h>
typedef struct
{
       int num;
       char name[10];
       char sex;
       char job;
       union
       {
             int banji;
             char position[10];
       }category;
}persons;  //将这种结构体数据类型定义为persons
int main()
{

       int i;
       int total;//定义总人数
           persons person[total];//按人数确定定义结构体个数   这样改了还是有3个错
       printf("输入总人数:");
       scanf("%d",&total);
      
      
       for(i=0; i < total; i++)//根据总人数确定循环次数
       {
              
            
                         printf("Please input the num: ");
             scanf("%d", &person[i].num);
             printf("Please input the name: ");
             scanf("%s", person[i].name);
             fflush(stdin);
             printf("Please input the sex(M/F): ");
             scanf("%c", &person[i].sex);
             fflush(stdin);
             person[i].job |= 0x20;//改进了一下,即使输入大写也可以识别
             printf("Please input the job(s/t): ");
             scanf("%c", &person[i].job);
             fflush(stdin);
             if( person[i].job == 's' )
             {
                   printf("Please input the class: ");
                   scanf("%d", &person[i].category.banji);
                   fflush(stdin);
             }
             else if( person[i].job == 't' )
             {
                   printf("Please input the position: ");
                   scanf("%s", &person[i].category.position);
                   fflush(stdin);
             }
             else
             {
                   printf("Input Error!!\n");
             }            
            
             printf("\n");
            
       }
      
       // 以下是打印数据……
       printf("No.    name    sex job class/position\n");
       for( i=0; i < total; i++ )//根据总人数确定循环次数
       {
             if( person[i].job == 's')
             {
                   printf("%-6d%-10s%-3c%-3c%10d\n", person[i].num,
                        person[i].name, person[i].sex, person[i].job,
                        person[i].category.banji);
             }
             else
             {
                   printf("%-6d%-10s%-3c%-3c%10s\n", person[i].num,
                        person[i].name, person[i].sex, person[i].job,
                        person[i].category.position);
             }
       }
      
       getchar();       return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-14 09:54:31 | 显示全部楼层

“persons person[total];//按人数确定定义结构体个数 ”         这样可以吗?? 用一个变量来定义数组个数。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-14 10:52:19 | 显示全部楼层
本帖最后由 the_one 于 2012-8-14 10:56 编辑
骗子死全家 发表于 2012-8-14 03:46
编译不了  可否改下
  1. #include <stdio.h>
  2. #include <malloc.h>//要包含这个头文件
  3. typedef struct
  4. {
  5.        int num;
  6.        char name[10];
  7.        char sex;
  8.        char job;
  9.        union
  10.        {
  11.              int banji;
  12.              char position[10];
  13.        }category;
  14. }persons;  //将这种结构体数据类型定义为persons

  15. int main()
  16. {
  17.        int i;
  18.        int total;//定义总人数
  19.        printf("输入总人数:");
  20.        scanf("%d",&total);
  21.        persons *person;
  22.     person = (persons*)malloc(sizeof(persons) * total);//按人数分配堆内存空间
  23.       
  24.        for(i=0; i < total; i++)//根据总人数确定循环次数
  25.        {
  26.              printf("Please input the num: ");
  27.              scanf("%d", &person[i].num);
  28.              printf("Please input the name: ");
  29.              scanf("%s", person[i].name);
  30.              fflush(stdin);
  31.              printf("Please input the sex(M/F): ");
  32.              scanf("%c", &person[i].sex);
  33.              fflush(stdin);
  34.              printf("Please input the job(s/t): ");
  35.              scanf("%c", &person[i].job);
  36.              person[i].job |= 0x20;//改进了一下,即使输入大写也可以识别
  37.              fflush(stdin);
  38.              if( person[i].job == 's' )
  39.              {
  40.                    printf("Please input the class: ");
  41.                    scanf("%d", &person[i].category.banji);
  42.                    fflush(stdin);
  43.              }
  44.              else if( person[i].job == 't' )
  45.              {
  46.                    printf("Please input the position: ");
  47.                    scanf("%s", person[i].category.position);
  48.                    fflush(stdin);
  49.              }
  50.              else
  51.              {
  52.                    printf("Input Error!!\n");
  53.              }            
  54.              printf("\n");
  55.        }       // 以下是打印数据……
  56.        printf("No.    name    sex job class/position\n");
  57.        for( i=0; i < total; i++ )//根据总人数确定循环次数
  58.        {
  59.              if( person[i].job == 's')
  60.              {
  61.                    printf("%-6d%-10s%-3c%-3c%10d\n", person[i].num,
  62.                         person[i].name, person[i].sex, person[i].job,
  63.                         person[i].category.banji);
  64.              }
  65.              else
  66.              {
  67.                    printf("%-6d%-10s%-3c%-3c%10s\n", person[i].num,
  68.                         person[i].name, person[i].sex, person[i].job,
  69.                         person[i].category.position);
  70.              }
  71.        }
  72.     free(person);//最后要释放堆内存空间
  73.       
  74.     return 0;
  75. }已经改过了,原来的不知道为什么VC6编译不了,但DEV C++却可以
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-14 17:49:21 | 显示全部楼层

还是不行额   是我的VC出错??
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-14 17:54:08 | 显示全部楼层
骗子死全家 发表于 2012-8-14 17:49
还是不行额   是我的VC出错??

我的没问题啊,出错信息是什么
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-14 21:23:57 | 显示全部楼层
the_one 发表于 2012-8-14 17:54
我的没问题啊,出错信息是什么

E:\aaa\fgsdfg\fadfa.c(22) : error C2275: 'Persons' : illegal use of this type as an expression
        E:\aaa\fgsdfg\fadfa.c(14) : see declaration of 'Persons'
E:\aaa\fgsdfg\fadfa.c(22) : error C2065: 'person' : undeclared identifier
E:\aaa\fgsdfg\fadfa.c(23) : error C2065: 'persons' : undeclared identifier
E:\aaa\fgsdfg\fadfa.c(23) : error C2059: syntax error : ')'
E:\aaa\fgsdfg\fadfa.c(28) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(28) : error C2224: left of '.num' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(30) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(30) : error C2224: left of '.name' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(33) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(33) : error C2224: left of '.sex' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(36) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(36) : error C2224: left of '.job' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(37) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(37) : error C2224: left of '.job' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(39) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(39) : error C2224: left of '.job' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(42) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(42) : error C2224: left of '.category' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(45) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(45) : error C2224: left of '.job' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(48) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(48) : error C2224: left of '.category' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(60) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(60) : error C2224: left of '.job' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(62) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(62) : error C2224: left of '.num' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(63) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(63) : error C2224: left of '.name' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(63) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(63) : error C2224: left of '.sex' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(63) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(63) : error C2224: left of '.job' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(64) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(64) : error C2224: left of '.category' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(68) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(68) : error C2224: left of '.num' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(69) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(69) : error C2224: left of '.name' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(69) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(69) : error C2224: left of '.sex' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(69) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(69) : error C2224: left of '.job' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(70) : error C2109: subscript requires array or pointer type
E:\aaa\fgsdfg\fadfa.c(70) : error C2224: left of '.category' must have struct/union type
E:\aaa\fgsdfg\fadfa.c(73) : warning C4022: 'free' : pointer mismatch for actual parameter 1
Error executing cl.exe.

fadfa.obj - 44 error(s), 1 warning(s)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-14 21:25:22 | 显示全部楼层
the_one 发表于 2012-8-14 17:54
我的没问题啊,出错信息是什么

麻烦你了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-15 15:21:07 | 显示全部楼层

改成CPP文件格式  就编译通过了  谢谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 11:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表