作业2
1.1字符切割
#include<stdio.h>
int judge_char(char *c);
int judge_black(char *c);
int judge_3(char *c);
int main(void) {
char s[100];
int i = 0, count = 0;
gets_s(s);
while (s[i] != '\0') {
if (judge_char(&s[i])==1) {
count++;
i++;
continue;
}
if (judge_black(&s[i])==1) {
while (s[i] == ' ') {
i++;
if (s[i] == '\0') {
break;
}
}
continue;
}
if (judge_3(&s[i]) == 1) {
while (judge_3(&s[i]) == 1) {
i++;
if (s[i] == '\0') {
break;
}
}
count++;
continue;
}
}
printf("%d", count);
return 0;
}
int judge_char(char* c) {//判断是否为标点符号
if (*c != ' ' && ((*c >= '0' && *c <= '9') || (*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z')) == 0) {
return 1;
}
return 0;
}
int judge_black(char* c) {//判断是否为空格
if (*c == ' ') {
return 1;
}
return 0;
}
int judge_3(char* c) { //判断是否为字母或者数字
if ((*c >= '0' && *c <= '9') || (*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z')) {
return 1;
}
return 0;
}
1.2字符输出
#include<stdio.h>
int judge_char(char *c);
int judge_black(char *c);
int judge_3(char *c);
int main(void) {
char s[100];
int i = 0, count = 0;
gets_s(s);
while (s[i] != '\0') {
if (judge_char(&s[i])==1) {
char *p = &s[i];
printf("%c\n",*p);
count++;
i++;
continue;
}
if (judge_black(&s[i])==1) {
while (s[i] == ' ') {
i++;
if (s[i] == '\0') {
break;
}
}
continue;
}
if (judge_3(&s[i]) == 1) {
while (judge_3(&s[i]) == 1) {
char* p = &s[i];
printf("%c",*p);
i++;
if (s[i] == '\0') {
break;
}
}
printf("\n");
count++;
continue;
}
}
printf("%d", count);
return 0;
}
int judge_char(char* c) {//判断是否为标点符号
if (*c != ' ' && ((*c >= '0' && *c <= '9') || (*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z')) == 0) {
return 1;
}
return 0;
}
int judge_black(char* c) {//判断是否为空格
if (*c == ' ') {
return 1;
}
return 0;
}
int judge_3(char* c) { //判断是否为字母或者数字
if ((*c >= '0' && *c <= '9') || (*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z')) {
return 1;
}
return 0;
}
1.4变长数组
#include <stdio.h>
#include <stdlib.h>
int* MyArray();
int main()
{
int* myArray = MyArray();//myArray接受动态创建的数组返回的地址
//printf("%d ", myArray[0]);
//printf("%d ", myArray[1]);
//printf("%d ", myArray[2]);检测用
return 0;
}
int* MyArray() { //当输入为9999时结束
int* p = NULL, * q = NULL, x, count = 0;//p为传输 q为记录
scanf_s("%d", &x);
while (x != 9999) {
count++;
if (count == 1) {
p = (int*)malloc(sizeof(int));
p[count - 1] = x;
}
else {
q = (int*)malloc((count - 1) * sizeof(int));
q = p;
p = (int*)malloc(count * sizeof(int));
p = q;
p[count - 1] = x;
}
scanf_s("%d", &x);
}
return q;
}
2.1神仙吧!!!(我是废物呜呜)
#include<iostream>
#include<stdlib.h>
#include<sstream>
#include<string.h>
#include<string>
#define FILE_BUFFER_LENGTH 30000
using namespace std;
void cheng(string s1, string s2) {
int length_1 = s1.length(); //字符串1的长度
int length_2 = s2.length(); //字符串2的长度
int sum_len = length_1 + length_2;//字符串的总长 两数字相乘最大的位数为 数字1的位数+数字2的位数
int max_len, min_len; //较大的字符串长度 和 较小的字符串长度
string max_num, min_num; //通过字符串长度比较原数字大小
if (length_1 > length_2) { //比较大小
max_num = s1;
min_num = s2;
max_len = length_1;
min_len = length_2;
}
else {
max_num = s2;
min_num = s1;
max_len = length_2;
min_len = length_1;
}
int* tmp_num = new int[sum_len]; //记录 较小数字的某一位数 与 较大数字的每一位数字 的积
int* sum_num = new int[sum_len]; //相乘后最终的结果
for (int i = 0; i < sum_len; i++) { //申请的数组清零
sum_num[i] = 0;
tmp_num[i] = 0;
}
int mulJin = 0, addJin = 0; //mulJin 为代替进位
for (int i = min_len - 1; i >= 0; i--) {
int k = --sum_len;
for (int j = max_len - 1; j >= 0; j--) {
tmp_num[k] = ((max_num[j] - '0') * (min_num[i] - '0') + mulJin) % 10; //
mulJin = ((max_num[j] - '0') * (min_num[i] - '0') + mulJin) / 10;
k--;
}
if (mulJin > 0) {
tmp_num[k] = mulJin;
}
mulJin = 0;
for (int i = length_1 + length_2 - 1; i >= 0; i--) {
int tmp = (sum_num[i] + tmp_num[i] + addJin); // 代替同一位上的数字相加的和
sum_num[i] = tmp % 10; // 超过10进位
addJin = tmp / 10; // 进多少位
}
addJin = 0;
}
for (int i = 0; i < length_1 + length_2; i++) { //代替用的数组清零
tmp_num[i] = 0;
}
int t = 0;
while (true) { //剔除前导0
if (sum_num[t] == 0) {
t++;
}
else {
break;
}
}
for (; t < length_1 + length_2; t++) {
cout << sum_num[t];
}
}
int zhuanghua(string s) { //将所得到的地址转化为字符串后 动态分配内存生存一个数组 十六进制转化十进制后返回数字
int* p = NULL;
int i = sizeof(s);//字符串长度;
int len = 0;
int index = 0;
int sum = 0; //转化进制后的数字
p = (int*)malloc(i * sizeof(int));
if (p == NULL) {
printf("内存分配失败\n");
}
else {
while (s[len] != '\0') {
switch (s[len])
{
case '0':
p[len++] = 0;
break;
case '1':
p[len++] = 1;
break;
case '2':
p[len++] = 2;
break;
case '3':
p[len++] = 3;
break;
case '4':
p[len++] = 4;
break;
case '5':
p[len++] = 5;
break;
case '6':
p[len++] = 6;
break;
case '7':
p[len++] = 7;
break;
case '8':
p[len++] = 8;
break;
case '9':
p[len++] = 9;
break;
case 'A':
p[len++] = 10;
break;
case 'B':
p[len++] = 11;
break;
case 'C':
p[len++] = 12;
break;
case 'D':
p[len++] = 13;
break;
case 'E':
p[len++] = 14;
break;
case 'F':
p[len++] = 15;
break;
case 'a':
p[len++] = 10;
break;
case 'b':
p[len++] = 11;
break;
case 'c':
p[len++] = 12;
break;
case 'd':
p[len++] = 13;
break;
case 'e':
p[len++] = 14;
break;
case 'f':
p[len++] = 15;
break;
}
}
}
index = len;
for (i = 0; i < len; i++)
{
sum += p[i] * pow(16, index - 1);
index--;
}
return sum;
}
int main(void)
{
int x;
cin >> x;
int* p=&x; //定义一个int类型的变量 给予任意数值 并获取该变量的地址
string str="";
stringstream ss;
ss << p;
ss >> str; // 将变量的地址转化为字符串
int num_1=zhuanghua(str); //得到地址转化进制后的数值
string str_1="";
stringstream ss_1;
ss_1 << num_1;
ss_1 >> str_1; //将得到的地址转化数值转化为字符串1
string str_2="";
stringstream ss_2;
ss_2 << x;
ss_2 >> str_2;//将给予了任意数值的变量转化为字符串2
cheng(str_1, str_2);
return 0;
}
3.1 链表
#include<stdio.h>
#include<stdlib.h>
struct num{
int number;
struct num* next;
};
int main() {
struct num* head=NULL, * tail=NULL;
struct num *p= NULL;
int number;
scanf_s("%d",&number);
while (number != 9999) {
p = (struct num*)malloc(sizeof(struct num));
p->number = number;
p->next = NULL;
if (head == NULL) {
head = p;
}
else {
tail->next = p;
}
tail = p;
scanf_s("%d", &number);
}
}