liunx 下编写c++程序

问题描述:

  • 2个cpp文件:prog.cpp, aux.cpp
  • 1个h文件:aux.h
  • aux.h头文件定义函数Max和Min,它们分别计算四个数(参数)的最大值和最小值;aux.cpp实现这两个函数
  • prog.c中定义主函数,循环输入4个数,输出他们的最大和最小值
  • 编译该项目,调试、跟踪程序执行过程;并在控制台界面运行编译的可执行文件

 

代码实现:

  • aux.cpp
/*
 * Get the maximum of four numbers
 */
int Max(int arry[]);
/*
 * Get the maximum of four numbers
 */
int Min(int arry[]);
  • aux.h

/*
 * BubbleSort
 */
void BubbleSort(int a[], int n)
{
    int i, j, temp;
    for (j = 0; j < n - 1; j++)
        for (i = 0; i < n - 1 - j; i++)
        {
            if(a[i] > a[i + 1])
            {
                temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
            }
        }
}
int Max(int arry[4]){
    BubbleSort(arry,4);
    return arry[3];
}
int Min(int arry[4]){
    BubbleSort(arry,4);
    return arry[0];
}
  • prog.cpp

/*
 * BubbleSort
 */
void BubbleSort(int a[], int n)
{
    int i, j, temp;
    for (j = 0; j < n - 1; j++)
        for (i = 0; i < n - 1 - j; i++)
        {
            if(a[i] > a[i + 1])
            {
                temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
            }
        }
}
int Max(int arry[4]){
    BubbleSort(arry,4);
    return arry[3];
}
int Min(int arry[4]){
    BubbleSort(arry,4);
    return arry[0];
}

编译调试:

  • 在终端进行如下操作
cedar@cedar:~/CLionProjects/Homework_5$ ll
总用量 32
-rw-rw-r-- 1 cedar 531 10月 17 22:30 aux.cpp
-rw-rw-r-- 1 cedar 168 10月 17 22:35 aux.h
-rw-rw-r-- 1 cedar 322 10月 17 22:41 prog.cpp

cedar@cedar:~/CLionProjects/Homework_5$ g++ -g -c aux.cpp
cedar@cedar:~/CLionProjects/Homework_5$ g++ -g -c prog.cpp
cedar@cedar:~/CLionProjects/Homework_5$ ll
总用量 40
-rw-rw-r-- 1 cedar  531 10月 17 22:37 aux.cpp
-rw-rw-r-- 1 cedar  168 10月 17 22:40 aux.h
-rw-rw-r-- 1 cedar 1848 10月 17 22:52 aux.o
drwxrwxr-x 3 cedar 4096 10月 17 22:41 cmake-build-debug/
-rw-rw-r-- 1 cedar  168 10月 16 17:14 CMakeLists.txt
drwxrwxr-x 2 cedar 4096 10月 17 22:45 .idea/
-rw-rw-r-- 1 cedar  322 10月 17 22:41 prog.cpp
-rw-rw-r-- 1 cedar 3352 10月 17 22:52 prog.o
g++ -Lcrypto -lpthread -o "homework_5" aux.o prog.o
cedar@cedar:~/CLionProjects/Homework_5$ ./homework_5
34 12 95 24
The Max is :95
The Min is :12
cedar@cedar:~/CLionProjects/Homework_5$ gdb ./homework_5 
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./homework_5...done.
cedar@cedar:~/CLionProjects/Homework_5$ gdb ./homework_5 
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./homework_5...done.
(gdb) l
1	/**
2	 * Created by cedar on 17-10-16.
3	 */
4	
5	#include <iostream>
6	#include "aux.h"
7	using namespace std;
8	
9	int main() {
10	    //int arr[4]={34,12,95,24};
(gdb) 
11	    int arr[4];
12	    for (int i = 0; i < 4; ++i) {
13	        cin>>arr[i];
14	    }
15	    cout<<"The Max is :"<<Max(arr)<<endl;
16	    cout<<"The Min is :"<<Min(arr)<<endl;
17	    return 0;
18	}(gdb) break 12
Breakpoint 1 at 0x400ae2: file prog.cpp, line 12.
(gdb) break 15
Breakpoint 2 at 0x400b13: file prog.cpp, line 15.
(gdb) r
Starting program: /home/cedar/CLionProjects/Homework_5/homework_5 
Breakpoint 1, main () at prog.cpp:12
12	    for (int i = 0; i < 4; ++i) {
(gdb) next
13	        cin>>arr[i];
(gdb) next
34
12	    for (int i = 0; i < 4; ++i) {
(gdb) c
Continuing.
12
95
24
Breakpoint 2, main () at prog.cpp:15
15	    cout<<"The Max is :"<<Max(arr)<<endl;

 

 

 

 

转载于:https://my.oschina.net/dwqdwd/blog/1555746

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值