① 如何用c语言编程打印2-170之间的所有素数,要求每行输出13个素数。
#include <stdio.h>
#include <math.h>
int ss(int n) /*检查n是否为素数,如果是则返回1,否则返回0*/
{
int i;
for (i=2; i<=(int)sqrt((double)n); i++)
if (n%i==0) return 0;
return 1;
}
int main(void)
{
int i,j=0;
for (i=2; i<=170; i++)
if(ss(i))
{
j++;
printf("%5d",i);
if (j%13==0) printf("\n");
}
printf("\ntotal prime=%d\n",j);
return 0;
}
② C语言编程
你这样的程序会出现数据段溢出的。char*
p
没有指向的空间,是个野指针。怎么能拿来赋值。C语言里的字符串是用连续的字符来表示的,'\0'表示结束。比如你一个"abcde",在物理空间上是abcde0('\0'的机器值就是0.)每个字符都是一个字节,用ASCII码表示。C语言定义字符串的方式有两个:(1)char*
p
=
"abced";这是定义一个字符指针,并且定义一个’abcde\0‘的字符串常量在进程空间中,并把这个字符串的首地址赋给p。这样的定义,如果p没有初始化,它将是一个野指针,不指向任何数据,千万要注意不能操作野指针的值。p在程序运行过程中也可以改变值,指向别的地址。而如果按另一个人说的sizeof(p),返回的是指针类型的大小,4字节。(2)char
a[6]
=
"abcde"这是定义一个字符数组,并且把abcde赋值给每个空间。a[0]就是a,a[1]就是b,a[5]就是'\0'。这里的定义,定义的是确定的地址空间,而不是一个指针。sizeof(a)将返回6。a在程序运行过程中,不能改变它指向别的地址,因为数组是指针常量。用strlen(char*)可以返回一个字符串的长度,这个函数从你传入的参数开始,一直读取到'\0'。如果你定义的char
a[6],没有初始化,用这个函数可能会发生非常严重的后果。还要注意strlen返回的是字符长度,"abcde"返回5,而它实际占空间是6.C语言的字符串不能当做简单的像int那些类型一样的来处理,一定要注意指针的使用,这也是C语言的精髓所在。
③ C语言编程
生命游戏
/* ------------------------------------------------------ */
/* PROGRAM game of life : */
/* This is a finite implementation of John H. Conway's */
/* Game of Life. Refere to my book for detail please. */
/* */
/* Copyright Ching-Kuang Shene July/25/1989 */
/* ------------------------------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 50 /* board size */
#define OCCUPIED 1 /* occupied flag */
#define UNOCCUPIED 0
#define YES 1
#define NO 0
char cell[MAXSIZE][MAXSIZE]; /* the board */
char work[MAXSIZE][MAXSIZE]; /* a working */
int row; /* No. of rows you want */
int column; /* no. of columns you want */
int generations; /* maximum no. of generation*/
/* ------------------------------------------------------ */
/* FUNCTION read_in : */
/* This function reads in the number of generations, */
/* the number of rows, the number of columns and finally */
/* the initial configuration (the generation 0). Then put*/
/* your configuration to the center of the board. */
/* ------------------------------------------------------ */
void read_in(void)
{
int max_row, max_col; /* max # of row and col. */
int col_gap, row_gap; /* incremnet of row and col */
int i, j;
char line[100];
gets(line); /* read in gens, row and col*/
sscanf(line, "%d%d%d", &generations, &row, &column);
for (i = 0; i < row; i++)/* clear the board */
for (j = 0; j < column; j++)
cell[i][j] = UNOCCUPIED;
max_col = 0; /* read in the config. */
for (max_row = 0; gets(line) != NULL; max_row++) {
for (i = 0; line[i] != '\0'; i++)
if (line[i] != ' ')
cell[max_row][i] = OCCUPIED;
max_col = (max_col < i) ? i : max_col;
}
row_gap = (row - max_row)/2; /* the moving gap */
col_gap = (column - max_col)/2;
for (i = max_row + row_gap - 1; i >= row_gap; i--) {
for (j = max_col + col_gap - 1; j >= col_gap; j--)
cell[i][j] = cell[i-row_gap][j-col_gap];
for ( ; j >= 0; j--)
cell[i][j] = UNOCCUPIED;
}
for ( ; i >= 0; i--)
for (j = 0; j < column; j++)
cell[i][j] = UNOCCUPIED;
}
/* ------------------------------------------------------ */
/* FUNCTION display : */
/* Display the board. */
/* ------------------------------------------------------ */
#define DRAW_BOARDER(n) { int i; \
printf("\n+"); \
for (i = 0; i < n; i++) \
printf("-"); \
printf("+"); \
}
void display(int gen_no)
{
int i, j;
if (gen_no == 0)
printf("\n\nInitial Generation :\n");
else
printf("\n\nGeneration %d :\n", gen_no);
DRAW_BOARDER(column);
for (i = 0; i < row; i++) {
printf("\n|");
for (j = 0; j < column; j++)
printf("%c", (cell[i][j] == OCCUPIED) ? '*' : ' ');
printf("|");
}
DRAW_BOARDER(column);
}
/* ------------------------------------------------------ */
/* FUNCTION game_of_life : */
/* This is the main function of Game of Life. */
/* ------------------------------------------------------ */
void game_of_life(void)
{
int stable; /* stable flag */
int iter; /* iteration count */
int top, bottom, left, right; /* neighborhood bound */
int neighbors; /* # of neighbors */
int cell_count; /* # of cells count */
int done;
int i, j, p, q;
display(0); /* display initial config. */
done = NO;
for (iter = 1; iter <= generations && !done; iter++) {
memmove(work, cell, MAXSIZE*MAXSIZE); /**/
stable = YES; /* assume it is in stable */
cell_count = 0; /* # of survived cells = 0 */
for (i = 0; i < row; i++) { /* scan each cell...*/
top = (i == 0) ? 0 : i - 1;
bottom = (i == row - 1) ? row-1 : i + 1;
for (j = 0; j < column; j++) {
left = (j == 0) ? 0 : j - 1;
right = (j == column - 1) ? column-1 : j + 1;
/* compute number of neighbors */
neighbors = 0;
for (p = top; p <= bottom; p++)
for (q = left; q <= right; q++)
neighbors += work[p][q];
neighbors -= work[i][j];
/* determine life or dead */
if (work[i][j] == OCCUPIED)
if (neighbors == 2 || neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
else if (neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
stable = stable && (work[i][j] == cell[i][j]);
}
}
if (cell_count == 0) {
printf("\n\nAll cells die out.");
done = YES;
}
else if (stable) {
printf("\n\nSystem enters a stable state.");
done = YES;
}
else
display(iter);
}
}
/* ------------------------------------------------------ */
void main(void)
{
read_in();
game_of_life();
}
请采纳答案,支持我一下。
④ C语言编程
C语言是一种计算机程序设计语言。它既有高级语言的特点,又具有汇编语言的特点。它可以作为系统设计语言,编写工作系统应用程序,也可以作为应用程序设计语言,编写不依赖计算机硬件的应用程序。因此,它的应用范围广泛。主要有以下特点:
C语言在很多方面都可以用,不仅仅是在软件开发上,各类科研都是需要用到C语言的。具体应用比如我是学硬件的,单片机以及嵌入式系统都可以用C来开发。
C 语言发展如此迅速, 而且成为最受欢迎的语言之一, 主要因为它具有强大的功能。许多着名的系统软件, 如DBASE Ⅲ PLUS、DBASE Ⅳ 都是由C 语言编写的。用C 语言加上一些汇编语言子程序, 就更能显示C 语言的优势了, 象PC- DOS 、WORDSTAR等就是用这种方法编写的。归纳起来C 语言具有下列特点:1. C是中级语言它把高级语言的基本结构和语句与低级语言的实用性结合起来。C 语言可以象汇编语言一样对位、字节和地址进行操作, 而这三者是计算机最基本的工作单元。
2. C是结构式语言结构式语言的显着特点是代码及数据的分隔化, 即程序的各个部分除了必要的信息交流外彼此独立。这种结构化方式可使程序层次清晰, 便于使用、维护以及调试。C 语言是以函数形式提供给用户的, 这些函数可方便的调用, 并具有多种循环、条件语句控制程序流向, 从而使程序完全结构化。
3. C语言功能齐全C 语言具有各种各样的数据类型, 并引入了指针概念, 可使程序效率更高。另外C 语言也具有强大的图形功能, 支持多种显示器和驱动器。而且计算功能、逻辑判断功能也比较强大, 可以实现决策目的编游戏,编3D游戏,做数据库,做联众世界,做聊天室,做PHOTOSHOP做FLASH,做3DMAX。
4. C语言适用范围大C语言还有一个突出的优点就是适合于多种操作系统, 如DOS、UNIX,也适用于多种机型。
C语言对操作系统和系统使用程序以及需要对硬件进行操作的场合,用C语言明显优于其它解释型高级语言,有一些大型应用软件也是用C语言编写的。
C语言具有绘图能力强,可移植性,并具备很强的数据处理能力,因此适于编写系统软件,三维,二维图形和动画。它是数值计算的高级语言。
⑤ c语言编程:计算170的阶乘
前几天正好做过一个,用数组记录结果,暴力算法模拟手工计算过程,谈不上效率
#include <stdio.h>
#define N 10000
int main()
{
int a[N]={1};
char d[4]={0};
int i,j,t,e,f=1,m=0;
int l=0;
int k=0;
int n;
char ch='N';
printf("n=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=0;j<=l;j++)
{
a[j]=a[j]*i+k;
k=a[j]/10000;
a[j]=a[j]%10000;
}
if(k)
{
l++;
a[j]=k;
k=0;
}
}
t=a[l];
while(t)
{
d[m]=t%10;
t/=10;
m++;
}
e=4*l+m-1;
if(e>30)
{
printf("%d!=%d.",n,d[m-1]);
for(i=m-2;i>=0;i--)
{
printf("%d",d[i]);
}
for(i=0;i<8;i++)
{
printf("%04d",a[l-1-i]);
}
printf("E+%d\n",e);
printf("输出精确值?(Y/N)");
getchar();
ch=getchar();
if(ch!='y'&&ch!='Y')
f=0;
}
if(f)
{
i=l;
printf("%d!=%d",n,a[i--]);
for(;i>=0;i--)
{
printf("%04d",a[i]);
}
}
printf("\n");
return 0;
}
n=170
170!=7.+306
输出精确值?(Y/N)y
170!=000000000000000000000000000
请按任意键继续. . .
⑥ c语言程序设计170行大作业
1 具
2 体
3 什
4 么
5 要
6 求
7
8
9
10
11
12
13
14
15
。。。。。。
⑦ C语言编程
#include<stdio.h>
#include<stdlib.h>
intmain()
{
chars2[100];//这个数自己设置
charc='