C++简单c 编程整数四则运算。输入两个整数,交换两个整数并输出。例如输入 1 2 输出 2 1。要用到指针

指针变量的应用:输入两个整数,按从大到小输出 用指针变量)_c++编程-织梦者
当前位置:&>&&>& > 指针变量的应用:输入两个整数,按从大到小输出 用指针变量)
指针变量的应用:输入两个整数,按从大到小输出 用指针变量)
本文将为大家说明指针变量的应用:输入两个整数,按从大到小输出 用指针变量)的相关介绍,具体实例请看下文没学指针之前如何操作?
用一个临时变量进行交换
#include &iostream&
void sort(int x,int y){
cout&&x&&"
//指向整型的指针
int main(){
cout&&"请输入两个整数。"&&
cin&&a&&b;
sort(a,b);
学习指针的方法之后,如何操作?
#include &iostream&
//指向整型的指针
int main(){
int a,b,*p1,*p2,*p;
cout&&"请输入两个整数。"&&
cin&&a&&b;
cout&&"a="&&a&&" b="&&b&&
cout&&"max="&&*p1&&" min="&&*p2&&
把指针作为函数参数的方法处理从大到小排序问题。
#include &iostream&
void swap(int *p1,int *p2){
//指向整型的指针
int main(){
int a,b,*point1,*point2;
cout&&"请输入两个整数。"&&
cin&&a&&b;
point1=&a;b
point2=&b;
swap(point1,point2);
cout&&"max="&&a&&"
min="&&b&&
这些内容可能对你也有帮助
更多可查看c++编程列表页。
猜您也会喜欢这些文章您所在位置: &
&nbsp&&nbsp&nbsp&&nbsp
简单程序设计(C++版)解析.ppt 98页
本文档一共被下载:
次 ,您可全文免费在线阅读后下载本文档。
下载提示
1.本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。
2.该文档所得收入(下载+内容+预览三)归上传者、原创者。
3.登录后可充值,立即自动返金币,充值渠道很便利
需要金币:350 &&
你可能关注的文档:
··········
··········
【上机练习】
6.计算三角形面积【1.3编程基础之算术表达式与顺序执行18】
平面上有一个三角形,它的三个顶点坐标分别为(x1, y1), (x2, y2), (x3, y3),那么请问这个三角形的面积是多少,精确到小数点后两位。 输入:
输入仅一行,包括6个单精度浮点数,分别对应x1, y1, x2, y2, x3, y3。 输出:
输出也是一行,输出三角形的面积,精确到小数点后两位。 样例输入:
0 0 4 0 0 3 样例输出:
6.00 【上机练习】
7.A*B问题【1.3编程基础之算术表达式与顺序执行19】
输入两个正整数A和B,求A*B的值。注意乘积的范围和数据类型的选择。 输入:
一行,包含两个正整数A和B,中间用单个空格隔开。1 &= A,B &= 50000。 输出:
一个整数,即A*B的值。 样例输入:
3 4 样例输出:
12 【上机练习】
8.计算2的幂【1.3编程基础之算术表达式与顺序执行20】
给定非负整数n,求2^n的值,即2的n次方。 输入:
一个整数n。0 &= n & 31。 输出:
一个整数,即2的n次方。 样例输入:
3 样例输出:
8 【上机练习】
9.苹果和虫子【1.3编程基础之算术表达式与顺序执行15】
你买了一箱n个苹果,很不幸的是买完时箱子里混进了一条虫子。虫子每x小时能吃掉一个苹果,假设虫子在吃完一个苹果之前不会吃另一个,那么经过y小时你还有多少个完整的苹果? 输入:
输入仅一行,包括n,x和y(均为整数)。 输出:
输出也仅一行,剩下的苹果个数 样例输入:
10 4 9 样例输出:
7 【上机练习】
10.求三角形面积【1.3编程基础之算术表达式与顺序执行17】
给定三条线段的长度,判断这三条线段是否能够构成三角形。如果能够构成,则计算其面积。 输入:
输入只有一行,包含三个浮点数,分别表示三角形三边的边长,数与数之间以一个空格分开。 输出:
对于可以构成三角形的情况,输出三角形的面积,保留4位小数。
对于不能构成三角形的情况, 输出“Data Error”。 样例输入:
3 4 5 样例输出:
3.换行符的使用
必须注意,除非我们明确指定,cout并不会自动在其输出内容的末尾加换行符,因此下面的语句:   例2.12
cout && &This is a sentence.& ;
cout && &This is another sentence.& ;   将会有如下内容输出到屏幕:     This is a sentence.This is another sentence.
虽然我们分别调用了两次cout,两个句子还是被输出在同一行。所以,为了在输出中换行,我们必须插入一个换行符来明确表达这一要求,在C++中换行符可以写作\n。
cout && &First sentence.\n&;
cout && &Second sentence.\nThird sentence.&;
 将会产生如下输出:
First sentence.
Second sentence.
Third sentence.
另外,你也可以用操作符endl来换行,例如:
cout && &First sentence.& &&
cout && &Second sentence.& &&
将会输出:
First sentence.
Second sentence.
例2.13 在屏幕上输出 2
3 4 cout&&2&&&
&&&3&& cout&&4; 或 cout&&&2
3\n4&; 除了以上两种写法外,还可以有其它的的写法,请试试看。你可以使用\n或endl来指定cout输出换行,注意两者的不同用法。
四、通过cin流读入数据   流读取运算符&&和cin结合在一起使用,可从键盘输入数据。 格式1:   功能:是从键盘读取一个数据并将其赋给“变量”。   说明:在使用
正在加载中,请稍后...#include&stack&&&
// A program to implement a calculator accepting parentheses
// For stream input/output
// For the exit() function
// For the isdigit() function
// For the strcpy() function
using std::
using std::
using std::
using std::
void eatspaces(char* str);
// Function to eliminate blanks
double expr(char* str);
// Function evaluating an expression
double term(const char* str, size_t& index);
// Function analyzing a term
double number(const char* str, size_t& index);
// Function to recognize a number
char* extract(const char* str, size_t& index);
// Function to extract a substring
const size_t MAX{ 80 };
// Maximum expression length, including '\0'
int main()
char buffer[MAX] {};
// Input area for expression to be evaluated
cout && endl
&& &Welcome to your friendly calculator.&
&& &Enter an expression, or an empty line to quit.&
cin.getline(buffer, sizeof buffer);
// Read an input line
eatspaces(buffer);
// Remove blanks from input
if (!buffer[0])
// Empty line ends calculator
cout && &\t= & && expr(buffer)
// Output value of expression
&& endl &&
catch (const char* pEx)
cerr && pEx &&
cerr && &Ending program.& &&
// Function to eliminate spaces from a string
void eatspaces(char* str)
size_t i{};
// 'Copy to' index to string
size_t j{};
// 'Copy from' index to string
while ((*(str + i) = *(str + j++)) != '\0')
// Loop while character
// copied is not \0
if (*(str + i) != ' ')
// Increment i as long as
// character is not a space
// Function to evaluate an arithmetic expression
double expr(char* str)
double value{};
// Store result here
size_t index{};
// Keeps track of current character position
value = term(str, index);
// Get first term
// Indefinite loop, all exits inside
switch (*(str + index++))
// Choose action based on current character
case '\0':
// We're at the end of the string
// so return what we have got
// + found so add in the
value += term(str, index);
// next term
// - found so subtract
value -= term(str, index);
// the next term
// If we reach here the string
char message[38] {&Expression evaluation error. Found: &};
strcat(message, str + index - 1);
// Append the character
// Function to get the value of a term
double term(const char* str, size_t& index)
double value{};
// Somewhere to accumulate
// the result
value = number(str, index);
// Get the first number in the term
// Loop as long as we have a good operator
while (true)
if (*(str + index) == '*')
// If it's multiply,
value *= number(str, ++index);
// multiply by next number
else if (*(str + index) == '/')
// If it's divide,
value /= number(str, ++index);
// divide by next number
// We've finished, so return what
// we've got
// Function to recognize a number in a string
double number(const char* str, size_t& index)
double value{};
// Store the resulting value
if (*(str + index) == '(')
// Start of parentheses
char* psubstr{};
// Pointer for substring
psubstr = extract(str, ++index);
// Extract substring in brackets
value = expr(psubstr);
// Get the value of the substring
// Clean up the free store
// Return substring value
// There must be at least one digit...
if (!isdigit(*(str + index)))
{ // There's no digits so input is junk...
char message[31] {&Invalid character in number: &};
strcat(message, str + index);
// Append the character
while (isdigit(*(str + index)))
// Loop accumulating leading digits
value = 10 * value + (*(str + index++) - '0');
// Not a digit when we get to here
if (*(str + index) != '.')
// so check for decimal point
// and if not, return value
double factor{ 1.0 };
// Factor for decimal places
while (isdigit(*(str + (++index))))
// Loop as long as we have digits
factor *= 0.1;
// Decrease factor by factor of 10
value = value + (*(str + index) - '0')*
// Add decimal place
// On loop exit we are done
// Function to extract a substring between parentheses
// (requires cstring)
char* extract(const char* str, size_t& index)
char* pstr{};
// Pointer to new string for return
int numL{};
// Count of left parentheses found
size_t bufindex{ index };
// Save starting value for index
switch (*(str + index))
if (0 == numL)
pstr = new char[index - bufindex];
if (!pstr)
throw &Memory allocation failed.&;
strcpy(pstr, str + bufindex); // Copy substring to new memory
// Return substring in new memory
// Reduce count of '(' to be matched
// Increase count of '(' to be
// matched
} while (*(str + index++) != '\0');
// Loop - don't overrun end of string
throw &Ran off the end of the expression, must be bad input.&;
这道题你会答吗?花几分钟告诉大家答案吧!
在距离适量路由选择协议中,水平...
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题
浙ICP备号-2
扫一扫,把题目装进口袋相关文章推荐
int main()
int max(int a,int b,int c);
int a=8,b=-12,c=27;
?解题思路:
(1)函数名应是见名知意,今定名为max
(2)由于给定的两个数是整数,返回主调函数的值(即较大数)应该是整型
(3)max函数应当有两个参数,以便从主函数接收两个整数,因此参数的类型应...
SeqList.h//顺序表的实现
#include "stdio.h"
#include "math.h"
//#define MaxSize 10
//typedef int DataTy...
常用的图论最短路算法详解(dijkstra/SPFA/floyd)
用java实现分数各种运算(加减乘除,求余,求幂,求两个数中的较大值,较小值)
用户以分数形式输入,以分数形式输出,用java语言实现分数中的各种运算(加减乘除,求余,求幂,求两个...
#include//0和任意的一个数的最大公约数就是那个数(最小公倍数就是0)
int yue(int x,int y){
另解,c/c++笔试题:不用判断语句求得两个整数的最大值
因为涉及main函数参数,所以在linux 下面比较容易实现int main(int argc, char *argv[], char *env[])main的三个参数 :
int argc,表示命令...
(1)第一种实现方法#include
int Add(int a, int b)
return a+b;
int Sub(int a, i...
他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)}

我要回帖

更多关于 c 简单编程 输入梯形 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信