1. 程式人生 > >C語言程式設計---9:自定義資料型別

C語言程式設計---9:自定義資料型別

/*輸入學生的學號、姓名、性別、年齡、成績、家庭住址,輸出成績較高的學生資訊*/
#include<stdio.h>
void main()
{
    struct student
        {
            int num;
            char name[20];
            int age;
            char sex;           
            float score;
            char addr[30];
        }stdt1={10101,"Li Lin",'M',18,99.5,"123Beijing Road"
},stdt2; /*注意最後一個分號*/ /*如%d%c之間有空格,輸入的時候,數字和字元之間有沒有間隔都行*/ /*如%d%c之間沒有有空格,輸入的時候,數字和字元之間就不能有間隔*/ /*會把中間的空格當做字元處理*/ scanf("%d%s%d%c%f%s",&stdt2.num,stdt2.name,&stdt2.age,&stdt2.sex,&stdt2.score,stdt2.addr); printf("%d\t%s\t%d\t%c\t%5.2f\t%s\n",stdt2.num,stdt2.name,stdt2.age,stdt2.sex,stdt2.score,stdt2.addr); if
(stdt1.score>stdt2.score) printf("%d\t%s\t%6.2f\n",stdt1.num,stdt1.name,stdt1.score); else printf("%d\t%s\t%6.2f\n",stdt2.num,stdt2.name,stdt2.score); }

結果:

10102 pepe 22w 99.8 adb
10102   pepe    22      w       99.80   adb
10102   pepe     99.80
/*有N各結構體變數,內含學生學號、姓名、3門課程成績,輸出平均成績最高的學生的資訊*/
#include<stdio.h> #define N 3 struct student { int num; char name[20]; float score[3]; float aver; }; void main() { void input(struct student stu[]); struct student max(struct student stu[]); void print(struct student stu); struct student stu[N],* p =stu; input(p); print(max(p)); } void input(struct student stu[]) { int i; printf("請輸入各學生的資訊:學號、姓名、三門課成績:\n"); for(i=0;i<N;i++) { scanf("%d%s%f%f%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); stu[i].aver = (stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0; } } struct student max(struct student stu[]) { int m=0; for(int i=0;i<N;i++) if(stu[i].aver>stu[m].aver) m=i; return stu[m]; } void print(struct student stud) { printf("學號:%d\n姓名:%s\n三門課成績:%5.1f,%5.1f,%5.1f\n平均成績:%5.1f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver); }

結果:

請輸入各學生的資訊:學號、姓名、三門課成績:
111 ee 88 99 77
222 rr 88 99 88
333 tt 88 77 66
學號:222
姓名:rr
三門課成績: 88.0, 99.0, 88.0
平均成績: 91.7

建立簡單的靜態連結串列

#include<stdio.h>
#define NULL 0
struct student
{
    int num;
    float score;
    struct student *next;
};
void main()
{
    struct student a,b,c,*head,*p;
    a.num = 11;
    a.score = 88;
    b.num = 22;
    b.score = 99;
    c.num = 18;
    c.score = 95;
    head  = &a;
    a.next = &b;
    b.next = &c;
    c.next = NULL;
    p = head;
    do
    {
        printf("%d,%5.1f\n",(*p).num,p->score);
        p = (*p).next;
    }while(p != NULL);
}

結果:

11, 88.0
22, 99.0
18, 95.0

建立動態連結串列

#include<stdio.h>
/*在C語言中,開闢記憶體單元用malloc函式*/
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
    int num;
    float score;
    struct student *next;
};
void main()
{
    struct student *head,*p;
    head = p = (struct student *)malloc(LEN);
    scanf("%d%f",&p->num,&p->score);
    p = (struct student *)malloc(LEN);

    scanf("%d%f",&p->num,&p->score);
    head->next = p;
    p->next = NULL;

    p = head;
    printf("節點1:%d%5.1f\n",head->num,head->score);
    p = p->next;
    printf("節點2:%d%5.1f\n",p->num,p->score);
}

結果:

101 33.6
1112 66.5
節點1:101 33.6
節點2:1112 66.5

列舉型別

    enum weekday(sun,mon,tue,wed,thu,fri,sat);
    enum weekday workday,week_end;
    workday = mon;
    week_end = sun;

相關推薦

C語言程式設計---9定義資料型別

/*輸入學生的學號、姓名、性別、年齡、成績、家庭住址,輸出成績較高的學生資訊*/ #include<stdio.h> void main() { struct student

C/C++基礎】11_使用者定義資料型別

1. 結構體型別1.1 結構體型別定義的一般形式       在實際問題中,一組資料往往具有不同的資料型別。例如,在學生登記表中,姓名應為字元型;學號可為整型或字元型;年齡應為整型;性別應為字元型;成績可為整型或實型。顯然不能用一個數組來存放這一組資料。因為陣列中各元素的型別

C語言】typedef(定義資料型別)與#define(巨集定義)用法比較

  不管是在C語言還是在C++中,typedef這個詞都不少見,當然出現頻率較高的還是在C程式碼中。typedef和#define有些相似,但更多的是不同,特別是在一些複雜的用法上,就完全不同了。      1.巨集定義(#define)      巨集定義又稱為巨集代換

C語言】字串處理定義函式

1、字串求長度 #include <stdio.h> int Mystrlen1(const char *str) { int i=0; while(*(str++)!='\0') { i++; } return i; } int Mystrlen2(cons

