#include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; int main(){ char s[4][100]; int i,j; cin>>s[1]>>s[2]>>s[3]; int sum=0; for(i=1;i<=3;i++) for(j=0;j<=99;j++) { if(s[i][j]>=65&&s[i][j]<=90&&s[i][j]!='A'&&s[i][j]!=
时间: 2023-12-12 15:05:18 浏览: 76
这段代码是一个用C++语言编写的程序,用于统计三个字符串中大写字母出现的次数(不包括'A'和'E')。具体来说,首先定义一个二维字符数组s,用于存储三个字符串。然后使用cin语句分别读入三个字符串s[1]、s[2]、s[3]。接着循环遍历三个字符串的每个字符,如果当前字符是大写字母且不等于'A'和'E',则将计数器sum加1。最后输出大写字母出现的次数sum。
需要注意的是,这段代码使用了C++的特性,如命名空间using namespace std,如果使用C语言编译器编译会出现错误。
相关问题
/*十六进制异或运算出原密码*/ /*/#include <string.h> #include<iostream> using namespace std; int main() { char result[] = {0x66, 0x6D, 0x63, 0x64, 0x7F, 0x5C, 0x49, 0x52, 0x57,0x4F, 0x43, 0x45 ,0x48, 0x52, 0x47, 0x5B, 0x4F, 0x59, 0x53, 0x5B, 0x55,0x68,'\0'}; for(int i = 0;i < strlen(result) ;i++) result[i]= result[i]^i; cout << result << endl; return 0 ; }*/ //密码加解密,源码:CocaColacosohthreeyuan$ /*#include <string.h> #include <stdio.h> #include<iostream> using namespace std; int main() { char result[] = {"Cnnb$lu'|axni-j`|}sag17"}; for (int i = 0; i < strlen(result); i++) result[i] = result[i] ^ i; cout << result << endl; return 0; }*/ //密码是否正确CocaColacosohthreeyuan$ EE.exe /*#include <string.h> #include <stdio.h> #include <stdlib.h> #include<iostream> using namespace std; int main() { char temp[] = {0}; int i = 0; static const char* image = (char*)"CnabGjjfkfyddyf}utkfu{2"; std::cout << "Please input a password: "; std::cin >> temp; //scanf("%s", temp); for (i = 0;i < strlen(temp);++i) { temp[i] ^= i;} if (!strcmp(temp,image)) std::cout << "you are right!\n" << std::endl; else std::cout << "you are wrong!\n" << std::endl; system("pause"); return 0; } */
这是三段 C++ 代码,分别实现了密码加解密和密码验证功能。
第一段代码使用了十六进制异或运算得出原密码。其中,char 数组 result 存储了加密后的密码,for 循环中的异或运算逆推出了原密码,最后输出原密码。注意,这里使用了 i < strlen(result) 作为循环条件,strlen 函数计算的是字符串的长度,因此需要保证 result 是以 '\0' 结尾的字符串。
第二段代码实现了密码加解密功能。同样是使用了异或运算,将每个字符与其下标进行异或运算,得到加密后的密码。解密同理。
第三段代码实现了密码验证功能。首先要求用户输入密码,然后进行加密,最后与预设的密码比较。如果相同,输出 "you are right!",否则输出 "you are wrong!"。注意,这里使用了 strcmp 函数比较字符串是否相等,并且使用了 system("pause") 使程序暂停等待用户按任意键。
1.创建文件夹: #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <iostream> using namespace std; int main() { string folder_name = "new_folder"; mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); //创建文件夹 return 0; } 2.复制文件: #include <stdio.h> #include <stdlib.h> int main() { FILE *fp1, *fp2; //定义两个文件指针 char ch; fp1 = fopen("file1.txt", "r"); //打开要复制的文件 fp2 = fopen("file2.txt", "w"); //打开要复制到的文件 while ((ch = fgetc(fp1)) != EOF) { fputc(ch, fp2); //复制文件 } fclose(fp1); fclose(fp2); return 0; } 3.移动文件: #include <stdio.h> #include <stdlib.h> int main() { char old_path[100] = "old_folder/file1.txt"; char new_path[100] = "new_folder/file1.txt"; int result = rename(old_path, new_path); //移动文件 if (result == 0) { printf("移动成功\n"); } else { printf("移动失败\n"); } return 0; } 4.删除文件夹: #include <unistd.h> #include <stdio.h> int main() { char folder_name[100] = "new_folder"; int result = rmdir(folder_name); //删除文件夹 if (result == 0) { printf("删除成功\n"); } else { printf("删除失败\n"); } return 0; } 5.显示文件夹中的内容: #include <dirent.h> #include <stdio.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { printf("%s\n", ent->d_name); //遍历文件夹中的文件 } closedir(dir); return 0; } 6.查看文件内容: #include <stdio.h> int main() { FILE *fp; char ch; fp = fopen("file1.txt", "r"); //打开文件 while ((ch = fgetc(fp)) != EOF) { printf("%c", ch); //输出文件内容 } fclose(fp); return 0; } 7.修改文件权限: #include <sys/stat.h> #include <stdio.h> int main() { char file_name[100] = "file1.txt"; chmod(file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); //修改文件权限 return 0; } 8.搜索文件: #include <dirent.h> #include <stdio.h> #include <string.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; char search_name[100] = "file1.txt"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { if (strcmp(ent->d_name, search_name) == 0) //搜索文件 { printf("找到文件:%s\n", ent->d_name); break; } } closedir(dir); return 0; }将上述代码整合成一个完整的程序代码
以下是整合后的完整代码:
```
#include <iostream>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
int createFolder(string folder_name) {
int result = mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); //创建文件夹
if (result == 0) {
cout << "创建文件夹成功" << endl;
} else {
cout << "创建文件夹失败" << endl;
}
return result;
}
int copyFile(char* old_path, char* new_path) {
FILE *fp1, *fp2; //定义两个文件指针
char ch;
fp1 = fopen(old_path, "r"); //打开要复制的文件
fp2 = fopen(new_path, "w"); //打开要复制到的文件
if (fp1 == NULL || fp2 == NULL) {
cout << "打开文件失败" << endl;
return -1;
}
while ((ch = fgetc(fp1)) != EOF) {
fputc(ch, fp2); //复制文件
}
fclose(fp1);
fclose(fp2);
return 0;
}
int moveFile(char* old_path, char* new_path) {
int result = rename(old_path, new_path); //移动文件
if (result == 0) {
cout << "移动文件成功" << endl;
} else {
cout << "移动文件失败" << endl;
}
return result;
}
int deleteFolder(char* folder_name) {
int result = rmdir(folder_name); //删除文件夹
if (result == 0) {
cout << "删除文件夹成功" << endl;
} else {
cout << "删除文件夹失败" << endl;
}
return result;
}
int displayFolder(char* folder_name) {
DIR *dir;
struct dirent *ent;
dir = opendir(folder_name); //打开文件夹
while ((ent = readdir(dir)) != NULL) {
cout << ent->d_name << endl; //遍历文件夹中的文件
}
closedir(dir);
return 0;
}
int displayFile(char* file_name) {
FILE *fp;
char ch;
fp = fopen(file_name, "r"); //打开文件
if (fp == NULL) {
cout << "打开文件失败" << endl;
return -1;
}
while ((ch = fgetc(fp)) != EOF) {
cout << ch; //输出文件内容
}
fclose(fp);
return 0;
}
int changePermission(char* file_name) {
int result = chmod(file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); //修改文件权限
if (result == 0) {
cout << "修改文件权限成功" << endl;
} else {
cout << "修改文件权限失败" << endl;
}
return result;
}
int searchFile(char* folder_name, char* search_name) {
DIR *dir;
struct dirent *ent;
dir = opendir(folder_name); //打开文件夹
while ((ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name, search_name) == 0) { //搜索文件
cout << "找到文件:" << ent->d_name << endl;
closedir(dir);
return 0;
}
}
cout << "未找到文件" << endl;
closedir(dir);
return -1;
}
int main() {
string folder_name = "new_folder";
char old_path[100] = "old_folder/file1.txt";
char new_path[100] = "new_folder/file1.txt";
char file_name[100] = "file1.txt";
char search_name[100] = "file1.txt";
createFolder(folder_name);
copyFile(old_path, new_path);
moveFile(old_path, new_path);
deleteFolder("new_folder");
displayFolder("new_folder");
displayFile(file_name);
changePermission(file_name);
searchFile("new_folder", search_name);
return 0;
}
```
阅读全文
相关推荐













