当前位置:首页 » 编程语言 » c语言设计统计代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言设计统计代码

发布时间: 2022-09-11 01:53:29

‘壹’ c语言程序设计: 任意输入n个整数,分别统计奇数的和、奇数的个数、偶数的和、偶数的个数。

程序为:

#include<stdio.h>

void main()

{

int a[100],i,item,sum1,sum2,item1,item2;//sum1,sum2分别储存奇数、偶数之和,item1,item2分别表示奇数、偶数的个数

printf("请输入所要输入整数的个数:");

scanf("%d",&item);

printf("请输入%d个整数: ",item);

for(i=0;i<item;i++)

scanf("%d",&a[i]);

sum1=sum2=item1=item2=0;

for(i=0;i<item;i++)

{

if(a[i]==0) item2++;//0是特殊偶数

else if(a[i]%2==0)

{

sum2=sum2+a[i];

item2++;

}

else

{

sum1=sum1+a[i];

item1++;

}

}

printf("奇数之和为:%d,偶数之和为:%d ",sum1,sum2);

}

(1)c语言设计统计代码扩展阅读:

for表达式

表达式1:一般为赋值表达式,给控制变量赋初值;

表达式2:关系表达式或逻辑表达式,循环控制条件;

表达式3:一般为赋值表达式,给控制变量增量或减量。

语句:循环体,当有多条语句时,必须使用复合语句。

‘贰’ 用c语言设计一个程序统计一个班的学生成绩

#include<stdio.h>

#include<stdlib.h>

//定义一个学生结构体数组

struct Student{

int id;

double score;

}Student[100];

//记录实际导入学生个数

int len = 0;

//求最高分

void getmax() {

int maxid = 0;

double maxscore = -1;

for (int i = 0; i < len; i++) {

if (Student[i].score > maxscore) {

maxscore = Student[i].score;

maxid = Student[i].id;

}

}

printf("班级最高分为:%.2lf学号是:%d ", maxscore, maxid);

}

//求最低分

void getmin() {

int minid = 0;

double minscore = 101;

for (int i = 0; i < len; i++) {

if (Student[i].score < minscore) {

minscore = Student[i].score;

minid = Student[i].id;

}

}

printf("班级最低分为:%.2lf学号是:%d ", minscore, minid);

}

//求总分

double getsum() {

double sum = 0;

for (int i = 0; i < len; i++) {

sum += Student[i].score;

}

return sum;

}

//求平均分

double getave() {

double ave = 0;

return getsum() / len;

}

//导入学生信息

void input() {

int id = 0;

double score = 0;

while (1) {

printf("请输入学生学号:");

scanf_s("%d", &id);

if (id == -1) { break; }

printf("请输入学生成绩:");

scanf_s("%lf", &score);

Student[len].id = id;

Student[len].score = score;

len++;

}

printf("信息导入完毕! ");

}

int main() {

input();

printf("全班总分为:%.2lf ", getsum());

printf("全班平均分为:%.2lf ", getave());

getmax();

getmin();

return 0;

}

注:此代码运行于VS2017,如果在VC6.0中运行,可将scanf_s改成scanf

并且for(int i=0;i<len;i++){xxx}改成int i=0;for(i=0;i<len;i++){xxx}即可正常运行

程序运行测试图如下:

‘叁’ 用C语言怎么编写一个统计名字次数的程序急!急!急!!!!

代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ()
{
struct person /*定义一个结构体*/
{
char name[20]; /*默认名字不超过20长度,且没有空格。*/
int time; /*出现次数*/
};
struct person data[200]; /*默认不超过200个不同名*/

int i;
for (i = 0; i < 200; i ++) /*初始化一下*/
{
data[i].name[0] = 0;
data[i].time = 0;
}

char temp[20];
int total = 0; /*一开始一共有0个人*/
while (scanf ("%s", temp) != EOF) /*循环读入所有姓名*/
{
int i = 0;
for (i = 0; i < total; i ++) /*和已经存在的姓名对比,看是否一样*/
{
if (strcmp (temp, data[i].name) == 0) /*如果一样*/
{
data[i].time ++;
break;
}
}
if (i == total) /*找了一圈没有找到已经存在的,就证明这个名字从来没有出现过,存一个新的*/
{
strcpy (data[total].name, temp);
data[total].time = 1;
total ++;
}

}
for (i = 0; i < total; i ++) /*最后循环把结果输出*/
printf ("%s ---- %d\n", data[i].name, data[i].time);

system ("pause");
return 0;
}

