zoukankan      html  css  js  c++  java
  • Linux_C语言编程基础

    Linux_C语言编程基础

    20191331 lyx

    任务

    1. 基于Ubuntu或OpenEuler完成下面的任务(OpenEuler有加分)
    2. 选择教材第二章的一节进行编程基础练习(2.10,2.11,2.12,2.13,2.14任选一个)
    3. 建立自己的项目目录,包含自己学号信息(如20190100linkedlist),构建项目结构(src, include,bin, lib, docs, test...),然后把相应代码和文档放置到正确位置,用tree命令查看项目结构,提交截图(5分)
    4. 进行gcc相关练习(ESc, iso, -I等)提交相关截图(5分)
    5. 进行静态库,动态库制作和调用练习,提交相关截图(5分)
    6. 进行gdb相关练习,至少包含四种断点的设置,提交相关截图(10分)
    7. 编写makefile(5分)

    环境

    安装 银河麒麟UIOpenEuler 20.03LTS 系统

    c语言编程实现

    利用链表实现一元疏松多项式的相关计算

    部分代码展示:

    使用链表建立多项式:

    Node Create_Poly(){//建立多项式
        Node head = NULL;
        Node p1,p2=NULL;
        int n;//项数
        int i;
        count++;
        printf(" 请输入多项式");
        if(count==1)printf(" A ");
        else printf(" B ");
        printf("的项数");
        scanf("%d",&n);
        head = (Node)malloc(sizeof(node));//创建动态链表
        for(i=1;i<=n;i++){//根据项数n创建多项式链表
            if((p1=(Node)malloc(sizeof(node)))==NULL){
                printf("内存不足空间申请失败");
                exit(0);
            }
            p1->next = NULL;
            printf("请输入多项式");
            if(count==1){
            printf(" A ");
            }
            else{
            printf(" B ");
            }
            printf("的第 %d 项系数和指数: ",i);
            scanf("%lf",&p1->xishu);
            scanf("%d",&p1->index);
            if(i==1){head->next = p1;}
            //输入是第一项时,头结点指向第一项
            else{
            p2->next = p1;//连接链表
            }
            p2 = p1;//置换p1 p2 使用尾插法建立多项式 p2始终指向最后一项
        }
        printf(" 多项式");
        if(count==1)
        printf(" A = ");
        else 
        printf(" B = ");
        Sortpoly(head);
        Displaypoly(head);
        printf("------------------------------
    ");
        return head;
    }
    

    main函数:

    void main(){
        int choice,i=0;
        Node head1=NULL;
    	Node head2=NULL;
        do{
            system("cls");
            printf("
    ");
            printf("|-----------20191331一元稀疏多项式-------------| 
    ");
            printf("|-------------   1——建立A、B多项式  ---------- | 
    ");
            printf("|---------------   2——加法 A+B  --------------| 
    ");
            printf("|--------  3——减法 A-B    4——减法 B-A  --------| 
    ");
            printf("|---------------   5——乘法 A*B  ---------------| 
    ");
            printf("|---------------   0——退出程序  ----------------| 
    ");
            printf("|----------------------------------------------| 
    ");
            printf(" 请输入您的选择 : ");
            scanf("%d",&choice);
            printf("----------------------------
    ");
            switch(choice){
                case 0:break;
                case 1:{
                    count = 0;
                    head1 = Create_Poly();
                    head2 = Create_Poly();
                    system("pause");
                    break;
                }
                case 2:{
                    if(head1==NULL){
                        count=0;
                        printf("A多项式不存在 
    ");
                        head1= Create_Poly();
                    }
                    if(head2==NULL){
                        count=1;
                        printf("B多项式不存在 
    ");
                        head2= Create_Poly();
                    }
                    count=2;
                    printf(" 多项式 A = ");
                    Displaypoly(head1);
                    printf(" 多项式 B = ");
                    Displaypoly(head2);
                    Add_Sub(head1,head2);
                    system("pause");
                    break;
                }
                case 3:{
                    if(head1==NULL){
                        count=0;
                        printf("A多项式不存在 
    ");
                        head1= Create_Poly();
                    }
                    if(head2==NULL){
                        count=1;
                        printf("A多项式不存在 
    ");
                        head2= Create_Poly();
                    }
                    count=3;
                    printf(" 多项式 A = ");
    				Displaypoly(head1);
    				printf(" 多项式 B = ");
    				Displaypoly(head2);
    				Add_Sub(head1,head2); 
    				system("pause"); 
    				break;
                }
                case 4:{
                    if(head1==NULL){
                        count=0;
                        printf("A多项式不存在 
    ");
                        head1= Create_Poly();
                    }
                    if(head2==NULL){
                        count=1;
                        printf("A多项式不存在 
    ");
                        head2= Create_Poly();
                    }
                    count=4;
                    printf(" 多项式 A = ");
    				Displaypoly(head1);
    				printf(" 多项式 B = ");
    				Displaypoly(head2);
    				Add_Sub(head2,head1); 
    				system("pause"); 
    				break;
                }
                case 5:{
                    if(head1==NULL){
                        count=0;
                        printf("A多项式不存在 
    ");
                        head1= Create_Poly();
                    }
                    if(head2==NULL){
                        count=1;
                        printf("B多项式不存在 
    ");
                        head2= Create_Poly();
                    }
                    count=5;
                    printf(" 多项式 A = ");
    				Displaypoly(head1);
    				printf(" 多项式 B = ");
    				Displaypoly(head2);
    				Mult(head1,head2); 
    				system("pause"); 
    				break;
                }
            }
           
        }while (choice);
    }
    

    其余代码见附录 码云链接

    项目构建

    GCC编译练习

    使用默认源码编译:gcc -Iinclude -c src/hello.c -o libs/hello.o

    使用默认静态库编译:gcc src/testhello.c libs/hello.o -Iinclude -o bin/hw1

    使用指定静态库编译:gcc src/testhello.c -Iinclude -Llibs -lhello -o bin/hw2

    产生静态库:ar rcs libhello.a xx.o yy.o

    产生动态库:gcc src/hello.c -Iinclude -fPIC -o libs/hello.o

    gcc文件处理四步:

    功能 范例
    预处理 gcc -E xx.c -o xx.i c->c
    编译 gcc -S xx.i -o xx.s c->asm
    汇编 gcc -c xx.s -o xxx.o asm->ade
    链接 gcc xx.o -o xx 可执行

    问题1:

    原因: 没有将结构体单独定义为一个头文件,而是放在head.h头文件周。

    解决: 单独建立struct.h头文件,生命结构体。

    问题2:

    原因: 想直接编译函数进行gcc-ESC练习,却忽略了缺少main函数,无法直接编译。

    解决: 将功能函数使用gcc -c 编译,测试时链接 main 函数。

    gcc-ESC练习:

    静态库制作

    动态库制作

    调试练习

    使用cgbd对功能函数进行调试:

    函数断点,行断点,条件断点,临时断点

    makefile的编写与执行

    问题3:

    原因:"未使用链接器输入文件,因为链接尚未完成"的意思是:gcc说:你让我链我也不链,因为还没编译完。

    解决:使用-o选项链接其他的.o文件。

    程序测试

    参考资料&附录

    未定义引用main编译错误:https://blog.csdn.net/weixin_44023406/article/details/103486416

    码云链接:https://gitee.com/DKY2019/xxaqxt/commit/bf62efa81aa0dfdf921cd1156c1ef23701b44e8d





    20191331lyx
    2021/9/25

  • 相关阅读:
    2. Redis哨兵、复制、集群的设计原理与区别
    1. 详解Redis的存储类型、集群架构、以及应用场景
    博客园主题优化
    【Java基础】Java面试题精选
    【Java集合】——集合类分析总结
    新零售供应链的三大闭环
    Comparable和Comparator比较实现排序 场景分析
    Java基础-枚举类
    Java基础-泛型
    微服务架构~BFF和网关
  • 原文地址:https://www.cnblogs.com/DKYcaiji/p/15334405.html
Copyright © 2011-2022 走看看