The environment of C++ Programing in Linux

1. 熟悉Linux、vi及g++编程环境;

2. 编制简单C++程序并运行;

3. 熟悉C++的编辑、编译、连接和运行过程;

4. 能够使用C++标准输入输出流进行键盘输入和屏幕输出。

1. 个人目录规划与vi编辑器常用命令:

(1)登录linux系统后,在个人系统目录下建立CPP-Lab目录,在此目录中依次建立各个实验目录。

(2)练习vi常用命令,掌握基本用法

2. 使用vi建立一个标准C++程序,并通过g++编译、运行以下程序:

具体实验步骤:

(1)启动vi编辑器

在Linux命令行输入vi hello.cpp,打开vi编辑器。

(2)进入插入模式

按i进入插入模式,输入以下程序代码。

(3)退出插入模式

程序编辑完成后,按esc退出插入模式,再按“:”,进入末行模式。

(4)保存并返回linux命令行

在“:”提示符后面,输入wq即可保存并返回linux命令行。

(5)程序编译

在linux命令行输入g++编译命令进行。

在linux命令行输入g++编译命令编译程序

(6)运行程序

在linux命令行输入./hello运行程序。

程序编译及运行结果

3. 编写一个C++程序,计算两个整数的最大值,要求用主函数和Max函数实现并份文件书写。

4. 编写C++程序,输入两个整型变量,用函数实现两个值的交换。

5. 让计算机随机给出一个100以内的正整数让人猜,如果猜对了,计算机给出正确提示并给出猜测的总次数,然后退出游戏。否则给出错误提示并给出猜的数是大了还是小了。游戏最多猜10次,如果10次之内仍未猜中,则提示失败并退出游戏。

#include<iostream>
#include<time.h>
using namespace std;

int main()
{
	int num,x,i=0;
	time_t t;
	srand(time(&t));
	num=rand()%100;
	cout<<"guess my number!\nyou have 10 times.\n";
	while(cin>>x)
		{
			i++;

			if(x==num)
				{
				cout<<"Good!you guess the number."<<endl;
				break;
				}
			else if(x>num)
				{
				cout<<"your number is too big!\n";
				}
			else if(x<num)
				{
				cout<<"your number is too small\n";
				}
		    if(i==10)
				{
				cout<<"YOU FAIL THE GAME!!!\n";
				break;
				}		

		}

		cout<<"you guess times:"<<i<<endl;
		return 0;
}
结果:
1-3运行结果:
[[email protected] ~]# cd /c
[[email protected] c]# g++ 11.cpp 12.cpp -o 1
[[email protected] c]# ./1
please input a and b
2
3
max = 3
[[email protected] c]#

猜数:
[[email protected] ~]# cd /CPP-Lab/shiyan1/
[[email protected] shiyan1]# ls
1      1.cpp~  2.cpp~  3.cpp~  4.cpp   5.cpp   hello      hello.cpp~
1.cpp  2.cpp   3.cpp   4.c~    4.cpp~  5.cpp~  hello.cpp
[[email protected] shiyan1]# g++ 5.cpp -o 2
[[email protected] shiyan1]# ./2
guess my number!
you have 10 times.
4
your number is too small
78
your number is too big!
45
your number is too big!
34
your number is too big!
23
your number is too big!
18
your number is too small
20
Good!you guess the number.
you guess times:7
[[email protected] shiyan1]#

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 11:53:05

The environment of C++ Programing in Linux的相关文章

ubuntu14.04 android develop environment setup for android and linux driver

//For android and linux driver// 1,install ubuntu14.04 usb boot, CD boot, vmware workstation install, etc take usb boot for an example, download Universal-USB-Installer-1.9.5.6.exe and ubuntu-14.04.2-desktop-amd64.iso at windows xp platform. i take m

Install Oracle Linux 7 on Oracle VirtualBox

