C语言课程设计--背单词系统的程序代码
- 格式:doc
- 大小:64.50 KB
- 文档页数:12
c语言管理系统设计源代码以下是一个简单的C语言管理系统示例,用于管理学生信息。
该系统可以添加、删除、修改和查找学生信息。
c复制代码#include<stdio.h>#include<stdlib.h>#include<string.h>struct student {char name[50];int roll;float marks;};struct student students[100];int count = 0;void add_student() {struct student new_student;printf("Enter name: ");scanf("%s", new_);printf("Enter roll number: ");scanf("%d", &new_student.roll);printf("Enter marks: ");scanf("%f", &new_student.marks);students[count] = new_student;count++;}void delete_student() {int index;printf("Enter index of student to delete: ");scanf("%d", &index);for (int i = index; i < count - 1; i++) {students[i] = students[i + 1];}count--;}void modify_student() {int index;struct student new_student;printf("Enter index of student to modify: "); scanf("%d", &index);printf("Enter new name: ");scanf("%s", new_);printf("Enter new roll number: ");scanf("%d", &new_student.roll);printf("Enter new marks: ");scanf("%f", &new_student.marks);students[index] = new_student;}void find_student() {char name[50];printf("Enter name of student to find: "); scanf("%s", name);for (int i = 0; i < count; i++) {if (strcmp(students[i].name, name) == 0) { printf("Roll number: %d\n", students[i].roll); printf("Marks: %.2f\n", students[i].marks); return;}}printf("Student not found.\n");}int main() {int choice;do {printf("\nMenu:\n");printf("1. Add student\n");printf("2. Delete student\n");printf("3. Modify student\n");printf("4. Find student\n");printf("5. Exit\n");printf("Enter choice: ");scanf("%d", &choice);switch (choice) {case1: add_student(); break;case2: delete_student(); break;case3: modify_student(); break;case4: find_student(); break;case5: exit(0); break; // exit the program here, otherwise the loop will keep running indefinitely, as it is in the do-while loop above. We are not returning, but rather exiting the program completely, so we do not need to return anything. The return value of main is always 0, and this is how the program exits. If you want to return a value other than 0, you can do so like this: return 1; or return -1; or return any other integer value you want to represent an error condition. The operating system will interpret this as the program's exit status. In this case, it will be seen as successful, as it is returning 0. The return value of main is not used for anything in this program, but it can be used in other programs to determine whether the program exited successfully or with an error. For example, if you were writing a shell script that executed this program and needed to know if it was successful or not, you could check the return value of the program and act accordingly. This is a common practice in programming, and it is important to understand how it works so that you can use it effectively in your own programs.。
以下是一个简单的C语言程序示例,它使用了结构体和函数指针等高级特性:c#include<stdio.h>#include<stdlib.h>// 定义一个结构体,表示一个点typedef struct {int x;int y;} Point;// 定义一个函数指针类型,表示计算两点距离的函数typedef double(*DistanceFunc)(Point a, Point b);// 计算两点之间的欧氏距离double euclideanDistance(Point a, Point b) {return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));}// 计算两点之间的曼哈顿距离double manhattanDistance(Point a, Point b) {return abs(a.x - b.x) + abs(a.y - b.y);}// 计算两点之间距离的函数,使用函数指针作为参数double calculateDistance(Point a, Point b, DistanceFunc func) {return func(a, b);}int main() {Point p1 = {1, 2};Point p2 = {4, 6};DistanceFunc funcs[] = {euclideanDistance, manhattanDistance};int numFuncs = sizeof(funcs) / sizeof(funcs[0]);for (int i = 0; i < numFuncs; i++) {printf("Distance using function %d: %f\n", i, calculateDistance(p1, p2, funcs[i]));}return0;}这个程序定义了一个Point结构体来表示二维平面上的点,然后定义了一个函数指针类型DistanceFunc 来表示计算两点距离的函数。
//1.成绩判断#include <stdio.h>int main(){//成绩int score;printf("请输入你的成绩:\n");scanf("%d", &score);//判断if(score >=0 && score < 60){printf("不及格\n");}else if(60 <= score && score < 80){printf("中等\n");}else if(80 <= score && score < 100){printf("优秀\n");}else{printf("输入错误!\n");}}//2.计算1到100的和#include <stdio.h>int main(){int sum = 0;//存结果变量int i;for(i=1;i <= 100;i++){sum = sum + i;}printf("sum=%d\n", sum);}//3.最大公约数#include <stdio.h>//求m,n的最大公约数int main(){int m, n;int i, k;printf("请输入两个数:");scanf("%d %d", &m, &n);//三元运算符找较小的那个k = m < n ? m : n;//从较小的那个数倒着往前找for(i=k; i>=1; i--){//这是公约数if((m % i == 0) && (n % i ==0)){printf("最大公约数是%d\n", i);break;//跳出for循环}}}//4.最小公倍数#include <stdio.h>//求m,n的最小公倍数int main(){int m, n;int max, min;//m,n中较大,较小的那个int k;//max, 2*max, 3*max, .....printf("请输入两个数:");scanf("%d %d", &m, &n);//也可以交换m,n,保证m小n大max = m > n ? m : n;min = m < n ? m : n;k = max;//从max开始while(k % min != 0){k += max;//每次倍增}printf("最小公倍数是%d\n", k); }//5.金字塔#include <stdio.h>//金字塔int main(){int i;//外层int j;//内层for(i=1;i<=10;i++){//当前是在第i行//先补空格10-i个for(j=1;j<=10-i;j++){printf(" ");}//再打2i-1个*for(j=1;j<=2*i-1;j++){printf("*");}printf("\n");}}//6.九九乘法表#include <stdio.h>//打印九九乘法表int main(){int i,j;for(i=1;i<=9;i++)//外层一定是9行{for(j=1; j<=i; j++)//内层第几行走几遍{printf("%d*%d=%d ", i, j, i*j);}printf("\n");}}//7.百钱买百鸡#include <stdio.h>/**百钱买百鸡,类似1,2,5凑100银币问题*/int main2(){int i,j;//公鸡,母鸡个数for(i=0; i<=20; i++)//公鸡{for(j=0; j<=33; j++)//母鸡{if( (15*i + 9*j + (100-i-j)) == 300){printf("公鸡%d,母鸡%d,小鸡%d\n", i, j, 100-i-j);}}}}//1,2,5凑100银币问题int main3(){int count = 0;//情况数int i,j;//5分个数,2分个数for(i=0; i<=20; i++)//5分个数{for(j=0; j<=50; j++)//2分个数{if( ( 5*i + 2*j ) <= 100 ){count++;printf("%d: 5分%d个,2分%d 个,1分%d个\n", count, i, j, 100-5*i-2*j);}}}}//8.一维数组的最大值、最小值、平均值#include <stdio.h>#define N 10//宏定义常量int main(){int i;//下标索引int max, min;double sum = 0;//累加和int a[N] = {58, 58, 96, 100, 25, 55, 66, 88, 99, 77};max = a[0];//假设第一个最大min = a[0];//假设第一个最小for(i=1; i<N; i++){if(a[i] > max)//比最大值还大max = a[i];//你才是最大if(a[i] < min)//比最小值还小min = a[i];//你才是最小sum += a[i];}printf("max=%d, min=%d\n", max, min);printf("average = %.2lf\n", sum/N);}//9.二维数组的最大值、最小值、平均值#include <stdio.h>int main(){int i; //第几行int j; //第几列int a[3][4] = { {1,2,3,4}, {5,-6,7,8}, {9,19,39,0}};int max = a[0][0];//假设你最大int min = a[0][0];//假设你最小double average;//平均值double sum = 0; //总和for(i=0; i<3; i++)//必定3行{for(j=0; j<4; j++)//必定4列{printf("%5d ", a[i][j]);sum += a[i][j];if(a[i][j] > max)max = a[i][j];if(a[i][j] < min)min = a[i][j];}printf("\n");}average = sum / (3*4);printf("max=%d, min=%d, avg=%.2lf\n", max, min, average);}//10.二维数组转置#include <stdio.h>//二维数组转置:行变列,列变行int main(){int i; //第几行int j; //第几列int a[3][4] = { {1,2,3,4}, {5,-6,7,8}, {9,19,39,0}};int b[4][3];for(i=0; i<3; i++){for(j=0; j<4; j++){printf("%5d", a[i][j]);}printf("\n");}//矩阵转置for(i=0; i<3; i++){for(j=0; j<4; j++){b[j][i] = a[i][j];}}for(i=0; i<4; i++){for(j=0; j<3; j++){printf("%5d", b[i][j]);}printf("\n");}}//11.冒泡排序#include <stdio.h>#define N 10//宏定义常量int main(){int i;//下标索引int j;int tmp;//临时交换用int a[N] = {58, 58, 96, 100, 25, 55, 66, 88, 99, 77};//外层循环一定是N-1for(i=0; i<N-1; i++){//两两交换,大的往后走for(j=0; j<N-i-1; j++){//交换if(a[j] > a[j+1]){tmp = a[j];a[j] = a[j+1];a[j+1] = tmp;}}}for(i=0; i<N; i++){printf("%d ", a[i]);;}printf("\n");}//12.结构冒泡排序#include <stdio.h>//结构定义,用户自定义类型typedef struct student{char sno[20];//学号char name[20];//姓名int age;//年龄char gender;//性别char tel[20];//电话};int main(){int i;int j;double sum = 0;struct student tmp;//两两交换临时用;//结构数组struct student team[5];for(i=0; i<5; i++){printf("请输入第%d个队员的信息:\n", i+1);scanf("%s %s %d %c %s", team[i].sno, team[i].name, &team[i].age, &team[i].gender, team[i].tel);}//按年龄冒泡排序for(i=0; i<5; i++){for(j=0; j<5-i-1; j++){//两两交换if(team[j].age > team[j+1].age){tmp = team[j];team[j] = team[j+1];team[j+1] = tmp;}}}//取值printf("%-12s %-10s %-5s %-5s %-15s\n", "学号", "姓名", "年龄", "性别", "电话");for(i=0; i<5; i++){printf("%-12s %-10s %-5d %-5c %-15s\n", team[i].sno, team[i].name, team[i].age, team[i].gender, team[i].tel);}}//13.结构数组找年龄最大值#include <stdio.h>//结构定义,用户自定义类型typedef struct student{char sno[20];//学号char name[20];//姓名int age;//年龄char gender;//性别char tel[20];//电话};int main(){int i;struct student tmp;//找最大临时用//结构数组struct student team[5];for(i=0; i<5; i++){printf("请输入第%d个队员的信息:\n", i+1);scanf("%s %s %d %c %s", team[i].sno, team[i].name, &team[i].age, &team[i].gender, team[i].tel);}//取值printf("%-12s %-10s %-5s %-5s %-15s\n ", "学号", "姓名", "年龄", "性别", "电话");for(i=0; i<5; i++){printf("%-12s %-10s %-5d %-5c %-15s\ n", team[i].sno, team[i].name, team[i].age, team[i].gender, team[i].tel);}//找学号最大的那一个tmp = team[0];for(i=1; i<5; i++){if(strcmp(team[i].sno,tmp.sno) >0 ){tmp = team[i];}}printf("学号最大的队员如下:\n");printf("%-12s %-10s %-5d %-5c %-15s\ n", tmp.sno, , tmp.age, tmp.gender, tmp.tel);}//14.文件读写#include <stdio.h>#include <stdlib.h>//结构定义,用户自定义类型typedef struct student{char sno[20];//学号char name[20];//姓名int age;//年龄char gender;//性别char tel[20];//电话};//文件读写int main(){struct student * s, * p1;//个数未知FILE * fp;int i, n = 0;char buf[1024];//fgets缓冲区//打开文件fp = fopen("e:\\test.txt", "r");while(fgets(buf, 1024, fp) != NULL)n++;fclose(fp);//指向一个可以存储n个student结构的内存空间s = (struct student *)malloc(sizeof(struct student) * n);p1 = s;//不要动头位置s的值//打开文件fp = fopen("e:\\test.txt", "r");for(i=0; i<n; i++){//从文件中读入一行fscanf(fp, "%s %s %d %c %s", p1->sno, p1->name, &p1->age, &p1->gender, p1->tel);p1++;}fclose(fp);p1 = s;for(i=0; i<3; i++){printf("%s %s %d %c %s\n", p1->sno, p1->name, p1->age, p1->gender, p1->tel);}free(s);}//15.输入三角形三边长计算周长和面积#include<stdio.h>#include<math.h>int main(){double area,perimeter,s,a,b,c;printf("请输入三边长a b c:");scanf("%lf%lf%lf",&a,&b,&c);if((a+b>c) && (a+c>b) && (b+c>a)){s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c));perimeter=a+b+c;printf("area=%.2f,perimeter=%.2f\ n",area,perimeter);}else{printf("三边长无法构成三角形。
c语言初学必背代码C 语言初学必背代码C 语言作为一门基础的编程语言,对于初学者来说,掌握一些关键的代码片段是非常有帮助的。
这些代码不仅能够帮助你理解 C 语言的基本语法和概念,还能为你后续的学习打下坚实的基础。
接下来,让我们一起看看 C 语言初学必背的代码。
一、输出“Hello World”这可能是学习任何编程语言的第一步,它简单却具有象征意义。
```cinclude <stdioh>int main(){printf("Hello World\n");return 0;}```在这个代码中,`include <stdioh>`是预处理指令,用于包含标准输入输出头文件。
`main`函数是 C 语言程序的入口点。
`printf`函数用于输出指定的内容,`\n`是换行符。
二、变量的定义和使用```cinclude <stdioh>int main(){int num = 10; //定义一个整型变量并初始化float price = 125; //定义一个浮点型变量并初始化char letter ='A';//定义一个字符型变量并初始化printf("num =%d\n", num);printf("price =%f\n", price);printf("letter =%c\n", letter);return 0;}```在上述代码中,我们定义了整型、浮点型和字符型的变量,并使用`printf`函数输出它们的值。
其中,`%d`用于输出整型,`%f`用于输出浮点型,`%c`用于输出字符型。
三、算术运算```cint main(){int a = 5, b = 3;int sum = a + b;int difference = a b;int product = a b;int quotient = a / b;printf("sum =%d\n", sum);printf("difference =%d\n", difference);printf("product =%d\n", product);printf("quotient =%d\n", quotient);return 0;}```这里展示了 C 语言中的基本算术运算:加法、减法、乘法和除法。
c语言入门源代码C语言作为一种非常流行和广泛应用的编程语言,在计算机科学领域发挥了重要作用。
掌握C语言编程的基本知识和技能是初学者的首要任务。
本文将为你提供一些C语言入门的源代码示例,帮助你更好地理解和掌握C语言编程。
代码示例一:Hello World#include <stdio.h>int main() {printf("Hello World!\n");return 0;}这是一个经典的C语言程序,用于向屏幕输出"Hello World!"。
在C 语言中,使用printf函数来实现输出操作。
其中,"\n"表示换行符,用于使输出在新的一行显示。
代码示例二:求和#include <stdio.h>int main() {int num1, num2, sum;printf("请输入两个整数:\n");scanf("%d%d", &num1, &num2);sum = num1 + num2;printf("两个数的和为:%d\n", sum);return 0;}这段代码演示了如何实现两个整数的求和。
首先,使用printf函数提示用户输入两个整数。
接下来,使用scanf函数从用户处获取输入的整数值,并将它们分别赋给num1和num2变量。
然后,将num1和num2变量相加得到sum变量的值,并使用printf函数输出结果。
代码示例三:判断奇偶数#include <stdio.h>int main() {int num;printf("请输入一个整数:\n");scanf("%d", &num);if(num % 2 == 0) {printf("%d是偶数。
\n", num);}else {printf("%d是奇数。
#include <iostream>#include <stdio.h>#include <windows.h> //包涵暂停函数的头文件#include <time.h>using namespace std;int s3,s4;char selec;int time1,time2;class YouXi{public:void score(int score1,int right); //声明战况子函数void welcome(); //声明主菜单界面void number(); //声明数字练习函数void time(); //声明倒计时函数void sellet(); //声明字母选择菜单界面函数void letter(int x); //声明随机生成字母函数void all();void end();private:};int main(){time1=::time(NULL); //记录用户开始时的时间system("cls");YouXi Game;Game.welcome(); //调用主菜单return 0;}void YouXi::welcome() //定义主菜单函数{for(int i=0;i<3;i++){system("cls"); //执行DOS下的清屏命令cout<<"\n\n\n\n\n\n\n\n\n\n\t\t\tloading";for(int j=0;j<10;j++){Sleep(80);cout<<".";}}do{system("cls");cout<<"\n\n\n\n"<<endl;cout<<"\n\t\t\t********************************"<<endl;cout<<"\n\t\t\t* 欢迎挑战*"<<endl;cout<<"\n\t\t\t********************************"<<endl;cout<<"\n\t\t\t* *"<<endl;cout<<"\n\t\t\t* 1.数字记忆*"<<endl; // \n换行,\t水平制表(跳到下一个Tab位置)cout<<"\n\t\t\t* 2.字母记忆*"<<endl;cout<<"\n\t\t\t* 3.键盘记忆*"<<endl;cout<<"\n\t\t\t* 4.退出*"<<endl;cout<<"\n\t\t\t* *"<<endl;cout<<"\n\t\t\t********************************"<<endl;cout<<"\n\t\t\t请选择:";int i;cin>>i;switch(i){case 1:time();number();break;case 2:sellet(); //调用字母选择大小写菜单break;case 3:time();all(); //全键盘练习break;case 4:end();break;default:cout<<"您的输入有误!按任意键继续......";getchar();getchar();}}while(1);}void YouXi::time() //定义倒计时子函数{system("cls");cout<<"\n\n\n\n\n\t\t\t\tloading...\n\n\n\n\n"<<endl;for(int i=0;i<80;i++){cout<<">";Sleep(20);}}void YouXi::number(){int s1=0,s2=1,input,eng; //定义局部变量s1,s2用于随机函数的个数,eng,input随时产生数字与用户入.do{system("cls");srand((unsigned)::time(NULL)); //以当前时间作为随机种子eng=((s1=s1*10+1)+rand()%((s2=s2*10)-s1)); //随机产生一个数s4++; //统计总个数cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\n\t\t\t请记住此数字:"<<eng<<endl; //显示此数字Sleep(500);system("cls");cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\n\t\t\t请输入刚才的数字:";cin>>input;if(input==eng) //判断数字是否相等{s3++; //统计总正确数system("cls");cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\n"<<endl;cout<<"\t\t\t恭喜您!进入下一关!"<<endl;Sleep(600);system("cls");continue;}else{do{system("cls");cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\t\t\t输错了,是否继续?(y/n):";cin>>selec;if(selec=='y'||selec=='Y'){s1=s1/10; //恢复错误时的数字个数(例如:随机产生一个五位数,我们输入的与计算机产生的数值不匹配,所以要重新输入5位数与计算机随机产生的数值匹配的关卡,至到输入正确为止)s2=s2/10; //恢复错误时的数字个数(例如:随机产生一个五位数,我们输入的与计算机产生的数值不匹配,所以要重新输入5位数与计算机随机产生的数值匹配的关卡,至到输入正确为止)break;}else if(selec=='n'||selec=='N')score(s4,s3); //调用战况子函数}while(1);}}while(1);}void YouXi::sellet(){do{system("cls");cout<<"\n\n\n\n\t\t\t********************"<<endl;cout<<"\n\t\t\t* *"<<endl;cout<<"\n\t\t\t* 1.大写字母*"<<endl;cout<<"\n\t\t\t* 2.小写字母*"<<endl;cout<<"\n\t\t\t* 3.返回*"<<endl;cout<<"\n\t\t\t* *"<<endl;cout<<"\n\t\t\t********************"<<endl;cout<<"\n\t\t\t请选择:";int i;cin>>i;switch(i){case 1:time(); //调用倒计时子函数letter(1); //调用字母练习子函数(并告诉函数是大写字母)break;case 2:time(); //调用倒计时子函数letter(2); //调用字母练习子函数(并告诉函数是小写字母)break;case 3:welcome(); //调用欢迎界面default:cout<<"您的输入有误!按任意键继续...";getchar();getchar();}}while(1);}void YouXi::letter(int x){int n=1; //初始化循环变量值//int c;char eng[100],input[100]; //用于存放产生的字符串与用户输入的字符串srand((unsigned)::time(NULL)); //以当前时间为种子do{system("cls");cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\n\t\t\t请记住这个字母:";if(x==1)for(int i=0;i<n;i++){eng[i]=65+rand()%(90-65); //随机产生一个大写字母cout<<eng[i]; //输出一个大写字母}elsefor(int i=0;i<n;i++){eng[i]=97+rand()%(122-97); //随机产生一个小写字母cout<<eng[i]; //输出一个小写字母}eng[n]='\0'; //给数组加一个结束标志s4++; //统计字母总个数Sleep(1000);system("cls");cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\n\t\t\t请输入刚才的字母:";cin>>input;if(strcmp(eng,input)==0) //比较两个字符串是否相同{s3++; //记录正确个数n++;system("cls");cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\n\t\t\t恭喜您!进入下一关!"<<endl;Sleep(600);system("cls");continue;}else{do{system("cls");cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\n\t\t\t输错了!是否继续?(y/n):";cin>>selec;if(selec=='y'||selec=='Y')break;else if(selec=='N'||selec=='n')score(s4,s3); //调用战况子函数}while(1);}}while(1);}void YouXi::all(){int n=1;do{char input[100],eng[100]; //存放全键盘随时产生字符串与用户输入字符串system("cls");cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\n\t\t\t请记住这个字符串:";for(int i=0;i<n;i++){eng[i]=33+rand()%(125-33); //随机产生全键盘字符cout<<eng[i];}eng[n]='\0';s4++; //记录总个数Sleep(1000);system("cls");cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\n\t\t\t请输入刚才的字符串:";cin>>input;Sleep(1000);if(strcmp(eng,input)==0) //判断字符串是否相同{s3++;n++;system("cls");cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\n\t\t\t恭喜您!进入下一关!"<<endl;Sleep(600);system("cls");continue;}else{do{system("cls");cout<<"\n\n\n\n\t\t ***************游戏中***************"<<endl;cout<<"\n\n\n\t\t\t输错了!是否继续?(y/n)";cin>>selec;if(selec=='Y'||selec=='y')break;else if(selec=='N'||selec=='n')score(s4,s3); //调用战况子函数}while(1);}}while(1);}void YouXi::score(int score1,int right){time2=::time(NULL); //记录用户结束时时间do{system("cls");cout<<"\n\n\n\n"<<endl;cout<<"\n\t\t\t***************************"<<endl;cout<<"\n\t\t\t* ******战况****** *"<<endl;cout<<"\n\t\t\t* *"<<endl;cout<<"\n\t\t\t* 总数:"<<score1<<" *"<<endl;cout<<"\n\t\t\t* 正确:"<<right<<" *"<<endl;cout<<"\n\t\t\t* 错误:"<<score1-right<<" *"<<endl;printf("\n\t\t\t* 正确率:%3.0f *",(double)right/score1*100);// cout<<"\n\t\t\t* 正确率:"<<(double)right/score1*100<<" *"<<endl;cout<<"\n\n\t\t\t* 运行时间:"<<time2-time1<<" *"<<endl;cout<<"\n\t\t\t* *"<<endl;cout<<"\n\t\t\t***************************"<<endl;cout<<"\n\t\t\t确定退出游戏?(y/n):";cin>>selec;if(selec=='y'||selec=='Y')end();else if(selec=='n'||selec=='N')main();}while(1);}void YouXi::end(){system("cls");cout<<"\n\n\n\n"<<endl;cout<<"\n\t\t\t********************"<<endl;cout<<"\n\t\t\t* *"<<endl;cout<<"\n\t\t\t* 感谢您的使用! *"<<endl;cout<<"\n\t\t\t* Goodbye! *"<<endl;cout<<"\n\t\t\t* *"<<endl;cout<<"\n\t\t\t********************\n\n\n\n\n\n\n\t\t\t"<<endl;exit(0);}。
C语言入门训练代码(10条)1. 输入n,计算出n的阶乘。
#include <stdio.h>int main(){int n, i;long long ret = 1;printf("请输入一个正整数:");scanf("%d", &n);for (i=1; i<=n; ++i){ret *= i;}printf("%d的阶乘是%lld\n", n, ret);return 0;}2. 编写一个程序,将一个字符串中的字母全部转换为大写字母。
#include <stdio.h>#include <ctype.h>#define MAX_LEN 100int main(){char str[MAX_LEN + 1];int i;printf("请输入一个字符串:");fgets(str, MAX_LEN, stdin);for (i=0; str[i]!='\0'; ++i){str[i] = toupper(str[i]);}printf("转换后的字符串是:%s", str);return 0;}3. 输入两个正整数m和n,求它们的最大公约数和最小公倍数。
#include <stdio.h>int main(){int m, n, i, gcd, lcm;printf("请输入两个正整数m和n:");scanf("%d %d", &m, &n);for (i=1; i<=m && i<=n; ++i){if (m%i==0 && n%i==0){gcd = i;}}lcm = m*n / gcd;printf("%d和%d的最大公约数是%d,最小公倍数是%d\n", m, n, gcd, lcm);return 0;}4. 输入一组数据,计算它们的平均值以及大于平均值的个数。
【C语⾔程序设计】C语⾔统计单词个数,单词个数算法在实际⽣活中经常会遇到⼀个问题:写英语作⽂时,常常要求满⾜⼀定的字数。
在以往,要么我们⼀个⼀个地数;要么我们估算⼀⾏的单词数,然后⽤⾏数进⾏估算。
第⼀种⽅法太费时,若是长篇⼤论,那⼏乎是不可能统计的;⽽第⼆种⽅法不太准确。
这就给我们留下了⼀个问题:如何⼜快、⼜准确地统计⼀篇英⽂⽂章中的单词数?算法思想要解决这个问题,最⾃然的算法是,读取⽂章的所有内容,然后⼀个单词⼀个单词地统计,然⽽,我们在这⾥遇到了⼀个难题:程序看不懂英⽂,如何知道什么是⼀个单词,什么不是⼀个单词呢?似乎在这⾥遇到了障碍,可是,如果换个⾓度思考问题,也许会柳暗花明⼜⼀村:⽂章中的单词都是⽤空格间隔开的,换句话说,单词数=空格数+1。
程序不认识单词,但是程序认识空格啊!这样,整个问题实际上转换成了统计⽂章中的空格数。
有了这样的问题转换思路,整个问题就简单多了。
可以先按照这个思路⾃⼰实现,也可以直接看下⾯的代码实现。
程序代码#include <stdio.h>int main(){printf("输⼊⼀⾏字符:\n");char ch;int i,count=0,word=0;while((ch=getchar())!='\n')if(ch=='')word=0;else if(word==0){word=1;count++;}printf("总共有 %d 个单词\n",count);return0;}调试运⾏结果根据程序提⽰,分别输⼊“I love China”和“I love Xichang College”两个字符串代码,程序统计单词后输出结果分别如下所⽰:输⼊⼀⾏字符:I Love China总共有 3 个单词输⼊⼀⾏字符:I Love Peking University总共有 4 个单词总结本实例展⽰了如何对字符数组进⾏操作,类型为字符型的数组称为字符数组,C 语⾔中没有专门的字符串变量,但是有字符数组串常量,所以字符串常量的存储是通过对字符数组的操作来完成的。
沈阳工程学院课程设计设计题目:电子词典系别信息工程系班级计本083 学生姓名学号 22指导教师姜柳、吕海华职称讲师、讲师起止日期:2009年6月15日起——2009年6月26日止沈阳工程学院课程设计任务书课程设计题目:电子词典(第 1 组)系别信息工程系班级计本075学生姓名于满盛学号 2008412322指导教师姜柳、吕海华职称讲师、讲师课程设计进行地点:实训F任务下达时间: 2009年 6月 15日起止日期:2009年6月15日起——2009年6月26日止教研室主任姜柳 2009年6月15日批准一、课程设计的原始资料及依据在计算机中建立有限规模的电子英汉词典,利用程序实现电子英汉词典的查找、增加、删除、修改等功能。
查阅有关资料,进一步理解程序设计模块化的思想,并利用此思想编写一个简单的电子词典。
通过本设计可以加深理解利用程序设计思想开发一个系统的整个流程,提高分析问题、解决问题和实际动手的能力。
二、课程设计主要内容及要求1.认真阅读资料,掌握程序设计模块化的思想。
2.要求在设计的过程中,建立清晰的层次结构。
3.画出主要的功能结构图和主要模块的流程图。
4.实现功能:⑴查找单词。
⑵增加单词。
⑶万年历⑷修改单词。
⑸显示单词。
⑹保存单词。
⑺返回⑻帮助⑼退出5. 要求操作简单,用户界面友好。
6. 运行程序,检查结果是否和理论值一致。
7. 环境使用Windows,Turbo C环境。
三、对课程设计说明书撰写内容、格式、字数的要求1.课程设计说明书是体现和总结课程设计成果的载体,主要内容包括:设计题目、设计目的、设备器材、设计原理及内容、设计步骤、遇到的问题及解决方法、设计总结、参考文献等。
一般不应少于3000字。
2.在适当位置配合相应的实验原理图、功能模块图、算法流程图等图表进行说明。
应做到文理通顺,内容正确完整,书写工整,装订整齐。
3.设计总结部分主要写本人完成工作简介以及自己的设计体会,包括通过课程设计学到了什么,哪里遇到了困难,解决的办法以及今后的目标。
英汉电子词典设计报告设计C语言C语言程序设计设计报告:英汉电子词典设计目标:本次设计的目标是开发一个基于C语言的英汉电子词典,实现用户通过输入英文单词或汉字查询其对应的中文释义或英文翻译。
同时,用户还可以对查询结果进行添加、修改、删除操作,方便用户自定义个性化词库。
设计思路:1.使用C语言实现用户界面,包括菜单选项和用户输入功能;2.使用文件管理系统进行词库的存储和读取,并实现对词库的增删改查功能;3.使用字符串匹配算法进行单词或汉字的查询,保证查询的精确性;4.使用二叉查找树(BST)作为数据结构,实现词库的快速查找和插入功能。
模块设计:1.用户界面模块:-显示菜单选项,包括查询、添加、修改、删除和退出;-提示用户输入相应选项,并获取用户输入;2.文件管理模块:-创建存储词库的文件,并检查文件是否存在;-实现读取文件内容到内存和将内存内容写入文件的功能;3.数据结构模块:-设计结构体,包含英文单词和中文释义;-使用二叉查找树作为词库的数据结构,实现快速查找和插入功能;4.查询模块:-根据用户输入的英文单词或汉字,进行查询;-使用字符串匹配算法进行匹配,找到对应的词条并显示;5.添加模块:-接受用户输入的英文单词和中文释义,并将其插入词库中;6.修改模块:-接受用户输入的英文单词和新的中文释义,并替换原有词条的中文释义;7.删除模块:-根据用户输入的英文单词或汉字,从词库中删除对应的词条;测试计划:-对每个模块进行单元测试,确保其功能正常;-集成测试,检查各个模块之间的交互是否正常;-进行用户测试,验证整个电子词典系统的可用性和稳定性。
总结:通过以上的设计,我们可以实现一个基于C语言的英汉电子词典,能够方便用户进行英文单词和汉字的查询,并支持用户对词库进行个性化操作。
该电子词典设计简洁,功能完备,并且具有较好的扩展性,适用于不同平台和系统的实际应用。
综合性程序设计报告设计题目:背单词系统指导教师:班级:学号:设计者:成绩:设计时间:2010年5月27日目录1 题目描述 (3)2 变量、函数和结构体说明 (4)3 树形结构图 (5)4 用户操作说明 (6)5 代码 (8)6设计体会 (12)1 题目描述该系统是帮助学生背诵单词的软件,有词语预览功能;用户可以编辑自己的词库,即可增加或删除单词;系统可以进行测试,即给出中文,让学生输入其英文,每十个单词一组,测试结束能够显示成绩,即单词的正确个数;能够输入中文或英文进行单词的查询。
基本功能:词库的增加或删除;单词预览;显示中文用户输入英文进行测试;用户输入中文或英文进行单词的查询。
2变量、函数和结构体说明(1)关键变量说明int x 结构体数组中元素的数目 int n 测试时单词的正确个数int a 进行菜单选择int m 进行菜单选择(2)函数定义说明void foresee(); 单词预览及进行测试int intest(); 产生随机单词以进行测试void test2(int n); 测试单词的成绩void testchinese(); 测试单词void translation(); 单词查询void EtoC(); 输入单词,查询意思void CtoE(); 输入中文,查询单词void word_add(); 单词添加void word_delete(); 单词删除(3)结构体说明struct words 单词结构体定义{char eword[20]; 单词char cixing[10]; 词性char chinese[50]; 意思}struct words bank[SIZE] 定义数组结构体,存储单词3 树形结构图4 用户操作说明运行程序后进行选择选择1.单词预览是否要运行测试功能?Y\N输入Y:进行单词测试输入N:返回主菜单选择2.英汉互译1.英译汉2.汉译英3.返回上级菜单选择3.单词添加选择4.单词删除选择5.退出系统5 代码程序如下:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<conio.h>#include<time.h>#define SIZE 50 //定义词库单词数量void foresee(); //单词预览及进行测试int intest(); //产生随机单词以进行测试void test2(int n); //测试单词的成绩void testchinese(); //测试单词void translation(); //单词查询void EtoC(); //输入单词,查询意思void CtoE(); //输入中文,查询单词void word_add(); //单词添加void word_delete(); //单词删除struct words{char eword[20];char cixing[10];char chinese[50];}; //单词结构体定义int x;struct words bank[SIZE]={{"aspect","n.","方面,朝向,样子"},{"according","ad.","依照"},{"accustomde","a.","惯常的,习惯的"},{"basis","n.","基础,根据,原则"},{"confidence","n.","信心,信任"},{"dismiss","vt.","不再考虑,解散"},{"economy","n.","经济,节约,节省"},{"fatal","a.","致命的,命运的"},{"global","a.","全球的,总的"},{"helpful","a.","给予帮助的,有用的"},{"impose","vt.","把……加强,征"},{"largely","ad.","大部分,大量的"},{"mystery","n.","神秘,神秘的人或事"},{"nuclear","a.","核子的,核能的"},{"obtain","v.","获得,通用"},{"occasion","n.","场合,时刻"},{"particularly","ad.","尤其,特别"},{"patient","a.","忍耐的,有耐心的"},{"railway","n.","铁路"},{"reaction","n.","反应,反作用"},{"remote","a.","遥远的,偏僻的"},{"selection","n.","选择,挑选"},{"setting","n.","环境,背景"},{"shrink","v.","起皱,收缩"},{"translation","n.","翻译,译文"},{"troublesome","a.","令人烦恼的,麻烦的"},{"undergo","vt.","经历,经受"},{"unlike","prep.","不像"},{"volunteer","n.","志愿者"},{"virtue","n.","善,美德"},{"wideapread","a.","分布广泛的,普通的"},{"withdraw","v.","收回,撤回"},{"absence","n.","缺席,不在"},{"abstract","a.","抽象的,抽象派的"},{"bacteria","n.","细菌"},{"biology","n.","生物学,生态学"},{"characterize","vt.","成为…的特征"},{"debate","n.,vi.","争论,辩论"},{"display","vt.","陈列,展览"}};//词库void test2(int n) //测试单词的成绩{switch(n){case 0:case 1:case 2:case 3:printf("正确个数:%d \n还需努力!",n);getch();break;case 4:case 5:case 6:printf("正确个数:%d \n还不错哈!",n);getch();break;case 7:case 8:case 9:case 10:printf("正确个数:%d \nGreat!好样的!",n);getch();break;default:printf("系统故障!");getch();}}int intest() //产生随机单词以进行测试{srand( (unsigned)time( NULL ) );return (rand() %39+1);}void testchinese() //测试单词{int i,j=0,a;char word[30];for(i=0;i<10;i++){ a=intest();printf("%s\n请输入对应的单词:",bank[a].chinese);scanf("%s",word);if(strcmp(bank[a].eword,word)==0)j++;}test2(j);}void foresee() //单词预览及进行测试{int a;char ch;puts("单词词性意思");for(a=0;a<SIZE;a++)printf("%-16s %-8s %-20s\n",bank[a].eword,bank[a].cixing,bank[a].chinese);printf("是否要运行测试功能?N/Y\n");fflush(stdin);ch=getchar();if(ch=='y'||ch=='Y'){system("cls");testchinese();}}void EtoC() //输入单词,查询意思{char word[20];int i=0;puts("请输入您要查询的单词:");scanf("%s",word);while(strcmp(bank[i].eword,word)!=0&&i<SIZE)i++;if(i<SIZE)printf("\n该单词的中文意思为:%s",bank[i].chinese);else puts("抱歉,没有查到您要找的单词。