c++经典编程题_【经典C语言知识】C/C++编程难点总结

知识点一:指针

1.  指针:变量在内存中所存储空间的首编号,就称作为该变量的地址,也叫做指针。

指针变量: 他专门存放另外一个变量的指针     

int*    p_age;

p_age=&age;

2.数组与指针

使用指针访问一维数组元素:如果指针变量p_score已经指向数组中的一个元素,则p_score+1表示指向同一数组中的下一个元素。

#include

using namespace std;

int 

main()

{

int score[5];

int *p_score;

int sum = 0;

for (int i = 0; i < 5; i++)

{

cin >> score[i];

}

p_score = score;

for (int i = 0; i < 5; i++)

{

sum += *(p_score + i);

}

cout << sum << endl;

system("pause");

return 0;

}

二维数组的指针

#include

using namespace std;

int main()

{

int score[2][3];

int* p;

int sum = 0;

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 3; j++)

{

cin >> score[i][j];

}

}

p = score[0];     //二维数组中,不能够是p=score

for (; p < score[0] + 6; p++)

{

sum += *p;   //对输入的6个值进行求和

}

cout << sum << endl;

system("pause");

return 0;

}

指针和函数

使用指针作为函数的参数

#include

using namespace std;

void swap(int* 

data1, int* data2)

{

int tmp;

tmp = *data1;

*data1 = *data2;

*data2 = tmp;

}

int main()

{

int a = 10, b = 20;

swap(&a,&b);

cout << a << " " << b << endl;

system("pause");

return 0;

}

使用数组名作为函数的参数

#include

using namespace std;

int average(int* arr, int num)

{

int sum = 0;

for (int i = 0; i < num; i++)

{

sum = sum + arr[i];

}

return (sum / num);

}

int main()

{

int 

score[5] = {60,50,70,80,90};

cout << average(score, 5) << endl;

system("pause");

return 0;

}

知识点二:使用new运算符动态开辟空间

C++中允许在程序中动态开辟空间,即用多少开辟多少运算符new:

在堆内存区中进行内存的动态分配

例如:

double* d;

d=new double;

*d=30.5;或者 double* d;

d=new double(30.5);

使用数组动态开辟空间

动态数组:指针变量=new[整形表达式]

例子:

#include

using namespace std;

int main()

{

int n;

int* p;

int sum=0;

cout << "请输入班级的人数";

cin >> n;

p = new int[n];

if (!p)

{

return 1;

}

cout << "请输入数学的成绩";

for (int i = 0; i < n; i++)

{

cin >> p[i];

}

for (int i = 0; i < n; i++)

{

sum += p[i];

}

cout << "平均值为" << sum / n << endl;

system("pause");

delete[n]p;

 p = NULL;

return 0;

}

知识点三:使用delete动态释放空间delete d;

d=NULL; //一个好的习惯

知识点四:结构体

struct 结构体名

{

 类型 标识符 成员名;

 类型标识符 成员名;

......

};

#include

using namespace std;

struct goods {

char  name[15];

float price;

int amount;

float total;

};

int main()

{

struct goods 

myGoods;

strcpy(myGoods.name, "连衣裙");

myGoods.price = 50;

myGoods.amount = 2;

myGoods.total = myGoods.price*myGoods.amount;

cout << "宝贝名称:" << myGoods.name << "单价:" <

myGoods.price << "数量:  " << myGoods.amount << "总价:" << myGoods.total << endl;

system("pause");

return 0;

}

知识点四:共用体

概念:使几个不同的变量共占同一段内存的结构称为“共用体

”,也叫做“联合体”。

union 共用体名

{

int  i;

char ch;

float f; 

}; //这个共用体会 

开辟四个空间

union data a b;  

例子:

#include

using namespace std;

union category

{

int c1;

char position[10];

};

struct person

{

char name[10];

int num;

char job;

union category cat;

};

int main()

{

struct person stu;

struct person tea;

strcpy(stu.name, "zhang");

stu.num = 501;

stu.job = 's';

stu.cat.c1 = 5001;

strcpy(tea.name, "li");

tea.num = 200;

tea.job = 't';

strcpy(tea.cat.position, "prof");

cout << "学生的情况: " << stu.name << "," << stu.num <

"," << stu.cat.c1 << endl;

cout << "老师的情况:" << tea.name << "," << tea.num << "," <

tea.cat.position << endl;

 system("pause");

return 0;

}

总结:同一个内存段可以用来存放几种不同类型的成员,但是在每一个瞬间只能够存放其中一种,而不是同时存放几种;

共用体变量中起作用的成员是最后一次存放的成员,在存入新的一个成员后原有的成员的作用就会失去作用;共用体变量的地址和它的各成员的地址都是同一地址。不能对共用体变量名赋值,也不能企图引用变量名得到一个值,又不能在定义共用体变量时候对它初始化。

我们官方的QQ群:281549832

特别感谢网友的大力支持。

我们的开源团队不断扩大,希望大家快来一起加入我们吧。

在这里还是要谢谢大家的大力支持!

大家快来关注吧!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442100.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

asp.net mvc 缓存CaChe使用