I'll start coding with JEE soon. Product environment adopts Oracle + WebLogic in Linux, technology adopts EJB3 and JPA. So I'm going to set a development environment in my own computer with them. For iMac installing JDK, Eclipse or Maven will not be

【深入理解Linux内核】《第一章 绪论》笔记

1.商用Unix操作系统包括: - AT&T公司开发的(System V Release 4) SVR4. - 加州伯克利分校发布的4.4BSD - Dec公司(现属于HP)的Digital Unix - IBM公司的AIX - HP公司的HP-UX - Sun公司的Solaris   - Apple公司的Mac OS X 所有商业版本都是SVR4或4.4BSD的变体,并且都趋向于遵循某些通用标准:如IEEE的POSIX(Portable Operating Systems based on U

推荐几个学习嵌入式及Linux比较好的网站或博客

Uboot下载:http://ftp.denx.de/pub/u-boot/ 鸟哥的linux私房菜 :http://vbird.dic.ksu.edu.tw/ 本自由软件网络应用团队是一群由鸟哥历届学生所组成的团队,实际运作则是以当前的大三大四学生为主要,由学长带学弟的方式处理各项事务,主要事务有以下:全系计算机教室维护与定期检查:虚拟计算机教室建置:系上计算机教室服务器建置与维护更新. 网人社区:http://www.netren.org/index.php/home.html 网人社区,是

HOWTO install Oracle 11g on Ubuntu Linux 12.04 (Precise Pangolin) 64bits

安装了Ubuntu 12.04 64bit, 想在上面安装Oracle 11gr2,网上找了好多文档都没成功,最后完全参考了MordicusEtCubitus的文章. 成功安装的关键点:install additional packages, 在link过程中处理错误的方法. 原文:http://www.makina-corpus.org/blog/howto-install-oracle-11g-ubuntu-linux-1204-precise-pangolin-64bits Before

Java获取系统环境变量(System Environment Variable)和系统属性(System Properties)以及启动http://m.jb51.net/article/83454.htm参数的方法

系统环境变量(System Environment Variable): 在Linux下使用export $ENV=123指定的值.获取的方式如下: Map<String,String> map = System.getenv(); Set<Map.Entry<String,String>> entries = map.entrySet(); for (Map.Entry<String, String> entry : entries) { System.o

在Linux中使用VS Code编译调试C++项目

最近项目需求,需要在Linux下开发C++相关项目,经过一番摸索,简单总结了一下如何通过VS Code进行编译调试的一些注意事项. 关于VS Code在Linux下的安装这里就不提了,不管是CentOS还是Ubuntu,如果不懂且搜Q足够的情况下,你会解决的. 一. 前置知识——gcc/g++的编译链接过程 在Windows下,如果你用Visual Studio进行开发,C/C++的编译器一般采用微软提供的MSBuild:在Linux下C/C++的编译器大多采用gcc/g++.既然要在Linux

IDE:集成开发环境(Integrated Development Environment)

IDE:集成开发环境(Integrated Development Environment) VIM:经典的Linux文本编译器 Emacs:Linux文本编译器,比vim更容易使用 Eclipse:Java IDE,支持python,c,c++ Visual Studio:Windows开发的IDE,支持Java,python,c,c#, notepad++: sublime: python开发的 Pycharm:主要用于python开发的IDE 原文地址:https://www.cnblog

whatweb.rb 未完待续

#!/usr/bin/env ruby #表示ruby的执行环境 =begin # ruby中用=begin来表示注释的开始 .$$$ $. .$$$ $. $$$$ $$. .$$$ $$$ .$$$$$$. .$$$$$$$$$$. $$$$ $$. .$$$$$$$. .$$$$$$. $ $$ $$$ $ $$ $$$ $ $$$$$$. $$$$$ $$$$$$ $ $$ $$$ $ $$ $$ $ $$$$$$. $ `$ $$$ $ `$ $$$ $ `$ $$$ $$' $ `$