鱼C论坛

 找回密码
 立即注册
查看: 3136|回复: 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-8-13 18:30:47 | 显示全部楼层
#include <stdio.h>
#include <malloc.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;//把这句放到前面
       printf("输入总人数:");
       scanf("%d",&total);
           person = (persons*)malloc(sizeof(persons) * total);//按人数分配堆内存空间
把开头的部分改一下试试
或者把拓展名由.c改成.cpp


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2012-8-14 00:06:09 | 显示全部楼层
本帖最后由 the_one 于 2012-8-14 00:53 编辑
#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;//定义总人数
       printf("输入总人数:");
       scanf("%d",&total);
       persons person[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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

编译不了  可否改下
想知道小甲鱼最近在做啥?请访问 -> 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.num);
             printf("Please input the name: ");
             scanf("%s", person.name);
             fflush(stdin);
             printf("Please input the sex(M/F): ");
             scanf("%c", &person.sex);
             fflush(stdin);
             person.job |= 0x20;//改进了一下,即使输入大写也可以识别
             printf("Please input the job(s/t): ");
             scanf("%c", &person.job);
             fflush(stdin);
             if( person.job == 's' )
             {
                   printf("Please input the class: ");
                   scanf("%d", &person.category.banji);
                   fflush(stdin);
             }
             else if( person.job == 't' )
             {
                   printf("Please input the position: ");
                   scanf("%s", &person.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.job == 's')
             {
                   printf("%-6d%-10s%-3c%-3c%10d\n", person.num,
                        person.name, person.sex, person.job,
                        person.category.banji);
             }
             else
             {
                   printf("%-6d%-10s%-3c%-3c%10s\n", person.num,
                        person.name, person.sex, person.job,
                        person.category.position);
             }
       }
      
       getchar();       return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

“persons person[total];//按人数确定定义结构体个数 ”         这样可以吗?? 用一个变量来定义数组个数。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2012-8-14 10:52:19 | 显示全部楼层
本帖最后由 the_one 于 2012-8-14 10:56 编辑
骗子死全家 发表于 2012-8-14 03:46
编译不了  可否改下
#include <stdio.h>
#include <malloc.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;//定义总人数
       printf("输入总人数:");
       scanf("%d",&total); 
       persons *person;
    person = (persons*)malloc(sizeof(persons) * 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);
             printf("Please input the job(s/t): ");
             scanf("%c", &person[i].job);
             person[i].job |= 0x20;//改进了一下,即使输入大写也可以识别
             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);
             }
       }
    free(person);//最后要释放堆内存空间
       
    return 0;
}已经改过了,原来的不知道为什么VC6编译不了,但DEV C++却可以
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

还是不行额   是我的VC出错??
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

我的没问题啊,出错信息是什么
想知道小甲鱼最近在做啥?请访问 -> 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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

麻烦你了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

改成CPP文件格式  就编译通过了  谢谢了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-15 05:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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