当前位置:首页 » 编程语言 » c语言例程

c语言例程

发布时间: 2022-01-15 04:33:18

1. c语言编程实例

第一个问题用switch语句
第二个用if else语句的嵌套

呵呵不好意思 我比较懒不想打代码了

2. C语言编程实例

首先main中对x进行赋值,是12345678这个无符号长整形数,然后传到part()函数中,结构体那部分不用考虑,其它地方也没有对num进行更改,不会对内容有影响,所以输出结果为12345678;
然后p=&n把n的地址赋给指针p,n.w=num把12345678赋给w,此时结构体n的地址其实就是12345678所在的地址,p指向n,同时结构体*p中的数组part的初始地址也在p上,也就是说p指向的数组part于n.w的首地址是相同的;
看你的输出结果,你的C中int和long应该都是32位吧,所以输出part[0]就是输出part地址上的前32位,即num,part[1]上都是空的,什么也没有,所以就是8个C;
PS:我想这个程序的意义是把long型整数进行拆分,分别输出高低位两部分,但是你的C中的int和long都是32位,所以没成功吧
呵呵,也不知道说的对不,C很久没用了

3. c语言 程序

这个是比较早期的作品。。。没有文件读取和用户界面

#include <iostream>
#include <cstring>
struct st
{
int no;
char name[20];
int math;
int eng;
int pro;
int sum;
double ave;
};
struct Node
{
st content;
Node *next;
};
void input(st ,Node *&head);
void output(Node *head);
void del(int a,Node *&head);
void out(Node *p);
using namespace std;
Node *head=NULL;
int main()
{
A:
cout << "请不要搞恶,会崩的!!!!\n";
cout << "请输入要求操作的代号\n";
cout << "1 添加新数据。\n";
cout << "2 查找数据。\n";
cout << "3 修改数据。\n";
cout << "4 删除数据。\n";
cout << "5 统计。\n";
cout << "6 说明文档\n";
cout << "7 退出。\n";
int choice;
cin >> choice;
switch (choice)
{
case 1:
{
cout << "开始输入,请依次输入这位同学的学号、姓名、数学、英语、程序设计的成绩;输完请输-1" << endl;
st cont;
for(;;)
{
cin >> cont.no ;
if(cont.no==-1) break;
cin >> cont.name >> cont.math >> cont.eng >> cont.pro;
cont.sum = cont.math + cont.eng + cont.pro;
cont.ave = cont.sum / 3.0;
input(cont,head);
}
break;
}
case 2:
{
if(head==NULL) {cout << "已经为空!!\n"; break;}
cout << "请输入要查找的类型" <<endl;
cout << "1 学号\n" ;
cout << "2 姓名\n" ;
cout << "3 数学成绩\n" ;
cout << "4 英语成绩\n" ;
cout << "5 程序设计成绩\n" ;
cout << "6 总成绩\n" ;
cout << "7 平均成绩\n" ;
int type;
cin >> type;
switch (type)
{
case 1:
{
int target;
cout << "请输入学号。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.no==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.no==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 2:
{
char target[20];
cout << "请输入姓名。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(strcmp(target , p->content.name)==0)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(strcmp(target , p->content.name)==0)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 3:
{
int target;
cout << "请输入数学成绩。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.math==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.math==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 4:
{
int target;
cout << "请输入英语成绩。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.eng==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.eng==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 5:
{
int target;
cout << "请输入程序设计成绩。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.pro==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.pro==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 6:
{
int target;
cout << "请输入总成绩。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.sum==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.sum==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 7:
{

int target;
cout << "请输入平均成绩。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.ave==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.ave==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
default:cerr << "跟你说了别恶搞,真的会崩的\n" ;
}//switch (type)
break;
}
case 3:
{
if(head==NULL) {cout << "已经为空!!\n"; break;}
cout << "请输入要修改的数据的编号"<<endl;
int a;
cin >> a;
cout << "请输入要更改的类型"<<endl;
cout << "1 学号\n" ;
cout << "2 姓名\n" ;
cout << "3 数学成绩\n" ;
cout << "4 英语成绩\n" ;
cout << "5 程序设计成绩\n" ;
int typ;
cin >> typ;
switch(typ)
{
case 1:
{
cout << "请输入新值。" <<endl;
int n;
cin >> n;
Node *p=head;
if(a==1)
{
p->content.no = n;
}
else
{
for(int i=1;i<a;i++)
{
p=p->next;
}
p->content.no = n;
}
break;
}
case 2:
{
cout << "请输入新值。" <<endl;
char n[20];
cin >> n;
Node *p=head;
if(a==1)
{
strcpy(p->content.name , n);
}
else
{
for(int i=1;i<a;i++)
{
p=p->next;
}
strcpy(p->content.name , n);
}
break;
}
case 3:
{
cout << "请输入新值。" <<endl;
int n;
cin >> n;
Node *p=head;
int temp ;
if(a==1)
{
temp=p->content.math ;
p->content.math = n;
}
else
{
for(int i=1;i<a;i++)
{
p=p->next;
}
temp=p->content.math ;
p->content.math = n;
}
p->content.sum=p->content.sum - temp + n;
p->content.ave=p->content.sum/3.0;
break;
}
case 4:
{
cout << "请输入新值。" <<endl;
int n;
cin >> n;
Node *p=head;
int temp ;
if(a==1)
{
temp=p->content.eng ;
p->content.eng = n;
}
else
{
for(int i=1;i<a;i++)
{
p=p->next;
}
temp=p->content.eng;
p->content.eng = n;
}
p->content.sum=p->content.sum - temp + n;
p->content.ave=p->content.sum/3.0;
break;
}
case 5:
{
cout << "请输入新值。" <<endl;
int n;
cin >> n;
Node *p=head;
int temp ;
if(a==1)
{
temp=p->content.pro ;
p->content.pro = n;
}
else
{
for(int i=1;i<a;i++)
{
p=p->next;
}
temp=p->content.pro;
p->content.pro = n;
}
p->content.sum=p->content.sum - temp + n;
p->content.ave=p->content.sum/3.0;
break;
}
default:cerr << "跟你说了别恶搞,真的会崩的\n" ;
}//switch(typ)
break;
}
case 4:
{
if(head==NULL) {cout << "已经为空!!\n"; break;}
cout << "请输入要删除的数据的位置" <<endl;
int loc;
cin >> loc;
del(loc,head);
break;
}
case 5:
{
if(head==NULL) {cout << "已经为空!!\n"; break;}

cout << "请选择统计类型" << endl;
cout << "1 每门课90分以上的" <<endl;
cout << "2 每门课60分以下的" <<endl;
cout << "3 单科分数排名" <<endl;
cout << "4 总分排名" <<endl;
int tp;
cin >> tp;
switch (tp)
{
case 1:
{
cout << "选择科目\n" << "1 数学\n2 英语\n3 程序设计\n";
int dec;
cin >> dec;
if(dec == 1)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.math>90) out(q);
}
if(q->content.math>90) out(q);
}
else if(dec == 2)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.eng>90) out(q);
}
if(q->content.eng>90) out(q);
}
else if(dec == 3)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.pro>90) out(q);
}
if(q->content.pro>90) out(q);
}
else
cout << "输入错误\n";
break;
}
case 2:
{
cout << "选择科目\n" << "1 数学\n2 英语\n3 程设\n";
int dec;
cin >> dec;
if(dec == 1)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.math<60) out(q);
}
if(q->content.math<60) out(q);
}
else if(dec == 2)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.eng<60) out(q);
}
if(q->content.eng<60) out(q);
}
else if(dec == 3)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.pro<60) out(q);
}
if(q->content.pro<60) out(q);
}
else
cout << "输入错误\n";
break;
}
case 3:
{
cout << "选择科目\n" << "1 数学\n2 英语\n3 程序设计\n";
int dec;
cin >> dec;
if(dec == 1)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.math > q_max->content.math)
q_max=q2;
if(q1->content.math !=q_max->content.math )
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}

}
output(head);

}
else if(dec == 2)
{
for(Node *q=head;q->next!=NULL;q=q->next)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.eng > q_max->content.eng )
q_max=q2;
if(q1->content.eng !=q_max->content.eng )
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}

}
}
output(head);
}
else if(dec == 3)
{
for(Node *q=head;q->next!=NULL;q=q->next)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.pro > q_max->content.pro)
q_max=q2;
if(q1->content.pro!=q_max->content.pro)
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}
}

}
output(head);
}
else
cout << "输入错误\n";
break;
}
case 4:
{
for(Node *q=head;q->next!=NULL;q=q->next)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.sum > q_max->content.sum)
q_max=q2;
if(q1->content.sum!=q_max->content.sum)
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}
}

}
output(head);
break;
}
default:cerr << "跟你说了别恶搞,真的会崩的\n" ;
}//switch(tp)
break;
}
case 6:
{
cout <<"################################################################################\n";
cout <<"这是B版,容易崩溃,请按照正常方式输入,如果您想测试它是否会崩,那肯定崩!!!!\n";
cout <<endl;
cout <<"################################################################################\n";
break;
}
case 7:
{
cout << "谢谢使用,没崩说明你强\n";
return 0;
break ;
}
default:cerr << "跟你说了别恶搞,真的会崩的\n" ;
} //switch(choice)
goto A;
}

