这是一个学C语言同学问的,所有这里用C语言解答了。其实思想都一样。
//输入一个字符串,内有数字和非数字字符
//例如:A123x456 1760? 302tab5876
//将其中连续的数字作为一个整数,一次存放到数组 a 中。
#include"stdio.h"
int main()
{
char s[1000];
gets(s);
int geshu=0;
for(int i=0;s[i]!='\0';i++)
{
if((s[i]>='0'&&s[i]<='9')&&(s[i+1]<'0'||s[i+1]>'9'))
{
geshu++;
}
}
printf("数字总数为:%d\n",geshu);
int a[geshu];
for(int i=0;i='0'&&s[i]<='9')
{
a[temp]=a[temp]*10+(s[i]-'0');
}
if((s[i]>='0'&&s[i]<='9')&&(s[i+1]<'0'||s[i+1]>'9'))
{
temp++;
}
}
for(int i=0;i
#include"iostream"
#include"windows.h"
using namespace std;
//空类型函数,不反回。
void f()
{
cout<<"调用了 1 次 f 函数"<
#include"iostream"
#include"windows.h"
using namespace std;
double yiban(double a)//传递,不改变原来的值。
{
a=a/2;
return a;
}
double makeyiban(double *a)//引用改变原来的值
{
*a=*a/2;
return *a;
}
void paixu(int a[],int lenth)//引用,改变原来的值
{
for(int i=0;ia[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
int main()
{
double a=100.0;
cout<
#include"iostream"
#include"windows.h"
using namespace std;
int main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9};
int (*p)[3];//宽度为 3 的一个指针。不能 写成*p[3]
p=a;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<
字符串其实也是一种数组。唯一多的是 以 '\0' 来结尾数组。通常判断一个字符串是否结束用 ’\0‘ 来标记。
先来看一些字符串函数。
#include"iostream"
#include"cstring"
using namespace std;
int main()
{
char a[100];
char *b="abcdefg";//与 字符串 a 不同,b不可改变。因为 b 是指向该字符串的指针。而 a 数组组成的字符串。
strcpy(a,"*******");//A目标地址。B源地址。拷贝函数。
//strncpy();
cout<
#include"iostream"
#include"windows.h"
#include"cstring"
using namespace std;
int main()
{
char a[100];
cin.getline(a,100);//用于输入字符串。可以出入空格。但是不能输入回车。
//如果输入,则存放在缓冲区。
//cin.getline(A,B,C),A位置是字符串首地址,B位置是需要输入的长度,C位置是结束标志。回车也是该函数的截止标志。
//cin.getline(a,30,'w');是在字符串 a 的前 30 位输入,到 w 就截止输入。回车也截止。
char b[100];
cin>>b; //用于输入字符串,但是不能输入空格也不能输入回车。
//如果输入,则存放在缓冲区。
//上文 a 和 b 的输入,第一个读取了输入的信息,但是到回车就结束了,该回车存放于缓冲区,在输入 b 的时候,cin 跳过 该回车读取其他缓冲区信息。
//getchar()函数可以读取空格和回车。但是这样需要一个字符来判断何时停止。
cout<<"******"<
#include"iostream"
#include"windows.h"
#include"cstring"
#include"iomanip"//setw()用到的头文件
#include"time.h"//time()函数用到的头文件
using namespace std;
int main()
{
srand(time(NULL));//使用时间作为随机数种子。也可以使用进程号等其他一切可变的东西做种子。
int a=rand();//用这个种子进行一系列+-*/运算得到一个相对随机的正整数。
//rand()的最大值为 RAND_MAX;一般 RAND_MAX=32767 ,不同环境不一样。
int b[100];
for(int i=0;i<100;i++)
{
b[i]=rand()%301+50;//让随机数的范围在 50到 350 之间。
}
b[99]=RAND_MAX;//让 b[99] 为 RAND_MAX
for(int i=0;i<100;i++)
{
cout<
抽奖,要求,不能抽到已经抽到的数。所以如果该数已经抽到就要重新抽。
做界面前缀的过程见C++培训_004。
双击 抽奖的按钮。在onbnclickedbutton1函数中写代码。在此之前先写用与判断是否出现的 bool m[181];
bool m[181];//用于判断是否出现过已经出现的数字。
//bool类型 在 vs编译器里默认为 false。
void CrandomDlg::OnBnClickedButton1()
{
bool n = false;
for (int i = 0; i < 180; i++)
{
if (m[i] == false)
{
n = true;
break;
}
}
if (n == true)
{
CString mstr;// 定义字符串类。
srand((unsigned)time(0));//以时间为种子。
int a;
a = rand() % 180 + 1;//让 a 为 1 到 180 之间的整数。
while (m[a] == true)//如果出现过了,则再生成一次。
{
a = rand() % 180 + 1;
}
m[a] = true;//把出现过的排除。
mstr.Format("%d", a);
GetDlgItem(IDC_BUTTON1)->SetWindowTextA(mstr);
}
else
{
CString mstr2 = "已经抽奖 180 次。抽奖结束。重新开始";
GetDlgItem(IDC_BUTTON1)->SetWindowTextA(mstr2);
for (int i = 0; i < 181; i++)
{
m[i] = 0;
}
}
}
#include"iostream"
#include"windows.h"
using namespace std;
int main()
{
int (*p1)[5];//宽度为 5 的一个指针。
int *p2[5];//宽度为 1 的 5 个指针。
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
int b=0;
p1=a;
p2[0]=&b;
system("pause");
return 0;
}
#include"iostream"
#include"windows.h"
using namespace std;
int main()
{
/*
int a[][3]={1,2,3,4};//每行宽度为 3 的二维数组。因为出现了四个数。所以共计 2行。
int b[3][4];
for(int i=0;i<3;i++)//行
{
for(int j=0;j<4;j++)//列
{
cin>>b[i][j];
}
}
for(int i=0;i<3;i++)//行
{
for(int j=0;j<4;j++)//列
{
cout<
#include"iostream"
#include"windows.h"
using namespace std;
int main()
{
int N,M,X;
cin>>N>>M>>X;
int a[N][M];
for(int i=0;i
#include"iostream"
using namespace std;
void insert(int *a,int pos)
{
int pos1=pos;
for(int i=0;ipos1;i--)
{
a[i]=a[i-1];
}
a[pos1]=temp;
}
void paixu(int *a,int nowlength)
{
if(nowlength==0)
{
return;
}
else
{
paixu(a,nowlength-1);
insert(a,nowlength);
}
}
int main()
{
int a[10]={1,1,3,5,1,2,6,5,9,0};
paixu(a,9);
for(int i=0;i<10;i++)
{
cout<