祝学习进步!

‘肆’ 用c语言设计一个程序统计一个班的学生成绩

代码如下:

注:此代码运行于VS2017,如果在VC6.0中运行,可将scanf_s改成scanf

并且for(int i=0;i<len;i++){xxx}改成int i=0;for(i=0;i<len;i++){xxx}即可正常运行

#include<stdio.h>

#include<stdlib.h>

//定义一个学生结构体数组

struct Student{

int id;

double score;

}Student[100];

//记录实际导入学生个数

int len = 0;

//求最高分

void getmax() {

int maxid = 0;

double maxscore = -1;

for (int i = 0; i < len; i++) {

if (Student[i].score > maxscore) {

maxscore = Student[i].score;

maxid = Student[i].id;

}

}

printf("班级最高分为:%.2lf学号是:%d ", maxscore, maxid);

}

//求最低分

void getmin() {

int minid = 0;

double minscore = 101;

for (int i = 0; i < len; i++) {

if (Student[i].score < minscore) {

minscore = Student[i].score;

minid = Student[i].id;

}

}

printf("班级最低分为:%.2lf学号是:%d ", minscore, minid);

}

//求总分

double getsum() {

double sum = 0;

for (int i = 0; i < len; i++) {

sum += Student[i].score;

}

return sum;

}

//求平均分

double getave() {

double ave = 0;

return getsum() / len;

}

//导入学生信息

void input() {

int id = 0;

double score = 0;

while (1) {

printf("请输入学生学号:");

scanf_s("%d", &id);

if (id == -1) { break; }

printf("请输入学生成绩:");

scanf_s("%lf", &score);

Student[len].id = id;

Student[len].score = score;

len++;

}

printf("信息导入完毕! ");

}

int main() {

input();

printf("全班总分为:%.2lf ", getsum());

printf("全班平均分为:%.2lf ", getave());

getmax();

getmin();

return 0;

}

程序运行测试图如下:

‘伍’ 求一篇用C语言编写的使用数组和指针统计成绩的程序代码

void main()
{
int a[8] = {75, 80, 83, 85, 86, 30, 0, 0};
string name[] = {"数学", "物理", "外语", "政治", "体育"};
string MeanLow[4];
a[6] = (a[0] + a[1] + a[2] + a[3] + a[4]) / 5;
char* strAve = (char*)malloc(10);
MeanLow[0] = itoa(a[5], strAve, 10);
MeanLow[1] = itoa(a[6], strAve, 10);
int b = a[0];
for (int i = 0; i < 4; i++)
{
b = min(b, a[i + 1]);
}
MeanLow[2] = itoa(b, strAve, 10);
for (int i = 0; i < 5; i++)
{
if (a[i] == b)
{
MeanLow[3] = name[i];
}
}
printf("数学:75\n物理:80\n外语:83\n政治:85\n体育:86\n统计结果如下:\n");
printf("人数:30\n平均成绩:%s\n最低分数科目的成绩:%s\n最低分数的科目:%s\n", MeanLow[1], MeanLow[2], MeanLow[3]);
free(strAve);
}

‘陆’ 编写C语言程序统计输入字符串的个数、

很简单了,代码如下:
#include
void
main()
{
char
a[100];
int
i,count=0,flag=1,n=0;
gets(a);//从键盘输入字符串
for(i=0;a[i];i++)
{
if((a[i]>='a'
&&
a[i]<='z')
||
(a[i]>='a'
&&
a[i]<='z'))//判断是不是字符
{
flag=0;//设置字符标志
continue;//返回下一个循环
}
if(!flag)//前面一位是字符,即当前是某个单词字符结束
{
count++;//统计单词个数
flag=1;//设置非字符标志
}
}
i--;
if((a[i]>='a'
&&
a[i]<='z')
||
(a[i]>='a'
&&
a[i]<='z'))
count++;//增加字符串结束的单词
printf("共有%d单词\n",count);
}

‘柒’ 用C语言怎样写数据统计代码

....
for(i =0;i <10;i++)
{
scanf("%d",a);
if(a>0)
cout1++;//正数计数
if(a == 0)
count2++;//0个数计数
if(a<0)
count3++;//负数个数
}
printf("%d\n",count1);
..
..

‘捌’ C语言编写程序统计输入的行数

循环读入字符,遇到 累计行数,最终输出累计值即可。

以EOF作为终止条件的代码如下:

