
C/C++
wenjiashun
这个作者很懒,什么都没留下…
展开
-
39_字符串指针
//_39_字符串指针//_39_main.cpp//将字符串a复制到字符串b#include #include #include int main(){ char a[]="I am a student."; char b[20]; char *p1,*p2; p1=a;//将数组a的首地址赋给字符型指针p1 p2=&b[0]; for(;*p1!='\0';p1+原创 2014-08-10 17:00:12 · 415 阅读 · 0 评论 -
19_一维数组
//_19_一维数组//_19_main.cpp#include #include int main(){ int array1[10]; for(int i=0;i<10;i++) { printf("array1[%d]=",i); scanf("%d",&array1[i]); } printf("the array is:\n"); for(int i=0;原创 2014-08-08 13:20:30 · 407 阅读 · 0 评论 -
18_有关循环结构的综合实例
//_18_综合实例//_18_main.cpp//有关循环结构的综合实例//任意给出一个数,判断其是否为素数,如果是,找出组成该素数的基本元素#include #include #include //判断一个数是否是素数bool judgePrimeNumber(int);//判断一个素数是否可以分解成两个素数之和bool judgeBasicElement(int);原创 2014-08-08 13:19:53 · 536 阅读 · 0 评论 -
15_do_while语句
//_15_do_while语句//_15_main.cpp//本例原题是:sin(x)=x-(x^3)/3!+(x^5)/5!-(x^7)/7!......#include #include #include int main(){ double s = 0;//多项式的值 double t;//每一项的值 int j = 1;//每一项中阶乘的值 double x;//原创 2014-08-08 13:19:41 · 332 阅读 · 0 评论 -
14_while语句
//_14_while语句//_14_main.cpp#include #include void change(int,int);int main(){ int temp,num1,num2; int x=0,y=0; printf("请输入两个正整数:"); scanf("%d %d",&num1,&num2); if(num1>num2)//令num1为较小者原创 2014-08-08 13:19:09 · 374 阅读 · 0 评论 -
17_exit()函数
//_17_exit()函数//_17_main.cpp//例题原型:求1990年各个月份的天数//exit()函数在中#include #include int main(){ int month;//定义变量存放月数 int day;//定义变量存放天数 printf("please input the month number:"); scanf("%d",&mont原创 2014-08-08 13:19:03 · 426 阅读 · 0 评论 -
13_for循环
//_13_for循环//_13_main.cpp//画一个菱形#include #include int main(){ int number = 10; //变量从0到number,表示所画的菱形第一到第number+1行 for(int i=0;i<=number;i++)//0~number行 { //第i行有number-i个空格 for(int j=0;原创 2014-08-08 13:18:19 · 333 阅读 · 0 评论 -
16_break_continue语句
//_16_break_continue语句//_16_main.cpp#include #include int main(){ int radius;//存放圆的半径 float area;//存放圆的面积 for(int i=1;i<=10;i++) { area=3.1417*i*i; radius=i; //若圆面积超过120,则跳出循环,不予输出原创 2014-08-08 13:18:17 · 371 阅读 · 0 评论 -
12_switch语句
//_12_switch语句//_12_main.cpp/*给出一个不多于6位的正整数,要求:1、求它是几位数2、分别打印每一位数字3、按逆序打印出各位数字,例如,原数为489,应输出984*/#include #include int main(){ int number;//要输入的数据 //下面定义的变量分别代表个位十位百位千位万位十万位以及位数 int ind原创 2014-08-08 13:17:38 · 501 阅读 · 0 评论 -
20_二维数组_输出魔方阵
//_20_二维数组//_20_main.cpp//输出魔方阵:各行各列以及对角线元素相加之和相等#include #include int main(){ int intArray[16][16];//因为魔方阵最大只能为15*15,故用16*16规格,[0][0]不用 int n;//魔方阵规模 int m=1;//便于利用while循环 while(m==1) {原创 2014-08-08 13:23:46 · 2364 阅读 · 0 评论 -
21_字符数组
//_21_字符数组//_21_main.cpp//一个简单的文本编辑器#include #include #define LINE 5 //行#define ROW 30 //列int main(){ //定义字符数组 char testArray[LINE][ROW]; //逐行输入数据 for(int i=0;i<LINE;i++) { printf("原创 2014-08-08 13:24:26 · 410 阅读 · 0 评论 -
29_函数的嵌套调用
//_29_函数的嵌套调用//_29_main.cpp//使用弦截法求解方程的根#include #include #include //定义函数f,从而实现x^3-8*x^2+12*x-30=0float f(float);//定义函数xpoint,求出弦与x轴的焦点横坐标float xpoint(float,float);//定义函数root,求解区间(x1,x2)的原创 2014-08-10 16:45:23 · 473 阅读 · 0 评论 -
28_函数返回值
//_28_函数返回值//_28_main.cpp#include #include //功能函数:返回在第一个参数内第二个参数的开始位置//如果在第一个参数内没有发现第二个参数,则返回-1int find_substr(char *,char *);int main(){ if(find_substr("C is fun","is")!=-1) printf("Sub原创 2014-08-10 16:44:50 · 650 阅读 · 0 评论 -
24_函数的值调用
//_24_函数的值调用//_24_main.cpp//函数的值调用不改变原来的数值#include #include int square(int);//求平方函数int cube(int);//求立方函数int main(){ int m = 12; int n = 4; printf("%d %d\n",square(m),m); printf("%d %d\原创 2014-08-10 16:42:52 · 399 阅读 · 0 评论 -
26_数组函数的调用
//_26_数组函数的调用//_26_main.cpp//本例是矩阵的转置#include #include #define N 3//转置函数声明void convert(int element[N][N]);int main(){ //定义一个二维数组 int testArray[N][N]; for(int i=0;i<N;i++) for(int j=0;j原创 2014-08-10 16:42:20 · 689 阅读 · 0 评论 -
23_数组应用
//_23_数组应用//_23_main.cpp//本程序是一个建议的学生成绩查询系统#include #include int main(){ int score[5][7];//存放学生成绩的数组 int average;//某个学生平均成绩 int sum;//总分 do{ printf("\n本程序有四项功能:\n"); printf("1.根据学号查询学原创 2014-08-10 16:42:08 · 411 阅读 · 0 评论 -
22_数组初始化
//_22_数组初始化//_22_main.cpp//重点掌握一维数组和二维数组初始化!!!!#include #include int main(){ int array1[]={1,2,3,4,5,6,7,8,9,10}; char array2[13]="how are you?"; char array3[13]={'h','o','w',' ','a','r','原创 2014-08-10 16:41:31 · 435 阅读 · 0 评论 -
25_函数的引用调用
//_25_函数的引用调用//_25_main.cpp//引用调用就是给参数取了一个别名,改变函数中引用的变量会改变原变量#include #include void swap(int *,int *);//交换函数int main(){ int i,j; i = 12; j = 36; printf("i and j bafore swapping: %d %d\n"原创 2014-08-10 16:41:14 · 679 阅读 · 0 评论 -
27_命令行变元
//_27_命令行变元//_27_main.cpp//命令行变元是操作系统命令中执行程序名字之后的信息#include #include #include #include int main(int argc,char * argv[]){ int disp,count; if(argc<2) { printf("You must enter the length原创 2014-08-08 13:29:06 · 443 阅读 · 0 评论 -
11_嵌套if语句
//_11_嵌套if语句//_11_main.cpp//本例要实现的功能是//根据给出的输血者的资料(性别,体重),程序判断出输血者对应的输血量#include #include int main(){ //sex代表输血者的性别,weight代表输血者体重,cubage代表输血量 int sex,weight,cubage; printf("请给出输血者的性别和体重:")原创 2014-08-08 13:16:43 · 454 阅读 · 0 评论 -
10_else-if语句
//_10_else-if语句//_10_main.cpp//本例原题:/*有一个分段函数,y=f(x),当x小于6时,y=x-12;当x大于等于6且小于15时,y=5x+9;当x大于等于15时,y=5x+9;*/#include #include int main(){ int x,y; printf("请输入自变量x:"); scanf("%d",&x); if原创 2014-08-08 13:15:56 · 506 阅读 · 0 评论 -
C++大学基础教程_11_10_实例研究:String类
//String.h#ifndef STRING_H_#define STRING_H_#include using namespace std;class String{ //,将重载运算符> 设为友元函数,二元运算符的重载运算符函数可以作为成员 //函数的条件是仅当左操作数是该类所在类的对象时; friend ostream &operator<<(ostream &,原创 2014-07-16 13:46:58 · 793 阅读 · 0 评论 -
C++大学基础教程_11_8_实例研究:Array类
//Array.h#ifndef ARRAY_H_#define ARRAY_H_#include using namespace std;class Array{ //友元函数---同时也是重载运算符函数 friend ostream &operator<<(ostream &,const Array &); friend istream &operator>>(istre原创 2014-07-16 08:59:34 · 449 阅读 · 0 评论 -
C++大学基础教程_10_10_代理类
//Implementation.h//包含欲隐藏的私有实现的类生成类定义class Implementation{public: Implementation(int v) :value(v) { //空函数体 } void setValue(int v) { value = v; } int getValue() { return value;原创 2014-07-15 13:54:16 · 487 阅读 · 0 评论 -
C++大学基础教程_10_67_new和delete和static
//Employee.h#ifndef EMPLOYEE_H_#define EMPLOYEE_H_class Employee{public: Employee(const char * const,const char * const); ~Employee(); const char *getFirstName() const; const char *getLastNa原创 2014-07-15 11:43:02 · 764 阅读 · 0 评论 -
C++大学基础教程_10_5_使用this指针
//testMain1.cpp//隐式和显式使用this指针来访问对象的数据成员#include using namespace std;class Test{public: Test(int = 0); void print() const;private: int x;};Test::Test(int val) :x(val){}//空函数体void Tes原创 2014-07-14 22:56:43 · 519 阅读 · 0 评论 -
C++大学基础教程_10_4_friend函数和friend类
//testMain.cpp#include using namespace std;class Count{ //使用非friend函数来修改private成员是错误尝试 //用friend函数修改类的private数据 friend void setX(Count &,int);public: Count() :x(0) {}//函数体为空 void print原创 2014-07-14 22:51:19 · 538 阅读 · 0 评论 -
C++大学基础教程 _10_3_组成:对象作为类的成员
//Date.h#ifndef DATE_H#define DATE_Hclass Date{public: Date(int = 1,int = 1,int = 1900);//参数分别为月日年 ~Date(); void print() const ;private: int month; int day; int year; int checkDay(int)原创 2014-07-14 17:29:29 · 556 阅读 · 0 评论 -
C++大学基础教程_12_面向对象编程:继承
//CommisionEmployee.h//佣金雇员类,薪水完全是销售提成/*#ifndef x //先测试x是否被宏定义过#define x程序段 1 //如果x没有被宏定义过,定义x,并编译程序段 1#endif程序段 2 //如果x已经定义过了则编译程序段2的语句,“忽视”程序段 1。*/#ifndef COMMISION_H#define COMMISION_H原创 2014-07-13 23:35:31 · 604 阅读 · 0 评论 -
分治策略求解子数组最大和并输出下标
//FindMaxSubarray.h//寻找最大子数组的和,分成三种情况来讨论/*在数组A[low...high]中,任何连续子数组A[i...j]的位置有三种一、完全位于数组A[low...mid]中,因此low<=i<=j<=mid二、完全位于数组A[mid+1...high]中,因此mid<i<=j<=mid三、跨越了中点,因此low<=i<=mid<j<=high*/原创 2014-08-01 15:58:31 · 739 阅读 · 0 评论 -
2_转义字符
//_2_转义字符//_2_main.cpp#include #include int main(){ //换行符'\n',用于输出换行 printf("How are you ?\n"); printf("I am fine,thank you.\n"); //横向跳格符'\t',使跳到下一个输出区 printf("\nHow are you ?\t"); print原创 2014-08-08 13:06:24 · 527 阅读 · 0 评论 -
8_指针操作符
//_8_指针操作符//_8_main.cpp#include #include int main(){ int *p;//定义一个整形指针 int begin,end; begin = 10; p = &begin;//给指针p赋初值 end = *p;//将指针指向的值传给变量end printf("begin = %d\n",begin); printf("end原创 2014-08-08 13:13:50 · 348 阅读 · 0 评论 -
9_if判断语句
//_9_if判断语句//_9_main.cpp#include #include int main(){ int x,y,z,mid,dec; printf("please input three integer:\n"); scanf("%d %d %d",&x,&y,&z); if(x<y) { mid = x; x = y; y = mid; } if(x<z原创 2014-08-08 13:12:42 · 474 阅读 · 0 评论 -
6_位移运算
//_6_位移运算//_6_main.cpp#include #include int main(){ unsigned a,b,c,d; int n; a = 64; n = 2; //将操作数a右移(6-n)位.....64/(2^4) b = a >> (6-n); printf("b = %d\n",b); //将操作数a左移n位 c = a << n原创 2014-08-08 13:12:17 · 367 阅读 · 0 评论 -
5_普通位运算
//_5_普通位运算//_5_main.cpp#include #include //C语言提供了六个位运算符,本例介绍其中四个//分别是:按位与(&)、按位或(|)、按位异或(^)、以及取反(~)//仅仅取反(~)是一元运算符//位操作只是对int型和char型而言int main(){ //定义一个无符号字符型变量,此变量只能用来存储无符号数 unsigned char原创 2014-08-08 13:11:27 · 355 阅读 · 0 评论 -
7_字符译码
//_7_字符译码//_7_main.cpp//一个字符数据既可以以字符形式输出,也可以以整数形式输出#include #include int main(){ //定义字符型常量,并给他们赋值 char c1,c2,c3,c4,c5,c6,c7; c1 = 'C'; c2 = 'h'; c3 = 'i'; c4 = 'n'; c5 = 'e'; c6 = 's';原创 2014-08-08 13:10:49 · 507 阅读 · 0 评论 -
4_自增自减
//_4_自增自减//_4_main.cpp#include #include int main(){ int i=8,j=10,k=12; int m,n,p; m = ++i;//自增在操作数之前 printf("i = %d\n",i); printf("m = %d\n",m); n = j--;//自减在操作数之后 printf("j = %d\n",j);原创 2014-08-08 13:10:16 · 345 阅读 · 0 评论 -
3_关系和逻辑运算
//_3_关系和逻辑运算//_3_main.cpp#include #include //使用逻辑或者关系运算符的表达式返回零作为假值,返回1作为真值int main(){ //定义一个整形变量用来存放后面的算式的值 int logic; int a = 1; int b = 2; int c = 3; logic = a+b>c&&b<=c; printf("lo原创 2014-08-08 13:09:21 · 429 阅读 · 0 评论 -
1_数据类型转换
//_1_数据类型转换//_1_main.cpp#include #include int main(){ //定义变量并且赋值 int a = 5; char c = 'a'; float f = 5.3; double m = 12.65; double result; /*同类型数据之间进行运算并输出结果*/ printf("a + c = %d\n",a+c);原创 2014-08-08 13:07:17 · 393 阅读 · 0 评论 -
31_局部和全局变量
//_31_局部和全局变量//_31_main.cpp//全局变量和某一变量的局部变量同名时,该函数对该名的所有访问//仅针对局部变量,对全局变量无影响#include #include int count;//count是全局变量void function1();//函数声明void function2();int main(){ count = 100; fun原创 2014-08-10 16:47:34 · 375 阅读 · 0 评论