帮看看下面的题目,该如何处理
帮看看下面的题目
五.编程题
1.编程读取一段文本,统计其中包含多少个一个字母的单词,两个字母的单词,等等(设文本中共只含单词,单词间用空格或标点符号分隔)
2.从键盘输入一个字符串到字符数组中,然后逆序存放
3.用公式s=1+x/1!+x^2/2!+x^3/3!+⋯,-∞<x<∞,求s的值,其中x从键盘输入,直到1/n!小于0.000001为止
4.打印如下钻石图形
*
* * *
* * * * *
* * *
*
5.求"S=a0+a1x+a2" x^2+⋯.+anx^n,其中x,n,ai从键盘输入,要求ai用数组存放
6.实现十进制到十六进制的转换
-----------------------------------------
第一题和第三题不会,
------解决方案--------------------
第一题看看行不
五.编程题
1.编程读取一段文本,统计其中包含多少个一个字母的单词,两个字母的单词,等等(设文本中共只含单词,单词间用空格或标点符号分隔)
2.从键盘输入一个字符串到字符数组中,然后逆序存放
3.用公式s=1+x/1!+x^2/2!+x^3/3!+⋯,-∞<x<∞,求s的值,其中x从键盘输入,直到1/n!小于0.000001为止
4.打印如下钻石图形
*
* * *
* * * * *
* * *
*
5.求"S=a0+a1x+a2" x^2+⋯.+anx^n,其中x,n,ai从键盘输入,要求ai用数组存放
6.实现十进制到十六进制的转换
-----------------------------------------
第一题和第三题不会,
------解决方案--------------------
第一题看看行不
- C/C++ code
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LEN 20 #define MAX_NUM 100 //*lenth[x].word[y]是单词长度为x的第y个单词 struct lenth { char *word[MAX_NUM]; int sum; //lenth[x].sum是长度为x的单词的总数 int num; //lenth[x].num是长度为x的单词第num个 }lenth[MAX_LEN] = {{ {""}, 0, 0} }; int main() { FILE *fp, *out; char p[MAX_NUM]; char *q; char temp; int i, j; //fp打开原文件word.txt读数据 if((fp = fopen("word.txt", "r")) == NULL) { printf("cannot open word.txt"); exit(0); } //out打开out.txt写处理后的数据 if((out = fopen("out.txt", "w+")) == NULL) { printf("cannot open out.txt"); exit(0); } while(!feof(fp)) { temp = fgetc(fp);//一个一个第从word.txt中读字符 //将word.txt中出英文字母外的符号全换成逗号,(但'\'换不了...) if (temp < 'A' || temp > 'z') { temp = ','; } fputc(temp, out);//一个一个地往out.txt中写word.txt中的字符(遇到非字母就换成逗号) } fclose(out);//写入完成 fclose(fp);//读出完成 if((fp = fopen("out.txt", "r")) == NULL)//用fp来读out.txt { printf("cannot open word.txt"); exit(0); } //out.txt中没有空格,只有逗号分隔开的,所以整个out.txt的内容就是一个字符串 fscanf(fp, "%s", p);//p就是out.txt的全部内容 q = strtok(p, ",");//strtok可以根据用户所提供的分割符(同时分隔符也可以为复数比如“,。”)将一段字符串分割直到遇到"\0". while (q) { i = strlen(q);//分割后字符串的长度 j = lenth[i].num; lenth[i].word[j] = q;//将长度为i的第一个字符串保存到lenth[i].word[0]中,第二个长度为i的字符串保存在lenth[i].word[1]中... q = strtok(NULL, ","); lenth[i].sum++;//长度为i的字符串的总数加1. lenth[i].num++; } //输出,只输出lenth>0的字符串 for (i = 0; i < MAX_LEN; i++) { if (lenth[i].sum > 0) { printf("lenth : %d\tsum : %d\n", i, lenth[i].sum); for (j = 0; j < lenth[i].sum; j++) { printf("%s\t", lenth[i].word[j]); } printf("\n\n"); } } fclose(fp); return 0; }
------解决方案--------------------
第三题就是求e的x次方,那个公式就是e的x方的级数展开式:
- C/C++ code
#include <stdio.h> #include <stdlib.h> #include <math.h> int factorial(int n) { if(n == 0) return 1; int result = 1; for(int i = 1; i <= n; ++i) { result *= i; } return result; } int main(int argc, char** argv) { double s = 1.0; double x; int n = 1; scanf("%lf", &x); printf("%f\n", x); while(factorial(n) < 10000000) { s += (pow(x, n) / (double)factorial(n)); ++n; } printf("%f\n", s); return 0; }