// huaweiDemo2.cpp: 定义控制台应用程序的入口点:平台-VS2017,2019-01-01
//
//1-问题:就算串长度
//输入一个字符串,用指针求出字符串的长度。
/*
#include "stdafx.h"
#include "stdio.h"
int main()
{
char str[20], *p;
int length = 0;
printf("Please input a string : ");
scanf("%s", str);// gets(str);
p = str;
while (*p++)
{
length++;
}
printf("The length of string is %d\n", length);
scanf("%s", str);//
return 0;
}
*/
//2-问题:查找并替换子串
//使用C语言实现字符串中子字符串的替换
/*
#include "stdafx.h"
#include "stdio.h"
#include <string.h>
void StrReplace(char* strSrc, char* strFind, char* strReplace);
#define M 100;
int main()
{
char s[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char s1[] = "RST";
char s2[] = "ggg";
printf("%s\n", s);
StrReplace(s, s1, s2);
printf("%s\n", s);
scanf("%s",s);//显示
return 0;
}
void StrReplace(char* strSrc, char* strFind, char* strReplace)
{
int i = 0;
int j;
int n = strlen(strSrc);
int k = strlen(strFind);
for (i = 0; i<n; i++)
{
if (*(strSrc + i) == *strFind)
{
for (j = 0; j<k; j++)
{
if (*(strSrc + i + j) == *(strFind + j))
{
*(strSrc + i + j) = *(strReplace + j);
}
else continue;
}
}
}
}
*/
//3-问题:间隔输出串
//编写一个程序实现功能:将字符串”Computer Secience”赋给一个字符数组,然后从第一个字母开始间隔的输出该串,用指针完成。
/*
#include "stdafx.h"
#include "stdio.h"
#include <string.h>
int main()
{
char s[] = "Computer Secience";
char *str=s;
int flag_t = 1;
while(*str)
{
if (flag_t)
{
printf("%c",*str);
}
flag_t = (flag_t + 1) % 2;
str++;
}
scanf("%d",flag_t);
return 0;
}
*/
//4问题:拼串
//编写一个程序实现功能:将两个字符串合并为一个字符串并且输出,用指针实现。char str1[20] = { “Hello ” }, str2[20] = { “World ” };
/*
#include "stdafx.h"
#include <stdio.h>
int main()
{
char str1[60] = { "Hello " }, str2[40] = { "World!" },str3[30] = {" Welcome to C world!"};
char *p = str1, *q1 = str2,*q2=str3;
while (*p) //str1和p都指向同一个地址,利用p来操作-修改str1
p++;
while (*q1)
{
*p = *q1;
p++;
q1++;
}
while (*q2)
{
*p = *q2;
p++;
q2++;
}
*p = '\0';
printf("%s\n", str1); //str1和p都指向同一个地址,修改p即修改str1
scanf("%s", str1);
return 0;
}
*/
//5问题:递归数列的计算
//以下函数的功能是用递归的方法计算x的n阶勒让德多项式的值。已有调用语句p(n,x);编写函数实现功能。
/*
#include "stdafx.h"
#include <stdio.h>
float p(int x, int n)
{
float t, t1, t2;
if (n == 0) return 1;
else if (n == 1) return x;
else
{
t1 = (2 * n - 1)*x*p(x, (n - 1));
t2 = (n - 1)*p(x, (n - 2));
t = (t1 - t2) / n;
return t;
}
}
int main()
{
int x, n;
printf("input two int(x and n) : ");
scanf("%d%d", &x, &n);
printf("%.2f\n", p(x, n));
printf("input two int(x and n) : ");
scanf("%d%d", &x, &n);
printf("%.2f\n", p(x, n));
printf("input two int(x and n) : ");
scanf("%d%d", &x, &n);
printf("%.2f\n", p(x, n));
return 0;
}
*/
//6问题:echo功能
//给主函数传递参数实现echo功能:
/*
#include "stdafx.h"
#include "stdio.h"
int main(int argc, char *argv[])
{
int i = 1;
while (i < argc)
{
printf("%s",argv[i]);
i++;
}
printf("\n");
scanf("%d",&i);
return 0;
}
*/
//7-两数组比较(对应比较)
//要求从数组最后一个元素开始逐个元素向前比较,如果2个数组长度不等,则只比较较短长度数组个数元素。
/*
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "stdafx.h"
int array_compare(int len1, int array1[], int len2, int array2[])
{
int count = 0;
for (; len1 >= 0 && len2 >= 0; len1--, len2--)
{
if (array1[len1 - 1] == array2[len2 - 1])
{
count++;
}
}
return count;
}
int main()
{
int result = 0;
int array1[] = { 1,3,5 };
int len1 = 3;
int array2[] = { 77,12,1,3,5 };
int len2 = 5;
result = array_compare(len1, array1, len2, array2);
printf("the result is %d", result);
//scanf("%d",&result);
return 0;
}
*/
//8约瑟夫环--指针和链表(可用指针和数组来做)
//约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。
//从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;
/*
//依此规律重复下去,直到圆桌周围的人全部出列。
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int num;
struct Node *next;//一个Node类型的指针
}LinkList;
LinkList *creat(int n)//返回一个LinkList类型的指针
{
LinkList *p, *q, *head;
int i = 1;
p = (LinkList *)malloc(sizeof(LinkList));
p->num = i;
head = p;
for (i = 2; i <= n; i++)
{
q = (LinkList *)malloc(sizeof(LinkList)); //Malloc()向系统申请分配指定size个字节的内存空间。返回类型是 void* 类型。void* 表示未确定类型的指针。C,C++规定,void* 类型可以强制转换为任何其它类型的指针。
q->num = i;
p->next = q;
p = q;
}
p->next = head; //使链表尾指向链表头 形成循环链表
return head;
}
void fun(LinkList *L, int m)
{
int i;
LinkList *p, *s, *q;
p = L;
printf("出列顺序为:");
while (p->next != p)
{
for (i = 1; i<m; i++)//*从1开始
{
q = p;
p = p->next;
}
printf("%5d",p->num);
s = p;
q->next = p->next;
p = p->next; //使p指向新的起点
free(s);//free()与malloc()函数配对使用,释放malloc函数申请的动态内存
}
printf("%5d\n", p->num);
}
int main()
{
LinkList *L;
int n, m;
n = 9;//n=9个人
m = 5;//出列的号数为5
L = creat(n);
fun(L, m);
scanf("%d",&n);
return 0;
}
*/
//9-号码有效性判断
//特点如下:1、 长度13位;2、 以86的国家码打头;3、 手机号码的每一位都是数字。
/*
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define LENGTH 13
int verifyMsisdn(char *inMsisdn)
{
//char *pchar=NULL;
assert(inMsisdn != NULL);
if (LENGTH == strlen(inMsisdn))
{
if (('8' == *inMsisdn) && (*(inMsisdn + 1) == '6'))
{
while (*inMsisdn != '\0')
{
if ((*inMsisdn >= '0') && (*inMsisdn <= '9'))
inMsisdn++;
else
return 2;//非法字符
}
}
else return 3; //开头不合法
}
else return 1; //长度不合法
return 0; //号码合法!
}
int main()
{
char *pchar = NULL;
unsigned char ichar = 3;
int result;
switch (ichar)
{
case 0:
pchar = "8612345363789"; break; //返回: 1
case 1:
pchar = "861111111111111"; break; //
case 2:
pchar = "86s1234536366"; break; //
case 3:
pchar = "1392222222222"; break;
default:
break;
}
result = verifyMsisdn(pchar);
printf("result is %d\n", result);
//scanf("%d",result);
return 0;
}
*/
//10-回文数组判断
/*
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include "string.h"
int huiwen(char p[])
{
int len = strlen(p);//sizeof(p);
int i=len/2;
for (int j = 0; j < i; j++)
{
if (p[i] != p[len-i-1])
{
return 0;//NO
}
}
return 1; //YES
}
int main()
{
char str1[12];
printf("输入数组:\n");
scanf("%s",str1);
printf("输入数组:%d \n",huiwen(str1));
//==========================
//char str1[12];
printf("输入数组:\n");
scanf("%s", str1);
printf("输入数组:%d \n", huiwen(str1));
//==============================
//char str1[12];
printf("输入数组:\n");
scanf("%s", str1);
printf("输入数组:%d \n", huiwen(str1));
}
*/
//10-选秀打分
//总分 = 专家评委平均分 * 0.6 + 大众评委 * 0.4,总分取整。如果没有大众评委,则 总分 = 专家评委平均分,总分取整。函数最终返回选手得分
/*
#include "stdafx.h"
#include "iostream"
using namespace std;
float cal_score(int score[], int judge_type[], int n)
{
if (NULL == score || NULL == judge_type || 0 == n) return 0;
float sum = 0;
int sum1 = 0, count1 = 0;
int sum2 = 0, count2 = 0;
for (int i = 0; i<n; i++)
{
if (judge_type[i] == 1)
{
sum1 = sum1 + score[i];
count1++;
}
else
{
sum2 = sum2 + score[i];
count2++;
}
}
if (0 == count2) sum = sum1 / count1;
else sum = (sum1 / count1)*0.6 + (sum2 / count2)*0.4;
return sum;
}
void main()
{
int score[3] = { 12,13,15 };
int judge_type[3] = { 1,1,2 };
printf("%d\n", cal_score(score, judge_type, 3));
}
*/
//11-问题:给定一个数组 input[] ,如果数组长度 n 为奇数,则将数组中最大的元素放到 output[] 数组最中间的位置,
//如果数组长度 n 为偶数,则将数组中最大的元素放到 output[] 数组中间两个位置偏右的那个位置上,
//然后再按从大到小的顺序,依次在第一个位置的两边,按照一左一右的顺序,依次存放剩下的数。
/*
#include "stdafx.h"
#include "iostream"
using namespace std;
void bubblesort(int data[], int n)
{
int temp = 0;
for (int i = 0; i<n; i++)
{
for (int j = i + 1; j<n; j++)
{
if (data[i]<data[j])
{
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
}
void sort(int input[], int n, int output[])
{
int *sort_input = new int[n];//用new和delete动态创建和释放数组或单个对象
for (int i = 0; i<n; i++)
{
sort_input[i] = input[i];
}
bubblesort(sort_input, n); //先做冒泡排序
if (1 == n % 2)//奇
{
int mid = n / 2;
int k = 0;
output[mid] = sort_input[k++];
for (int j = 1; j <= n / 2; j++)
{
output[mid - j] = sort_input[k++];
output[mid + j] = sort_input[k++];
}
}
else //偶数
{
int mid = n / 2;
int k = 0;
output[mid] = sort_input[k++];
for (int j = 1; j<n / 2; j++)
{
output[mid - j] = sort_input[k++];
output[mid + j] = sort_input[k++];
}
output[0] = sort_input[k++];
}
delete sort_input;//用new和delete动态创建和释放数组或单个对象
}
void main()
{
int input1[] = { 3, 6, 1, 9, 7 };
int output1[5];
memset(output1, 0, 5 * sizeof(int));
int input2[] = { 3, 6, 1, 9, 7, 8 };
int output2[6];
memset(output2, 0, 6 * sizeof(int));
sort(input1, 5, output1);
sort(input2, 6, output2);
for (int k = 0; k<5; k++)
printf("%d", output1[k]);
printf("\n");
for (int k = 0; k<6; k++)
printf("%d", output2[k]);
printf("\n");
}
*/
//12删除字符串中所有给定的子串问题描述:
//在给定字符串中查找所有特定子串并删除,如果没有找到相应子串,则不作任何操作。
/*
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
void del_substr(char s1[],char s2[]);
void del_substr(char s1[],char s2[])
{
int i=0,k=0,j=0,len_s1,len_s2;
len_s1=strlen(s1);
len_s2=strlen(s2);
//子串比主串长,没有意义
if(len_s1<len_s2)
return;
while(s1[i])//遍历s1
{
if(s1[i]-s2[0])//如果和子串的第一个字符不相等,则s1[i]存到"另一个串"
s1[k++]=s1[i++];
else//如果和子串以一个字符相等,则有可能存在子串
{
j=1;
while(!(s1[i+j]-s2[j]) && j<len_s2)
++j;
if(!(j-len_s2))//如果是子串,则将i直接跳过子串,然后继续执行,将不是子串的存在"另一个串"
i+=j;//跳过子串
else
s1[k++]=s1[i++];
}
}
s1[k]=0;//删除后,末尾补0
return;
}
int main(void)
{
char date[1001], substring[1001], len1, len2;
printf("输入字符串:");
gets_s(date,100);
len1 = strlen(date);//
printf("输入子串:");
gets_s(substring,100);
del_substr(date, substring);//删除子串
len2 = strlen(date);
if (len1 > len2)
{
printf("删除后:");
puts(date);
}
else
printf("Error!");//没有子串,输出error
return 0;
}
*/
//13-操作系统任务调度问题。操作系统任务分为系统任务和用户任务两种。其中,系统任务的优先级 < 50,用户任务的优先级 >= 50且 <= 255。
//优先级大于 255的为非法任务,应予以剔除。现有一任务队列 task[],长度为 n,task中的元素值表示任务的优先级,数值越小,优先级越高。
//函数 scheduler 实现如下功能,将 task[] 中的任务按照系统任务用户任务依次存放到 system_task[] 数组和 user_task[] 数组中
//(数组中元素的值是任务在 task[] 数组中的下标),并且优先级高的任务排在前面,优先级相同的任务按照入队顺序排列(即先入队的任务排在前面),数组元素为-1表示结束。
/*
#include "iostream"
#include "stdafx.h"
using namespace std;
void change(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void bubblesort(int data[], int n, int index[])//冒泡排序并记录排序后下标
{
int temp = 0;
for (int j = 0; j<n; j++)
index[j] = j;
for (int i = 0; i<n; i++)
{
for (int j = i + 1; j<n; j++)
{
if (data[i]>data[j])
{
change(&data[i], &data[j]);
change(&index[i], &index[j]);
}
}
}
}
void scheduler(int task[], int n, int system_task[], int user_task[])
{
int *sort_task = new int[n];
int *index = new int[n];
for (int i = 0; i<n; i++)
{
sort_task[i] = task[i];
}
bubblesort(sort_task, n, index);
int i = 0;
while (sort_task[i]<50)
{
system_task[i] = index[i];
i++;
}
system_task[i] = -1;
for (int m = 0; m <= i; m++)
{
printf("%d ", system_task[m]);
}
printf("\n");
int k = 0;
while (sort_task[i]>50 && sort_task[i] <= 255)
{
user_task[k++] = index[i++];
}
user_task[k] = -1;
for (int l = 0; l <= k; l++)
{
printf("%d ", user_task[l]);
}
printf("\n");
delete sort_task;
delete index;
}
void main()
{
int task[] = { 0, 30, 155, 1, 80, 300, 170,40,99 };
int n = sizeof(task) / sizeof(int);
int *system_task = new int[n];
int *user_task = new int[n];
scheduler(task, n, system_task, user_task);
}
*/
//14-简单四则运算问题描述:输入一个只包含个位数字的简单四则运算表达式字符串,计算该表达式的值
//注:
//1、表达式只含 + , -, *, / 四则运算符,不含括号
//2、表达式数值只包含个位整数(0 - 9),且不会出现 0作为除数的情况
//3、要考虑加减乘除按通常四则运算规定的计算优先级
//4、除法用整数除法,即仅保留除法运算结果的整数部分。比如 8 / 3 = 2。输入表达式保证无0作为除数情况发生
//5、输入字符串一定是符合题意合法的表达式,其中只包括数字字符和四则运算符字符,除此之外不含其它任何字符,不会出现计算溢出情况。
/*
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "assert.h"
struct stack
{//存放后续排列的表达式,模拟栈
char str[80];
int top;
};
struct sstack
{//存放计算表达式的值,模拟栈???
int str[80];
int top;
};
int calculate(int len, char *expStr)
{
char *postexp = new char[len + 1];
stack opstack;
sstack calstack;
calstack.top = -1;
opstack.top = -1;
int i = 0;
int k = 0;
while (expStr[i] != '\0')
{
if (expStr[i] >= '0'&&expStr[i] <= '9')
{
postexp[k++] = expStr[i];
}
else if (expStr[i] == '+' || expStr[i] == '-')
{
while (opstack.top >= 0)
{
postexp[k++] = opstack.str[opstack.top--];
}
opstack.top++;
opstack.str[opstack.top] = expStr[i];
}
else if (expStr[i] == '*' || expStr[i] == '/')
{
while (opstack.top >= 0 && (opstack.str[opstack.top] == '*'
|| opstack.str[opstack.top] == '/'))
{
postexp[k++] = opstack.str[opstack.top--];
}
opstack.top++;
opstack.str[opstack.top] = expStr[i];
}
i++;
}
while (opstack.top >= 0)
{
postexp[k++] = opstack.str[opstack.top--];
}
int temp1 = 0;
int temp2 = 0;
for (i = 0; i<len; i++)
{
if (postexp[i] >= '0'&&postexp[i] <= '9')
{
calstack.top++;
calstack.str[calstack.top] = postexp[i] - '0';
}
else if (postexp[i] == '+')
{
temp1 = calstack.str[calstack.top--];
temp2 = calstack.str[calstack.top];
calstack.str[calstack.top] = temp2 + temp1;
}
else if (postexp[i] == '-')
{
temp1 = calstack.str[calstack.top--];
temp2 = calstack.str[calstack.top];
calstack.str[calstack.top] = temp2 - temp1;
}
else if (postexp[i] == '*')
{
temp1 = calstack.str[calstack.top--];
temp2 = calstack.str[calstack.top];
calstack.str[calstack.top] = temp2 * temp1;
}
else if (postexp[i] == '/')
{
temp1 = calstack.str[calstack.top--];
temp2 = calstack.str[calstack.top];
calstack.str[calstack.top] = temp2 / temp1;
}
}
printf("%d\n", calstack.str[calstack.top]);
return calstack.str[calstack.top];
}
int main()
{
char *expStr = "6+8*4-9/2";
int len = strlen(expStr);
calculate(len, expStr);
return 0;
}
*/
//15-德州扑克问题:一副牌中发五张扑克牌给你:让你判断数字的组成:
//有以下几种情况:
//1:四条:即四张一样数值的牌(牌均不论花色)2:三条带 一对
//3:三条带两张不相同数值的牌
//4:两对
//5:顺子 包括 10,J,Q,K,A
//6:什么都不是
//7:只有一对
/*
#include "stdafx.h"
#include "stdio.h"
void sort(int data[], int n)
{
int temp = 0;
for (int i = 0; i<n; i++)
{
for (int j = i + 1; j<n; j++)
{
if (data[i]<data[j])
{
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
}
void test(int a[], int len)
{
int *b = new int[len];
int count = 0;
bool temp = false;
for (int i = 0; i<len; i++)
{
b[i] = a[i];
}
sort(b, 5);
for (int i = 0; i<len - 1; i++)
{
if (b[i] == b[i + 1])
count++;
}
switch (count)
{
case 0:
if (b[0] - b[4] == 4 && b[0] - b[3] == 3 && b[0] - b[2] == 2 && b[0] - b[1] == 1)
{
printf("顺子");
}
else
printf("什么都不是");
break;
case 1:
printf("只有一对");
break;
case 2:
for (int i = 0; i<3; i++)
{
if (b[i] == b[i + 2])
{
printf("三条带两张不相同数值的牌");
temp = true;
break;
}
}
if (!temp)
{
printf("两对");
}
break;
case 3:
if (b[1] == b[3])
printf("四条:即四张一样数值的牌");
else
printf("三条带一对");
break;
}
}
int main()
{
int a[5] = { 3,3,3,3,12 };
printf("\n\t");
test(a, 5);
printf("\n\t");
int b[5] = { 1,3,3,3,12 };
test(b, 5);
return 0;
}
*/
//16-删除数组中的重复元素
#include "stdafx.h"
#include <iostream>
using namespace std;
int de(int a[], int n)
{
for (int i = 0; i<n; i++)
for (int j = i + 1; j<n; j++)
if (a[j] == a[i])
{
for (int k = j; k<n - 1; k++)
a[k] = a[k + 1];
n--;
}
for (int i = 0; i<n; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
int main()
{
int a[10];
int m = 10;
for (int l = 0; l<10; l++)
cin >> a[l];
de(a, m);
return 0;
}