zoukankan      html  css  js  c++  java
  • 结构类型1.2.2

    #include<cstdio>//c++中用的,在这里也可以用 
    #include<iostream>//c++库文件。指输入(in)输出(out)流(stream)
    using namespace std;//C++语法
    //当碰到有一种数据类型有多个成分,并且各个成分的数据类型不一定相同时我们需要自己定义,格式如下 
    struct student{
        char num[12];
        char name[10];
        int age;
        float score;
    }; //定义结构类型及成员名 
    int main()
    {
         struct student s;
         scanf("%s%s%d%f",s.num,s.name,&s.age,&s.score);
         //s.num;s.name是数组的变量名相当于地址故前面不用加地址符 ,age和score需要加 
         cout<<s.num<<'	'<<s.name<<'	'<<s.age<<'	'<<s.score<<endl;
        
        return 0;
     } 
     要通过变量名.成员名取 

    其中输入这一行scanf("%s%s%d%f",s.num,s.name,&s.age,&s.score);可以替换为:

    gets(s.num);
    gets(s.name);
    scanf("%d%f",&s.age,s.score);

    对与结构的定义也可以换为

    typedef struct student{
        char num[12];
        char name[10];
        int age;
        float score;
    }SStudent; //这里的SStudent名称不一定和上面这个student一致,随便
    int main()
    {
         SStudent s;//注意这里

    这样换之后结果都是一样的!!

  • 相关阅读:
    231. Power of Two
    204. Count Primes
    205. Isomorphic Strings
    203. Remove Linked List Elements
    179. Largest Number
    922. Sort Array By Parity II
    350. Intersection of Two Arrays II
    242. Valid Anagram
    164. Maximum Gap
    147. Insertion Sort List
  • 原文地址:https://www.cnblogs.com/lysun/p/12553149.html
Copyright © 2011-2022 走看看