#include<stdio.h>
intmain()
{
intcnt=1;//最少会输出一行,每遇到一个换行,表示多输入了一行。
intc;
while((c=getchar())!=EOF)
{
if(c==' ')cnt++;//统计行数。
}
printf("%d ",cnt);

return0;
}

‘玖’ 用C语言来编写:商品销售统计程序

#include<iostream>
#include<cstring>
#include<fstream>
#include<stdlib.h>//system("cls")//清屏
#include<conio.h>//getche()
using namespace std;
//全局变量
int i=0;//已录入商品总个数
char ch;//cin>>ch
int n;//case(n)
char code[10];
char name[10];
char unit[10];
int amount;
float unitprice;
float total=0;//总价
ofstream f1("./test.txt");
ofstream f2("./sell.txt");//构建输出流,没有文件就建立

class Goods
{
private:
char code[10];//代码
char name[10];//名称
char unit[10];//单位
int amount;//总数
float unitprice;//单价
public:
Goods();
Goods(char co[10],char na[10],char un[10],int am,float unpr);//构造函数
void f_write();// 录入
void f_change();//改变
void f_delete();//删除
void display();//显示全部商品信息
void s_buy();//买入
};
//构造函数
Goods::Goods(){}
Goods::Goods(char co[10],char na[10],char un[10],int am,float unpr)
{
strcpy(code,co);
strcpy(name,na);
strcpy(unit,un);
amount=am;
unitprice=unpr;
}
Goods *g[50];
//商品信息录入
void Goods::f_write()
{
cout<<"请输入第"<<i+1<<"件商品代码:"<<endl;
cin>>code;
cout<<"请输入第"<<i+1<<"件商品名称:"<<endl;
cin>>name;
cout<<"请输入第"<<i+1<<"件商品计量单位:"<<endl;
cin>>unit;
cout<<"请输入第"<<i+1<<"件商品总数:"<<endl;
cin>>amount;
cout<<"请输入第"<<i+1<<"件商品单价:"<<endl;
cin>>unitprice;
g[i]=new Goods(code,name,unit,amount,unitprice);
i++;
cout<<"信息录入成功!(继续录入按y,返回上一层按n)"<<endl;
cin>>ch;
if(ch=='y')
{
f_write();
}
}
//改变商品信息
void Goods::f_change()
{
cout<<"请输入要改变的商品代码:";
cin>>code;
for(int h=0;h<i;h++)
{
if(0 == strcmp(g[h]->code,code))
{
cout<<"商品信息如下:"<<endl;
cout<<"代码 名称 单价 总数 单位"<<endl;
cout<<g[h]->code<<"\t"<<g[h]->name<<"\t"<<g[h]->unitprice
<<"\t"<<g[h]->amount<<"\t"<<g[h]->unit<<endl;
char newco,newna,newun;
int newam;
float newunpr;
cout<<"你想要修改:1、代码;2、名称;3、单价;4、总数;5、单位。"<<endl;
cin>>n;
switch(n)
{
case 1:
cout<<"请输入修改后的商品代码:";
cin>>newco;
g[h]->code[10]=newco;
cout<<"修改成功!"<<endl;
break;
case 2:
cout<<"请输入修改后的商品名称:";
cin>>newna;
g[h]->name[10]=newna;
cout<<"修改成功!"<<endl;
break;
case 3:
cout<<"请输入商品单价:";
cin>>newunpr;
g[h]->unitprice=newunpr;
cout<<"修改成功!"<<endl;
break;
case 4:
cout<<"请输入修改后的商品总数:";
cin>>newam;
g[h]->amount=newam;
cout<<"修改成功!"<<endl;
break;
case 5:
cout<<"请输入修改后的商品单位:";
cin>>newun;
g[h]->unit[10]=newun;
cout<<"修改成功!"<<endl;
break;
}
break;
}//if
}//for循环
cout<<"是否继续修改?(y/n)"<<endl;
cin>>ch;
if(ch=='y')
{
f_change();
}
}
//删除信息
void Goods::f_delete()
{
cout<<"请输入要删除的商品代码:";
cin>>code;
for(int h=0;h<i;h++)
{
if(0 == strcmp(g[h]->code,code))
{
for(int k=h;k<i;k++)
{
g[k]=g[k+1];
i--;
}
}
}
cout<<"删除成功!"<<endl;
cout<<"是否继续删除?(y/n)"<<endl;
cin>>ch;
if(ch=='y')
{
f_delete();
}
}
//打印信息
void Goods::display()
{
system("cls");
cout<<" "<<endl;
cout<<"-----------全部商品信息如下-------------------"<<endl;
cout<<" "<<endl;
cout<<"代码 名称 单价 总数 单位"<<endl;
f1<<" "<<endl;
f1<<"---------------全部商品信息如下--------------"<<endl;
f1<<" "<<endl;
f1<<"代码 名称 单价 总数 单位"<<endl;
if(i==0)
{
cout<<"系统未曾录入任何商品信息,或记录被删除!";
}
for(int k=0;k<i;k++)
{
cout<<g[k]->code<<"\t"<<g[k]->name<<"\t"<<g[k]->unitprice
<<"\t"<<g[k]->amount<<"\t"<<g[k]->unit<<endl;
f1<<g[k]->code<<"\t"<<g[k]->name<<"\t"<<g[k]->unitprice
<<"\t"<<g[k]->amount<<"\t"<<g[k]->unit<<endl;
}
cout<<endl;
}
//买入
void Goods::s_buy()
{
float price=0;//单个商品价格
cout<<"请输入想要买的商品代码:";
cin>>code;
for(int h=0;h<i;h++)
{
if(0 == strcmp(g[h]->code,code))
{
cout<<"请输入想要购买的商品数量:";
cin>>amount;//当前要购买的数量
price=amount*g[h]->unitprice;
g[h]->amount=g[h]->amount-amount;
cout<<endl;
cout<<"代码 名称 单价 数量 小计"<<endl;
cout<<g[h]->code<<"\t"<<g[h]->name<<"\t"<<g[h]->unitprice
<<"\t"<<g[h]->amount<<g[h]->unit<<"\t"<<price<<endl;
f2<<"代码 名称 单价 数量 小计"<<endl;
f2<<g[h]->code<<"\t"<<g[h]->name<<"\t"<<g[h]->unitprice
<<"\t"<<g[h]->amount<<g[h]->unit<<"\t"<<price<<endl;
total=total+price;
break;
}
}
cout<<"按1继续购买,按2结束。"<<endl;
cin>>n;
if(n==2)
{
cout<<endl;
cout<<"购买结束,总计:"<<total<<"元!"<<endl;
}
else
{
s_buy();
}
}