缓存存储帮助类 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Caching;public class TCatche{/// <summary> /// 获取数据缓存 /// </summary> /// <param name"cacheKey">键<…

【CF#459 A 】Pashmak and Garden (水题)

题干:Pashmak has fallen in love with an attractive girl called Parmida since one year ago... Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden l…

c++ 低位在前 高位在后_A股市场:如果股票涨停后第二天“高开低走”,你知道怎么操作才能利益最大化吗?...

(本文由公众号越声策略(yslc188)整理&#xff0c;仅供参考&#xff0c;不构成操作建议。如自行操作&#xff0c;注意仓位控制和风险自负。)如果你的股票涨停后第二天高开低走&#xff0c; 后市怎么操作&#xff1f;简单来讲&#xff0c;高开低走就是开盘价高于上个交易日的收盘…

2018年工业机器人销量排位_工业机器人年销量连续七年居世界首位 专家建议开辟新市场...

12月17日&#xff0c;粤港澳大湾区人工智能与机器人大会在广东四会举行。本次大会的主题是“智慧湾区&#xff0c;引领未来”。大会邀请了国际机器人联合会、粤港澳大湾区相关专家、国内外学者和企业专家深度交流&#xff0c;共话机器人发展的新风向。中国机械工业联合会执行副…

*【HDU - 2473】Junk-Mail Filter (并查集--删点操作)

题干&#xff1a; Recognizing junk mails is a tough task. The method used here consists of two steps: 1) Extract the common characteristics from the incoming email. 2) Use a filter matching the set of common characteristics extracted to determine whether…

文件内容查找方式

第一种&#xff0c;使用windows自带的查找工具 搜索工具里面有”高级选项“&#xff0c;选择”文件内容“然后进行搜索即可 第二种&#xff0c;使用命令行 在需要进行搜索的文件夹下使用命令行&#xff1a; Get-ChildItem -Path F:\ -Recurse | Select-String -Pattern &qu…

【HDU - 1863】 畅通工程(并查集+最小生成树)

题干&#xff1a; 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通&#xff08;但不一定有直接的公路相连&#xff0c;只要能间接通过公路可达即可&#xff09;。经过调查评估&#xff0c;得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编…

C# 删除文件夹下的文件

/// <summary>/// 删除某个时间之前的所有文件/// </summary>/// <param name"aUrl"></param>/// <param name"aTime"></param>public static void RemoveFile(string aUrl,DateTime aTime) {var bFiles Directory.G…

【POJ - 1287】 Networking (并查集 + 最小生成树)

题干&#xff1a; You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between…

C# 程序xml文件生成和读写操作

public class XmlFile{private string fileFolder;private string fileName;private string fPathBase Directory.GetCurrentDirectory(); //结尾没有反斜杠public TimeConfig(string aFileFolder "xmlConfig", string aFileName "config"){fileFolder…

【HDU - 1301】Jungle Roads(并查集+最小生成树)(内附最小生成树两种算法 克鲁斯特尔算法amp;amp;普里姆算法)

题干&#xff1a; Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5505 Accepted Submission(s): 3976 Problem Description The Head Elder of the tropical island of Lagrishan has a proble…

vscode中怎样格式化js代码_[VSCode插件推荐] Bracket Pair Colorizer: 为代码中的括号添上一抹亮色...

在代码编写过程中&#xff0c;各种括号 {[()]} 必不可少。然而&#xff0c;随着代码量的增加&#xff0c;你有没有因为括号的嵌套太多&#xff0c;而导致代码难以阅读&#xff1f;我们来看看下面的代码&#xff0c;在第三行代码的最后部分&#xff0c;连续出现了5个右括号&…

Asp.net MVC 从ftp服务器读取文件保存到网站本地

1 FTP连接辅助类 /// <summary>/// FTP操作单元/// </summary>public class TFTPOperationUnit{/// <summary>/// ftp地址/// </summary>private string FFTPUrl;/// <summary>/// ftp用户名/// </summary>string FFTPUserName;/// <s…

多帧点云数据拼接合并_自动驾驶:Lidar 3D传感器点云数据和2D图像数据的融合标注...

自动驾驶汽车的发展已经见证了硬件传感器记录感官数据的容量和准确度的发展。传感器的数量增加了&#xff0c;新一代传感器正在记录更高的分辨率和更准确的测量结果。 在本文中&#xff0c;我们将探讨传感器融合如何在涉及环环相扣的数据标记过程中实现更高程度的自动化。所有自…

*【HDU - 1506】【POJ - 2559】Largest Rectangle in a Histogram(单调栈或动态规划)

题干&#xff1a; Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consist…

pdf转图片记录

1 winform将pdf转图片&#xff0c;有案例&#xff0c;连接为&#xff1a;https://download.csdn.net/download/mingjing941018/20216747 2 Asp.net MVC将pdf转图片 使用Nuget安装包安装Freespire.pdf&#xff0c;控制器中相关代码&#xff1a; /// <summary>/// 将本地…

【基础知识】大数据组件HBase简述

HBase是一个开源的、面向列&#xff08;Column-Oriented&#xff09;、适合存储海量非结构化数据或半结构化数据的、具备高可靠性、高性能、可灵活扩展伸缩的、支持实时数据读写的分布式存储系统。 只是面向列&#xff0c;不是列式存储 mysql vs hbase vs clickhouse HMaster …

改变定时器获取传感器频度_广东梅州梅县压力传感器*校对

广东梅州梅县压力传感器*校对看门狗寄存器不会改变或改变不大&#xff0c;如果看门狗寄存器发生了改变或改变很大&#xff0c;则说明系统陷入“死循环”.需要进行出错处理。在工业应用中&#xff0c;严重的干扰有时会破坏中断方式控制字&#xff0c;关闭中断&#xff0c;造成看…

**【POJ - 2389】 Bull Math (高精度乘法)

题干&#xff1a; Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls answers. Read in two …

nodeType的类型

1&#xff1a;元素节点   2&#xff1a;属性节点   3&#xff1a;文本节点   4&#xff1a;CDATA区段   5&#xff1a;实体应用元素   6&#xff1a;实体   7&#xff1a;表示处理指令   8&#xff1a;注释节点   9&#xff1a;最外层的Root element,包括所有其…