void del (int a,Node *&head)
{
if(head==NULL) {cout << "已经为空!!\n" <<endl;return ;}
Node *p=head;
if(a==1)
{
head=p->next;
delete p;
}
else
{
Node *q=p->next;
for(int i=1;i<a-1;i++)
{
p=p->next;
q=p->next;
}
p->next=q->next;
delete q;
}
}
void out(Node *p)
{

cout << p->content.no <<'\t';
cout << p->content.name <<'\t';
cout << p->content.math <<'\t';
cout << p->content.eng <<'\t';
cout << p->content.pro <<'\t';
cout << p->content.sum <<'\t';
cout << p->content.ave <<'\n';
}

void output(Node *head)
{
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for (Node *p=head;p!=NULL;p=p->next)
{
cout << p->content.no<<'\t';
cout << p->content.name<<'\t';
cout << p->content.math<<'\t';
cout << p->content.eng <<'\t';
cout << p->content.pro<<'\t';
cout << p->content.sum<<'\t';
cout << p->content.ave<<'\t';
cout << endl;
}
}

void input(st cont,Node *&head)
{
Node *p=new Node;
p->content=cont;
if (head==NULL)
{
head=p;
p->next=NULL;
}
else
{
p->next=head;
head=p;
}
}

4. C语言例程是什么

