c++经典编程题_【经典C语言知识】C/C++编程难点总结
知識(shí)點(diǎn)一:指針
1. ?指針:變量在內(nèi)存中所存儲(chǔ)空間的首編號(hào),就稱作為該變量的地址,也叫做指針。
指針變量: 他專門存放另外一個(gè)變量的指針? ? ?
int*? ? p_age;
p_age=&age;
2.數(shù)組與指針
使用指針訪問一維數(shù)組元素:如果指針變量p_score已經(jīng)指向數(shù)組中的一個(gè)元素,則p_score+1表示指向同一數(shù)組中的下一個(gè)元素。
#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;
}
二維數(shù)組的指針
#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];? ? ?//二維數(shù)組中,不能夠是p=score
for (; p < score[0] + 6; p++)
{
sum += *p;? ?//對(duì)輸入的6個(gè)值進(jìn)行求和
}
cout << sum << endl;
system("pause");
return 0;
}
指針和函數(shù)
使用指針作為函數(shù)的參數(shù)
#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;
}
使用數(shù)組名作為函數(shù)的參數(shù)
#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;
}
知識(shí)點(diǎn)二:使用new運(yùn)算符動(dòng)態(tài)開辟空間
C++中允許在程序中動(dòng)態(tài)開辟空間,即用多少開辟多少運(yùn)算符new:
在堆內(nèi)存區(qū)中進(jìn)行內(nèi)存的動(dòng)態(tài)分配
例如:
double* d;
d=new double;
*d=30.5;或者 double* d;
d=new double(30.5);
使用數(shù)組動(dòng)態(tài)開辟空間
動(dòng)態(tài)數(shù)組:指針變量=new[整形表達(dá)式]
例子:
#include
using namespace std;
int main()
{
int n;
int* p;
int sum=0;
cout << "請(qǐng)輸入班級(jí)的人數(shù)";
cin >> n;
p = new int[n];
if (!p)
{
return 1;
}
cout << "請(qǐng)輸入數(shù)學(xué)的成績(jī)";
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;
}
知識(shí)點(diǎn)三:使用delete動(dòng)態(tài)釋放空間delete d;
d=NULL; //一個(gè)好的習(xí)慣
知識(shí)點(diǎn)四:結(jié)構(gòu)體
struct 結(jié)構(gòu)體名
{
?類型 標(biāo)識(shí)符 成員名;
?類型標(biāo)識(shí)符 成員名;
......
};
#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 << "單價(jià):" <
myGoods.price << "數(shù)量:? " << myGoods.amount << "總價(jià):" << myGoods.total << endl;
system("pause");
return 0;
}
知識(shí)點(diǎn)四:共用體
概念:使幾個(gè)不同的變量共占同一段內(nèi)存的結(jié)構(gòu)稱為“共用體
”,也叫做“聯(lián)合體”。
union 共用體名
{
int? i;
char ch;
float f;?
}; //這個(gè)共用體會(huì)?
開辟四個(gè)空間
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 << "學(xué)生的情況: " << stu.name << "," << stu.num <
"," << stu.cat.c1 << endl;
cout << "老師的情況:" << tea.name << "," << tea.num << "," <
tea.cat.position << endl;
?system("pause");
return 0;
}
總結(jié):同一個(gè)內(nèi)存段可以用來存放幾種不同類型的成員,但是在每一個(gè)瞬間只能夠存放其中一種,而不是同時(shí)存放幾種;
共用體變量中起作用的成員是最后一次存放的成員,在存入新的一個(gè)成員后原有的成員的作用就會(huì)失去作用;共用體變量的地址和它的各成員的地址都是同一地址。不能對(duì)共用體變量名賦值,也不能企圖引用變量名得到一個(gè)值,又不能在定義共用體變量時(shí)候?qū)λ跏蓟?/p>
我們官方的QQ群:281549832
特別感謝網(wǎng)友的大力支持。
我們的開源團(tuán)隊(duì)不斷擴(kuò)大,希望大家快來一起加入我們吧。
在這里還是要謝謝大家的大力支持!
大家快來關(guān)注吧!
總結(jié)
以上是生活随笔為你收集整理的c++经典编程题_【经典C语言知识】C/C++编程难点总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dnar.exe是什么进程 dnar进程
- 下一篇: c++ 低位在前 高位在后_A股市场:如