《我的第①本c語言程式設計C語言從入門到精通》掃描版.pdf

書籍簡介: 《C語言從入門到精通》以零基礎講解為宗旨,用例項引導讀者深入學習,採取“基礎知識→核心技術→趣味題解→專案實戰”的講解模式,深入淺出地講解C語言的各項技術及實戰技能。《C語言從入門到精通》第1篇【基礎知識】主要講解步入C的世界、常量與變數、資料型別、運算子和表示式、程式控制結構

C++ protobuf 定義資料型別的賦值

對於C++ protobuf 自定義資料型別的賦值,有兩種方式 set_allocate_XXX mutable_XXX 舉例說明 message SAT_JSON_CONFIG { required int32 AxisYMax = 1; required i

c++--使用者定義資料型別詳細篇

結構體型別 在一個組合中包含若干個型別不同的資料項。相當於其他高階語言中的記錄。 宣告 一般形式: struct 結構名 { 資料型別 成員名 1; 資料型別 成員名 2; : 資料型別 成員名 n; }; 結構體型別名作為結構體型別的標誌。 宣告一個結構體

程式猿之---C語言細節9(巨集定義、max(a,b)巨集定義細節、大小端判斷、(int&)a什麼意思)

主要內容:巨集定義、max(a,b)巨集定義細節、大小端判斷、(int&)a什麼意思 #if 1 #include <stdio.h> // 注意空格 #define F (x) ((x) - 1) // F代表後面 #define F(x)

C++ STL 有關於SET集合部分 的定義資料型別的排序 以及 pair的使用

#include<iostream> using namespace std; #include"set" //set集合容器的標頭檔案 #include<functional> //用於識別set從大到小中greater的識別 /*知識點集合*/

語言定義資料型別的大小

先說一下易語言的變數儲存機制 易語言有基本資料型別和複合資料型別兩種 基本資料型別包括:1. 各種整數 2.各種浮點 3. 邏輯值 他們都是儲存在棧上的 大小都是固定的 用不著取 複合型別一般儲存在堆上 然後在棧上儲存堆上資料的引用 其中有幾種比較特殊:字串 位元組集 陣列

C++基礎學習筆記定義陣列模板類

//!時間:2017年9月12日(週二)下午 //!內容:陣列模板類 /* 修改:2017年9月13上午 成員方法中delete未正確匹配 改進:2017年9月13晚上 陣列總量改為固定 */ #define _CRTDBG_MAP_ALLOC #include <iostream>

C++ map以定義資料型別做鍵值

struct Time { UINT16 nYear; UINT8 nMonth; UINT8 nDay; UINT8 nHour; UINT8 nMinute; UINT8 nSecond; UINT16 nTick; Time() { } Time(UINT16 nYear,UIN

C語言程式設計---3順序程式設計

賦值表示式 賦值運算子按照“自右而左”的結合順序運算。因此 a = (b = 5); a = b = c; a = 5 + (c = 6); a = 11 a = (b = 4) + (c = 6); a = 10; a = (b = 10)/(c =

C語言程式設計——9,預處理命令

    以“#”開頭的預處理命令一般都放在函式之外,而且一般放在原始檔前面,他們成為預處理部分。    預處理指的是進行編譯的第一遍掃描(語法掃描和語法分析)之前所作的工作。它由預處理程式負責完成。當對一個原始檔進行編譯時,系統自動引用預處理程式對預處理部分進行處理,處理完畢

C語言的三種整型資料型別int、short int和long int

int資料型別的位數為16位,short int資料型別的位數也是16位。而long int的位數為32位,可用來儲存比較大的整數。  short int 和 long int可以縮寫為short 和 long。               C語言中的整型資料型別int、

unity3d學習日記使用[System.Serializable]在inspector面板內顯示定義資料型別類例項物件的內部資料

在unity裡,自定義資料型別無法顯示在inspectior面板裡,需要對定義資料型別的類或者結構體使用[System.Serializable]。效果如圖: using System.Collec

linux c 語言程式設計環境動態庫和靜態庫的製作

庫: 庫用於將相似函式打包在一個單元中。然後這些單元就可為其他開發人員所共享,並因此有了模組化程式設計這種說法 — 即,從模組中構建程式。Linux 支援兩種型別的庫,每一種庫都有各自的優缺點。靜態庫包

C語言程式設計用泰勒級數求自然數e的近似值

題目:C語言中用泰勒級數求e的近似值,直到最後一項小於 10的負6次方為止次方 e=1+1/1!+1/2!+...+1/n! 描述:觀察公式前兩項可以直接不用計算,合併為2,設定三個float型變數,e為結果,s為分子,i為分母,通過i累加再相乘之後實現分母

C# RDLC報表(二)--使用定義資料

 <!--[if !supportLists]-->1<!--[endif]-->新建窗體   <!--[if !supportLists]-->2<!--[endif]-->建立資料來源   3<!--[endif

[ASP.NET Core 3框架揭祕] 配置[9]定義配置源

我們在前面對配置模型中預設提供的各種IConfigurationSource實現型別進行了深入詳盡的介紹,如果它們依然不能滿足專案中的需求,我們還可以通過自定義IConfigurationSource實現型別來支援我們希望的配置源。就配置資料的持久化方式來說,將配置儲存在資料庫中應該是一種常見的方式。接下來我