例程的作用类似于函数,但含义更为丰富一些。例程是某个系统对外提供的功能接口或服务的集合。比如操作系统的API、服务等就是例程;Delphi或C++Builder提供的标准函数和库函数等也是例程。我们编写一个DLL的时候,里面的输出函数就是这个DLL的例程。 可以这么简单地来理解:把一段相对独立的代码写成单独的一个模块就是函数的概念。我们可以在自己的程序中编写很多个函数,从而实现模块化编程。但这些模块或者说函数并不一定向外输出(即提供给别的程序使用),只用于当前这个程序里面。此时这些函数就仅仅具有独立函数的意义,但不是例程。

转网络

5. 求简单C语言程序代码!

输入2个正整数m和n,求其最大公约数和最小公倍数

#include

#include

int main()

int m,n,p,q,s,r;

printf("请输入两个正整数;m,n ");

scanf("%d,%d",&m,&n);

#include<stdio.h>

main()

int a,b,t=0;

scanf("%d %d",&a,&b);

if (a<b)

printf("%d %d %d %d %d",(a+b),(a-b),(a/b),(a*b),(a%b));

}

主要特点

C语言是一种结构化语言,它有着清晰的层次,可按照模块的方式对程序进行编写,十分有利于程序的调试,且c语言的处理和表现能力都非常的强大,依靠非常全面的运算符和多样的数据类型,可以轻易完成各种数据结构的构建,通过指针类型更可对内存直接寻址以及对硬件进行直接操作,因此既能够用于开发系统程序,也可用于开发应用软件。

