/*
猜数:首先生成一个介于1-100之间的整数,从键盘
不断输入数值直到该数值与生成的那个数相符为止,
并打印出猜数的次数(在主函数中定义intsecret用
于存放待猜数据,设计函数
void create_secret(int*secretp)用于生成待猜数据)
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void create_secret(int *secretp)
{
srand(time(NULL));
*secretp = rand()%100 + 1;
}
void guess(int n)
{
int a;
int count = 0;
while(1){
scanf("%d", &a);
if(a == n){
count++;
break;
}
if(a > n){
count++;
printf("num too high\n");
}
if(a < n){
count++;
printf("num too low\n");
}
}
printf("Get it by %d\n", count);
}
int main(void)
{
int n;
create_secret(&n);
guess(n);
return 0;
}
/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./1
84
num too high
55
num too high
32
num too high
22
num too low
28
num too high
26
Get it by 6
*/
/*
使用指针完成数组元素的存数及数据的反向输出
*/
#include <stdio.h>
int main(void)
{
int st[10];
int i, *p;
int j;
p = st;
for(i = 0; i < 10; i++){
scanf("%d", (p + i));
}
for(j = i - 1; j >= 0; j--){
printf("%d ", *(p + j));
}
printf("\n");
return 0;
}
/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./3
2 3 5 6 8 9 7 55 6 2
2 6 55 7 9 8 6 5 3 2
*/
/*
函数实现:生成26个排列好的英文字母
*/
#include <stdio.h>
int main(void)
{
char st[26];
char *p;
int i;
p = st;
for(i = 0; i < 26; i++){
*(p + i) = 'a' + i;
}
printf("%s\n", p);
return 0;
}
/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./4
abcdefghijklmnopqrstuvwxyz
*/
/*
函数实现:删除字符数组中的空格
*/
#include <stdio.h>
#define N 1024
char *del_space(char *src)
{
int i, j;
for(i = j = 0; src[i] != '\0'; i++){
if(src[i] != ' '){
src[j++] = src[i];
// j++;
}
}
src[j] = '\0';
return src;
}
int main(void)
{
char str[N];
fgets(str, N, stdin);
del_space(str);
puts(str);
return 0;
}
/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./5.1
hello world ni hao
helloworldnihao
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$
*/
/*
函数实现:删除字符数组中的空格
扩展:删除数组中的指定字符
*/
#include <stdio.h>
#define N 1024
char *del_space(char *src, char c)
{
int i, j;
for(i = j = 0; src[i] != '\0'; i++){
if(src[i] != c){
src[j++] = src[i];
// j++;
}
}
src[j] = '\0';
return src;
}
int main(void)
{
char str[N];
char c;
printf("Enter a string\n");
fgets(str, N, stdin);
printf("Enter del's character\n");
scanf("%c", &c);
del_space(str, c);
puts(str);
return 0;
}
/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./5.2
Enter a string
hello world ni hao
Enter del's character
h
ello world ni ao
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$
*/<pre name="code" class="cpp">
/*
函数实现:删除字符数组中的空格(扩展:删除数组中的指定字符)
*/
#include <stdio.h>
#include <string.h>
#define N 1024
char *del(char *src);
//char dest[1024];
int main(void)
{
// char str[] = "hello world haha";
char str[N];
fgets(str, N, stdin);
printf("%s\n", del(str));
return 0;
}
char *del(char *p)
{
int i, j;
static char dest[1024];
for(i = j = 0; *(p + i) != '\0'; i++){
if( *(p + i) != ' ' ){
dest[j] = *(p + i);
j++;
}
}
dest[j] = '\0';
return dest;
}
/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./5
hello world
helloworld
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$*/