//类外函数
//第一部分操作显示
void f_screen()
{
system("cls");
Goods g;
cout<<"按相应键操作:"<<endl;
cout<<"0.录入信息 1.更改信息 2.删除信息 3.返回上一层"<<endl;
cin>>n;
switch(n)
{
case 0:
g.f_write();
if(ch=='n'||ch=='N')
f_screen();
break;
case 1:
g.f_change();
if(ch=='n'||ch=='N')
f_screen();
break;
case 2:
g.f_delete();
if(ch=='n'||ch=='N')
f_screen();
break;
}
}
//第二部分操作显示
void s_screen()
{
Goods g;
g.display();
cout<<endl;
g.s_buy();
}
//初始屏幕显示
void screen()
{
system("cls");
cout<<" "<<endl;
cout<<"-----------------商品销售统计系统---------------"<<endl;
cout<<" "<<endl;
f2<<" "<<endl;
f2<<"----------------商品销售统计系统--------------"<<endl;
f2<<" "<<endl;
cout<<"更改商品信息请按1,进行销售统计请按-1。"<<endl;
cin>>ch;
if(ch=='1')
{
f_screen();
if(n==3)
screen();
}
else if(ch=='-1')
{
s_screen();
}
else
{
cout<<"输入错误,系统重新启动!";
screen();
}
}
int main()
{
screen();
return 0;
}

‘拾’ c语言:编写程序,从键盘输入80个字符,统计其中数字字符的个数,并输出统计结果

可以参考下面的代码:#include <stdio.h>intmain(){inta,b,c,ch;a=b=c=0;//计数器初始化为0.while((ch=getchar())!='\n')//循环读取字符,到换行结束。{if(ch>='0' && ch<='9')//数字a++;else if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))//字母b++;else//其它c++;}printf("%d%d%d\n",a,b,c);//输出结果。return0;}(10)c语言设计统计代码扩展阅读:printf()函数函数printf()函数是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息。在编写程序时经常会用到此函数。函数的原型为:int printf(const char *format, ...);函数返回值为整型。若成功则返回输出的字符数,输出出错则返回负值,printf()函数的调用格式为:printf("<格式化字符串>", <参量表>);while语句的一般表达式为:while(表达式){循环体}。参考资料来源:网络-printf()参考资料来源:网络-while (循环语句及英文单词)