以上内容参考:网络-c语言

6. 求最简单的C语言程序

#include<stdio.h>

main()

{

int a,b,t=0;

scanf("%d %d",&a,&b);

if (a<b)

{

t=a;

a=b;

b=t;

}

printf("%d %d %d %d %d",(a+b),(a-b),(a/b),(a*b),(a%b));

}

7. C语言实例

#include
<stdio.h>
#define
N
50
//
排队人数(可任意更改)
#define
CAL
3
//凡报3的人出列(可任意更改)
//下面是排队编号函数:从h
开始的n个人依次编号1到n
void
stdline(int
*h,int
n)
{
int
i;
for(i=1;i<n+1;i++)
*(h+i-1)=i;
}
/*下面函数表示从指针h处开始的人数为boy个人排队,从1报数,每报到call的人出列*/
void
outline(int
*h,int
boy,int
call)
{
int
*p,
chu,
callnum;
/*说明:
p
工作指针,表示从头依次指向每个元素,点名
chu
计数器,记录出列的人数
callnum
计数器,记录点名次序
*/
chu=0;
callnum=0;//各计数器清零
p=h;
//开始时,工作指针指向数组首
printf("出列顺序是:\n");
while(chu<boy)
{
if(*p!=0)
callnum++;
//每次加报数
if(callnum==call)
//如果某一个人报到出列数call...
{
printf("%5d",*p);
//打印编号,表示出列
chu++;
//出列人数加1
if(chu==boy)//如果全部出列....
{
*h=*p;
//把最后一个出列人的编号记入地址开始处
return;
//结束
}
if(chu%10==0)printf("\n");//每输出10个换行
callnum=0;
//出列后,重新报数
*p=0;
//出列后,将其编号赋零,以示区别
}
p++;
//工作指针移向下一个人,即下一个数组元素
if(p>h+boy-1)p=h;/*如果移到最后一个元素的后面,则让指向地址开头继续报数*/
}
}
void
main()
{
int
a[N];
//用数组模拟队列,每个元素代表一个人
stdline(a,N);//编号
outline(a,N,CAL);//计算并打印出列顺序
printf("\n最后留下来的是
%d
号\n",*a);/*在函数中,已经把最后一个人的编号写入了数组首地址处,
这里输出就可以了*/
}

8. C语言 程序

在手机上用易历知食软件里的微C程序设计来编写个示例,并运行和测试。手机上的代码如下图所示:

9. C语言简单例子

C语言中,一般会用到函数。系统默认从主函数开始运行,即main()函数。一般结构为:
void main(void)
{
....
}
一般函数是带有参数的,即后面括号中需要一个或n个变量。中间用逗号隔开。例如
void add(int a, int b)
{
...
}
其中a,b为整形的参数,在大部分系统中int 代表16位的数。
有的函数还有返回值:像上面的一个加法函数,可以写成:
int add(int a,int b)
{
return (a+b); //retuen为返回值
}
当我们要调用该函数时,只需要这要写:
void main(void)
{
int sum = 0;
sum = add(x1,x2); //x1,x2为两个常数
}

10. 经典C语言程序例子

题目01:在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,空格用来分隔不同的单词。

(10)c语言例程扩展阅读:

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。

热点内容
上传汽车保单 发布:2024-09-20 20:30:46 浏览:435
樱花服务器测试ip 发布:2024-09-20 20:10:39 浏览:279
炉石传说安卓怎么玩 发布:2024-09-20 20:09:59 浏览:312
ios开会员为什么比安卓贵 发布:2024-09-20 20:09:55 浏览:568
缓存服务器redis 发布:2024-09-20 20:09:01 浏览:75
优酷上传ts 发布:2024-09-20 19:55:58 浏览:273
minecraft怎么开服务器地址 发布:2024-09-20 19:52:14 浏览:651
android弹出布局 发布:2024-09-20 19:14:29 浏览:981
预算法包括 发布:2024-09-20 18:52:07 浏览:764
什么数字后面跟着密码 发布:2024-09-20 18:52:07 浏览:879